请问有基于ESP_IDF从SD卡读取音频数据到I2S播放的案例参考吗?

huangjun
Posts: 8
Joined: Thu May 20, 2021 2:46 am

请问有基于ESP_IDF从SD卡读取音频数据到I2S播放的案例参考吗?

Postby huangjun » Wed Jun 02, 2021 7:49 am

我从a2dp_sink跟sd_card提取了一下代码,但是工作不正常
一直报错
E (10549) I2S: C:/Users/baoan/Desktop/esp-idf/components/driver/i2s.c:1037 (i2s_
write):tx NULL


dac_audio_config_t dac_cfg;
dac_cfg.i2s_num = I2S_NUM_0;
dac_cfg.sample_rate = 16000;// 采样率
dac_cfg.bits_per_sample = 8;//位数
dac_cfg.dac_mode = I2S_DAC_CHANNEL_BOTH_EN;
dac_cfg.dma_buf_count = 8;
dac_cfg.dma_buf_len = 1024;
dac_cfg.max_data_size = 1024;
dac_audio_init(&dac_cfg);

ESP_LOGI(TAG, "\n------------------------------");
// ESP_LOGI(TAG, "hw info: resolution: %dbit, ch: %d", duty_resolution, hw_ch);
dac_audio_set_volume(0);


bt_i2s_task_start_up();

ESP_LOGI(TAG, "Reading file");
FILE* f = fopen(MOUNT_POINT"/A001.bin", "r");
if (f == NULL) {
ESP_LOGE(TAG, "Failed to open file for reading");
return;
}
char line[1024];
while(1)
{
fgets(line, sizeof(line), f);
write_ringbuf((uint8_t *)line, 1024);
int x = fseek(f,1000,SEEK_CUR);
ESP_LOGI(TAG, "...\n");
if(x != 0)break;
}


fclose(f);
dac_audio_deinit();

ESP_LOGI(TAG, "------------------------------\n");






static xTaskHandle s_bt_i2s_task_handle = NULL;
static RingbufHandle_t s_ringbuf_i2s = NULL;;




static void bt_i2s_task_handler(void *arg)
{
uint8_t *data = NULL;
size_t item_size = 0;
size_t bytes_written = 0;

for (;;) {
data = (uint8_t *)xRingbufferReceive(s_ringbuf_i2s, &item_size, (portTickType)portMAX_DELAY);
if (item_size != 0){
i2s_write(0, data, item_size, &bytes_written, portMAX_DELAY);
vRingbufferReturnItem(s_ringbuf_i2s,(void *)data);
}
}
}


void bt_i2s_task_start_up(void)
{
s_ringbuf_i2s = xRingbufferCreate(8 * 1024, RINGBUF_TYPE_BYTEBUF);//创建一个环形缓冲区
if(s_ringbuf_i2s == NULL){
return;
}

xTaskCreate(bt_i2s_task_handler, "BtI2ST", 1024, NULL, configMAX_PRIORITIES - 3, &s_bt_i2s_task_handle);
return;
}


//关闭任务
void bt_i2s_task_shut_down(void)
{
if (s_bt_i2s_task_handle) {
vTaskDelete(s_bt_i2s_task_handle);
s_bt_i2s_task_handle = NULL;
}

if (s_ringbuf_i2s) {
vRingbufferDelete(s_ringbuf_i2s);
s_ringbuf_i2s = NULL;
}
}


//写入缓存区
size_t write_ringbuf(const uint8_t *data, size_t size)
{
BaseType_t done = xRingbufferSend(s_ringbuf_i2s, (void *)data, size, (portTickType)portMAX_DELAY);//将项目插入环形缓冲区
if(done){
return size;
} else {
return 0;
}
}

ESP_Gargamel
Posts: 786
Joined: Wed Nov 14, 2018 8:45 am

Re: 请问有基于ESP_IDF从SD卡读取音频数据到I2S播放的案例参考吗?

Postby ESP_Gargamel » Thu Jun 03, 2021 2:01 am

i2s_config_t 里 I2S_MODE_TX 设了吗?你要提供完整一些的代码。

Who is online

Users browsing this forum: No registered users and 167 guests