Skip to content

Commit 39770d1

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

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
@@ -164,7 +164,7 @@ STATIC mp_obj_t microbit_microphone_record(mp_uint_t n_args, const mp_obj_t *pos
164164
microbit_audio_frame_obj_t *audio_frame = microbit_audio_frame_make_new(size);
165165

166166
// Start the recording.
167-
microbit_hal_microphone_start_recording(audio_frame->data, audio_frame->alloc_size, args[ARG_rate].u_int);
167+
microbit_hal_microphone_start_recording(audio_frame->data, audio_frame->alloc_size, &audio_frame->used_size, args[ARG_rate].u_int);
168168

169169
// Wait for the recording to finish.
170170
while (microbit_hal_microphone_is_recording()) {
@@ -189,10 +189,14 @@ STATIC mp_obj_t microbit_microphone_record_into(mp_uint_t n_args, const mp_obj_t
189189
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
190190
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
191191

192-
mp_buffer_info_t bufinfo;
193-
mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_READ);
192+
// Check that the buffer is an AudioFrame instance.
193+
if (!mp_obj_is_type(args[ARG_buffer].u_obj, &microbit_audio_frame_type)) {
194+
mp_raise_TypeError(MP_ERROR_TEXT("expecting an AudioFrame"));
195+
}
196+
microbit_audio_frame_obj_t *audio_frame = MP_OBJ_TO_PTR(args[ARG_buffer].u_obj);
194197

195-
microbit_hal_microphone_start_recording(bufinfo.buf, bufinfo.len, args[ARG_rate].u_int);
198+
// Start the recording.
199+
microbit_hal_microphone_start_recording(audio_frame->data, audio_frame->alloc_size, &audio_frame->used_size, args[ARG_rate].u_int);
196200

197201
return mp_const_none;
198202
}

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
}
@@ -332,7 +332,7 @@ STATIC mp_obj_t microbit_audio_frame_new(const mp_obj_type_t *type_in, mp_uint_t
332332
STATIC mp_obj_t audio_frame_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value_in) {
333333
microbit_audio_frame_obj_t *self = (microbit_audio_frame_obj_t *)self_in;
334334
mp_int_t index = mp_obj_get_int(index_in);
335-
if (index < 0 || index >= self->alloc_size) {
335+
if (index < 0 || index >= self->used_size) {
336336
mp_raise_ValueError(MP_ERROR_TEXT("index out of bounds"));
337337
}
338338
if (value_in == MP_OBJ_NULL) {
@@ -355,7 +355,7 @@ static mp_obj_t audio_frame_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
355355
microbit_audio_frame_obj_t *self = (microbit_audio_frame_obj_t *)self_in;
356356
switch (op) {
357357
case MP_UNARY_OP_LEN:
358-
return MP_OBJ_NEW_SMALL_INT(self->alloc_size);
358+
return MP_OBJ_NEW_SMALL_INT(self->used_size);
359359
default:
360360
return MP_OBJ_NULL; // op not supported
361361
}
@@ -365,14 +365,14 @@ static mp_int_t audio_frame_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufin
365365
(void)flags;
366366
microbit_audio_frame_obj_t *self = (microbit_audio_frame_obj_t *)self_in;
367367
bufinfo->buf = self->data;
368-
bufinfo->len = self->alloc_size;
368+
bufinfo->len = self->used_size;
369369
bufinfo->typecode = 'b';
370370
return 0;
371371
}
372372

373373
static void add_into(microbit_audio_frame_obj_t *self, microbit_audio_frame_obj_t *other, bool add) {
374374
int mult = add ? 1 : -1;
375-
size_t size = MIN(self->alloc_size, other->alloc_size);
375+
size_t size = MIN(self->used_size, other->used_size);
376376
for (int i = 0; i < size; i++) {
377377
unsigned val = (int)self->data[i] + mult*(other->data[i]-128);
378378
// Clamp to 0-255
@@ -385,6 +385,7 @@ static void add_into(microbit_audio_frame_obj_t *self, microbit_audio_frame_obj_
385385

386386
static microbit_audio_frame_obj_t *copy(microbit_audio_frame_obj_t *self) {
387387
microbit_audio_frame_obj_t *result = microbit_audio_frame_make_new(self->alloc_size);
388+
result->used_size = self->used_size;
388389
for (int i = 0; i < self->alloc_size; i++) {
389390
result->data[i] = self->data[i];
390391
}
@@ -395,7 +396,7 @@ mp_obj_t copyfrom(mp_obj_t self_in, mp_obj_t other) {
395396
microbit_audio_frame_obj_t *self = (microbit_audio_frame_obj_t *)self_in;
396397
mp_buffer_info_t bufinfo;
397398
mp_get_buffer_raise(other, &bufinfo, MP_BUFFER_READ);
398-
uint32_t len = MIN(bufinfo.len, self->alloc_size);
399+
uint32_t len = MIN(bufinfo.len, self->used_size);
399400
for (uint32_t i = 0; i < len; i++) {
400401
self->data[i] = ((uint8_t *)bufinfo.buf)[i];
401402
}
@@ -432,7 +433,7 @@ int32_t float_to_fixed(float f, uint32_t scale) {
432433

433434
static void mult(microbit_audio_frame_obj_t *self, float f) {
434435
int scaled = float_to_fixed(f, 15);
435-
for (int i = 0; i < self->alloc_size; i++) {
436+
for (int i = 0; i < self->used_size; i++) {
436437
unsigned val = ((((int)self->data[i]-128) * scaled) >> 15)+128;
437438
if (val > 255) {
438439
val = (1-(val>>31))*255;
@@ -488,6 +489,7 @@ microbit_audio_frame_obj_t *microbit_audio_frame_make_new(size_t size) {
488489
microbit_audio_frame_obj_t *res = m_new_obj_var(microbit_audio_frame_obj_t, uint8_t, size);
489490
res->base.type = &microbit_audio_frame_type;
490491
res->alloc_size = size;
492+
res->used_size = 0;
491493
memset(res->data, 128, size);
492494
return res;
493495
}

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)