audioesp32arduino-c++

How to reproduce audio files with i2s library and MAX98357A from SD file in ESP32?


I am developing a project with an ESP32 where after passing an NFC card through a module, it reads it and is supposed to play a .wav file stored in an SD that matches the NFC card. I looked into the ESP32-audioI2S library, and I thought I could use it.
The way you are supposed to reproduce a file in an SD with this library is like this:

#include <Audio.h>
#include <FS.h>
#include <SD.h>

#define I2S_DOUT 25
#define I2S_BCLK 27
#define I2S_LRC 26

Audio audio;

void setup() {
  Serial.begin(9600);

  SPI.begin();
  if (!SD.begin()) {
    Serial.println("SD Card Mount Failed");
    return;
  }

  audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
  audio.setVolume(20);
  audio.connecttoFS(SD, "/audios/me llamo joaquin.wav"); // Select file to reproduce
}

void loop() { 
  audio.loop(); // Reproduce connected file
}

That works just fine. However, I want to be able to use other files. I thought I could just use the audio.connecttoFS() function inside the loop(). Still, no sound is reproduced when doing so.
I think that this library doesn't allow setting the file outside the setup().

I got a way to reproduce the file, it is just not doing it "correctly". It reproduces way to fast, like it is sped up * 1000. Here's the code:

#define I2S_DOUT 25
#define I2S_BCLK 27
#define I2S_LRC 26
void setup() {
// Start SD card and sqlite
  SPI.begin();
  if (!SD.begin()) {
    Serial.println("SD card not found");
    while (1) {};
  }
// Configure I2S
  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 = 2,
      .dma_buf_len = 1024,
      .tx_desc_auto_clear = true,
  };

  i2s_pin_config_t pin_config = {
      .bck_io_num = I2S_BCLK,
      .ws_io_num = I2S_LRC,
      .data_out_num = I2S_DOUT,
      .data_in_num = -1,
  };

  i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
  i2s_set_pin(I2S_NUM_0, &pin_config);
  i2s_set_dac_mode(I2S_DAC_CHANNEL_RIGHT_EN);
  i2s_set_sample_rates(I2S_NUM_0, 44100);
}

void loop() {
// Play the audio file
  string filename = "/audios/" + card->get_value() + ".wav";
  File file = SD.open(filename.c_str());
  if (!file) {
    Serial.println("Failed to open file for reading");
    return;
  }

  const size_t buffer_size = 512;
  uint8_t buffer[buffer_size];
  size_t bytes_read;
  size_t bytes_written;
  while (file.available() && (bytes_read = file.read(buffer, buffer_size)) > 0) {
    i2s_write(I2S_NUM_0, buffer, bytes_read, &bytes_written, portMAX_DELAY);
    if (bytes_read == 0) break;
  }
  file.close();
}

How can I make it reproduce the file "correctly"?


Solution

  • This code works for me:

    #include <SD.h>
    #include <SPI.h>
    #include <driver/i2s.h>
    
    #define I2S_DOUT 25
    #define I2S_BCLK 27
    #define I2S_LRC 26
    
    i2s_config_t i2sConfig = {
        .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_ONLY_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,
    };
    
    i2s_pin_config_t pinConfig = {
        .bck_io_num = I2S_BCLK,
        .ws_io_num = I2S_LRC,
        .data_out_num = I2S_DOUT,
        .data_in_num = -1,
    };
    
    struct WavHeader {
      uint32_t sample_rate;
      uint16_t bits_per_sample;
      uint16_t num_channels;
    };
    
    File file;
    
    bool parseWavHeader(File &file, WavHeader &header);
    
    void setup() {
      Serial.begin(9600);
    
      SPI.begin();
      if (!SD.begin()) {
        Serial.println("SD Card Mount Failed");
        while (1) delay(1);
      }
    
      i2s_driver_install(I2S_NUM_0, &i2sConfig, 0, NULL);
      i2s_set_pin(I2S_NUM_0, &pinConfig);
    }
    
    void loop() {
      file = SD.open("/audios/me llamo joaquin.wav");
      if (!file) {
        Serial.println("Failed to open file for reading");
        while (1) delay(1);
      }
    
      WavHeader header;
      if (!parseWavHeader(file, header)) {
        Serial.println("Invalid WAV file");
        return;
      }
    
      i2s_set_sample_rates(I2S_NUM_0, header.sample_rate);
    
      const size_t buffer_size = 512;
      uint8_t buffer[buffer_size];
      size_t bytes_read;
      size_t bytes_written;
      while (file.available() && (bytes_read = file.read(buffer, buffer_size)) > 0) {
        i2s_write(I2S_NUM_0, buffer, bytes_read, &bytes_written, portMAX_DELAY);
        if (bytes_read == 0) break;
      }
    
      delay(1000);
    }
    
    bool parseWavHeader(File &file, WavHeader &header) {
      if (file.read() != 'R' || file.read() != 'I' || file.read() != 'F' || file.read() != 'F') {
        Serial.println("Invalid WAV file");
        return false;
      }
    
      file.seek(22);
      header.num_channels = file.read() | (file.read() << 8);
      header.sample_rate = file.read() | (file.read() << 8) | (file.read() << 16) | (file.read() << 24);
      file.seek(34);
      header.bits_per_sample = file.read() | (file.read() << 8);
      file.seek(44);  // Skip to the audio data
      return true;
    }