Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AudioFileSourceSD playback buffering and crashing #642

Open
DimitriosProvatas opened this issue Jul 24, 2023 · 0 comments
Open

AudioFileSourceSD playback buffering and crashing #642

DimitriosProvatas opened this issue Jul 24, 2023 · 0 comments

Comments

@DimitriosProvatas
Copy link

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");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant