|
26 | 26 |
|
27 | 27 | #include "py/runtime.h" |
28 | 28 | #include "py/mphal.h" |
| 29 | +#include "modaudio.h" |
29 | 30 | #include "modmicrobit.h" |
30 | 31 |
|
31 | 32 | #define EVENT_HISTORY_SIZE (8) |
@@ -147,11 +148,40 @@ STATIC mp_obj_t microbit_microphone_get_events(mp_obj_t self_in) { |
147 | 148 | } |
148 | 149 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(microbit_microphone_get_events_obj, microbit_microphone_get_events); |
149 | 150 |
|
| 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 | + |
150 | 180 | STATIC mp_obj_t microbit_microphone_record_into(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
151 | 181 | enum { ARG_buffer, ARG_rate, ARG_wait, }; |
152 | 182 | static const mp_arg_t allowed_args[] = { |
153 | 183 | { 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} }, |
155 | 185 | { MP_QSTR_wait, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} }, |
156 | 186 | }; |
157 | 187 |
|
@@ -188,6 +218,7 @@ STATIC const mp_rom_map_elem_t microbit_microphone_locals_dict_table[] = { |
188 | 218 | { MP_ROM_QSTR(MP_QSTR_is_event), MP_ROM_PTR(µbit_microphone_is_event_obj) }, |
189 | 219 | { MP_ROM_QSTR(MP_QSTR_was_event), MP_ROM_PTR(µbit_microphone_was_event_obj) }, |
190 | 220 | { MP_ROM_QSTR(MP_QSTR_get_events), MP_ROM_PTR(µbit_microphone_get_events_obj) }, |
| 221 | + { MP_ROM_QSTR(MP_QSTR_record), MP_ROM_PTR(µbit_microphone_record_obj) }, |
191 | 222 | { MP_ROM_QSTR(MP_QSTR_record_into), MP_ROM_PTR(µbit_microphone_record_into_obj) }, |
192 | 223 | { MP_ROM_QSTR(MP_QSTR_is_recording), MP_ROM_PTR(µbit_microphone_is_recording_obj) }, |
193 | 224 | { MP_ROM_QSTR(MP_QSTR_stop_recording), MP_ROM_PTR(µbit_microphone_stop_recording_obj) }, |
|
0 commit comments