Skip to content

Commit 4d71da8

Browse files
committed
codal_port/modaudio: Properly support sound exprs with wait=False/True.
Now they will play in the foreground (block) by default, ctrl-C will stop them playing (if wait=True), and any active expression will be stopped before playing another. Fixes issue #76. Signed-off-by: Damien George <[email protected]>
1 parent 5b90b75 commit 4d71da8

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/codal_port/modaudio.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,23 @@ void microbit_audio_play_source(mp_obj_t src, mp_obj_t pin_select, bool wait, ui
143143
microbit_pin_audio_select(pin_select);
144144

145145
if (mp_obj_is_type(src, &microbit_sound_type)) {
146-
// TODO support wait=True mode
147146
const microbit_sound_obj_t *sound = (const microbit_sound_obj_t *)MP_OBJ_TO_PTR(src);
148147
microbit_hal_audio_play_expression_by_name(sound->name);
148+
if (wait) {
149+
nlr_buf_t nlr;
150+
if (nlr_push(&nlr) == 0) {
151+
// Wait for the expression to finish playing.
152+
while (microbit_hal_audio_is_expression_active()) {
153+
mp_handle_pending(true);
154+
microbit_hal_idle();
155+
}
156+
nlr_pop();
157+
} else {
158+
// Catch all exceptions and stop the audio before re-raising.
159+
microbit_hal_audio_stop_expression();
160+
nlr_jump(nlr.ret_val);
161+
}
162+
}
149163
return;
150164
}
151165

0 commit comments

Comments
 (0)