22
22
23
23
#define TAG "audio_render"
24
24
25
- static uint8_t buff_data [8 * 1024 ] = {0 };
25
+ static uint8_t buff_data [4 * 1024 ] = {0 };
26
26
static StaticRingbuffer_t buff_struct = {0 };
27
27
28
28
RingbufHandle_t audio_buff = NULL ;
@@ -57,9 +57,35 @@ void set_dac_sample_rate(int rate)
57
57
i2s_output_set_sample_rate (rate );
58
58
}
59
59
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
+
60
83
static void audio_render_task (void * pvParameter )
61
84
{
85
+ bool clear = true;
62
86
bool start = false;
87
+ uint16_t count = 0 ;
88
+ EventBits_t uxBits = 0 ;
63
89
64
90
ESP_LOGI (TAG , "started." );
65
91
@@ -76,6 +102,12 @@ static void audio_render_task(void *pvParameter)
76
102
portMAX_DELAY
77
103
);
78
104
105
+ if (!clear ) {
106
+ audio_buffer_reset ();
107
+
108
+ clear = true;
109
+ }
110
+
79
111
if (start ) {
80
112
remain = sizeof (buff_data ) - xRingbufferGetCurFreeSize (audio_buff );
81
113
@@ -89,28 +121,27 @@ static void audio_render_task(void *pvParameter)
89
121
90
122
data = (uint8_t * )xRingbufferReceiveUpTo (audio_buff , & size , 16 / portTICK_RATE_MS , remain );
91
123
} 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 );
93
129
94
- remain = sizeof (buff_data ) - xRingbufferGetCurFreeSize (audio_buff );
95
130
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;
103
132
start = false;
104
133
}
105
134
135
+ count = 0 ;
136
+
106
137
continue ;
107
138
}
108
139
109
140
if (data == NULL ) {
110
141
continue ;
111
142
}
112
143
} else {
113
- if (xRingbufferGetCurFreeSize (audio_buff ) == 0 ) {
144
+ if (xRingbufferGetCurFreeSize (audio_buff ) < 512 ) {
114
145
start = true;
115
146
} else {
116
147
vTaskDelay (1 / portTICK_RATE_MS );
@@ -119,19 +150,12 @@ static void audio_render_task(void *pvParameter)
119
150
continue ;
120
151
}
121
152
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 )) {
131
156
vRingbufferReturnItem (audio_buff , (void * )data );
132
157
continue ;
133
158
}
134
- #endif
135
159
136
160
set_dac_sample_rate (a2d_sample_rate );
137
161
@@ -192,11 +216,7 @@ void audio_render_init(void)
192
216
xEventGroupSetBits (user_event_group , AUDIO_RENDER_RUN_BIT );
193
217
194
218
memset (& buff_struct , 0x00 , sizeof (StaticRingbuffer_t ));
195
-
196
219
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 );
202
222
}
0 commit comments