Skip to content

Commit 0441aa2

Browse files
committed
codal_port/microbit_microphone: Require rate to be positive.
Signed-off-by: Damien George <damien@micropython.org>
1 parent e8a5d61 commit 0441aa2

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/codal_port/microbit_microphone.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,7 @@ static mp_obj_t microbit_microphone_get_events(mp_obj_t self_in) {
166166
}
167167
static MP_DEFINE_CONST_FUN_OBJ_1(microbit_microphone_get_events_obj, microbit_microphone_get_events);
168168

169-
static void microbit_microphone_record_helper(microbit_audio_frame_obj_t *audio_frame, int rate, bool wait) {
170-
// Set the rate of the AudioFrame, if specified.
171-
if (rate > 0) {
172-
audio_frame->rate = rate;
173-
}
174-
169+
static void microbit_microphone_record_helper(microbit_audio_frame_obj_t *audio_frame, bool wait) {
175170
// Start the recording.
176171
microbit_hal_microphone_start_recording(audio_frame->data, audio_frame->alloc_size, &audio_frame->used_size, audio_frame->rate);
177172

@@ -208,7 +203,7 @@ static mp_obj_t microbit_microphone_record(mp_uint_t n_args, const mp_obj_t *pos
208203
microbit_audio_frame_obj_t *audio_frame = microbit_audio_frame_make_new(size, args[ARG_rate].u_int);
209204

210205
// Start recording and wait.
211-
microbit_microphone_record_helper(audio_frame, args[ARG_rate].u_int, true);
206+
microbit_microphone_record_helper(audio_frame, true);
212207

213208
// Return the new AudioFrame.
214209
return MP_OBJ_FROM_PTR(audio_frame);
@@ -219,7 +214,7 @@ static mp_obj_t microbit_microphone_record_into(mp_uint_t n_args, const mp_obj_t
219214
enum { ARG_buffer, ARG_rate, ARG_wait, };
220215
static const mp_arg_t allowed_args[] = {
221216
{ MP_QSTR_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
222-
{ MP_QSTR_rate, MP_ARG_INT, {.u_int = 0} },
217+
{ MP_QSTR_rate, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
223218
{ MP_QSTR_wait, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} },
224219
};
225220

@@ -233,8 +228,18 @@ static mp_obj_t microbit_microphone_record_into(mp_uint_t n_args, const mp_obj_t
233228
}
234229
microbit_audio_frame_obj_t *audio_frame = MP_OBJ_TO_PTR(args[ARG_buffer].u_obj);
235230

231+
// Check if the rate is specified.
232+
if (args[ARG_rate].u_obj != mp_const_none) {
233+
// Update the AudioFrame to use the specified rate.
234+
mp_int_t rate = mp_obj_get_int(args[ARG_rate].u_obj);
235+
if (rate <= 0) {
236+
mp_raise_ValueError(MP_ERROR_TEXT("rate out of bounds"));
237+
}
238+
audio_frame->rate = rate;
239+
}
240+
236241
// Start recording and wait if requested.
237-
microbit_microphone_record_helper(audio_frame, args[ARG_rate].u_int, args[ARG_wait].u_bool);
242+
microbit_microphone_record_helper(audio_frame, args[ARG_wait].u_bool);
238243

239244
return mp_const_none;
240245
}

0 commit comments

Comments
 (0)