From 8893ced2afbfde5e81080c68528c486290300522 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Fri, 8 Dec 2017 13:07:59 -0800 Subject: [PATCH] Add WebRadio example, a web page controller Simple example web interface to the player. Can change the volume and the station through a slider and menu box. --- examples/WebRadio/WebRadio.ino | 256 +++++++++++++++++++++++++++++++++ src/AudioGeneratorAAC.h | 2 +- src/AudioGeneratorFLAC.h | 2 +- src/AudioGeneratorMIDI.h | 2 +- src/AudioGeneratorMOD.h | 2 +- src/AudioGeneratorWAV.h | 2 +- 6 files changed, 261 insertions(+), 5 deletions(-) create mode 100644 examples/WebRadio/WebRadio.ino diff --git a/examples/WebRadio/WebRadio.ino b/examples/WebRadio/WebRadio.ino new file mode 100644 index 0000000..54309b8 --- /dev/null +++ b/examples/WebRadio/WebRadio.ino @@ -0,0 +1,256 @@ +/* + WebRadio Example + Very simple HTML app to control web streaming + + 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 +#include +#include "AudioFileSourceICYStream.h" +#include "AudioFileSourceBuffer.h" +#include "AudioGeneratorMP3.h" +#include "AudioGeneratorAAC.h" +#include "AudioOutputI2SDAC.h" +#include "ESP8266WebServer.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 = "...."; + +ESP8266WebServer server(80); + +AudioGenerator *decoder; +AudioFileSourceICYStream *file; +AudioFileSourceBuffer *buff; +AudioOutputI2SDAC *out; + +int volume = 100; +char title[64]; +char url[64]; +char status[64]; + +// C++11 multiline string constants are neato... +const char INDEX[] PROGMEM = R"KEWL( + +ESP8266 Web Radio + + +ESP8266 Web Radio! +
+Currently Playing: %s
+ +Volume: %d%% + +
+Status: %s +
+
+Current URL: %s
+Change URL: + +
+ +)KEWL"; + +// Static spot to store the web sprintf's + +void IndexHTML() +{ + char *webbuff = (char*)malloc(2048); + snprintf_P(webbuff, 2048, INDEX, title, volume, volume, status, url); + webbuff[2047] = 0; + server.send(200, "text/html", webbuff ); + free(webbuff); +} + +void Send404() +{ + server.send(404, "text/plain", "404: Not Found"); +} + +void GetTitle() +{ + server.send(200, "text/plain", title); +} + +void SetVol() +{ + if (server.hasArg("vol")) { + int vol = 100; + int ret = sscanf(server.arg("vol").c_str(), "%d", &vol); + if (ret==1) { + if (vol < 0) vol = 0; + if (vol > 150) vol = 150; + if (out) out->SetGain(((float)vol)/100.0); + volume = vol; + Serial.printf("Set volume: %d\n", volume); + server.sendHeader("Location", "/", true); + server.send(301, "text/plain", ""); + } + } +} + +void ChangeURL() +{ + if (server.hasArg("url") && server.hasArg("type")) { + // Stop and free existing ones + if (decoder) { + decoder->stop(); + delete decoder; + } + if (buff) { + buff->close(); + delete buff; + } + if (out) { + out->stop(); + delete out; + } + if (file) { + file->close(); + delete file; + } + + strncpy(url, server.arg("url").c_str(), sizeof(url)-1); + url[sizeof(url)-1] = 0; + file = new AudioFileSourceICYStream(server.arg("url").c_str()); + file->RegisterMetadataCB(MDCallback, (void*)"ICY"); + buff = new AudioFileSourceBuffer(file, 2048); + buff->RegisterStatusCB(StatusCallback, (void*)"buffer"); + out = new AudioOutputI2SDAC(); + if (!strcmp(server.arg("type").c_str(), "aac")) { + decoder = new AudioGeneratorAAC(); + } else { + decoder = new AudioGeneratorMP3(); + } + decoder->RegisterStatusCB(StatusCallback, (void*)"mp3"); + decoder->begin(buff, out); + out->SetGain(((float)volume)/100.0); + server.sendHeader("Location", "/", true); + server.send(301, "text/plain", ""); + } else { + Send404(); // Need to specify those params or you're in error! + } +} +void MDCallback(void *cbData, const char *type, bool isUnicode, Stream *stream) +{ + const char *ptr = reinterpret_cast(cbData); + (void) isUnicode; // Punt this ball for now + if (strstr(type, "Title")) { + strncpy(title, stream->readString().c_str(), sizeof(title)); + title[sizeof(title)-1] = 0; + Serial.printf("Set title: %s\n", title); + } else { + Serial.printf("METADATA(%s) '%s' = '%s'\n", ptr, type, stream->readString().c_str()); + } + + Serial.flush(); +} +void StatusCallback(void *cbData, int code, const char *string) +{ + const char *ptr = reinterpret_cast(cbData); + Serial.printf("STATUS(%s) '%d' = '%s'\n", ptr, code, string); + strncpy(status, string, sizeof(status)-1); + status[sizeof(status)-1] = 0; + Serial.flush(); +} + +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); + } + Serial.println("Connected\n"); + + Serial.print("Go to http://"); + Serial.print(WiFi.localIP()); + Serial.println("/ to control the web radio."); + + server.on("/", HTTP_GET, IndexHTML); + server.on("/setvol", HTTP_GET, SetVol); + server.on("/title", HTTP_GET, GetTitle); + server.on("/changeurl", HTTP_POST, ChangeURL); + server.onNotFound(Send404); + server.begin(); + + strcpy(url, "none"); + strcpy(status, "OK"); + strcpy(title, "Idle"); + + file = NULL; + buff = NULL; + out = NULL; + decoder = NULL; +} + +void loop() +{ + static int lastms = 0; + server.handleClient(); + if (!decoder) return; + + if (decoder->isRunning()) { + strcpy(status, "OK"); // By default we're OK unless the decoder says otherwise + if (millis()-lastms > 1000) { + lastms = millis(); + Serial.printf("Running for %d seconds...\n", lastms/1000); + Serial.flush(); + } + if (!decoder->loop()) { + Serial.println("Stopping decoder"); + decoder->stop(); + strcpy(title, "Stopped"); + } + } else { + // Nothing here + } +} + diff --git a/src/AudioGeneratorAAC.h b/src/AudioGeneratorAAC.h index bd8df1d..2b0681a 100644 --- a/src/AudioGeneratorAAC.h +++ b/src/AudioGeneratorAAC.h @@ -24,7 +24,7 @@ #include "AudioGenerator.h" #include "libhelix-aac/aacdec.h" -class AudioGeneratorAAC : AudioGenerator +class AudioGeneratorAAC : public AudioGenerator { public: AudioGeneratorAAC(); diff --git a/src/AudioGeneratorFLAC.h b/src/AudioGeneratorFLAC.h index 020d87e..b8a5022 100644 --- a/src/AudioGeneratorFLAC.h +++ b/src/AudioGeneratorFLAC.h @@ -26,7 +26,7 @@ extern "C" { #include "libflac/FLAC/stream_decoder.h" }; -class AudioGeneratorFLAC : AudioGenerator +class AudioGeneratorFLAC : public AudioGenerator { public: AudioGeneratorFLAC(); diff --git a/src/AudioGeneratorMIDI.h b/src/AudioGeneratorMIDI.h index fb93dbb..243dbf2 100644 --- a/src/AudioGeneratorMIDI.h +++ b/src/AudioGeneratorMIDI.h @@ -26,7 +26,7 @@ #define TSF_NO_STDIO #include "libtinysoundfont/tsf.h" -class AudioGeneratorMIDI : AudioGenerator +class AudioGeneratorMIDI : public AudioGenerator { public: AudioGeneratorMIDI() { freq=44100; }; diff --git a/src/AudioGeneratorMOD.h b/src/AudioGeneratorMOD.h index 3260820..4eb61f4 100644 --- a/src/AudioGeneratorMOD.h +++ b/src/AudioGeneratorMOD.h @@ -23,7 +23,7 @@ #include "AudioGenerator.h" -class AudioGeneratorMOD : AudioGenerator +class AudioGeneratorMOD : public AudioGenerator { public: AudioGeneratorMOD(); diff --git a/src/AudioGeneratorWAV.h b/src/AudioGeneratorWAV.h index 6431d22..9fe62d0 100644 --- a/src/AudioGeneratorWAV.h +++ b/src/AudioGeneratorWAV.h @@ -23,7 +23,7 @@ #include "AudioGenerator.h" -class AudioGeneratorWAV : AudioGenerator +class AudioGeneratorWAV : public AudioGenerator { public: AudioGeneratorWAV();