microbit-foundation / micropython-microbit-v2-simulator

Wasm-based MicroPython micro:bit v2 simulator
https://python-simulator.usermbit.org/v/0.1/demo.html
MIT License
13 stars 10 forks source link

modspeech doesn't work on iPad Safari 13.x and 14.1 (works on 14.5) #110

Open microbit-matt-hillsdon opened 9 months ago

microbit-matt-hillsdon commented 9 months ago

On #109 we fixed audio for older Safari by using the prefixed APIs and older style of node/buffer creation.

It didn't fix speech because modspeech uses a 19000 sample rate but old Safari has a lower bound of 22050 (contrary to the spec but it was early days).

Coincidentally (I assume!), modspeech has a mode=3 option that doubles the sample rate. That mode works in Safari 13. There’s also some code for linear interpolation but not sure it happens with mode=3 or whether it's needed for an OK result.

So far as I can tell we can only trigger mode=3 by passing an undocumented mode keyword argument from user code.

@dpgeorge what is the mode used for? Do you think we could use this or something similar to get a >= 22050 sample rate in the simulator? We could consider doing this just for older Safari if we felt it would impact the output. We could detect the need to do this in the speech hal init function or any point after a user interaction with the simulator.

Alternatively we could look at resampling in JavaScript between modspeech and Web Audio. The older Web Audio API has no support for this so we'd be doing it ourselves.

microbit-matt-hillsdon commented 9 months ago

CC @microbit-robert

microbit-matt-hillsdon commented 8 months ago

Since #111 (unreleased, available on staging) we now handle the error and just skip playing rather than leaving the sim in a bad state. Still interested in persuing a sample rate change.

dpgeorge commented 8 months ago

Coincidentally (I assume!), modspeech has a mode=3 option that doubles the sample rate. That mode works in Safari 13. There’s also some code for linear interpolation but not sure it happens with mode=3 or whether it's needed for an OK result.

I've just tested mode=3 and fixed a small bug related to it (now pushed to master micropython-microbit-v2). I think it would be fine to use this mode, it sounds similar enough to the default mode=1.

There's also a mode=4 which is a linearly interpolated version of mode=3. You can also try this mode 4, it may be better (or worse!) on a PC's speaker.

So far as I can tell we can only trigger mode=3 by passing an undocumented mode keyword argument from user code.

Yes, that's correct.

I guess you would like to have the default mode compile-time configurable?

microbit-matt-hillsdon commented 8 months ago

I guess you would like to have the default mode compile-time configurable?

@dpgeorge yes, I think it would make sense for the simulator build to define the mode.

dpgeorge commented 8 months ago

OK, I've now pushed a commit to the micropython-microbit-v2 repository which allows setting the default mode. You can add this line to the simulator's mpconfigport.h file:

#define MICROPY_PY_SPEECH_DEFAULT_MODE (3)
microbit-matt-hillsdon commented 7 months ago

OK, I've now pushed a commit to the micropython-microbit-v2 repository which allows setting the default mode. You can add this line to the simulator's mpconfigport.h file:

#define MICROPY_PY_SPEECH_DEFAULT_MODE (3)

Thanks, confirmed this works in Safari 13. I'll close this issue when the simulator changes are merged.