g_audio_buffer = (uint8_t *)audio_dma_buffer; wm_info.buffers[0] = g_audio_buffer; wm_info.buffers[1] = g_audio_buffer + 1024 * 2; wm_info.buffer_length = 1024; wm_info.spi_x = SPI0; wm_info.feed_data = audio_send_data; wm_info.sample_rate = 16000; wm_info.dma_tx_ch = 2; wm_info.dma_rx_ch = 3;
时间: 2024-04-26 18:26:00 浏览: 99
这段代码看起来是关于音频采集的配置,其中:
- `g_audio_buffer` 是一个指向音频 DMA 缓冲区的指针。
- `wm_info.buffers[0]` 和 `wm_info.buffers[1]` 是音频缓冲区的两个部分,每个部分的大小为 1024*2 字节。
- `wm_info.buffer_length` 是每个缓冲区的长度为 1024 字节。
- `wm_info.spi_x` 是使用的 SPI 接口为 SPI0。
- `wm_info.feed_data` 是一个回调函数,用于将音频数据发送到 DMA 缓冲区。
- `wm_info.sample_rate` 是采样率为 16000。
- `wm_info.dma_tx_ch` 和 `wm_info.dma_rx_ch` 分别是 DMA 的传输通道。
综上所述,这段代码是为了设置音频采集的相关参数和配置 DMA 传输通道。
相关问题
优化这段代码uint8_t *audio_buffer = (uint8_t *)calloc(1, AUDIO_BUFFER_SIZE); assert(audio_buffer); size_t r_bytes = 0; size_t w_bytes = 0; uint8_t volume = 1; FILE *f_read_audio = fopen("/sdcard/wav/bell/CueTone.WAV", "rb"); if (f_read_audio == NULL) { ESP_LOGI(TAG, "Failed to open file"); } else { while (1) { r_bytes = fread(audio_buffer, 1, AUDIO_BUFFER_SIZE, f_read_audio); if (r_bytes == 0) { ESP_LOGI(TAG, "i2s_task will delete"); break; } ESP_LOGI(TAG, ":read %s bytes\n", audio_buffer); // 调整音量大小 int16_t *pcmdata = (int16_t *)audio_buffer; for (int i = 0; i < r_bytes / 2; i++) { int32_t temp = (int32_t)(*pcmdata); temp = temp * volume; temp = temp / 10; *pcmdata = (int16_t)temp; pcmdata++; } // for (int i = 0; i < r_bytes; i += 2) // { // audio_buffer[i] = audio_buffer[i + 1]; // audio_buffer[i + 1] = audio_buffer[i]; // } /* Write i2s data */ if (i2s_channel_write(tx_chan, audio_buffer, AUDIO_BUFFER_SIZE, &w_bytes, portMAX_DELAY) == ESP_OK) { ESP_LOGI(TAG, "Write Task: i2s write %d bytes\n", w_bytes); } else { ESP_LOGI(TAG, "Write Task: i2s write failed\n"); } } }
这段代码需要做以下优化:
1. 避免使用动态内存分配函数 calloc,因为它会频繁调用 malloc 和 free 函数,影响性能,可以使用静态分配的数组代替。
2. 避免在循环中频繁调用 fread 函数,可以将文件读取到缓存中,然后再从缓存中读取数据。
3. 避免在循环中频繁调用 i2s_channel_write 函数,可以将数据写入缓存中,然后再从缓存中写入 i2s 通道。
4. 调整音量大小时可以使用移位运算代替除法运算,提高性能。
下面是优化后的代码:
```c
#define AUDIO_BUFFER_SIZE 2048
uint8_t audio_buffer[AUDIO_BUFFER_SIZE];
size_t r_bytes = 0;
size_t w_bytes = 0;
uint8_t volume = 10;
FILE *f_read_audio = fopen("/sdcard/wav/bell/CueTone.WAV", "rb");
if (f_read_audio == NULL)
{
ESP_LOGI(TAG, "Failed to open file");
}
else
{
// 读取整个文件到缓存中
fseek(f_read_audio, 0, SEEK_END);
size_t file_size = ftell(f_read_audio);
fseek(f_read_audio, 0, SEEK_SET);
size_t read_size = fread(audio_buffer, 1, file_size, f_read_audio);
fclose(f_read_audio);
int16_t *pcmdata = (int16_t *)audio_buffer;
for (int i = 0; i < read_size / 2; i++)
{
int32_t temp = (int32_t)(*pcmdata);
temp = (temp * volume) >> 3; // 移位运算代替除法运算
*pcmdata = (int16_t)temp;
pcmdata++;
}
// 将数据写入 i2s 缓存中
int offset = 0;
while (offset < read_size)
{
size_t write_size = MIN(read_size - offset, AUDIO_BUFFER_SIZE);
memcpy(audio_buffer, audio_buffer + offset, write_size);
if (i2s_channel_write(tx_chan, audio_buffer, write_size, &w_bytes, portMAX_DELAY) == ESP_OK)
{
ESP_LOGI(TAG, "Write Task: i2s write %d bytes\n", w_bytes);
}
else
{
ESP_LOGI(TAG, "Write Task: i2s write failed\n");
}
offset += write_size;
}
}
```
uint8_t *audio_buffer = (uint8_t *)calloc(1, AUDIO_BUFFER_SIZE); assert(audio_buffer); size_t r_bytes = 0; size_t w_bytes = 0; FILE *f_read_audio = fopen("/sdcard/wav/bell/CueTone.WAV", "r"); if (f_read_audio == NULL) { ESP_LOGI(TAG, "Failed to open file"); } else { while (1) { r_bytes = fread(audio_buffer, 1, AUDIO_BUFFER_SIZE, f_read_audio); if (r_bytes == 0) { ESP_LOGI(TAG, "i2s_task will delete"); break; } ESP_LOGI(TAG, ":read %s bytes\n", audio_buffer); /* Write i2s data */ if (i2s_channel_write(tx_chan, audio_buffer, AUDIO_BUFFER_SIZE, &w_bytes, portMAX_DELAY) == ESP_OK) { ESP_LOGI(TAG, "Write Task: i2s write %d bytes\n", w_bytes); } else { ESP_LOGI(TAG, "Write Task: i2s write failed\n"); } } }更改成输出不失真的程序
要输出不失真的程序,需要对几个方面进行改进:
1. 读取音频文件的方式:使用二进制方式读取音频文件,而不是以文本方式读取。
2. 内存分配:为避免内存分配失败,可以提前分配好足够的内存,而不是每次都动态分配。
3. I2S写入数据:写入I2S数据时,需要将数据按照采样位宽进行字节对齐,同时需要保证写入的数据长度是采样位宽的整数倍。
改进后的代码如下:
```
#define AUDIO_BUFFER_SIZE 4096
uint8_t audio_buffer[AUDIO_BUFFER_SIZE];
FILE *f_read_audio = fopen("/sdcard/wav/bell/CueTone.WAV", "rb");
if (f_read_audio == NULL) {
ESP_LOGI(TAG, "Failed to open file");
} else {
// 分配足够的内存
uint8_t *buffer = (uint8_t *)malloc(AUDIO_BUFFER_SIZE);
if (buffer == NULL) {
ESP_LOGI(TAG, "Failed to allocate memory");
return;
}
size_t w_bytes = 0;
while (1) {
size_t r_bytes = fread(buffer, 1, AUDIO_BUFFER_SIZE, f_read_audio);
if (r_bytes == 0) {
ESP_LOGI(TAG, "i2s_task will delete");
break;
}
// 将数据按照采样位宽进行字节对齐
for (int i = 0; i < r_bytes; i += 2) {
audio_buffer[i] = buffer[i + 1];
audio_buffer[i + 1] = buffer[i];
}
// 写入I2S数据
if (i2s_channel_write(tx_chan, audio_buffer, r_bytes, &w_bytes, portMAX_DELAY) == ESP_OK) {
ESP_LOGI(TAG, "Write Task: i2s write %d bytes\n", w_bytes);
} else {
ESP_LOGI(TAG, "Write Task: i2s write failed\n");
}
}
free(buffer);
}
```
这样就可以输出不失真的音频数据了。
阅读全文