diff --git a/examples/StreamOnHost/StreamOnHost.ino b/examples/StreamOnHost/StreamOnHost.ino new file mode 100644 index 0000000..4f79777 --- /dev/null +++ b/examples/StreamOnHost/StreamOnHost.ino @@ -0,0 +1,108 @@ +#include + +#ifdef ESP32 + #include +#else + #include +#endif +#include "AudioFileSourceICYStream.h" +#include "AudioFileSourceBuffer.h" +#include "AudioGeneratorMP3.h" +#include "AudioOutputNull.h" + +// To run, set your ESP8266 build to 160MHz, update the SSID info, and upload. + +// Enter your WiFi setup here: +#ifndef STASSID +#define STASSID "" +#define STAPSK "" +#endif + +const char* ssid = STASSID; +const char* password = STAPSK; + +// Randomly picked URL +const char *URL="http://icecast.radiofrance.fr/franceinter-lofi.mp3"; +//const char *URL="http://kvbstreams.dyndns.org:8000/wkvi-am"; + +AudioGeneratorMP3 *mp3; +AudioFileSourceICYStream *file; +AudioFileSourceBuffer *buff; +AudioOutputNull *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(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(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); + delay(1000); + Serial.println("Connecting to WiFi"); + + WiFi.disconnect(); + WiFi.softAPdisconnect(true); + 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, 4096); + buff->RegisterStatusCB(StatusCallback, (void*)"buffer"); + out = new AudioOutputNull(); + mp3 = new AudioGeneratorMP3(); + mp3->RegisterStatusCB(StatusCallback, (void*)"mp3"); + mp3->begin(buff, out); +} + + +void loop() +{ + static int lastms = 0; + + if (mp3->isRunning()) { + if (millis()-lastms > 1000) { + lastms = millis(); + Serial.printf("Running for %d ms...\n", lastms); + Serial.flush(); + } + if (!mp3->loop()) mp3->stop(); + } else { + Serial.printf("MP3 done\n"); + delay(1000); + } +} + diff --git a/examples/StreamOnHost/onHost b/examples/StreamOnHost/onHost new file mode 100755 index 0000000..5220cb6 --- /dev/null +++ b/examples/StreamOnHost/onHost @@ -0,0 +1,50 @@ +#!/bin/bash + +ino=${PWD##*/} + +if [ ! -d "${ESP8266ARDUINO}/tests/host" ]; then + echo "\${ESP8266ARDUINO} should point to ESP8266 Arduino core directory" + exit 1 +fi + +THISLIB=$(pwd)/../.. +MAD=$(ls ${THISLIB}/src/libmad/*.c) + +cd ${ESP8266ARDUINO}/tests/host + +if [ "$1" = "clean" ]; then + make clean + cd ${THISLIB} + rm -f src/*.o src/libmad/*.o + exit 0 +fi + +if [ "$1" = "-h" ]; then + echo "usage:" + echo " $0" + echo " $0 clean" + echo " FORCE32=0 $0 (run with valgrind)" + echo " FORCE32=1 $0 (run in 32 bits)" + echo "variable ESP8266ARDUINO must point to esp8266 Arduino core directory" + exit 0 +fi + +[ -z "${FORCE32}" ] && FORCE32=0 + +if [ "${FORCE32}" = 0 ]; then + run=valgrind +else + run= +fi + +eval make FORCE32=${FORCE32} V=1 -j \ + USERCSOURCES=\"${MAD}\" \ + USERCXXSOURCES=\"${THISLIB}/src/AudioFileSourceBuffer.cpp ${THISLIB}/src/AudioLogger.cpp ${THISLIB}/src/AudioGeneratorMP3.cpp ${THISLIB}/src/AudioFileSourceICYStream.cpp ${THISLIB}/src/AudioFileSourceHTTPStream.cpp\" \ + USERCFLAGS=\"-I${THISLIB}/src/\" \ + ${THISLIB}/examples/${ino}/${ino} + +set -x + +$run ./bin/${ino}/${ino} "$@" +stty sane + diff --git a/src/AudioGeneratorMP3.cpp b/src/AudioGeneratorMP3.cpp index 7c09289..44e3f6f 100644 --- a/src/AudioGeneratorMP3.cpp +++ b/src/AudioGeneratorMP3.cpp @@ -302,7 +302,7 @@ extern "C" { { return 8192; } -#elif defined(ESP8266) +#elif defined(ESP8266) && !defined(CORE_MOCK) #include extern cont_t g_cont;