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 };
2626static StaticRingbuffer_t buff_struct = {0 };
2727
2828RingbufHandle_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+
6083static 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}
0 commit comments