Add a HTTP streamer music "file" reader class

Implements a simple and stupid HTTP stream reader to allow for ShoutCast
and other MP3 radio streaming to happen.  Presently seems to stutter on
128K, 44.1KHz streams, but may be better on lower bitrates.
This commit is contained in:
Earle F. Philhower, III committed 2017-08-14 19:47:22 -07:00
1 parent 4985aab58c
commit 64ceb33a31
5 files changed
+206

No files matched your search

+2
View File
@@ -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.
@@ -0,0 +1,58 @@
#include <Arduino.h>
#include <ESP8266WiFi.h>
#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);
}
}
+93
View File
@@ -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 <http://www.gnu.org/licenses/>.
*/
#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<uint8_t*>(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;
}
+51
View File
@@ -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 <http://www.gnu.org/licenses/>.
*/
#ifndef _AUDIOFILESOURCEHTTPSTREAM_H
#define _AUDIOFILESOURCEHTTPSTREAM_H
#include <Arduino.h>
#include <ESP8266HTTPClient.h>
#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
+2
View File
@@ -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