diff --git a/README.md b/README.md index b8c4791..5ab3126 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,8 @@ AudioFileSourceSPIFFS: Reads a file from the SPIFFS filesystem AudioFileSourcePROGMEM: Reads a file from a PROGMEM array. Under UNIX you can use "xxd -i file.mp3 > file.h" to get the basic format, then add "const" and "PROGMEM" to the generated array and include it in your sketch. See the example .h files for a concrete example. +AudioFileSourceHTTPStream: Simple implementation of a streaming HTTP reader for ShoutCast-type MP3 streaming. Not yet resilient, and at 44.1khz 128bit stutters due to CPU limitations, but it works more or less. + ## AudioGenerator classes AudioGenerator: Base class for all file decoders. Takes a AudioFileSource and an AudioOutput object to get the data from and to write decoded samples to. Call its loop() function as often as you can to ensure the buffers are always kept full and your music won't skip. diff --git a/examples/StreamMP3FromHTTP/StreamMP3FromHTTP.ino b/examples/StreamMP3FromHTTP/StreamMP3FromHTTP.ino new file mode 100644 index 0000000..ef6d1c6 --- /dev/null +++ b/examples/StreamMP3FromHTTP/StreamMP3FromHTTP.ino @@ -0,0 +1,58 @@ +#include +#include +#include "AudioFileSourceHTTPStream.h" +#include "AudioGeneratorMP3.h" +#include "AudioOutputI2SNoDAC.h" + +// To run, set your ESP8266 build to 160MHz, update the SSID info, and upload. + +// Enter your WiFi setup here: +const char *SSID = "....."; +const char *PASSWORD = "....."; + +// Randomly picked URL +const char *URL="http://streaming.shoutcast.com/80sPlanet?lang=en-US"; + +AudioGeneratorMP3 *mp3; +AudioFileSourceHTTPStream *file; +AudioOutputI2SNoDAC *out; + +void setup() +{ + Serial.begin(115200); + delay(1000); + Serial.println("Connecting to WiFi"); + + WiFi.disconnect(); + WiFi.softAPdisconnect(true); + WiFi.mode(WIFI_STA); + + WiFi.hostname("melody"); + + byte zero[] = {0,0,0,0}; + WiFi.config(zero, zero, zero, zero); + + WiFi.begin(SSID, PASSWORD); + + // Try forever + while (WiFi.status() != WL_CONNECTED) { + Serial.println("...Connecting to WiFi"); + delay(1000); + } + + file = new AudioFileSourceHTTPStream(URL); + out = new AudioOutputI2SNoDAC(); + mp3 = new AudioGeneratorMP3(); + mp3->begin(file, out); +} + +void loop() +{ + if (mp3->isRunning()) { + if (!mp3->loop()) mp3->stop(); + } else { + Serial.printf("MP3 done\n"); + delay(1000); + } +} + diff --git a/src/AudioFileSourceHTTPStream.cpp b/src/AudioFileSourceHTTPStream.cpp new file mode 100644 index 0000000..2178675 --- /dev/null +++ b/src/AudioFileSourceHTTPStream.cpp @@ -0,0 +1,93 @@ +/* + AudioFileSourceHTTPStream + Streaming HTTP source + + Copyright (C) 2017 Earle F. Philhower, III + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "AudioFileSourceHTTPStream.h" + +AudioFileSourceHTTPStream::AudioFileSourceHTTPStream() +{ + pos = 0; +} + +AudioFileSourceHTTPStream::AudioFileSourceHTTPStream(const char *url) +{ + open(url); +} + +bool AudioFileSourceHTTPStream::open(const char *url) +{ + pos = 0; + http.begin(url); + http.setReuse(true); + int code = http.GET(); + if (code != HTTP_CODE_OK) { + http.end(); + return false; + } + return true; +} + +AudioFileSourceHTTPStream::~AudioFileSourceHTTPStream() +{ + http.end(); +} + +uint32_t AudioFileSourceHTTPStream::read(void *data, uint32_t len) +{ + if (!http.connected()) return 0; + + WiFiClient *stream = http.getStreamPtr(); + while (stream->available() < len) yield(); + + size_t avail = stream->available(); + if (!avail) return 0; + if (avail < len) len = avail; + + int read = stream->readBytes(reinterpret_cast(data), len); + pos += read; + return read; +} + +bool AudioFileSourceHTTPStream::seek(int32_t pos, int dir) +{ + (void) pos; + (void) dir; + return false; +} + +bool AudioFileSourceHTTPStream::close() +{ + http.end(); + return true; +} + +bool AudioFileSourceHTTPStream::isOpen() +{ + return http.connected(); +} + +uint32_t AudioFileSourceHTTPStream::getSize() +{ + return 1L<<31; +} + +uint32_t AudioFileSourceHTTPStream::getPos() +{ + return pos; +} diff --git a/src/AudioFileSourceHTTPStream.h b/src/AudioFileSourceHTTPStream.h new file mode 100644 index 0000000..ace7a94 --- /dev/null +++ b/src/AudioFileSourceHTTPStream.h @@ -0,0 +1,51 @@ +/* + AudioFileSourceHTTPStream + Connect to a HTTP based streaming service + + Copyright (C) 2017 Earle F. Philhower, III + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef _AUDIOFILESOURCEHTTPSTREAM_H +#define _AUDIOFILESOURCEHTTPSTREAM_H + +#include +#include + +#include "AudioFileSource.h" + +class AudioFileSourceHTTPStream : public AudioFileSource +{ + public: + AudioFileSourceHTTPStream(); + AudioFileSourceHTTPStream(const char *url); + virtual ~AudioFileSourceHTTPStream() override; + + virtual bool open(const char *url) override; + virtual uint32_t read(void *data, uint32_t len) override; + virtual bool seek(int32_t pos, int dir) override; + virtual bool close() override; + virtual bool isOpen() override; + virtual uint32_t getSize() override; + virtual uint32_t getPos() override; + + private: + HTTPClient http; + uint32_t pos; +}; + + +#endif + diff --git a/src/AudioGeneratorMOD.cpp b/src/AudioGeneratorMOD.cpp index 98dc2b2..800f765 100644 --- a/src/AudioGeneratorMOD.cpp +++ b/src/AudioGeneratorMOD.cpp @@ -32,6 +32,8 @@ moving constants to PROGMEM and minor tweaks to allow non pow2 buffer sizes. */ +#pragma GCC optimize ("O3") + #define NOTE(r, c) (Player.currentPattern.note8[r][c]==NONOTE8?NONOTE:8*Player.currentPattern.note8[r][c]) #ifndef min