37
37
#define audio_source_iter MP_STATE_PORT(audio_source_iter_state)
38
38
39
39
#ifndef AUDIO_OUTPUT_BUFFER_SIZE
40
- #define AUDIO_OUTPUT_BUFFER_SIZE (32 )
40
+ #define AUDIO_OUTPUT_BUFFER_SIZE (64 )
41
41
#endif
42
42
43
43
#define DEFAULT_AUDIO_FRAME_SIZE (32)
@@ -56,6 +56,8 @@ static size_t audio_source_frame_offset;
56
56
static uint32_t audio_current_sound_level ;
57
57
static mp_sched_node_t audio_data_fetcher_sched_node ;
58
58
59
+ static void audio_data_schedule_fetch (void );
60
+
59
61
static inline bool audio_is_running (void ) {
60
62
return audio_source_frame != NULL || audio_source_track != NULL || audio_source_iter != MP_OBJ_NULL ;
61
63
}
@@ -169,7 +171,7 @@ static void audio_data_fetcher(mp_sched_node_t *node) {
169
171
170
172
if (audio_output_buffer_offset < AUDIO_OUTPUT_BUFFER_SIZE ) {
171
173
// 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 ( );
173
175
} else {
174
176
// Output buffer is full, process it and prepare for next buffer fill.
175
177
audio_output_buffer_offset = 0 ;
@@ -192,6 +194,18 @@ static void audio_data_fetcher(mp_sched_node_t *node) {
192
194
}
193
195
}
194
196
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
+
195
209
void microbit_hal_audio_raw_ready_callback (void ) {
196
210
if (audio_output_state == AUDIO_OUTPUT_STATE_DATA_READY ) {
197
211
// 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) {
202
216
audio_output_state = AUDIO_OUTPUT_STATE_IDLE ;
203
217
}
204
218
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 ( );
207
221
}
208
222
209
223
static void audio_init (uint32_t sample_rate ) {
0 commit comments