I have a nodemcu12E and 1 transistor connected to the RX pin and a small speaker. Just tests.
I encoded a WAV in hexadecimal and attached it to the code below in the sons.h tab (this tab does not appear here).
As soon as I turn it on I hear the audio. But I can't listen to it several times. In other words, it seems like there is something in setup() that should be in loop()
I put an int cron; just to make it easier to help here. In practice I will use a button.
Does anyone know how to make the audio play whenever cron is >= 10000 ?
In loop() it would be something like this:
cron++;
if(cron >= 10000) {
plays wav;
}
Thanks
#include <Arduino.h>
#include "AudioFileSourcePROGMEM.h"
#include "AudioGeneratorWAV.h"
#include "AudioOutputI2SNoDAC.h"
#include "sons.h"
AudioGeneratorWAV *wav;
AudioFileSourcePROGMEM *file;
AudioOutputI2SNoDAC *out;
int cron;
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->stop();
} else {
Serial.printf("WAV done\n");
delay(1000);
}
}
I don't see anywhere in loop() where you actually restart the playback.
Maybe add an extra else {} block like this:
if(wav->isRunning()) {
if (!wav->loop()) {
wav->stop();
currentFileIndex = 0;
}
}
else { // wav isn't running any more, start it again.
wav->begin(file, out); //Start playing the track loaded
}
Now I may be totally wrong, but then don't understand the use of currentFileIndex and some other things in that file. But this is basically what you ask for: run the wav in a loop, in other words: the moment it stops, restart it.
I tested it here but there was one detail missing. And I had just found the solution minutes ago. The 'secret' is GO, a simple boolean.
I hope it helps a lot of people because, based on the extensive research I did, this information is not that available to those who are not programmers.
#include <Arduino.h>
#include "AudioFileSourcePROGMEM.h"
#include "AudioGeneratorWAV.h"
#include "AudioOutputI2SNoDAC.h"
#include "viola.h"
AudioGeneratorWAV *wav;
AudioFileSourcePROGMEM *file;
AudioOutputI2SNoDAC *out;
uint32_t start = 0;
bool go = false;
void setup()
{
Serial.begin(115200);
delay(1000);
audioLogger = &Serial;
file = new AudioFileSourcePROGMEM(viola, sizeof(viola));
out = new AudioOutputI2SNoDAC();
wav = new AudioGeneratorWAV();
//wav->begin(file, out); //moved to loop()
}
void loop() {
if (!start) start = millis();
if (millis()-start > 10000) {
if (!go) {
Serial.printf("starting audio\n");
out->SetGain(0.1); //max. 3
wav = new AudioGeneratorWAV();
file = new AudioFileSourcePROGMEM(viola, sizeof(viola));
wav->begin(file, out);
go = true;
}
}
if (wav->isRunning()) {
if (!wav->loop()) {
wav->stop();
start = 0;
go = false;
}
}
}