Skip to content

Commit 79dc3bd

Browse files
committed
audio: more optimization
1 parent eb14ad2 commit 79dc3bd

File tree

3 files changed

+22
-27
lines changed

3 files changed

+22
-27
lines changed

main/src/user/audio_player.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@
1313
#include "driver/i2s.h"
1414

1515
#include "mad.h"
16+
1617
#include "frame.h"
1718
#include "synth.h"
1819
#include "stream.h"
1920

2021
#include "core/os.h"
22+
#include "chip/i2s.h"
23+
2124
#include "user/audio_player.h"
2225

2326
#define TAG "audio_player"
@@ -49,6 +52,22 @@ static const char *mp3_file_ptr[][2] = {
4952
static bool playback_pending = false;
5053
static mp3_file_t mp3_file = MP3_FILE_IDX_MAX;
5154

55+
/* render callback for the libmad synth */
56+
void render_sample_block(short *sample_ch0, short *sample_ch1, unsigned int sample_rate, unsigned int nch, unsigned int ns)
57+
{
58+
if (nch == 1) {
59+
sample_ch1 = sample_ch0;
60+
}
61+
62+
i2s_output_set_sample_rate(sample_rate);
63+
64+
size_t bytes_written = 0;
65+
for (int i = 0; i < ns; i++) {
66+
i2s_write(CONFIG_AUDIO_OUTPUT_I2S_NUM, sample_ch0++, sizeof(short), &bytes_written, portMAX_DELAY);
67+
i2s_write(CONFIG_AUDIO_OUTPUT_I2S_NUM, sample_ch1++, sizeof(short), &bytes_written, portMAX_DELAY);
68+
}
69+
}
70+
5271
static void audio_player_task(void *pvParameters)
5372
{
5473
ESP_LOGI(TAG, "started.");
@@ -149,5 +168,5 @@ void audio_player_init(void)
149168
xEventGroupSetBits(user_event_group, AUDIO_PLAYER_IDLE_BIT);
150169
}
151170

152-
xTaskCreatePinnedToCore(audio_player_task, "audioPlayerT", 8448, NULL, configMAX_PRIORITIES - 3, NULL, 0);
171+
xTaskCreatePinnedToCore(audio_player_task, "audioPlayerT", 8448, NULL, configMAX_PRIORITIES - 2, NULL, 0);
153172
}

main/src/user/audio_render.c

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
* Author: Jack Chen <redchenjs@live.com>
66
*/
77

8-
#include <string.h>
9-
108
#include "esp_log.h"
119

1210
#include "freertos/FreeRTOS.h"
@@ -28,22 +26,6 @@ RingbufHandle_t audio_buff = NULL;
2826
static uint8_t buff_data[10 * 1024] = {0};
2927
static StaticRingbuffer_t buff_struct = {0};
3028

31-
/* render callback for the libmad synth */
32-
void render_sample_block(short *sample_ch0, short *sample_ch1, unsigned int sample_rate, unsigned int nch, unsigned int ns)
33-
{
34-
if (nch == 1) {
35-
sample_ch1 = sample_ch0;
36-
}
37-
38-
i2s_output_set_sample_rate(sample_rate);
39-
40-
size_t bytes_written = 0;
41-
for (int i = 0; i < ns; i++) {
42-
i2s_write(CONFIG_AUDIO_OUTPUT_I2S_NUM, sample_ch0++, sizeof(short), &bytes_written, portMAX_DELAY);
43-
i2s_write(CONFIG_AUDIO_OUTPUT_I2S_NUM, sample_ch1++, sizeof(short), &bytes_written, portMAX_DELAY);
44-
}
45-
}
46-
4729
static void audio_render_task(void *pvParameter)
4830
{
4931
uint16_t delay = 0;
@@ -80,7 +62,7 @@ static void audio_render_task(void *pvParameter)
8062

8163
data = (uint8_t *)xRingbufferReceiveUpTo(audio_buff, &size, portMAX_DELAY, remain);
8264
} else {
83-
if (++delay < 16) {
65+
if (++delay <= 256000 / a2d_sample_rate) {
8466
vTaskDelay(1 / portTICK_RATE_MS);
8567
} else {
8668
delay = 0;
@@ -93,7 +75,7 @@ static void audio_render_task(void *pvParameter)
9375
continue;
9476
}
9577
} else {
96-
if (xRingbufferGetCurFreeSize(audio_buff) > FFT_BLOCK_SIZE) {
78+
if (xRingbufferGetCurFreeSize(audio_buff) >= FFT_BLOCK_SIZE) {
9779
vTaskDelay(1 / portTICK_RATE_MS);
9880
} else {
9981
start = true;
@@ -146,7 +128,6 @@ static void audio_render_task(void *pvParameter)
146128

147129
void audio_render_init(void)
148130
{
149-
memset(&buff_struct, 0x00, sizeof(StaticRingbuffer_t));
150131
audio_buff = xRingbufferCreateStatic(sizeof(buff_data), RINGBUF_TYPE_BYTEBUF, buff_data, &buff_struct);
151132

152133
xTaskCreatePinnedToCore(audio_render_task, "audioRenderT", 1920, NULL, configMAX_PRIORITIES - 3, NULL, 0);

main/src/user/bt_av.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,6 @@ 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-
if (uxBits & AUDIO_PLAYER_RUN_BIT) {
71-
return;
72-
}
73-
7469
if (audio_buff) {
7570
uint32_t pkt = 0, remain = 0;
7671

0 commit comments

Comments
 (0)