From 753c68e5954a622c100f80235fdcd8c4256b0070 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 25 Mar 2024 16:11:44 +1100 Subject: [PATCH] codal_port/microbit_microphone: Implement wait argument to record_into. Signed-off-by: Damien George --- src/codal_port/microbit_microphone.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/codal_port/microbit_microphone.c b/src/codal_port/microbit_microphone.c index 00f0644..b5deefd 100644 --- a/src/codal_port/microbit_microphone.c +++ b/src/codal_port/microbit_microphone.c @@ -149,8 +149,13 @@ STATIC mp_obj_t microbit_microphone_get_events(mp_obj_t self_in) { STATIC MP_DEFINE_CONST_FUN_OBJ_1(microbit_microphone_get_events_obj, microbit_microphone_get_events); static void microbit_microphone_record_helper(microbit_audio_frame_obj_t *audio_frame, int rate, bool wait) { + // Set the rate of the AudioFrame, if specified. + if (rate > 0) { + audio_frame->rate = rate; + } + // Start the recording. - microbit_hal_microphone_start_recording(audio_frame->data, audio_frame->alloc_size, &audio_frame->used_size, rate); + microbit_hal_microphone_start_recording(audio_frame->data, audio_frame->alloc_size, &audio_frame->used_size, audio_frame->rate); if (wait) { // Wait for the recording to finish. @@ -176,6 +181,7 @@ static mp_obj_t microbit_microphone_record(mp_uint_t n_args, const mp_obj_t *pos size_t size = args[ARG_duration].u_int * args[ARG_rate].u_int / 1000; microbit_audio_frame_obj_t *audio_frame = microbit_audio_frame_make_new(size, args[ARG_rate].u_int); + // Start recording and wait. microbit_microphone_record_helper(audio_frame, args[ARG_rate].u_int, true); // Return the new AudioFrame. @@ -201,13 +207,8 @@ static mp_obj_t microbit_microphone_record_into(mp_uint_t n_args, const mp_obj_t } microbit_audio_frame_obj_t *audio_frame = MP_OBJ_TO_PTR(args[ARG_buffer].u_obj); - // Set the rate of the AudioFrame, if specified. - if (args[ARG_rate].u_int > 0) { - audio_frame->rate = args[ARG_rate].u_int; - } - - // Start the recording. - microbit_hal_microphone_start_recording(audio_frame->data, audio_frame->alloc_size, &audio_frame->used_size, audio_frame->rate); + // Start recording and wait if requested. + microbit_microphone_record_helper(audio_frame, args[ARG_rate].u_int, args[ARG_wait].u_bool); return mp_const_none; }