Skip to content

Commit aed6229

Browse files
committed
codal_port/microbit_microphone: Implement microphone.record().
Signed-off-by: Damien George <damien@micropython.org>
1 parent 3881d10 commit aed6229

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

src/codal_port/microbit_microphone.c

Lines changed: 32 additions & 1 deletion
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,11 +148,40 @@ 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 mp_obj_t microbit_microphone_record(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
152+
enum { ARG_duration, ARG_rate, };
153+
static const mp_arg_t allowed_args[] = {
154+
{ MP_QSTR_duration, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} },
155+
{ MP_QSTR_rate, MP_ARG_INT, {.u_int = 7812} },
156+
};
157+
158+
// Parse the args.
159+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
160+
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
161+
162+
// Create the AudioFrame to record into.
163+
size_t size = args[ARG_duration].u_int * args[ARG_rate].u_int / 1000;
164+
microbit_audio_frame_obj_t *audio_frame = microbit_audio_frame_make_new(size);
165+
166+
// Start the recording.
167+
microbit_hal_microphone_start_recording(audio_frame->data, audio_frame->alloc_size, args[ARG_rate].u_int);
168+
169+
// Wait for the recording to finish.
170+
while (microbit_hal_microphone_is_recording()) {
171+
mp_handle_pending(true);
172+
microbit_hal_idle();
173+
}
174+
175+
// Return the new AudioFrame.
176+
return MP_OBJ_FROM_PTR(audio_frame);
177+
}
178+
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(microbit_microphone_record_obj, 1, microbit_microphone_record);
179+
150180
STATIC mp_obj_t microbit_microphone_record_into(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
151181
enum { ARG_buffer, ARG_rate, ARG_wait, };
152182
static const mp_arg_t allowed_args[] = {
153183
{ MP_QSTR_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
154-
{ MP_QSTR_rate, MP_ARG_INT, {.u_obj = MP_ROM_INT(7812)} },
184+
{ MP_QSTR_rate, MP_ARG_INT, {.u_int = 7812} },
155185
{ MP_QSTR_wait, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} },
156186
};
157187

@@ -188,6 +218,7 @@ STATIC const mp_rom_map_elem_t microbit_microphone_locals_dict_table[] = {
188218
{ MP_ROM_QSTR(MP_QSTR_is_event), MP_ROM_PTR(&microbit_microphone_is_event_obj) },
189219
{ MP_ROM_QSTR(MP_QSTR_was_event), MP_ROM_PTR(&microbit_microphone_was_event_obj) },
190220
{ MP_ROM_QSTR(MP_QSTR_get_events), MP_ROM_PTR(&microbit_microphone_get_events_obj) },
221+
{ MP_ROM_QSTR(MP_QSTR_record), MP_ROM_PTR(&microbit_microphone_record_obj) },
191222
{ MP_ROM_QSTR(MP_QSTR_record_into), MP_ROM_PTR(&microbit_microphone_record_into_obj) },
192223
{ MP_ROM_QSTR(MP_QSTR_is_recording), MP_ROM_PTR(&microbit_microphone_is_recording_obj) },
193224
{ MP_ROM_QSTR(MP_QSTR_stop_recording), MP_ROM_PTR(&microbit_microphone_stop_recording_obj) },

0 commit comments

Comments
 (0)