bbcmicrobit / bbcmicrobit/micropython

Async speech

Open
#549 0 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
C
Stars
646
Forks
290
PR merge metrics
No merged PRs in 30d

Description

It would be great to add a wait parameter to `speech.say()` and similar, to allow async behavior. I started to write a diff like this:

```diff
diff --git a/source/microbit/modspeech.c b/source/microbit/modspeech.c
index fe76fec..edeab3a 100644
--- a/source/microbit/modspeech.c
+++ b/source/microbit/modspeech.c
@@ -152,7 +152,8 @@ static mp_obj_t articulate(mp_obj_t phonemes, mp_uint_t n_args, const mp_obj_t *
{ MP_QSTR_speed, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = DEFAULT_SPEED} },
{ MP_QSTR_mouth, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = DEFAULT_MOUTH} },
{ MP_QSTR_throat, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = DEFAULT_THROAT} },
- { MP_QSTR_debug, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
+ { MP_QSTR_debug, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
+ { MP_QSTR_wait, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} },
};

// parse args
@@ -190,8 +191,11 @@ static mp_obj_t articulate(mp_obj_t phonemes, mp_uint_t n_args, const mp_obj_t *
}

last_frame = true;
+
/* Wait for audio finish before returning */
- while (microbit_audio_is_playing());
+ if (args[5].u_bool)
+ while (microbit_audio_is_playing());
+
MP_STATE_PORT(speech_data) = NULL;
if (debug) {
printf("Glitches: %d\r\n", glitches);

```

But I have a few concerns:

- It means we clear `speech_data` before the speech is over, and I don't know if that's an issue. If we need to clear it after the speech is over, we would need some kind of teardown callback, but I'm not sure how to proceed to add that since it looks a lot more low level.
- The yotta package of my distribution is broken right now so I have no way to build the project to test that on my Micro:bit.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.