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

Gapless looping of audio #696

Open
KyodaiKen opened this issue Aug 2, 2024 · 1 comment
Open

Gapless looping of audio #696

KyodaiKen opened this issue Aug 2, 2024 · 1 comment

Comments

@KyodaiKen
Copy link

Hi,

I'm trying to make a little noise box to make me sleep better at night.

So I tried to start very basic before I record or create flac files to play in loop from a local webserver.

#include <Arduino.h>
#include "AudioFileSourceFunction.h"
#include "AudioGeneratorWAV.h"
#include "AudioOutputI2S.h"

AudioGeneratorWAV* wav;
AudioFileSourceFunction* file;
AudioOutputI2S* out;

void setup() {
  Serial.begin(115200);
  delay(100);
  file = new AudioFileSourceFunction(8.);

  file->addAudioGenerators([&](const float time) {
    return random(0.0,2.0) * 0.125;
  });

  out = new AudioOutputI2S();
  wav = new AudioGeneratorWAV();
  wav->begin(file, out);
  delay(100);
}

void loop() {
  if (wav->isRunning()) {
    wav->loop();
  } else {
    file->seek(0,0);
    wav->begin(file, out);
  }
}

Unfortunately the file is closed when the file ends.
Is there no way to make it so the pointer is moved back to the beginning again when the next buffer is being read?

@sittelle
Copy link

sittelle commented Aug 20, 2024

Hi KyodaiKen,

the following works for me:

if (audioGeneratorMP3.isRunning()) {
    if (currentRequest.playInfinitely) {
        long millisLeft = audioFileSourceLittleFS.getSize() - audioFileSourceLittleFS.getPos();
        if (millisLeft < 5) {
          audioFileSourceLittleFS.seek(0, SEEK_SET);
        }
    }
}

This rewinds my audio when only 5 ms are left to play. I am sure you can even reduce it to 1.

Regards

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

2 participants