From dea94c9e142abf39681ba5933074831705331fdc Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Fri, 6 Apr 2018 13:14:57 +0200 Subject: [PATCH] add RTTTL player and alarm --- platformio.ini | 4 ++-- src/main.cpp | 7 +++++++ src/mp3.cpp | 18 +++++++++++++++++- src/mp3.h | 5 +++++ 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/platformio.ini b/platformio.ini index eb6a732..e8f322e 100644 --- a/platformio.ini +++ b/platformio.ini @@ -24,8 +24,8 @@ lib_deps = u8g2 NTPClient ; ESP8266Audio -; https://github.com/earlephilhower/ESP8266Audio.git - https://github.com/h3ndrik/ESP8266Audio.git + https://github.com/earlephilhower/ESP8266Audio.git +; https://github.com/h3ndrik/ESP8266Audio.git https://github.com/Gianbacchio/ESP8266_Spiram.git Adafruit Unified Sensor Adafruit BME280 Library diff --git a/src/main.cpp b/src/main.cpp index ff382e0..a67c650 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -436,6 +436,13 @@ void loop() // mp3.setVolume(10); // mp3.start_Progmem(); // delay(1500); + + static const char starwars[] PROGMEM = + "starwars:d=4,o=5,b=180:8f,8f,8f,2a#.,2f.6,8d#6,8d6,8c6,2a#.6,f.6,8d#6,8d6,8c6,2a#.6,f.6,8d#6,8d6,8d#6,2c6"; + // Plenty more at: http://mines.lumpylumpy.com/Electronics/Computers/Software/Cpp/MFC/RingTones.RTTTL + mp3.playRTTTL(starwars, strlen_P(starwars)); + delay(15000); + mp3.setVolume(2); mp3.start("http://radioessen.cast.addradio.de/radioessen/simulcast/high/stream.mp3"); led.changeAnimation(2, 0); diff --git a/src/mp3.cpp b/src/mp3.cpp index 07961f8..55603f7 100644 --- a/src/mp3.cpp +++ b/src/mp3.cpp @@ -6,6 +6,7 @@ #include "AudioFileSourcePROGMEM.h" #include "AudioFileSourceBuffer.h" #include "AudioGeneratorMP3.h" +#include "AudioGeneratorRTTTL.h" #include "AudioOutputI2S.h" //#include "driver/gpio.h" //#include "driver/periph_ctrl.h" @@ -84,6 +85,13 @@ void MP3::start_Progmem() { start(); } +void MP3::playRTTTL(const char* str, size_t len) { + rtttl = str; + rtttl_len = len; + volume = 2; + start(); +} + void MP3::setVolume(int volume) { if (volume > 0 && volume <= 100) { this->volume = volume; @@ -109,6 +117,7 @@ void MP3::mp3_decoder_task(void *pvParameters) { // strcpy(titleStr, "loading..."); if (fromProgmem) file = new AudioFileSourcePROGMEM(alarmsound, alarmsound_len); + else if (rtttl_len > 0) file = new AudioFileSourcePROGMEM( rtttl, rtttl_len ); else file = new AudioFileSourceICYStream(URL); file->RegisterMetadataCB(MDCallback, (void*)"ICY"); buff = new AudioFileSourceBuffer(file, preallocateBuffer, preallocateBufferSize); @@ -118,7 +127,13 @@ void MP3::mp3_decoder_task(void *pvParameters) { out = new AudioOutputI2S(I2S_NUM_0, false); out->SetPinout(12, 13, 25); } - decoder = new AudioGeneratorMP3(preallocateCodec, preallocateCodecSize); + if (rtttl_len > 0) { + decoder = new AudioGeneratorRTTTL(); + Serial.println(rtttl_len); Serial.println(FPSTR(rtttl)); + rtttl_len = 0; + } else { + decoder = new AudioGeneratorMP3(preallocateCodec, preallocateCodecSize); + } decoder->RegisterStatusCB(StatusCallback, (void*)"mp3"); decoder->begin(buff, out); if (volume > 0 && volume <= 100) { @@ -143,6 +158,7 @@ void MP3::mp3_decoder_task(void *pvParameters) { strcpy(titleStr, "stopped"); playing = false; + i2s_zero_dma_buffer(I2S_NUM_0); if (decoder) { decoder->stop(); diff --git a/src/mp3.h b/src/mp3.h index 0e739c4..1595698 100644 --- a/src/mp3.h +++ b/src/mp3.h @@ -1,6 +1,8 @@ #ifndef _MP3_H #define _MP3_H +#include + #include #include #include "AudioFileSourceICYStream.h" @@ -21,6 +23,7 @@ class MP3 { void start(void); void start(const char*); void start_Progmem(); + void playRTTTL(const char *, size_t); void setVolume(int); int getVolume(); bool playing = false; @@ -28,6 +31,8 @@ class MP3 { bool volumeChanged = true; uint32_t getBuffFill(void); bool fromProgmem; + const char* rtttl PROGMEM; + size_t rtttl_len = (size_t)0; private: TaskHandle_t audioTaskHandle; void mp3_decoder_task(void*);