earlephilhower / ESP8266Audio

Arduino library to play MOD, WAV, FLAC, MIDI, RTTTL, MP3, and AAC files on I2S DACs or with a software emulated delta-sigma DAC on the ESP8266 and ESP32
GNU General Public License v3.0
1.98k stars 432 forks source link

AudioFileSourceSD playback buffering and crashing #642

Open DimitriosProvatas opened 11 months ago

DimitriosProvatas commented 11 months ago

Hello, I am using the ESP32 to play some sounds from an SD card. When the files are smaller that the buffer size, the playback is fine, but if a file is longer that the buffer size, there is stuttering for loading the next buffer and crashes after a bit. I have tried to:

Is there a better way to avoid the problem without adding external RAM to my project?

Here is part of the code that handles audio playback:

#define SD_CARD_PIN 26
#define AUDIO_BUFFER_SIZE 48000

AudioGeneratorWAV *wav = nullptr;
AudioFileSourceSD *file = nullptr;
AudioFileSourceBuffer *fileBuffer = nullptr;
AudioOutputI2S *out = nullptr;

void beginFile(char * targetPath)
{
  if (!SD.exists(targetPath))
  {
    Serial.printf("File does not exist: %s\n", targetPath);
    return;
  }
  file->open(targetPath);
  wav->begin(fileBuffer, out);
}

void setup()
{
  Serial.begin(115200);
  audioLogger = &Serial;
  delay(1000);

  if (!SD.begin(SD_CARD_PIN, SPI, 0x0fffffff))
    logAndAbort("Failed to initialize SD card");
  SD.open("/");

  out = new AudioOutputI2S(0, 1);
  wav = new AudioGeneratorWAV();
  wav->SetBufferSize(AUDIO_BUFFER_SIZE);
  file = new AudioFileSourceSD();
  fileBuffer = new AudioFileSourceBuffer(file, 2 * AUDIO_BUFFER_SIZE);
}

void loop()
{
  if (wav->isRunning() && !wav->loop())
    stopCurrentFile();

  if (digitalRead(BUTTON) == 0)
    beginFile("/myfile.wav");
}