Skip to content

Commit 1347a07

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 18f7a78 commit 1347a07

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
@@ -174,7 +174,7 @@ static mp_obj_t microbit_microphone_record(mp_uint_t n_args, const mp_obj_t *pos
174174

175175
// Create the AudioFrame to record into.
176176
size_t size = args[ARG_duration].u_int * args[ARG_rate].u_int / 1000;
177-
microbit_audio_frame_obj_t *audio_frame = microbit_audio_frame_make_new(size);
177+
microbit_audio_frame_obj_t *audio_frame = microbit_audio_frame_make_new(size, args[ARG_rate].u_int);
178178

179179
microbit_microphone_record_helper(audio_frame, args[ARG_rate].u_int, true);
180180

src/codal_port/modaudio.c

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -304,18 +304,32 @@ MP_REGISTER_MODULE(MP_QSTR_audio, audio_module);
304304
/******************************************************************************/
305305
// AudioFrame class
306306

307-
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) {
307+
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) {
308308
(void)type_in;
309-
(void)args;
310-
mp_arg_check_num(n_args, n_kw, 0, 1, false);
311-
size_t size = AUDIO_CHUNK_SIZE;
312-
if (n_args == 1) {
313-
size = mp_obj_get_int(args[0]);
314-
if (size <= 0) {
315-
mp_raise_ValueError(MP_ERROR_TEXT("size out of bounds"));
316-
}
309+
310+
enum { ARG_duration, ARG_rate };
311+
static const mp_arg_t allowed_args[] = {
312+
{ MP_QSTR_duration, MP_ARG_INT, {.u_int = -1} },
313+
{ MP_QSTR_rate, MP_ARG_INT, {.u_int = 7812} },
314+
};
315+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
316+
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
317+
318+
mp_int_t rate = args[ARG_rate].u_int;
319+
if (rate <= 0) {
320+
mp_raise_ValueError(MP_ERROR_TEXT("rate out of bounds"));
321+
}
322+
323+
size_t size;
324+
if (args[ARG_duration].u_int < 0) {
325+
size = AUDIO_CHUNK_SIZE;
326+
} else if (args[ARG_duration].u_int == 0) {
327+
mp_raise_ValueError(MP_ERROR_TEXT("size out of bounds"));
328+
} else {
329+
size = args[ARG_duration].u_int * rate / 1000;
317330
}
318-
return microbit_audio_frame_make_new(size);
331+
332+
return microbit_audio_frame_make_new(size, rate);
319333
}
320334

321335
STATIC mp_obj_t audio_frame_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value_in) {
@@ -373,7 +387,7 @@ static void add_into(microbit_audio_frame_obj_t *self, microbit_audio_frame_obj_
373387
}
374388

375389
static microbit_audio_frame_obj_t *copy(microbit_audio_frame_obj_t *self) {
376-
microbit_audio_frame_obj_t *result = microbit_audio_frame_make_new(self->alloc_size);
390+
microbit_audio_frame_obj_t *result = microbit_audio_frame_make_new(self->alloc_size, self->rate);
377391
result->used_size = self->used_size;
378392
for (int i = 0; i < self->alloc_size; i++) {
379393
result->data[i] = self->data[i];
@@ -457,7 +471,26 @@ STATIC mp_obj_t audio_frame_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj
457471
}
458472
}
459473

474+
STATIC mp_obj_t audio_frame_get_rate(mp_obj_t self_in) {
475+
microbit_audio_frame_obj_t *self = MP_OBJ_TO_PTR(self_in);
476+
return MP_OBJ_NEW_SMALL_INT(self->rate);
477+
}
478+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(audio_frame_get_rate_obj, audio_frame_get_rate);
479+
480+
STATIC mp_obj_t audio_frame_set_rate(mp_obj_t self_in, mp_obj_t rate_in) {
481+
microbit_audio_frame_obj_t *self = MP_OBJ_TO_PTR(self_in);
482+
mp_int_t rate = mp_obj_get_int(rate_in);
483+
if (rate <= 0) {
484+
mp_raise_ValueError(MP_ERROR_TEXT("rate out of bounds"));
485+
}
486+
self->rate = rate;
487+
return mp_const_none;
488+
}
489+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(audio_frame_set_rate_obj, audio_frame_set_rate);
490+
460491
STATIC const mp_map_elem_t microbit_audio_frame_locals_dict_table[] = {
492+
{ MP_OBJ_NEW_QSTR(MP_QSTR_get_rate), (mp_obj_t)&audio_frame_get_rate_obj },
493+
{ MP_OBJ_NEW_QSTR(MP_QSTR_set_rate), (mp_obj_t)&audio_frame_set_rate_obj },
461494
{ MP_OBJ_NEW_QSTR(MP_QSTR_copyfrom), (mp_obj_t)&copyfrom_obj },
462495
};
463496
STATIC MP_DEFINE_CONST_DICT(microbit_audio_frame_locals_dict, microbit_audio_frame_locals_dict_table);
@@ -474,11 +507,12 @@ MP_DEFINE_CONST_OBJ_TYPE(
474507
locals_dict, &microbit_audio_frame_locals_dict
475508
);
476509

477-
microbit_audio_frame_obj_t *microbit_audio_frame_make_new(size_t size) {
510+
microbit_audio_frame_obj_t *microbit_audio_frame_make_new(size_t size, uint32_t rate) {
478511
microbit_audio_frame_obj_t *res = m_new_obj_var(microbit_audio_frame_obj_t, uint8_t, size);
479512
res->base.type = &microbit_audio_frame_type;
480513
res->alloc_size = size;
481514
res->used_size = 0;
515+
res->rate = rate;
482516
memset(res->data, 128, size);
483517
return res;
484518
}

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+
uint32_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, uint32_t rate);
4950

5051
const char *microbit_soundeffect_get_sound_expr_data(mp_obj_t self_in);
5152

0 commit comments

Comments
 (0)