rename esp32 project folder
This commit is contained in:
94
FW/leo_muziekdoos_esp32/src/audio.cpp
Normal file
94
FW/leo_muziekdoos_esp32/src/audio.cpp
Normal file
@@ -0,0 +1,94 @@
|
||||
#include "audio.h"
|
||||
|
||||
AudioGeneratorMP3 *mp3;
|
||||
AudioFileSourceID3 *id3;
|
||||
AudioFileSourceLittleFS *file;
|
||||
AudioOutputI2S *out;
|
||||
|
||||
uint8_t i = 0;
|
||||
uint8_t n = 0;
|
||||
|
||||
const char *waveFile[] =
|
||||
{"/ringoffire.mp3",
|
||||
"/Let_it_be.mp3",
|
||||
"/Billy-Jean.mp3"};
|
||||
|
||||
// Called when a metadata event occurs (i.e. an ID3 tag, an ICY block, etc.
|
||||
void MDCallback(void *cbData, const char *type, bool isUnicode, const char *string)
|
||||
{
|
||||
(void)cbData;
|
||||
Serial.printf("ID3 callback for: %s = '", type);
|
||||
|
||||
if (isUnicode)
|
||||
{
|
||||
string += 2;
|
||||
}
|
||||
|
||||
while (*string)
|
||||
{
|
||||
char a = *(string++);
|
||||
if (isUnicode)
|
||||
{
|
||||
string++;
|
||||
}
|
||||
Serial.printf("%c", a);
|
||||
}
|
||||
Serial.printf("'\n");
|
||||
Serial.flush();
|
||||
}
|
||||
|
||||
// Called when there's a warning or error (like a buffer underflow or decode hiccup)
|
||||
void StatusCallback(void *cbData, int code, const char *string)
|
||||
{
|
||||
const char *ptr = reinterpret_cast<const char *>(cbData);
|
||||
// Note that the string may be in PROGMEM, so copy it to RAM for printf
|
||||
char s1[64];
|
||||
strncpy_P(s1, string, sizeof(s1));
|
||||
s1[sizeof(s1) - 1] = 0;
|
||||
Serial.printf("STATUS(%s) '%d' = '%s'\n", ptr, code, s1);
|
||||
Serial.flush();
|
||||
}
|
||||
|
||||
void playSong(uint8_t index)
|
||||
{
|
||||
if (index > AUDIONSONGS)
|
||||
return;
|
||||
Serial.printf("now playing %s\n", waveFile[index]);
|
||||
file = new AudioFileSourceLittleFS(waveFile[index]);
|
||||
id3 = new AudioFileSourceID3(file);
|
||||
id3->RegisterMetadataCB(MDCallback, (void *)"ID3TAG");
|
||||
mp3->begin(id3, out);
|
||||
}
|
||||
|
||||
void initAudio()
|
||||
{
|
||||
Serial.println("init Audio");
|
||||
audioLogger = &Serial;
|
||||
|
||||
out = new AudioOutputI2S();
|
||||
out->SetPinout(I2S_BCLK, I2S_WCLK, I2S_DATA); //bclk, wclk, data
|
||||
out->SetGain(AUDIOGAIN);
|
||||
|
||||
mp3 = new AudioGeneratorMP3();
|
||||
mp3->RegisterStatusCB(StatusCallback, (void *)"mp3");
|
||||
Serial.println("init Audio Done");
|
||||
playSong(i);
|
||||
}
|
||||
|
||||
void handleAudio()
|
||||
{
|
||||
if (mp3->isRunning())
|
||||
{
|
||||
if (!mp3->loop())
|
||||
{
|
||||
//mp3->stop();
|
||||
if(n++ >= AUDIOREPEATS)
|
||||
{
|
||||
i++;
|
||||
n= 0;
|
||||
}
|
||||
playSong(i);
|
||||
;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user