Skip to content

Commit a83b48f

Browse files
committed
codal_port/microbit_microphone: Add methods to record.
Signed-off-by: Damien George <damien@micropython.org>
1 parent 45ed1b7 commit a83b48f

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

src/codal_port/microbit_microphone.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include "py/runtime.h"
2828
#include "py/mphal.h"
29+
#include "modaudio.h"
2930
#include "modmicrobit.h"
3031

3132
#define EVENT_HISTORY_SIZE (8)
@@ -147,13 +148,86 @@ STATIC mp_obj_t microbit_microphone_get_events(mp_obj_t self_in) {
147148
}
148149
STATIC MP_DEFINE_CONST_FUN_OBJ_1(microbit_microphone_get_events_obj, microbit_microphone_get_events);
149150

151+
static void microbit_microphone_record_helper(microbit_audio_frame_obj_t *audio_frame, int rate, bool wait) {
152+
// Start the recording.
153+
microbit_hal_microphone_start_recording(audio_frame->data, audio_frame->alloc_size, rate);
154+
155+
if (wait) {
156+
// Wait for the recording to finish.
157+
while (microbit_hal_microphone_is_recording()) {
158+
mp_handle_pending(true);
159+
microbit_hal_idle();
160+
}
161+
}
162+
}
163+
164+
static mp_obj_t microbit_microphone_record(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
165+
enum { ARG_duration, ARG_rate, };
166+
static const mp_arg_t allowed_args[] = {
167+
{ MP_QSTR_duration, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} },
168+
{ MP_QSTR_rate, MP_ARG_INT, {.u_int = 7812} },
169+
};
170+
171+
// Parse the args.
172+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
173+
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
174+
175+
// Create the AudioFrame to record into.
176+
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);
178+
179+
microbit_microphone_record_helper(audio_frame, args[ARG_rate].u_int, true);
180+
181+
// Return the new AudioFrame.
182+
return MP_OBJ_FROM_PTR(audio_frame);
183+
}
184+
static MP_DEFINE_CONST_FUN_OBJ_KW(microbit_microphone_record_obj, 1, microbit_microphone_record);
185+
186+
static mp_obj_t microbit_microphone_record_into(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
187+
enum { ARG_buffer, ARG_rate, ARG_wait, };
188+
static const mp_arg_t allowed_args[] = {
189+
{ MP_QSTR_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
190+
{ MP_QSTR_rate, MP_ARG_INT, {.u_int = 7812} },
191+
{ MP_QSTR_wait, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} },
192+
};
193+
194+
// Parse the args.
195+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
196+
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
197+
198+
mp_buffer_info_t bufinfo;
199+
mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_READ);
200+
201+
microbit_hal_microphone_start_recording(bufinfo.buf, bufinfo.len, args[ARG_rate].u_int);
202+
203+
return mp_const_none;
204+
}
205+
static MP_DEFINE_CONST_FUN_OBJ_KW(microbit_microphone_record_into_obj, 1, microbit_microphone_record_into);
206+
207+
static mp_obj_t microbit_microphone_is_recording(mp_obj_t self_in) {
208+
(void)self_in;
209+
return mp_obj_new_bool(microbit_hal_microphone_is_recording());
210+
}
211+
static MP_DEFINE_CONST_FUN_OBJ_1(microbit_microphone_is_recording_obj, microbit_microphone_is_recording);
212+
213+
static mp_obj_t microbit_microphone_stop_recording(mp_obj_t self_in) {
214+
(void)self_in;
215+
microbit_hal_microphone_stop_recording();
216+
return mp_const_none;
217+
}
218+
static MP_DEFINE_CONST_FUN_OBJ_1(microbit_microphone_stop_recording_obj, microbit_microphone_stop_recording);
219+
150220
STATIC const mp_rom_map_elem_t microbit_microphone_locals_dict_table[] = {
151221
{ MP_ROM_QSTR(MP_QSTR_set_threshold), MP_ROM_PTR(&microbit_microphone_set_threshold_obj) },
152222
{ MP_ROM_QSTR(MP_QSTR_sound_level), MP_ROM_PTR(&microbit_microphone_sound_level_obj) },
153223
{ MP_ROM_QSTR(MP_QSTR_current_event), MP_ROM_PTR(&microbit_microphone_current_event_obj) },
154224
{ MP_ROM_QSTR(MP_QSTR_is_event), MP_ROM_PTR(&microbit_microphone_is_event_obj) },
155225
{ MP_ROM_QSTR(MP_QSTR_was_event), MP_ROM_PTR(&microbit_microphone_was_event_obj) },
156226
{ MP_ROM_QSTR(MP_QSTR_get_events), MP_ROM_PTR(&microbit_microphone_get_events_obj) },
227+
{ MP_ROM_QSTR(MP_QSTR_record), MP_ROM_PTR(&microbit_microphone_record_obj) },
228+
{ MP_ROM_QSTR(MP_QSTR_record_into), MP_ROM_PTR(&microbit_microphone_record_into_obj) },
229+
{ MP_ROM_QSTR(MP_QSTR_is_recording), MP_ROM_PTR(&microbit_microphone_is_recording_obj) },
230+
{ MP_ROM_QSTR(MP_QSTR_stop_recording), MP_ROM_PTR(&microbit_microphone_stop_recording_obj) },
157231
};
158232
STATIC MP_DEFINE_CONST_DICT(microbit_microphone_locals_dict, microbit_microphone_locals_dict_table);
159233

0 commit comments

Comments
 (0)