Skip to content

Commit 305c396

Browse files
committed
codal_port/modaudio: Improve feeding of audio pipeline.
Signed-off-by: Damien George <[email protected]>
1 parent 180be6f commit 305c396

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/codal_port/modaudio.c

+18-4
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
#define audio_source_iter MP_STATE_PORT(audio_source_iter_state)
3838

3939
#ifndef AUDIO_OUTPUT_BUFFER_SIZE
40-
#define AUDIO_OUTPUT_BUFFER_SIZE (32)
40+
#define AUDIO_OUTPUT_BUFFER_SIZE (64)
4141
#endif
4242

4343
#define DEFAULT_AUDIO_FRAME_SIZE (32)
@@ -56,6 +56,8 @@ static size_t audio_source_frame_offset;
5656
static uint32_t audio_current_sound_level;
5757
static mp_sched_node_t audio_data_fetcher_sched_node;
5858

59+
static void audio_data_schedule_fetch(void);
60+
5961
static inline bool audio_is_running(void) {
6062
return audio_source_frame != NULL || audio_source_track != NULL || audio_source_iter != MP_OBJ_NULL;
6163
}
@@ -169,7 +171,7 @@ static void audio_data_fetcher(mp_sched_node_t *node) {
169171

170172
if (audio_output_buffer_offset < AUDIO_OUTPUT_BUFFER_SIZE) {
171173
// Output buffer not full yet, so attempt to pull more data from the source.
172-
mp_sched_schedule_node(&audio_data_fetcher_sched_node, audio_data_fetcher);
174+
audio_data_schedule_fetch();
173175
} else {
174176
// Output buffer is full, process it and prepare for next buffer fill.
175177
audio_output_buffer_offset = 0;
@@ -192,6 +194,18 @@ static void audio_data_fetcher(mp_sched_node_t *node) {
192194
}
193195
}
194196

197+
static void audio_data_schedule_fetch(void) {
198+
if (audio_source_track != NULL && audio_source_frame_offset < audio_source_track->size) {
199+
// An existing AudioTrack is being played, and still has some data remaining,
200+
// so fetch that immediately. This helps to keep audio playback smooth when the
201+
// playback rate is large.
202+
audio_data_fetcher(&audio_data_fetcher_sched_node);
203+
} else {
204+
// Schedule audio_data_fetcher to be executed ASAP to try and fetch more data.
205+
mp_sched_schedule_node(&audio_data_fetcher_sched_node, audio_data_fetcher);
206+
}
207+
}
208+
195209
void microbit_hal_audio_raw_ready_callback(void) {
196210
if (audio_output_state == AUDIO_OUTPUT_STATE_DATA_READY) {
197211
// there is data ready to send out to the audio pipeline, so send it
@@ -202,8 +216,8 @@ void microbit_hal_audio_raw_ready_callback(void) {
202216
audio_output_state = AUDIO_OUTPUT_STATE_IDLE;
203217
}
204218

205-
// schedule audio_data_fetcher to be executed to prepare the next buffer
206-
mp_sched_schedule_node(&audio_data_fetcher_sched_node, audio_data_fetcher);
219+
// Schedule the next fetch of audio data.
220+
audio_data_schedule_fetch();
207221
}
208222

209223
static void audio_init(uint32_t sample_rate) {

0 commit comments

Comments
 (0)