151 lines
3.1 KiB
C++
151 lines
3.1 KiB
C++
#include "audio.h"
|
|
|
|
AudioGeneratorMP3 *mp3;
|
|
AudioFileSourceID3 *id3;
|
|
AudioFileSourceLittleFS *file;
|
|
AudioOutputI2S *out;
|
|
|
|
uint8_t audio_current_Song = 0;
|
|
String nextAudioFile = "";
|
|
uint8_t n = 0;
|
|
|
|
bool audioState = false;
|
|
bool audioInitOk = false;
|
|
|
|
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;
|
|
// log_i("ID3 callback for: %s = '", type);
|
|
|
|
(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;
|
|
log_i("STATUS(%s) '%d' = '%s'\n", ptr, code, s1);
|
|
}
|
|
|
|
// void playSong(uint8_t index)
|
|
// {
|
|
// if (index > AUDIONSONGS)
|
|
// return;
|
|
// log_i("now playing %s\n", waveFile[index]);
|
|
// file = new AudioFileSourceLittleFS(waveFile[index]);
|
|
// id3 = new AudioFileSourceID3(file);
|
|
// id3->RegisterMetadataCB(MDCallback, (void *)"ID3TAG");
|
|
// mp3->RegisterStatusCB(StatusCallback, (void *)"mp3");
|
|
// mp3->begin(id3, out);
|
|
// }
|
|
|
|
void playSong(String filename)
|
|
{
|
|
if (filename != "")
|
|
{
|
|
nextAudioFile = filename;
|
|
log_i("now playing %s\n", filename.c_str());
|
|
file = new AudioFileSourceLittleFS(filename.c_str());
|
|
id3 = new AudioFileSourceID3(file);
|
|
id3->RegisterMetadataCB(MDCallback, (void *)"ID3TAG");
|
|
mp3->RegisterStatusCB(StatusCallback, (void *)"mp3");
|
|
mp3->begin(id3, out);
|
|
}
|
|
else
|
|
{
|
|
log_e("no filename specified");
|
|
}
|
|
}
|
|
|
|
void initAudio()
|
|
{
|
|
log_i("init Audio");
|
|
audioLogger = &Serial;
|
|
delay(500);
|
|
out = new AudioOutputI2S();
|
|
out->SetPinout(I2S_BCLK, I2S_WCLK, I2S_DATA); // bclk, wclk, data
|
|
out->SetGain(getFloatParam("AudioGain", AUDIOGAIN));
|
|
pinMode(DAC_SDMODE, OUTPUT);
|
|
setAudioState(false);
|
|
mp3 = new AudioGeneratorMP3();
|
|
audioInitOk = true;
|
|
log_i("init Audio Done");
|
|
}
|
|
|
|
void setAudioState(bool state)
|
|
{
|
|
if(state == audioState) return;
|
|
|
|
audioState = state;
|
|
if (state)
|
|
{
|
|
digitalWrite(DAC_SDMODE, HIGH);
|
|
}
|
|
else
|
|
{
|
|
digitalWrite(DAC_SDMODE, LOW);
|
|
}
|
|
log_i("set Audio state %d", state);
|
|
}
|
|
|
|
bool getAudioState(void)
|
|
{
|
|
return audioState;
|
|
}
|
|
|
|
bool getAudioInitStatus(void)
|
|
{
|
|
return audioInitOk;
|
|
}
|
|
|
|
void handleAudio()
|
|
{
|
|
if (!audioState)
|
|
{
|
|
if (mp3->isRunning())
|
|
{
|
|
log_w("Audio: stop playback");
|
|
mp3->stop();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (mp3->isRunning())
|
|
{
|
|
if (!mp3->loop())
|
|
{
|
|
mp3->stop();
|
|
log_w("Audio: loop");
|
|
playSong(nextAudioFile);
|
|
}
|
|
}
|
|
}
|
|
} |