Skip to content

Instantly share code, notes, and snippets.

@Robotto
Created January 14, 2025 12:11
Show Gist options
  • Save Robotto/10aefc5d5c1d218ba92df433e923a9c0 to your computer and use it in GitHub Desktop.
Save Robotto/10aefc5d5c1d218ba92df433e923a9c0 to your computer and use it in GitHub Desktop.
ESP32_MP3_I2S
/*
Rember to upload LittleFS data from data folder after code upload!
CTRL + SHIFT + P -> "Upload LittleFS ...."
Get the upload plugin for IDE V2.2.1+ from: https://github.com/earlephilhower/arduino-littlefs-upload
*/
#include <Arduino.h>
#ifdef ESP32
#include <WiFi.h>
#include <FS.h>
#define SPIFFS LittleFS //It't this thing that ESP32 uses instead of SPIFFS: https://github.com/espressif/arduino-esp32/tree/master/libraries/LittleFS -
#include <LittleFS.h>
#include "AudioFileSourceSPIFFS.h"
#include "AudioFileSourceID3.h"
#include "AudioGeneratorMP3.h"
#include "AudioOutputI2S.h"
// To run, set your ESP8266 build to 160MHz, and include a SPIFFS of 512KB or greater.
// Use the "Tools->ESP8266/ESP32 Sketch Data Upload" menu to write the MP3 to SPIFFS
// Then upload the sketch normally.
// pno_cs from https://ccrma.stanford.edu/~jos/pasp/Sound_Examples.html
AudioGeneratorMP3 *mp3;
AudioFileSourceSPIFFS *file;
AudioOutputI2S *out = NULL;
AudioFileSourceID3 *id3;
int BCLK = 16; //Bitclock
int LRCLK = 17; //wordclock (Left/right)
int DOUT = 21; //data
// 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();
}
void setup()
{
WiFi.mode(WIFI_OFF);
Serial.begin(115200);
delay(1000);
SPIFFS.begin();
Serial.printf("Sample MP3 playback begins...\n");
audioLogger = &Serial;
file = new AudioFileSourceSPIFFS("/creeper.mp3");
id3 = new AudioFileSourceID3(file);
id3->RegisterMetadataCB(MDCallback, (void*)"ID3TAG");
out = new AudioOutputI2S();
out->SetOutputModeMono(true);
out->SetPinout(BCLK, LRCLK, DOUT);
mp3 = new AudioGeneratorMP3();
mp3->begin(id3, out);
}
void loop()
{
if (mp3->isRunning()) {
if (!mp3->loop()) mp3->stop();
} else {
Serial.printf("MP3 done\n");
delay(1000);
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment