Skip to content

Commit e8a5d61

Browse files
committed
codal_port/modaudio: Make None the default to AudioFrame().
And allow duration to be a float, for more accurate sizing. Signed-off-by: Damien George <damien@micropython.org>
1 parent 61aaf2a commit e8a5d61

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/codal_port/modaudio.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ static mp_obj_t microbit_audio_frame_new(const mp_obj_type_t *type_in, size_t n_
317317

318318
enum { ARG_duration, ARG_rate };
319319
static const mp_arg_t allowed_args[] = {
320-
{ MP_QSTR_duration, MP_ARG_INT, {.u_int = -1} },
320+
{ MP_QSTR_duration, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
321321
{ MP_QSTR_rate, MP_ARG_INT, {.u_int = DEFAULT_SAMPLE_RATE} },
322322
};
323323
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
@@ -329,12 +329,14 @@ static mp_obj_t microbit_audio_frame_new(const mp_obj_type_t *type_in, size_t n_
329329
}
330330

331331
size_t size;
332-
if (args[ARG_duration].u_int < 0) {
332+
if (args[ARG_duration].u_obj == mp_const_none) {
333333
size = AUDIO_CHUNK_SIZE;
334-
} else if (args[ARG_duration].u_int == 0) {
335-
mp_raise_ValueError(MP_ERROR_TEXT("size out of bounds"));
336334
} else {
337-
size = args[ARG_duration].u_int * rate / 1000;
335+
mp_float_t duration = mp_obj_get_float(args[ARG_duration].u_obj);
336+
if (duration <= 0) {
337+
mp_raise_ValueError(MP_ERROR_TEXT("size out of bounds"));
338+
}
339+
size = duration * rate / 1000;
338340
}
339341

340342
return microbit_audio_frame_make_new(size, rate);

0 commit comments

Comments
 (0)