Skip to content

Commit 9075b89

Browse files
committed
codal_port/modaudio: Add rate to AudioFrame.
Make the constructor take duration and rate arguments, and add get_rate() / set_rate() methods. Signed-off-by: Damien George <damien@micropython.org>
1 parent 39770d1 commit 9075b89

File tree

3 files changed

+49
-14
lines changed

3 files changed

+49
-14
lines changed

src/codal_port/microbit_microphone.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ STATIC mp_obj_t microbit_microphone_record(mp_uint_t n_args, const mp_obj_t *pos
161161

162162
// Create the AudioFrame to record into.
163163
size_t size = args[ARG_duration].u_int * args[ARG_rate].u_int / 1000;
164-
microbit_audio_frame_obj_t *audio_frame = microbit_audio_frame_make_new(size);
164+
microbit_audio_frame_obj_t *audio_frame = microbit_audio_frame_make_new(size, args[ARG_rate].u_int);
165165

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

src/codal_port/modaudio.c

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -315,18 +315,32 @@ MP_REGISTER_MODULE(MP_QSTR_audio, audio_module);
315315
/******************************************************************************/
316316
// AudioFrame class
317317

318-
STATIC mp_obj_t microbit_audio_frame_new(const mp_obj_type_t *type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
318+
STATIC mp_obj_t microbit_audio_frame_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
319319
(void)type_in;
320-
(void)args;
321-
mp_arg_check_num(n_args, n_kw, 0, 1, false);
322-
size_t size = AUDIO_CHUNK_SIZE;
323-
if (n_args == 1) {
324-
size = mp_obj_get_int(args[0]);
325-
if (size <= 0) {
326-
mp_raise_ValueError(MP_ERROR_TEXT("size out of bounds"));
327-
}
320+
321+
enum { ARG_duration, ARG_rate };
322+
static const mp_arg_t allowed_args[] = {
323+
{ MP_QSTR_duration, MP_ARG_INT, {.u_int = -1} },
324+
{ MP_QSTR_rate, MP_ARG_INT, {.u_int = 7812} },
325+
};
326+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
327+
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
328+
329+
mp_uint_t rate = args[ARG_rate].u_int;
330+
if (rate <= 0) {
331+
mp_raise_ValueError(MP_ERROR_TEXT("rate out of bounds"));
332+
}
333+
334+
size_t size;
335+
if (args[ARG_duration].u_int < 0) {
336+
size = AUDIO_CHUNK_SIZE;
337+
} else if (args[ARG_duration].u_int == 0) {
338+
mp_raise_ValueError(MP_ERROR_TEXT("size out of bounds"));
339+
} else {
340+
size = args[ARG_duration].u_int * rate / 1000;
328341
}
329-
return microbit_audio_frame_make_new(size);
342+
343+
return microbit_audio_frame_make_new(size, rate);
330344
}
331345

332346
STATIC mp_obj_t audio_frame_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value_in) {
@@ -384,7 +398,7 @@ static void add_into(microbit_audio_frame_obj_t *self, microbit_audio_frame_obj_
384398
}
385399

386400
static microbit_audio_frame_obj_t *copy(microbit_audio_frame_obj_t *self) {
387-
microbit_audio_frame_obj_t *result = microbit_audio_frame_make_new(self->alloc_size);
401+
microbit_audio_frame_obj_t *result = microbit_audio_frame_make_new(self->alloc_size, self->rate);
388402
result->used_size = self->used_size;
389403
for (int i = 0; i < self->alloc_size; i++) {
390404
result->data[i] = self->data[i];
@@ -468,7 +482,26 @@ STATIC mp_obj_t audio_frame_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj
468482
}
469483
}
470484

485+
STATIC mp_obj_t audio_frame_get_rate(mp_obj_t self_in) {
486+
microbit_audio_frame_obj_t *self = MP_OBJ_TO_PTR(self_in);
487+
return MP_OBJ_NEW_SMALL_INT(self->rate);
488+
}
489+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(audio_frame_get_rate_obj, audio_frame_get_rate);
490+
491+
STATIC mp_obj_t audio_frame_set_rate(mp_obj_t self_in, mp_obj_t rate_in) {
492+
microbit_audio_frame_obj_t *self = MP_OBJ_TO_PTR(self_in);
493+
mp_uint_t rate = mp_obj_get_int(rate_in);
494+
if (rate <= 0) {
495+
mp_raise_ValueError(MP_ERROR_TEXT("rate out of bounds"));
496+
}
497+
self->rate = rate;
498+
return mp_const_none;
499+
}
500+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(audio_frame_set_rate_obj, audio_frame_set_rate);
501+
471502
STATIC const mp_map_elem_t microbit_audio_frame_locals_dict_table[] = {
503+
{ MP_OBJ_NEW_QSTR(MP_QSTR_get_rate), (mp_obj_t)&audio_frame_get_rate_obj },
504+
{ MP_OBJ_NEW_QSTR(MP_QSTR_set_rate), (mp_obj_t)&audio_frame_set_rate_obj },
472505
{ MP_OBJ_NEW_QSTR(MP_QSTR_copyfrom), (mp_obj_t)&copyfrom_obj },
473506
};
474507
STATIC MP_DEFINE_CONST_DICT(microbit_audio_frame_locals_dict, microbit_audio_frame_locals_dict_table);
@@ -485,11 +518,12 @@ MP_DEFINE_CONST_OBJ_TYPE(
485518
locals_dict, &microbit_audio_frame_locals_dict
486519
);
487520

488-
microbit_audio_frame_obj_t *microbit_audio_frame_make_new(size_t size) {
521+
microbit_audio_frame_obj_t *microbit_audio_frame_make_new(size_t size, uint16_t rate) {
489522
microbit_audio_frame_obj_t *res = m_new_obj_var(microbit_audio_frame_obj_t, uint8_t, size);
490523
res->base.type = &microbit_audio_frame_type;
491524
res->alloc_size = size;
492525
res->used_size = 0;
526+
res->rate = rate;
493527
memset(res->data, 128, size);
494528
return res;
495529
}

src/codal_port/modaudio.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ typedef struct _microbit_audio_frame_obj_t {
3535
mp_obj_base_t base;
3636
size_t alloc_size;
3737
size_t used_size;
38+
uint16_t rate;
3839
uint8_t data[];
3940
} microbit_audio_frame_obj_t;
4041

@@ -45,7 +46,7 @@ void microbit_audio_play_source(mp_obj_t src, mp_obj_t pin_select, bool wait, ui
4546
void microbit_audio_stop(void);
4647
bool microbit_audio_is_playing(void);
4748

48-
microbit_audio_frame_obj_t *microbit_audio_frame_make_new(size_t size);
49+
microbit_audio_frame_obj_t *microbit_audio_frame_make_new(size_t size, uint16_t rate);
4950

5051
const char *microbit_soundeffect_get_sound_expr_data(mp_obj_t self_in);
5152

0 commit comments

Comments
 (0)