fix: moved audio code to seperate module
This commit is contained in:
80
FW/m5stack_audio/src/audio.cpp
Normal file
80
FW/m5stack_audio/src/audio.cpp
Normal file
@@ -0,0 +1,80 @@
|
||||
#include "audio.h"
|
||||
|
||||
AudioGeneratorMP3 *mp3;
|
||||
AudioFileSourceID3 *id3;
|
||||
|
||||
AudioFileSourceLittleFS *file;
|
||||
|
||||
AudioOutputI2S *out;
|
||||
|
||||
const char *waveFile = "/Let_it_be.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 initAudio()
|
||||
{
|
||||
|
||||
|
||||
audioLogger = &Serial;
|
||||
file = new AudioFileSourceLittleFS(waveFile);
|
||||
id3 = new AudioFileSourceID3(file);
|
||||
id3->RegisterMetadataCB(MDCallback, (void *)"ID3TAG");
|
||||
|
||||
mp3 = new AudioGeneratorMP3();
|
||||
mp3->RegisterStatusCB(StatusCallback, (void*)"mp3");
|
||||
|
||||
out = new AudioOutputI2S();
|
||||
out->SetPinout(19, 33, 22); //bclk, wclk, data
|
||||
mp3->begin(id3, out);
|
||||
out->SetGain(0.25);
|
||||
}
|
||||
|
||||
void handleAudio()
|
||||
{
|
||||
if (mp3->isRunning())
|
||||
{
|
||||
if (!mp3->loop())
|
||||
{
|
||||
mp3->stop();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.printf("sound done\n");
|
||||
delay(1000);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user