From 8dfe3958031618e7474d28d0316aaa69467891e1 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 27 Feb 2024 10:30:38 +1100 Subject: [PATCH] codal_app/microbithal_audio: Set audio channel sample rate on init. Fixes the case of using `speech.say(..., mode=mode)` when the mode changes and selects a different output sample rate. Signed-off-by: Damien George --- src/codal_app/microbithal_audio.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/codal_app/microbithal_audio.cpp b/src/codal_app/microbithal_audio.cpp index 272efef..d27b429 100644 --- a/src/codal_app/microbithal_audio.cpp +++ b/src/codal_app/microbithal_audio.cpp @@ -33,6 +33,7 @@ class AudioSource : public DataSource { DataSink *sink; ManagedBuffer buf; void (*callback)(void); + MixerChannel *channel; AudioSource() : started(false) { @@ -109,7 +110,9 @@ void microbit_hal_audio_init(uint32_t sample_rate) { MicroBitAudio::requestActivation(); data_source.started = true; data_source.callback = microbit_hal_audio_ready_callback; - uBit.audio.mixer.addChannel(data_source, sample_rate, 255); + data_source.channel = uBit.audio.mixer.addChannel(data_source, sample_rate, 255); + } else { + data_source.channel->setSampleRate(sample_rate); } } @@ -126,7 +129,9 @@ void microbit_hal_audio_speech_init(uint32_t sample_rate) { MicroBitAudio::requestActivation(); speech_source.started = true; speech_source.callback = microbit_hal_audio_speech_ready_callback; - uBit.audio.mixer.addChannel(speech_source, sample_rate, 255); + speech_source.channel = uBit.audio.mixer.addChannel(speech_source, sample_rate, 255); + } else { + speech_source.channel->setSampleRate(sample_rate); } }