From 5d10493dbadaa8ab169c2f730a6147fc1d5dbe40 Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Thu, 1 Feb 2018 02:07:54 +0100 Subject: [PATCH] add decoder polymorphism --- src/mp3.cpp | 20 +++++++++++--------- src/mp3.h | 2 +- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/mp3.cpp b/src/mp3.cpp index df2a359..f1aa5a5 100644 --- a/src/mp3.cpp +++ b/src/mp3.cpp @@ -57,6 +57,7 @@ void MP3::start() { } void MP3::mp3_decoder_task(void *pvParameters) { + strcpy(titleStr, "..."); file = new AudioFileSourceICYStream(URL); file->RegisterMetadataCB(MDCallback, (void*)"ICY"); buff = new AudioFileSourceBuffer(file, preallocateBuffer, preallocateBufferSize); @@ -65,31 +66,32 @@ void MP3::mp3_decoder_task(void *pvParameters) { out = new AudioOutputI2S(I2S_NUM_0, false, 0); out->SetPinout(12, 13, 25); } - mp3 = new AudioGeneratorMP3(); - mp3->RegisterStatusCB(StatusCallback, (void*)"mp3"); - mp3->begin(buff, out); + decoder = new AudioGeneratorMP3(); + decoder->RegisterStatusCB(StatusCallback, (void*)"mp3"); + decoder->begin(buff, out); out->SetGain(1.0); playing = true; - while(mp3->isRunning()) { + while(decoder->isRunning()) { if (millis()-lastms > 1000) { lastms = millis(); Serial.printf("Running for %d ms...\n", lastms); Serial.flush(); } - if (!mp3->loop()) break; + if (!decoder->loop()) break; if (!playing) break; vTaskDelay(5 / portTICK_PERIOD_MS); } Serial.printf("MP3 done\n"); + strcpy(titleStr, "___"); playing = false; - if (mp3) { - mp3->stop(); - delete mp3; - mp3 = NULL; + if (decoder) { + decoder->stop(); + delete decoder; + decoder = NULL; } if (buff) { buff->close(); diff --git a/src/mp3.h b/src/mp3.h index 592a913..14c5958 100644 --- a/src/mp3.h +++ b/src/mp3.h @@ -22,7 +22,7 @@ class MP3 { void start(void); bool playing = false; private: - AudioGeneratorMP3 *mp3; + AudioGenerator *decoder; AudioFileSourceICYStream *file; AudioFileSourceBuffer *buff; AudioOutputI2S *out;