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"); } } }更改成输出不失真的程序
时间: 2024-02-14 14:06:38 浏览: 144
AVR.zip_avr_iom1284p.h
要输出不失真的程序,需要对几个方面进行改进:
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);
}
```
这样就可以输出不失真的音频数据了。
阅读全文