micropython icon indicating copy to clipboard operation
micropython copied to clipboard

Async speech

Open seirl opened this issue 7 years ago • 0 comments

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 --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.

seirl avatar Aug 30 '18 12:08 seirl