#include "driver/i2s.h"#define SAMPLE_RATE (44100)#define SAMPLE_BITS (16)#define CHANNELS (2)#define PIN_I2S_BCLK (26)#define PIN_I2S_LRC (25)#define PIN_I2S_DIN (33)#define PIN_I2S_DOUT (27)void app_main(void){ //配置I2S参数 i2s_config_t i2s_config = { .mode = I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_RX, //主机模式,发送和接收 .sample_rate = SAMPLE_RATE, .bits_per_sample = SAMPLE_BITS, .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, //双声道 .communication_format = I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB, .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, //中断优先级 .dma_buf_count = 6, .dma_buf_len = 60, .use_apll = false }; //配置I2S引脚 i2s_pin_config_t pin_config = { .bck_io_num = PIN_I2S_BCLK, //位时钟 .ws_io_num = PIN_I2S_LRC, //字选择 .data_out_num = PIN_I2S_DOUT, //数据输出 .data_in_num = PIN_I2S_DIN //数据输入 }; //安装I2S驱动程序 i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL); //设置I2S引脚 i2s_set_pin(I2S_NUM_0, &pin_config); //设置I2S时钟 i2s_set_clk(I2S_NUM_0, SAMPLE_RATE, SAMPLE_BITS, CHANNELS); //创建一个缓冲区,用于存储音频数据 int buffer_size = 1024; char buffer = malloc(buffer_size); while (1) { //从I2S读取音频数据 int bytes_read = 0; i2s_read(I2S_NUM_0, buffer, buffer_size, &bytes_read, portMAX_DELAY); //将音频数据写入I2S int bytes_written = 0; i2s_write(I2S_NUM_0, buffer, bytes_read, &bytes_written, portMAX_DELAY); }}
(图片来源网络,侵删)
0 评论