Skip to content

Commit 8528e89

Browse files
committed
codal_port/microbit_microphone: Implement wait argument to record_into.
Signed-off-by: Damien George <damien@micropython.org>
1 parent 71d9430 commit 8528e89

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/codal_port/microbit_microphone.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,13 @@ static mp_obj_t microbit_microphone_get_events(mp_obj_t self_in) {
160160
static MP_DEFINE_CONST_FUN_OBJ_1(microbit_microphone_get_events_obj, microbit_microphone_get_events);
161161

162162
static void microbit_microphone_record_helper(microbit_audio_frame_obj_t *audio_frame, int rate, bool wait) {
163+
// Set the rate of the AudioFrame, if specified.
164+
if (rate > 0) {
165+
audio_frame->rate = rate;
166+
}
167+
163168
// Start the recording.
164-
microbit_hal_microphone_start_recording(audio_frame->data, audio_frame->alloc_size, &audio_frame->used_size, rate);
169+
microbit_hal_microphone_start_recording(audio_frame->data, audio_frame->alloc_size, &audio_frame->used_size, audio_frame->rate);
165170

166171
if (wait) {
167172
// Wait for the recording to finish.
@@ -187,6 +192,7 @@ static mp_obj_t microbit_microphone_record(mp_uint_t n_args, const mp_obj_t *pos
187192
size_t size = args[ARG_duration].u_int * args[ARG_rate].u_int / 1000;
188193
microbit_audio_frame_obj_t *audio_frame = microbit_audio_frame_make_new(size, args[ARG_rate].u_int);
189194

195+
// Start recording and wait.
190196
microbit_microphone_record_helper(audio_frame, args[ARG_rate].u_int, true);
191197

192198
// Return the new AudioFrame.
@@ -212,13 +218,8 @@ static mp_obj_t microbit_microphone_record_into(mp_uint_t n_args, const mp_obj_t
212218
}
213219
microbit_audio_frame_obj_t *audio_frame = MP_OBJ_TO_PTR(args[ARG_buffer].u_obj);
214220

215-
// Set the rate of the AudioFrame, if specified.
216-
if (args[ARG_rate].u_int > 0) {
217-
audio_frame->rate = args[ARG_rate].u_int;
218-
}
219-
220-
// Start the recording.
221-
microbit_hal_microphone_start_recording(audio_frame->data, audio_frame->alloc_size, &audio_frame->used_size, audio_frame->rate);
221+
// Start recording and wait if requested.
222+
microbit_microphone_record_helper(audio_frame, args[ARG_rate].u_int, args[ARG_wait].u_bool);
222223

223224
return mp_const_none;
224225
}

0 commit comments

Comments
 (0)