Skip to content

Commit 18f7a78

Browse files
committed
codal_port/modaudio: Add a "used_size" field to AudioFrame.
Signed-off-by: Damien George <damien@micropython.org>
1 parent 1ed6669 commit 18f7a78

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

src/codal_port/microbit_microphone.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(microbit_microphone_get_events_obj, microbit_mi
150150

151151
static void microbit_microphone_record_helper(microbit_audio_frame_obj_t *audio_frame, int rate, bool wait) {
152152
// Start the recording.
153-
microbit_hal_microphone_start_recording(audio_frame->data, audio_frame->alloc_size, rate);
153+
microbit_hal_microphone_start_recording(audio_frame->data, audio_frame->alloc_size, &audio_frame->used_size, rate);
154154

155155
if (wait) {
156156
// Wait for the recording to finish.
@@ -195,10 +195,14 @@ static mp_obj_t microbit_microphone_record_into(mp_uint_t n_args, const mp_obj_t
195195
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
196196
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
197197

198-
mp_buffer_info_t bufinfo;
199-
mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_READ);
198+
// Check that the buffer is an AudioFrame instance.
199+
if (!mp_obj_is_type(args[ARG_buffer].u_obj, &microbit_audio_frame_type)) {
200+
mp_raise_TypeError(MP_ERROR_TEXT("expecting an AudioFrame"));
201+
}
202+
microbit_audio_frame_obj_t *audio_frame = MP_OBJ_TO_PTR(args[ARG_buffer].u_obj);
200203

201-
microbit_hal_microphone_start_recording(bufinfo.buf, bufinfo.len, args[ARG_rate].u_int);
204+
// Start the recording.
205+
microbit_hal_microphone_start_recording(audio_frame->data, audio_frame->alloc_size, &audio_frame->used_size, args[ARG_rate].u_int);
202206

203207
return mp_const_none;
204208
}

src/codal_port/modaudio.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ STATIC void audio_data_fetcher(void) {
7676

7777
if (audio_source_frame != NULL) {
7878
// An existing AudioFrame is being played, see if there's any data left.
79-
if (audio_raw_offset >= audio_source_frame->alloc_size) {
79+
if (audio_raw_offset >= audio_source_frame->used_size) {
8080
// AudioFrame is exhausted.
8181
audio_source_frame = NULL;
8282
}
@@ -321,7 +321,7 @@ STATIC mp_obj_t microbit_audio_frame_new(const mp_obj_type_t *type_in, mp_uint_t
321321
STATIC mp_obj_t audio_frame_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value_in) {
322322
microbit_audio_frame_obj_t *self = (microbit_audio_frame_obj_t *)self_in;
323323
mp_int_t index = mp_obj_get_int(index_in);
324-
if (index < 0 || index >= self->alloc_size) {
324+
if (index < 0 || index >= self->used_size) {
325325
mp_raise_ValueError(MP_ERROR_TEXT("index out of bounds"));
326326
}
327327
if (value_in == MP_OBJ_NULL) {
@@ -344,7 +344,7 @@ static mp_obj_t audio_frame_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
344344
microbit_audio_frame_obj_t *self = (microbit_audio_frame_obj_t *)self_in;
345345
switch (op) {
346346
case MP_UNARY_OP_LEN:
347-
return MP_OBJ_NEW_SMALL_INT(self->alloc_size);
347+
return MP_OBJ_NEW_SMALL_INT(self->used_size);
348348
default:
349349
return MP_OBJ_NULL; // op not supported
350350
}
@@ -354,14 +354,14 @@ static mp_int_t audio_frame_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufin
354354
(void)flags;
355355
microbit_audio_frame_obj_t *self = (microbit_audio_frame_obj_t *)self_in;
356356
bufinfo->buf = self->data;
357-
bufinfo->len = self->alloc_size;
357+
bufinfo->len = self->used_size;
358358
bufinfo->typecode = 'b';
359359
return 0;
360360
}
361361

362362
static void add_into(microbit_audio_frame_obj_t *self, microbit_audio_frame_obj_t *other, bool add) {
363363
int mult = add ? 1 : -1;
364-
size_t size = MIN(self->alloc_size, other->alloc_size);
364+
size_t size = MIN(self->used_size, other->used_size);
365365
for (int i = 0; i < size; i++) {
366366
unsigned val = (int)self->data[i] + mult*(other->data[i]-128);
367367
// Clamp to 0-255
@@ -374,6 +374,7 @@ static void add_into(microbit_audio_frame_obj_t *self, microbit_audio_frame_obj_
374374

375375
static microbit_audio_frame_obj_t *copy(microbit_audio_frame_obj_t *self) {
376376
microbit_audio_frame_obj_t *result = microbit_audio_frame_make_new(self->alloc_size);
377+
result->used_size = self->used_size;
377378
for (int i = 0; i < self->alloc_size; i++) {
378379
result->data[i] = self->data[i];
379380
}
@@ -384,7 +385,7 @@ mp_obj_t copyfrom(mp_obj_t self_in, mp_obj_t other) {
384385
microbit_audio_frame_obj_t *self = (microbit_audio_frame_obj_t *)self_in;
385386
mp_buffer_info_t bufinfo;
386387
mp_get_buffer_raise(other, &bufinfo, MP_BUFFER_READ);
387-
uint32_t len = MIN(bufinfo.len, self->alloc_size);
388+
uint32_t len = MIN(bufinfo.len, self->used_size);
388389
for (uint32_t i = 0; i < len; i++) {
389390
self->data[i] = ((uint8_t *)bufinfo.buf)[i];
390391
}
@@ -421,7 +422,7 @@ int32_t float_to_fixed(float f, uint32_t scale) {
421422

422423
static void mult(microbit_audio_frame_obj_t *self, float f) {
423424
int scaled = float_to_fixed(f, 15);
424-
for (int i = 0; i < self->alloc_size; i++) {
425+
for (int i = 0; i < self->used_size; i++) {
425426
unsigned val = ((((int)self->data[i]-128) * scaled) >> 15)+128;
426427
if (val > 255) {
427428
val = (1-(val>>31))*255;
@@ -477,6 +478,7 @@ microbit_audio_frame_obj_t *microbit_audio_frame_make_new(size_t size) {
477478
microbit_audio_frame_obj_t *res = m_new_obj_var(microbit_audio_frame_obj_t, uint8_t, size);
478479
res->base.type = &microbit_audio_frame_type;
479480
res->alloc_size = size;
481+
res->used_size = 0;
480482
memset(res->data, 128, size);
481483
return res;
482484
}

src/codal_port/modaudio.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
typedef struct _microbit_audio_frame_obj_t {
3535
mp_obj_base_t base;
3636
size_t alloc_size;
37+
size_t used_size;
3738
uint8_t data[];
3839
} microbit_audio_frame_obj_t;
3940

0 commit comments

Comments
 (0)