Esp8266 earlephilhower repeating cycles without pause

Maybe someone here is familiar with ESP 8266, here I am using the library from Earlephilhower, maybe someone can help with how the library plays audio in repeating cycles without pause

#include <Arduino.h>

#include "AudioFileSourcePROGMEM.h"
#include "AudioGeneratorWAV.h"
#include "AudioOutputI2SNoDAC.h"

// VIOLA sample taken from https://ccrma.stanford.edu/~jos/pasp/Sound_Examples.html
#include "viola.h"

AudioGeneratorWAV *wav;
AudioFileSourcePROGMEM *file;
AudioOutputI2SNoDAC *out;

void setup()
{
  Serial.begin(115200);
  delay(1000);
  Serial.printf("WAV start\n");

  audioLogger = &Serial;
  file = new AudioFileSourcePROGMEM( viola, sizeof(viola) );
  out = new AudioOutputI2SNoDAC();
  wav = new AudioGeneratorWAV();
  wav->begin(file, out);
}

void loop()
{
  if (wav->isRunning()) {
    if (!wav->loop()) wav->loop();
  } else {
    Serial.printf("WAV done\n");
    delay(1000);
  }
}


Hi @yuzeng_39 ,

Unfortunately I don't have any experience with this library but you may try this

Identified as not helpful
void loop()
{
  if (wav->isRunning()) {
    if (!wav->loop()) wav->stop();
  } else {
    wav->begin(file, out);
  }
}

No guarantee that it works or does not have unwanted side effects...

From theory it should stop the recent wave output when it ends and (almost) immediately start it again.

P.S.: My quick suggestion seems not to be a working solution as I found this

https://github.com/earlephilhower/ESP8266Audio/issues/8

At least not for mp3 files ...

Sorry ... :wink:

This thread seems to provide a possibility

https://forum.arduino.cc/t/how-to-repeat-the-wav-file-in-this-code-with-nodemcu12e/1202920

(You may give it a try, though I'm not convinced that it is a clean solution as it is creating new instances of wav and file without removing/freeing the previous resources)

After looking into the library on github I found this function

bool AudioFileSourcePROGMEM::seek(int32_t pos, int dir)
{
  if (!opened) return false;
  uint32_t newPtr;
  switch (dir) {
    case SEEK_SET: newPtr = pos; break;
    case SEEK_CUR: newPtr = filePointer + pos; break;
    case SEEK_END: newPtr = progmemLen - pos; break;
    default: return false;
  }
  if (newPtr > progmemLen) return false;
  filePointer = newPtr;
  return true;
}

If I understand it correctly it should be possible to set the filePointer to 0 again when the wave file ended. That could be a way to endlessly loop (if no other functions interfere) ... I cannot test it but will check if it compiles ..

I moved your topic to an appropriate forum category @yuzeng_39 .

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

After checking some of the examples of the ESP8266Audio lib this might be a solution (not tested, but compiles):

#include <Arduino.h>

#include "AudioFileSourcePROGMEM.h"
#include "AudioGeneratorWAV.h"
#include "AudioOutputI2SNoDAC.h"

// VIOLA sample taken from https://ccrma.stanford.edu/~jos/pasp/Sound_Examples.html
#include "viola.h"

AudioGeneratorWAV *wav;
AudioFileSourcePROGMEM *file;
AudioOutputI2SNoDAC *out;

void setup()
{
  Serial.begin(115200);
  delay(1000);
  Serial.printf("WAV start\n");

  audioLogger = &Serial;
  out = new AudioOutputI2SNoDAC();
  startPlaying();
}

void loop()
{
  if (wav->isRunning()) {
    if (!wav->loop()) {  
      stopPlaying();
      startPlaying();
    }
  } else {
    Serial.printf("WAV done\n");
    delay(1000);
  }
}

void startPlaying(){
  file = new AudioFileSourcePROGMEM( viola, sizeof(viola) );
  wav = new AudioGeneratorWAV();
  wav->begin(file, out);
}


void stopPlaying()
{
  if (wav) {
    wav->stop();
    delete wav;
    wav = NULL;
  }
  if (file) {
    file->close();
    delete file;
    file = NULL;
  }
}  

The idea is to free the necessary instances before they are created again ...

No idea if this works as coded, feel free to test and give a feedback ...

Good luck!
ec2021