Skip to content

Commit 00b1788

Browse files
committed
audio: wait for audioRenderT to clear before running audioPlayerT
1 parent 888e4b4 commit 00b1788

File tree

3 files changed

+17
-18
lines changed

3 files changed

+17
-18
lines changed

main/inc/core/os.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ typedef enum user_event_group_bits {
3131
AUDIO_INPUT_RUN_BIT = BIT10,
3232
AUDIO_INPUT_FFT_BIT = BIT11,
3333

34-
AUDIO_RENDER_RUN_BIT = BIT12,
34+
AUDIO_RENDER_CLR_BIT = BIT12,
3535

3636
AUDIO_PLAYER_RUN_BIT = BIT13,
3737
AUDIO_PLAYER_IDLE_BIT = BIT14,

main/src/user/audio_player.c

+8-4
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ static void audio_player_task(void *pvParameters)
6666
struct mad_synth *synth = malloc(sizeof(struct mad_synth));
6767

6868
if ((stream == NULL) || (frame == NULL) || (synth == NULL)) {
69-
xEventGroupSetBits(user_event_group, AUDIO_RENDER_RUN_BIT);
7069
xEventGroupSetBits(user_event_group, AUDIO_PLAYER_IDLE_BIT);
7170
xEventGroupClearBits(user_event_group, AUDIO_PLAYER_RUN_BIT);
7271

@@ -113,7 +112,6 @@ static void audio_player_task(void *pvParameters)
113112
if (playback_pending) {
114113
playback_pending = 0;
115114
} else {
116-
xEventGroupSetBits(user_event_group, AUDIO_RENDER_RUN_BIT);
117115
xEventGroupSetBits(user_event_group, AUDIO_PLAYER_IDLE_BIT);
118116
xEventGroupClearBits(user_event_group, AUDIO_PLAYER_RUN_BIT);
119117
}
@@ -136,13 +134,19 @@ void audio_player_play_file(uint8_t idx)
136134
return;
137135
}
138136
if (uxBits & AUDIO_PLAYER_RUN_BIT) {
139-
// Previous playback is still not complete
137+
// previous playback is still not complete
140138
playback_pending = 1;
141139
} else {
142-
xEventGroupClearBits(user_event_group, AUDIO_RENDER_RUN_BIT);
143140
xEventGroupClearBits(user_event_group, AUDIO_PLAYER_IDLE_BIT);
144141
xEventGroupSetBits(user_event_group, AUDIO_PLAYER_RUN_BIT);
145142
}
143+
xEventGroupWaitBits(
144+
user_event_group,
145+
AUDIO_RENDER_CLR_BIT,
146+
pdFALSE,
147+
pdFALSE,
148+
portMAX_DELAY
149+
);
146150
#endif
147151
}
148152

main/src/user/audio_render.c

+8-13
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ void render_sample_block(short *sample_buff_ch0, short *sample_buff_ch1, int num
4040
}
4141

4242
size_t bytes_written = 0;
43-
for (int i = 0; i < num_samples; i++) {
43+
for (int i=0; i<num_samples; i++) {
4444
/* low - high / low - high */
4545
const char samp32[4] = {ptr_l[0], ptr_l[1], ptr_r[0], ptr_r[1]}; // ESP32 CPU is little-endian
4646

@@ -82,7 +82,7 @@ static void audio_buffer_reset(void)
8282

8383
static void audio_render_task(void *pvParameter)
8484
{
85-
bool clear = true;
85+
bool clear = false;
8686
bool start = false;
8787
uint16_t count = 0;
8888
EventBits_t uxBits = 0;
@@ -94,18 +94,13 @@ static void audio_render_task(void *pvParameter)
9494
uint32_t size = 0;
9595
uint32_t remain = 0;
9696

97-
xEventGroupWaitBits(
98-
user_event_group,
99-
AUDIO_RENDER_RUN_BIT,
100-
pdFALSE,
101-
pdFALSE,
102-
portMAX_DELAY
103-
);
104-
105-
if (!clear) {
97+
uxBits = xEventGroupGetBits(user_event_group);
98+
if (!clear && !(uxBits & AUDIO_RENDER_CLR_BIT)) {
10699
audio_buffer_reset();
107100

108101
clear = true;
102+
103+
xEventGroupSetBits(user_event_group, AUDIO_RENDER_CLR_BIT);
109104
}
110105

111106
if (start) {
@@ -143,6 +138,8 @@ static void audio_render_task(void *pvParameter)
143138
} else {
144139
if (xRingbufferGetCurFreeSize(audio_buff) < 512) {
145140
start = true;
141+
142+
xEventGroupClearBits(user_event_group, AUDIO_RENDER_CLR_BIT);
146143
} else {
147144
vTaskDelay(1 / portTICK_RATE_MS);
148145
}
@@ -213,8 +210,6 @@ static void audio_render_task(void *pvParameter)
213210

214211
void audio_render_init(void)
215212
{
216-
xEventGroupSetBits(user_event_group, AUDIO_RENDER_RUN_BIT);
217-
218213
memset(&buff_struct, 0x00, sizeof(StaticRingbuffer_t));
219214
audio_buff = xRingbufferCreateStatic(sizeof(buff_data), RINGBUF_TYPE_BYTEBUF, buff_data, &buff_struct);
220215

0 commit comments

Comments
 (0)