#include <Arduino.h>
#include <SD.h>
#include <FS.h>
#include <driver/i2s.h>
#include <cstdlib>
// ==== 配置区 ====
#define I2S_NUM I2S_NUM_0
#define I2S_BCLK 41 // BCLK
#define I2S_WS 42 // LRC/WS
#define I2S_DOUT 39 // DIN (MAX98357A)
#define SD_CS 10 // SD卡模块的CS引脚(根据你实际连接修改)
#define WAV_FILE "/2.wav" // WAV文件名(确保存在SD卡中)
uint8_t volumeNum;
const char* wavFilePath = "/2.wav"; // 全局
// ==== 初始化I2S ====
void setupI2S() {
i2s_config_t i2s_config = {
.mode = (i2s_mode_t) (I2S_MODE_MASTER | I2S_MODE_TX),
.sample_rate = 44100,
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, // 立体声
.communication_format = I2S_COMM_FORMAT_I2S_MSB,
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
.dma_buf_count = 8,
.dma_buf_len = 1024,
.use_apll = false,
.tx_desc_auto_clear = true,
.fixed_mclk = 0
};
i2s_pin_config_t pin_config = {
.bck_io_num = I2S_BCLK,
.ws_io_num = I2S_WS,
.data_out_num = I2S_DOUT,
.data_in_num = I2S_PIN_NO_CHANGE
};
i2s_driver_install(I2S_NUM, &i2s_config, 0, NULL);
i2s_set_pin(I2S_NUM, &pin_config);
i2s_zero_dma_buffer(I2S_NUM);
}
// 设置音量函数(0~100%)
void applyVolume(int16_t *samples, size_t sampleCount, uint8_t volumePercent) {
if (volumePercent > 100) volumePercent = 100;
float volumeScale = volumePercent / 100.0f;
for (size_t i = 0; i < sampleCount; i++) {
int32_t sample = samples[i];
sample = (int32_t) (sample * volumeScale);
// 防止溢出
if (sample > 32767) sample = 32767;
if (sample < -32768) sample = -32768;
samples[i] = (int16_t) sample;
}
}
// ==== 播放WAV文件 ====
void playWav(const char *filename) {
File file = SD.open(filename);
if (!file) {
Serial.println("打开WAV文件失败");
return;
}
// 跳过WAV头部(44字节)
file.seek(44);
const int bufferSize = 1024;
uint8_t buffer[bufferSize];
size_t bytesRead, bytesWritten;
Serial.println("开始播放");
while ((bytesRead = file.read(buffer, bufferSize)) > 0) {
// 注意:16-bit 一次两个字节,所以要 bytesRead / 2 个样本
applyVolume((int16_t *) buffer, bytesRead / 2, volumeNum); // 30% 音量
i2s_write(I2S_NUM, buffer, bytesRead, &bytesWritten, portMAX_DELAY);
}
// while ((bytesRead = file.read(buffer, bufferSize)) > 0) {
// i2s_write(I2S_NUM, buffer, bytesRead, &bytesWritten, portMAX_DELAY);
// }
Serial.println("播放结束");
file.close();
}
void wavPlayerTask(void* param) {
const char* filename = (const char*)param;
File file = SD.open(filename);
if (!file) {
Serial.println("打开WAV文件失败");
vTaskDelete(NULL);
return;
}
file.seek(44); // 跳过 WAV header
const int bufferSize = 1024;
uint8_t buffer[bufferSize];
size_t bytesRead, bytesWritten;
while ((bytesRead = file.read(buffer, bufferSize)) > 0) {
// 使用当前音量调节
applyVolume((int16_t*)buffer, bytesRead / 2, volumeNum);
i2s_write(I2S_NUM, buffer, bytesRead, &bytesWritten, portMAX_DELAY);
}
file.close();
Serial.println("播放完成");
vTaskDelete(NULL); // 退出任务
}
// ==== 初始化 ====
void setup() {
Serial.begin(115200);
delay(1000);
volumeNum = 5;
Serial.println("初始化 SD 卡...");
if (!SD.begin(SD_CS)) {
Serial.println("SD卡初始化失败");
while (1);
}
Serial.println("初始化 I2S...");
setupI2S();
// 启动播放任务
xTaskCreatePinnedToCore(
wavPlayerTask,
"WAVPlayer",
4096,
(void*)wavFilePath,
1,
NULL,
1
);
delay(1000); // 稍等初始化稳定
// playWav(WAV_FILE);
}
void loop() {
if (Serial.available()) {
String num = Serial.readStringUntil('\n');
num.trim();
const char *str = num.c_str();
char* end;
uint8_t v = strtol(str, &end, 10);
Serial.println(v);
volumeNum = v;
}
// 循环播放(可选)
// delay(1000);
// playWav(WAV_FILE);
}