Skip to content

Instantly share code, notes, and snippets.

@CelliesProjects
Created November 22, 2019 20:50
Show Gist options
  • Select an option

  • Save CelliesProjects/4db2ace3ccd1c94bbb8c27dc0f476e77 to your computer and use it in GitHub Desktop.

Select an option

Save CelliesProjects/4db2ace3ccd1c94bbb8c27dc0f476e77 to your computer and use it in GitHub Desktop.
POC streaming for M5Stack ESP32 boards
#include <Arduino.h>
#ifdef ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#include "AudioFileSourceICYStream.h"
#include "AudioFileSourceBuffer.h"
#include "AudioGeneratorMP3a.h"
#include "AudioOutputI2S.h"
#define microphone 34
#define backlight 32
#define DAC 25
// Enter your WiFi setup here:
const char *SSID = "...";
const char *PASSWORD = "...";
// Randomly picked URL
const char *URL="http://icecast.omroep.nl/3fm-bb-mp3";
AudioGeneratorMP3a *mp3;
AudioFileSourceICYStream *file;
AudioFileSourceBuffer *buff;
AudioOutputI2S *out;
// 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)
{
const char *ptr = reinterpret_cast<const char *>(cbData);
(void) isUnicode; // Punt this ball for now
// Note that the type and string may be in PROGMEM, so copy them to RAM for printf
char s1[32], s2[64];
strncpy_P(s1, type, sizeof(s1));
s1[sizeof(s1)-1]=0;
strncpy_P(s2, string, sizeof(s2));
s2[sizeof(s2)-1]=0;
Serial.printf("METADATA(%s) '%s' = '%s'\n", ptr, s1, s2);
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 setup()
{
Serial.begin(115200);
pinMode( microphone, INPUT );
Serial.println("Connecting to WiFi");
WiFi.disconnect();
WiFi.setSleep(false);
WiFi.mode(WIFI_STA);
WiFi.begin(SSID, PASSWORD);
// Try forever
while (WiFi.status() != WL_CONNECTED) {
Serial.println("...Connecting to WiFi");
delay(1000);
}
Serial.println("Connected");
audioLogger = &Serial;
file = new AudioFileSourceICYStream(URL);
file->RegisterMetadataCB(MDCallback, (void*)"ICY");
buff = new AudioFileSourceBuffer(file, 0xFFFF/2);
buff->RegisterStatusCB(StatusCallback, (void*)"buffer");
out = new AudioOutputI2S(0, 1, 32);
out->SetGain(0.25);
out->SetRate(44100);
mp3 = new AudioGeneratorMP3a();
mp3->RegisterStatusCB(StatusCallback, (void*)"mp3");
mp3->begin(buff, out);
}
void loop()
{
if (mp3->isRunning()) {
if (!mp3->loop()) mp3->stop();
} else {
Serial.printf("MP3 done\n");
delay(1000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment