Skip to content

Commit 3bbbb7e

Browse files
committed
audio_render: fix startup glitches
1 parent 9fe5692 commit 3bbbb7e

File tree

3 files changed

+66
-51
lines changed

3 files changed

+66
-51
lines changed

main/inc/core/os.h

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,27 @@ typedef enum user_event_group_bits {
1515
OS_PWR_SLEEP_BIT = BIT0,
1616
OS_PWR_RESTART_BIT = BIT1,
1717

18-
BT_SPP_IDLE_BIT = BIT2,
19-
BT_A2DP_IDLE_BIT = BIT3,
20-
BT_OTA_LOCKED_BIT = BIT4,
21-
BLE_GATTS_IDLE_BIT = BIT5,
18+
BT_A2DP_IDLE_BIT = BIT2,
19+
BT_A2DP_DATA_BIT = BIT3,
2220

23-
VFX_RELOAD_BIT = BIT6,
24-
VFX_FFT_NULL_BIT = BIT7,
21+
BT_SPP_IDLE_BIT = BIT4,
22+
BT_OTA_LOCKED_BIT = BIT5,
2523

26-
KEY_SCAN_RUN_BIT = BIT8,
24+
BLE_GATTS_IDLE_BIT = BIT6,
2725

28-
AUDIO_INPUT_RUN_BIT = BIT9,
29-
AUDIO_INPUT_FFT_BIT = BIT10,
26+
VFX_RELOAD_BIT = BIT7,
27+
VFX_FFT_NULL_BIT = BIT8,
3028

31-
AUDIO_RENDER_RUN_BIT = BIT11,
29+
KEY_SCAN_RUN_BIT = BIT9,
3230

33-
AUDIO_PLAYER_RUN_BIT = BIT12,
34-
AUDIO_PLAYER_IDLE_BIT = BIT13,
35-
AUDIO_PLAYER_EXIT_BIT = BIT14,
31+
AUDIO_INPUT_RUN_BIT = BIT10,
32+
AUDIO_INPUT_FFT_BIT = BIT11,
33+
34+
AUDIO_RENDER_RUN_BIT = BIT12,
35+
36+
AUDIO_PLAYER_RUN_BIT = BIT13,
37+
AUDIO_PLAYER_IDLE_BIT = BIT14,
38+
AUDIO_PLAYER_EXIT_BIT = BIT15,
3639
} user_event_group_bits_t;
3740

3841
extern EventGroupHandle_t user_event_group;

main/src/user/audio_render.c

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
#define TAG "audio_render"
2424

25-
static uint8_t buff_data[8*1024] = {0};
25+
static uint8_t buff_data[4*1024] = {0};
2626
static StaticRingbuffer_t buff_struct = {0};
2727

2828
RingbufHandle_t audio_buff = NULL;
@@ -57,9 +57,35 @@ void set_dac_sample_rate(int rate)
5757
i2s_output_set_sample_rate(rate);
5858
}
5959

60+
static void audio_buffer_reset(void)
61+
{
62+
vTaskSuspendAll();
63+
64+
EventBits_t uxBits = xEventGroupGetBits(user_event_group);
65+
if (uxBits & BT_A2DP_DATA_BIT) {
66+
goto reset_abort;
67+
}
68+
69+
#ifdef CONFIG_ENABLE_VFX
70+
if (!(uxBits & AUDIO_INPUT_RUN_BIT) && (uxBits & AUDIO_INPUT_FFT_BIT)) {
71+
memset(vfx_fft_input, 0x00, sizeof(vfx_fft_input));
72+
xEventGroupClearBits(user_event_group, VFX_FFT_NULL_BIT);
73+
}
74+
#endif
75+
76+
memset(&buff_struct, 0x00, sizeof(StaticRingbuffer_t));
77+
audio_buff = xRingbufferCreateStatic(sizeof(buff_data), RINGBUF_TYPE_BYTEBUF, buff_data, &buff_struct);
78+
79+
reset_abort:
80+
xTaskResumeAll();
81+
}
82+
6083
static void audio_render_task(void *pvParameter)
6184
{
85+
bool clear = true;
6286
bool start = false;
87+
uint16_t count = 0;
88+
EventBits_t uxBits = 0;
6389

6490
ESP_LOGI(TAG, "started.");
6591

@@ -76,6 +102,12 @@ static void audio_render_task(void *pvParameter)
76102
portMAX_DELAY
77103
);
78104

105+
if (!clear) {
106+
audio_buffer_reset();
107+
108+
clear = true;
109+
}
110+
79111
if (start) {
80112
remain = sizeof(buff_data) - xRingbufferGetCurFreeSize(audio_buff);
81113

@@ -89,28 +121,27 @@ static void audio_render_task(void *pvParameter)
89121

90122
data = (uint8_t *)xRingbufferReceiveUpTo(audio_buff, &size, 16 / portTICK_RATE_MS, remain);
91123
} else {
92-
taskYIELD();
124+
do {
125+
vTaskDelay(1 / portTICK_RATE_MS);
126+
127+
remain = sizeof(buff_data) - xRingbufferGetCurFreeSize(audio_buff);
128+
} while (remain == 0 && ++count < 16);
93129

94-
remain = sizeof(buff_data) - xRingbufferGetCurFreeSize(audio_buff);
95130
if (remain == 0) {
96-
#ifdef CONFIG_ENABLE_VFX
97-
uxBits = xEventGroupGetBits(user_event_group);
98-
if (!(uxBits & AUDIO_INPUT_RUN_BIT) && (uxBits & AUDIO_INPUT_FFT_BIT)) {
99-
memset(vfx_fft_input, 0x00, sizeof(vfx_fft_input));
100-
xEventGroupClearBits(user_event_group, VFX_FFT_NULL_BIT);
101-
}
102-
#endif
131+
clear = false;
103132
start = false;
104133
}
105134

135+
count = 0;
136+
106137
continue;
107138
}
108139

109140
if (data == NULL) {
110141
continue;
111142
}
112143
} else {
113-
if (xRingbufferGetCurFreeSize(audio_buff) == 0) {
144+
if (xRingbufferGetCurFreeSize(audio_buff) < 512) {
114145
start = true;
115146
} else {
116147
vTaskDelay(1 / portTICK_RATE_MS);
@@ -119,19 +150,12 @@ static void audio_render_task(void *pvParameter)
119150
continue;
120151
}
121152

122-
EventBits_t uxBits = xEventGroupGetBits(user_event_group);
123-
124-
if ((uxBits & BT_A2DP_IDLE_BIT) || (uxBits & OS_PWR_SLEEP_BIT) || (uxBits & OS_PWR_RESTART_BIT)) {
125-
vRingbufferReturnItem(audio_buff, (void *)data);
126-
continue;
127-
}
128-
129-
#ifdef CONFIG_ENABLE_AUDIO_PROMPT
130-
if (!(uxBits & AUDIO_PLAYER_IDLE_BIT)) {
153+
uxBits = xEventGroupGetBits(user_event_group);
154+
if ((uxBits & AUDIO_PLAYER_RUN_BIT) || (uxBits & BT_A2DP_IDLE_BIT)
155+
|| (uxBits & OS_PWR_SLEEP_BIT) || (uxBits & OS_PWR_RESTART_BIT)) {
131156
vRingbufferReturnItem(audio_buff, (void *)data);
132157
continue;
133158
}
134-
#endif
135159

136160
set_dac_sample_rate(a2d_sample_rate);
137161

@@ -192,11 +216,7 @@ void audio_render_init(void)
192216
xEventGroupSetBits(user_event_group, AUDIO_RENDER_RUN_BIT);
193217

194218
memset(&buff_struct, 0x00, sizeof(StaticRingbuffer_t));
195-
196219
audio_buff = xRingbufferCreateStatic(sizeof(buff_data), RINGBUF_TYPE_BYTEBUF, buff_data, &buff_struct);
197-
if (!audio_buff) {
198-
ESP_LOGE(TAG, "failed to start audio render task");
199-
} else {
200-
xTaskCreatePinnedToCore(audio_render_task, "audioRenderT", 2048, NULL, configMAX_PRIORITIES - 3, NULL, 0);
201-
}
220+
221+
xTaskCreatePinnedToCore(audio_render_task, "audioRenderT", 2048, NULL, configMAX_PRIORITIES - 3, NULL, 0);
202222
}

main/src/user/bt_av.c

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,7 @@ void bt_app_a2d_cb(esp_a2d_cb_event_t event, esp_a2d_cb_param_t *param)
6666

6767
void bt_app_a2d_data_cb(const uint8_t *data, uint32_t len)
6868
{
69-
EventBits_t uxBits = xEventGroupGetBits(user_event_group);
70-
71-
if ((uxBits & BT_A2DP_IDLE_BIT) || (uxBits & OS_PWR_SLEEP_BIT) || (uxBits & OS_PWR_RESTART_BIT)) {
72-
return;
73-
}
74-
75-
#ifdef CONFIG_ENABLE_AUDIO_PROMPT
76-
if (!(uxBits & AUDIO_PLAYER_IDLE_BIT)) {
77-
return;
78-
}
79-
#endif
69+
xEventGroupSetBits(user_event_group, BT_A2DP_DATA_BIT);
8070

8171
if (audio_buff) {
8272
uint32_t pkt = 0, remain = 0;
@@ -92,6 +82,8 @@ void bt_app_a2d_data_cb(const uint8_t *data, uint32_t len)
9282
taskYIELD();
9383
}
9484
}
85+
86+
xEventGroupClearBits(user_event_group, BT_A2DP_DATA_BIT);
9587
}
9688

9789
static void bt_app_alloc_meta_buffer(esp_avrc_ct_cb_param_t *param)

0 commit comments

Comments
 (0)