Добрый день !
Я использую ESP32-S3-DevkitC-1 и INMP441 с пинами
#define I2S_WS - 15 ESP32-S3
#define I2S_SD - 13 ESP32-S3
#define I2S_SCK - 2 ESP32-S3
L/R to GND ESP32-S3
GND to GND ESP32-S3
VDD to 3.3V ESP32-S3
Мне хочется записывать в течение 5 секунд мой голос, записывать его в файл wav через LittleFS и затем загружать его для воспроизведения на мой ПК через веб-сервер. Все эти этапы я выполнил, но почему-то я слышу свой голос лишь в первую секунду, а дальше идут лишь помехи и шумы.
Я уже пробовал менять INMP441 на другой, но проблема та же самая.
В чем может быть дело ?
Мой код:
#include <driver/i2s.h>
#include <LittleFS.h>
#include <WiFi.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#define I2S_WS 15
#define I2S_SD 13
#define I2S_SCK 2
#define I2S_PORT I2S_NUM_0
#define I2S_SAMPLE_RATE (44100)
#define I2S_SAMPLE_BITS (16)
#define I2S_READ_LEN (32 * 1024)
#define RECORD_TIME (5) // Seconds
#define I2S_CHANNEL_NUM (1)
#define FLASH_RECORD_SIZE (I2S_CHANNEL_NUM * I2S_SAMPLE_RATE * I2S_SAMPLE_BITS / 8 * RECORD_TIME)
#define FILESYSTEM LittleFS
#define FORMAT_FILESYSTEM false
#define DBG_OUTPUT_PORT Serial
const char *ssid = "SSID";
const char *password = "PASSWORD";
const char *host = "esp32fs";
WebServer server(80);
File fsUploadFile;
File file;
const char filename[] = "/audio.wav";
const int headerSize = 44;
String formatBytes(size_t bytes) {
if (bytes < 1024) {
return String(bytes) + "B";
} else if (bytes < (1024 * 1024)) {
return String(bytes / 1024.0) + "KB";
} else if (bytes < (1024 * 1024 * 1024)) {
return String(bytes / 1024.0 / 1024.0) + "MB";
} else {
return String(bytes / 1024.0 / 1024.0 / 1024.0) + "GB";
}
}
String getContentType(String filename) {
if (server.hasArg("download")) {
return "application/octet-stream";
} else if (filename.endsWith(".htm")) {
return "text/html";
} else if (filename.endsWith(".html")) {
return "text/html";
} else if (filename.endsWith(".css")) {
return "text/css";
} else if (filename.endsWith(".js")) {
return "application/javascript";
} else if (filename.endsWith(".png")) {
return "image/png";
} else if (filename.endsWith(".gif")) {
return "image/gif";
} else if (filename.endsWith(".jpg")) {
return "image/jpeg";
} else if (filename.endsWith(".ico")) {
return "image/x-icon";
} else if (filename.endsWith(".xml")) {
return "text/xml";
} else if (filename.endsWith(".pdf")) {
return "application/x-pdf";
} else if (filename.endsWith(".zip")) {
return "application/x-zip";
} else if (filename.endsWith(".gz")) {
return "application/x-gzip";
}
return "text/plain";
}
bool exists(String path) {
return FILESYSTEM.exists(path);
}
bool handleFileRead(String path) {
DBG_OUTPUT_PORT.println("handleFileRead: " + path);
if (path.endsWith("/")) {
path += "index.htm";
}
String contentType = getContentType(path);
String pathWithGz = path + ".gz";
if (exists(pathWithGz) || exists(path)) {
if (exists(pathWithGz)) {
path += ".gz";
}
File file = FILESYSTEM.open(path, "r");
server.streamFile(file, contentType);
file.close();
return true;
}
return false;
}
void handleFileUpload() {
if (server.uri() != "/edit") {
return;
}
HTTPUpload &upload = server.upload();
if (upload.status == UPLOAD_FILE_START) {
String filename = upload.filename;
if (!filename.startsWith("/")) {
filename = "/" + filename;
}
DBG_OUTPUT_PORT.print("handleFileUpload Name: ");
DBG_OUTPUT_PORT.println(filename);
fsUploadFile = FILESYSTEM.open(filename, "w");
} else if (upload.status == UPLOAD_FILE_WRITE) {
if (fsUploadFile) {
fsUploadFile.write(upload.buf, upload.currentSize);
}
} else if (upload.status == UPLOAD_FILE_END) {
if (fsUploadFile) {
fsUploadFile.close();
}
DBG_OUTPUT_PORT.print("handleFileUpload Size: ");
DBG_OUTPUT_PORT.println(upload.totalSize);
}
}
void setup() {
Serial.begin(115200);
LittleFSInit();
i2sInit();
xTaskCreate(i2s_adc, "i2s_adc", 2048 * 2, NULL, 1, NULL);
Serial.printf("\nConnecting to %s\n", ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.printf("Connected! IP address: %s\n", WiFi.localIP().toString().c_str());
// Запуск HTTP-сервера
server.begin();
Serial.println("HTTP server started");
}
void loop() {
server.handleClient();
}
void LittleFSInit() {
if (!LittleFS.begin(true)) {
Serial.println("LittleFS initialization failed!");
while (1) yield();
}
if (LittleFS.exists(filename)) {
LittleFS.remove(filename);
}
file = LittleFS.open(filename, FILE_WRITE);
if (!file) {
Serial.println("File is not available!");
return;
}
byte header[headerSize];
wavHeader(header, FLASH_RECORD_SIZE);
file.write(header, headerSize);
}
void i2sInit() {
i2s_config_t i2s_config = {
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX),
.sample_rate = I2S_SAMPLE_RATE,
.bits_per_sample = i2s_bits_per_sample_t(I2S_SAMPLE_BITS),
.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
.communication_format = i2s_comm_format_t(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB),
.intr_alloc_flags = 0,
.dma_buf_count = 64,
.dma_buf_len = 1024,
.use_apll = 1
};
i2s_driver_install(I2S_PORT, &i2s_config, 0, NULL);
const i2s_pin_config_t pin_config = {
.bck_io_num = I2S_SCK,
.ws_io_num = I2S_WS,
.data_out_num = -1,
.data_in_num = I2S_SD
};
i2s_set_pin(I2S_PORT, &pin_config);
}
void i2s_adc(void *arg) {
int flash_wr_size = 0;
size_t bytes_read;
char *i2s_read_buff = (char *)calloc(I2S_READ_LEN, sizeof(char));
uint8_t *flash_write_buff = (uint8_t *)calloc(I2S_READ_LEN, sizeof(char));
Serial.println(" *** Recording Start *** ");
while (flash_wr_size < FLASH_RECORD_SIZE) {
i2s_read(I2S_PORT, (void *)i2s_read_buff, I2S_READ_LEN, &bytes_read, portMAX_DELAY);
if (bytes_read > 0) {
i2s_adc_data_scale(flash_write_buff, (uint8_t *)i2s_read_buff, bytes_read);
file.write((const byte *)flash_write_buff, bytes_read);
flash_wr_size += bytes_read;
Serial.printf("Sound recording %u%%\n", flash_wr_size * 100 / FLASH_RECORD_SIZE);
} else {
Serial.println("No bytes read from I2S, checking connection...");
}
}
file.close();
free(i2s_read_buff);
free(flash_write_buff);
Serial.println(" *** Recording End *** ");
vTaskDelete(NULL);
}
void i2s_adc_data_scale(uint8_t *d_buff, uint8_t *s_buff, uint32_t len) {
uint32_t j = 0;
for (uint32_t i = 0; i < len; i += 2) {
uint16_t dac_value = (((uint16_t)(s_buff[i + 1] & 0xf) << 8) | (s_buff[i]));
d_buff[j++] = dac_value >> 4; // Понижаем громкость
}
}
void wavHeader(byte *header, int wavSize) {
header[0] = 'R';
header[1] = 'I';
header[2] = 'F';
header[3] = 'F';
unsigned int fileSize = wavSize + headerSize - 8;
header[4] = (byte)(fileSize & 0xFF);
header[5] = (byte)((fileSize >> 8) & 0xFF);
header[6] = (byte)((fileSize >> 16) & 0xFF);
header[7] = (byte)((fileSize >> 24) & 0xFF);
header[8] = 'W';
header[9] = 'A';
header[10] = 'V';
header[11] = 'E';
header[12] = 'f';
header[13] = 'm';
header[14] = 't';
header[15] = ' ';
header[16] = 0x10; // Chunk size for "fmt" subchunk
header[17] = 0x00;
header[18] = 0x00;
header[19] = 0x00;
header[20] = 0x01; // Audio format (PCM)
header[21] = 0x00;
header[22] = 0x01; // Number of channels
header[23] = 0x00;
header[24] = (byte)(I2S_SAMPLE_RATE & 0xFF); // Sample rate
header[25] = (byte)((I2S_SAMPLE_RATE >> 8) & 0xFF);
header[26] = (byte)((I2S_SAMPLE_RATE >> 16) & 0xFF);
header[27] = (byte)((I2S_SAMPLE_RATE >> 24) & 0xFF);
header[28] = (byte)(I2S_SAMPLE_RATE * I2S_CHANNEL_NUM * I2S_SAMPLE_BITS / 8);
header[29] = (byte)((I2S_SAMPLE_RATE * I2S_CHANNEL_NUM * I2S_SAMPLE_BITS / 8) >> 8);
header[30] = (byte)((I2S_SAMPLE_RATE * I2S_CHANNEL_NUM * I2S_SAMPLE_BITS / 8) >> 16);
header[31] = (byte)((I2S_SAMPLE_RATE * I2S_CHANNEL_NUM * I2S_SAMPLE_BITS / 8) >> 24);
header[32] = (byte)(headerSize - 8);
header[33] = (byte)((headerSize - 8) >> 8);
header[34] = (byte)((headerSize - 8) >> 16);
header[35] = (byte)((headerSize - 8) >> 24);
header[36] = 'd';
header[37] = 'a';
header[38] = 't';
header[39] = 'a';
header[40] = (byte)(wavSize);
header[41] = (byte)((wavSize) >> 8);
header[42] = (byte)((wavSize) >> 16);
header[43] = (byte)((wavSize) >> 24);
}
void listLittleFS(void) {
Serial.println(F("\r\nListing LittleFS files:"));
static const char line[] PROGMEM = "=================================================";
Serial.println(FPSTR(line));
Serial.println(F(" File name Size"));
Serial.println(FPSTR(line));
File root = LittleFS.open("/");
if (!root) {
Serial.println(F("Failed to open directory"));
return;
}
if (!root.isDirectory()) {
Serial.println(F("Not a directory"));
return;
}
File file = root.openNextFile();
while (file) {
if (file.isDirectory()) {
Serial.print("DIR : ");
String fileName = file.name();
Serial.print(fileName);
} else {
String fileName = file.name();
Serial.print(" " + fileName);
// File path can be 31 characters maximum in SPIFFS
int spaces = 33 - fileName.length(); // Tabulate nicely
if (spaces < 1) spaces = 1;
while (spaces--) Serial.print(" ");
String fileSize = (String)file.size();
spaces = 10 - fileSize.length(); // Tabulate nicely
if (spaces < 1) spaces = 1;
while (spaces--) Serial.print(" ");
Serial.println(fileSize + " bytes");
}
file = root.openNextFile();
}
Serial.println(FPSTR(line));
Serial.println();
delay(1000);
}
void handleFileDelete() {
if (server.args() == 0) {
return server.send(500, "text/plain", "BAD ARGS");
}
String path = server.arg(0);
DBG_OUTPUT_PORT.println("handleFileDelete: " + path);
if (path == "/") {
return server.send(500, "text/plain", "BAD PATH");
}
if (!exists(path)) {
return server.send(404, "text/plain", "FileNotFound");
}
FILESYSTEM.remove(path);
server.send(200, "text/plain", "");
}
void handleFileCreate() {
if (server.args() == 0) {
return server.send(500, "text/plain", "BAD ARGS");
}
String path = server.arg(0);
DBG_OUTPUT_PORT.println("handleFileCreate: " + path);
if (path == "/") {
return server.send(500, "text/plain", "BAD PATH");
}
if (exists(path)) {
return server.send(500, "text/plain", "FILE EXISTS");
}
File file = FILESYSTEM.open(path, "w");
if (file) {
file.close();
} else {
return server.send(500, "text/plain", "CREATE FAILED");
}
server.send(200, "text/plain", "");
}
void handleFileList() {
if (!server.hasArg("dir")) {
server.send(500, "text/plain", "BAD ARGS");
return;
}
String path = server.arg("dir");
DBG_OUTPUT_PORT.println("handleFileList: " + path);
File root = FILESYSTEM.open(path);
path = String();
String output = "[";
if (root.isDirectory()) {
File file = root.openNextFile();
while (file) {
if (output != "[") {
output += ',';
}
output += "{\"type\":\"";
output += (file.isDirectory()) ? "dir" : "file";
output += "\",\"name\":\"";
output += String(file.path()).substring(1);
output += "\"}";
file = root.openNextFile();
}
}
output += "]";
server.send(200, "text/json", output);
}