Skip to content

AudioFileSourceSD playback buffering and crashing #642

@DimitriosProvatas

Description

@DimitriosProvatas

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:

  • up the buffer size, but there is a limit to RAM and I don't want to go to the external RAM route
  • up the read speed of the SD card, this helped but it was not enough to completely remove the stuttering
  • use the AudioFIleSourceBuffer class with double the buffer size, with no changes to performance

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");
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions