@@ -47,9 +47,9 @@ typedef enum {
47
47
48
48
static uint8_t audio_output_buffer [AUDIO_CHUNK_SIZE ];
49
49
static volatile audio_output_state_t audio_output_state ;
50
- static volatile bool audio_fetcher_scheduled ;
51
50
static size_t audio_raw_offset ;
52
51
static uint32_t audio_current_sound_level ;
52
+ static mp_sched_node_t audio_data_fetcher_sched_node ;
53
53
54
54
static inline bool audio_is_running (void ) {
55
55
return audio_source_frame != NULL || audio_source_iter != MP_OBJ_NULL ;
@@ -73,9 +73,7 @@ static void audio_buffer_ready(void) {
73
73
}
74
74
}
75
75
76
- static void audio_data_fetcher (void ) {
77
- audio_fetcher_scheduled = false;
78
-
76
+ static void audio_data_fetcher (mp_sched_node_t * node ) {
79
77
if (audio_source_frame != NULL ) {
80
78
// An existing AudioFrame is being played, see if there's any data left.
81
79
if (audio_raw_offset >= audio_source_frame -> used_size ) {
@@ -146,12 +144,6 @@ static void audio_data_fetcher(void) {
146
144
audio_buffer_ready ();
147
145
}
148
146
149
- static mp_obj_t audio_data_fetcher_wrapper (mp_obj_t arg ) {
150
- audio_data_fetcher ();
151
- return mp_const_none ;
152
- }
153
- static MP_DEFINE_CONST_FUN_OBJ_1 (audio_data_fetcher_wrapper_obj , audio_data_fetcher_wrapper ) ;
154
-
155
147
void microbit_hal_audio_raw_ready_callback (void ) {
156
148
if (audio_output_state == AUDIO_OUTPUT_STATE_DATA_READY ) {
157
149
// there is data ready to send out to the audio pipeline, so send it
@@ -161,14 +153,12 @@ void microbit_hal_audio_raw_ready_callback(void) {
161
153
// no data ready, need to call this function later when data is ready
162
154
audio_output_state = AUDIO_OUTPUT_STATE_IDLE ;
163
155
}
164
- if (!audio_fetcher_scheduled ) {
165
- // schedule audio_data_fetcher to be executed to prepare the next buffer
166
- audio_fetcher_scheduled = mp_sched_schedule (MP_OBJ_FROM_PTR (& audio_data_fetcher_wrapper_obj ), mp_const_none );
167
- }
156
+
157
+ // schedule audio_data_fetcher to be executed to prepare the next buffer
158
+ mp_sched_schedule_node (& audio_data_fetcher_sched_node , audio_data_fetcher );
168
159
}
169
160
170
161
static void audio_init (uint32_t sample_rate ) {
171
- audio_fetcher_scheduled = false;
172
162
audio_output_state = AUDIO_OUTPUT_STATE_IDLE ;
173
163
microbit_hal_audio_raw_init (sample_rate );
174
164
}
@@ -239,7 +229,7 @@ void microbit_audio_play_source(mp_obj_t src, mp_obj_t pin_select, bool wait, ui
239
229
// Start the audio running.
240
230
// The scheduler must be locked because audio_data_fetcher() can also be called from the scheduler.
241
231
mp_sched_lock ();
242
- audio_data_fetcher ();
232
+ audio_data_fetcher (& audio_data_fetcher_sched_node );
243
233
mp_sched_unlock ();
244
234
245
235
if (wait ) {
0 commit comments