diff --git a/examples/MixerSample/MixerSample.ino b/examples/MixerSample/MixerSample.ino index 6a9df6c..7a9a1e2 100644 --- a/examples/MixerSample/MixerSample.ino +++ b/examples/MixerSample/MixerSample.ino @@ -25,6 +25,8 @@ void setup() Serial.begin(115200); delay(1000); Serial.printf("WAV start\n"); + + audioLogger = &Serial; file[0] = new AudioFileSourcePROGMEM( viola, sizeof(viola) ); out = new AudioOutputI2S(); mixer = new AudioOutputMixer(32, out); diff --git a/examples/PlayAACFromPROGMEM/PlayAACFromPROGMEM.ino b/examples/PlayAACFromPROGMEM/PlayAACFromPROGMEM.ino index 628fe6c..af7f908 100644 --- a/examples/PlayAACFromPROGMEM/PlayAACFromPROGMEM.ino +++ b/examples/PlayAACFromPROGMEM/PlayAACFromPROGMEM.ino @@ -11,7 +11,8 @@ AudioOutputI2S *out; void setup() { Serial.begin(115200); - + + audioLogger = &Serial; in = new AudioFileSourcePROGMEM(sampleaac, sizeof(sampleaac)); aac = new AudioGeneratorAAC(); out = new AudioOutputI2S(); diff --git a/examples/PlayFLACFromPROGMEMToDAC/PlayFLACFromPROGMEMToDAC.ino b/examples/PlayFLACFromPROGMEMToDAC/PlayFLACFromPROGMEMToDAC.ino index e55f7f3..847b4d9 100644 --- a/examples/PlayFLACFromPROGMEMToDAC/PlayFLACFromPROGMEMToDAC.ino +++ b/examples/PlayFLACFromPROGMEMToDAC/PlayFLACFromPROGMEMToDAC.ino @@ -13,6 +13,8 @@ void setup() { Serial.begin(115200); Serial.println("Starting up...\n"); + + audioLogger = &Serial; file = new AudioFileSourcePROGMEM( sample_flac, sizeof(sample_flac) ); out = new AudioOutputI2S(); flac = new AudioGeneratorFLAC(); diff --git a/examples/PlayMIDIFromSPIFFS/PlayMIDIFromSPIFFS.ino b/examples/PlayMIDIFromSPIFFS/PlayMIDIFromSPIFFS.ino index 0dd1709..dcfa8f9 100644 --- a/examples/PlayMIDIFromSPIFFS/PlayMIDIFromSPIFFS.ino +++ b/examples/PlayMIDIFromSPIFFS/PlayMIDIFromSPIFFS.ino @@ -27,6 +27,7 @@ void setup() Serial.begin(115200); Serial.println("Starting up...\n"); + audioLogger = &Serial; sf2 = new AudioFileSourceSPIFFS(soundfont); mid = new AudioFileSourceSPIFFS(midifile); diff --git a/examples/PlayMODFromPROGMEMToDAC/PlayMODFromPROGMEMToDAC.ino b/examples/PlayMODFromPROGMEMToDAC/PlayMODFromPROGMEMToDAC.ino index 9d3c81b..44aac59 100644 --- a/examples/PlayMODFromPROGMEMToDAC/PlayMODFromPROGMEMToDAC.ino +++ b/examples/PlayMODFromPROGMEMToDAC/PlayMODFromPROGMEMToDAC.ino @@ -20,6 +20,8 @@ void setup() WiFi.mode(WIFI_OFF); //WiFi.forceSleepBegin(); Serial.begin(115200); delay(1000); + + audioLogger = &Serial; file = new AudioFileSourcePROGMEM( enigma_mod, sizeof(enigma_mod) ); // out = new AudioOutputI2S(0, 1); Uncomment this line, comment the next one to use the internal DAC channel 1 (pin25) on ESP32 out = new AudioOutputI2S(); diff --git a/examples/PlayMP3FromSPIFFS/PlayMP3FromSPIFFS.ino b/examples/PlayMP3FromSPIFFS/PlayMP3FromSPIFFS.ino index 001163b..364a7a8 100644 --- a/examples/PlayMP3FromSPIFFS/PlayMP3FromSPIFFS.ino +++ b/examples/PlayMP3FromSPIFFS/PlayMP3FromSPIFFS.ino @@ -51,6 +51,8 @@ void setup() delay(1000); SPIFFS.begin(); Serial.printf("Sample MP3 playback begins...\n"); + + audioLogger = &Serial; file = new AudioFileSourceSPIFFS("/pno-cs.mp3"); id3 = new AudioFileSourceID3(file); id3->RegisterMetadataCB(MDCallback, (void*)"ID3TAG"); diff --git a/examples/PlayRTTTLToI2SDAC/PlayRTTTLToI2SDAC.ino b/examples/PlayRTTTLToI2SDAC/PlayRTTTLToI2SDAC.ino index ea34692..fa12eaa 100644 --- a/examples/PlayRTTTLToI2SDAC/PlayRTTTLToI2SDAC.ino +++ b/examples/PlayRTTTLToI2SDAC/PlayRTTTLToI2SDAC.ino @@ -16,6 +16,8 @@ void setup() delay(1000); Serial.printf("RTTTL start\n"); + + audioLogger = &Serial; file = new AudioFileSourcePROGMEM( rudolph, strlen_P(rudolph) ); out = new AudioOutputI2S(); rtttl = new AudioGeneratorRTTTL(); diff --git a/examples/PlayWAVFromPROGMEM/PlayWAVFromPROGMEM.ino b/examples/PlayWAVFromPROGMEM/PlayWAVFromPROGMEM.ino index a28566c..5221c91 100644 --- a/examples/PlayWAVFromPROGMEM/PlayWAVFromPROGMEM.ino +++ b/examples/PlayWAVFromPROGMEM/PlayWAVFromPROGMEM.ino @@ -22,6 +22,8 @@ void setup() Serial.begin(115200); delay(1000); Serial.printf("WAV start\n"); + + audioLogger = &Serial; file = new AudioFileSourcePROGMEM( viola, sizeof(viola) ); out = new AudioOutputI2SNoDAC(); wav = new AudioGeneratorWAV(); diff --git a/examples/StreamMP3FromHTTP/StreamMP3FromHTTP.ino b/examples/StreamMP3FromHTTP/StreamMP3FromHTTP.ino index a250bb1..4569b6c 100644 --- a/examples/StreamMP3FromHTTP/StreamMP3FromHTTP.ino +++ b/examples/StreamMP3FromHTTP/StreamMP3FromHTTP.ino @@ -71,6 +71,7 @@ void setup() } Serial.println("Connected"); + audioLogger = &Serial; file = new AudioFileSourceICYStream(URL); file->RegisterMetadataCB(MDCallback, (void*)"ICY"); buff = new AudioFileSourceBuffer(file, 2048); diff --git a/examples/StreamMP3FromHTTP_SPIRAM/StreamMP3FromHTTP_SPIRAM.ino b/examples/StreamMP3FromHTTP_SPIRAM/StreamMP3FromHTTP_SPIRAM.ino index df01346..7b24414 100644 --- a/examples/StreamMP3FromHTTP_SPIRAM/StreamMP3FromHTTP_SPIRAM.ino +++ b/examples/StreamMP3FromHTTP_SPIRAM/StreamMP3FromHTTP_SPIRAM.ino @@ -69,6 +69,7 @@ void setup() } Serial.println("Connected"); + audioLogger = &Serial; file = new AudioFileSourceICYStream(URL); file->RegisterMetadataCB(MDCallback, (void*)"ICY"); // Initialize 23LC1024 SPI RAM buffer with chip select ion GPIO4 and ram size of 128KByte diff --git a/examples/WebRadio/WebRadio.ino b/examples/WebRadio/WebRadio.ino index 8045746..cb536b5 100644 --- a/examples/WebRadio/WebRadio.ino +++ b/examples/WebRadio/WebRadio.ino @@ -281,6 +281,7 @@ void setup() strcpy_P(status, PSTR("OK")); strcpy_P(title, PSTR("Idle")); + audioLogger = &Serial; file = NULL; buff = NULL; out = new AudioOutputI2S(); diff --git a/src/AudioFileSourceBuffer.cpp b/src/AudioFileSourceBuffer.cpp index 08b8292..2b2641b 100644 --- a/src/AudioFileSourceBuffer.cpp +++ b/src/AudioFileSourceBuffer.cpp @@ -27,7 +27,7 @@ AudioFileSourceBuffer::AudioFileSourceBuffer(AudioFileSource *source, uint32_t b { buffSize = buffSizeBytes; buffer = (uint8_t*)malloc(sizeof(uint8_t) * buffSize); - if (!buffer) Serial.printf_P(PSTR("Unable to allocate AudioFileSourceBuffer::buffer[]\n")); + if (!buffer) audioLogger->printf_P(PSTR("Unable to allocate AudioFileSourceBuffer::buffer[]\n")); deallocateBuffer = true; writePtr = 0; readPtr = 0; diff --git a/src/AudioFileSourceHTTPStream.cpp b/src/AudioFileSourceHTTPStream.cpp index 0506c7b..23b4644 100644 --- a/src/AudioFileSourceHTTPStream.cpp +++ b/src/AudioFileSourceHTTPStream.cpp @@ -59,7 +59,7 @@ AudioFileSourceHTTPStream::~AudioFileSourceHTTPStream() uint32_t AudioFileSourceHTTPStream::read(void *data, uint32_t len) { if (data==NULL) { - Serial.printf_P(PSTR("ERROR! AudioFileSourceHTTPStream::read passed NULL data\n")); + audioLogger->printf_P(PSTR("ERROR! AudioFileSourceHTTPStream::read passed NULL data\n")); return 0; } return readInternal(data, len, false); @@ -68,7 +68,7 @@ uint32_t AudioFileSourceHTTPStream::read(void *data, uint32_t len) uint32_t AudioFileSourceHTTPStream::readNonBlock(void *data, uint32_t len) { if (data==NULL) { - Serial.printf_P(PSTR("ERROR! AudioFileSourceHTTPStream::readNonBlock passed NULL data\n")); + audioLogger->printf_P(PSTR("ERROR! AudioFileSourceHTTPStream::readNonBlock passed NULL data\n")); return 0; } return readInternal(data, len, true); diff --git a/src/AudioFileSourceSPIRAMBuffer.cpp b/src/AudioFileSourceSPIRAMBuffer.cpp index 4edb5a2..dde5e68 100644 --- a/src/AudioFileSourceSPIRAMBuffer.cpp +++ b/src/AudioFileSourceSPIRAMBuffer.cpp @@ -36,7 +36,7 @@ AudioFileSourceSPIRAMBuffer::AudioFileSourceSPIRAMBuffer(AudioFileSource *source length = 0; filled = false; bytesAvailable = 0; - Serial.printf_P(PSTR("SPI RAM buffer size: %u Bytes\n"), ramSize); + audioLogger->printf_P(PSTR("SPI RAM buffer size: %u Bytes\n"), ramSize); } AudioFileSourceSPIRAMBuffer::~AudioFileSourceSPIRAMBuffer() @@ -81,7 +81,7 @@ uint32_t AudioFileSourceSPIRAMBuffer::read(void *data, uint32_t len) writePtr = readPtr = 0; uint16_t toRead = sizeof(buffer); // Fill up completely before returning any data at all - Serial.printf_P(PSTR("Buffering...\n")); + audioLogger->printf_P(PSTR("Buffering...\n")); while (bytesAvailable!=ramSize) { length = src->read(buffer, toRead); if(length>0) { @@ -98,10 +98,10 @@ uint32_t AudioFileSourceSPIRAMBuffer::read(void *data, uint32_t len) } writePtr = bytesAvailable % ramSize; filled = true; - Serial.printf_P(PSTR("Filling Done !\n")); + audioLogger->printf_P(PSTR("Filling Done !\n")); } -// Serial.printf("Buffer: %u%\n", bytesAvailable*100/ramSize); +// audioLogger->printf("Buffer: %u%\n", bytesAvailable*100/ramSize); uint8_t *ptr = reinterpret_cast(data); uint32_t toReadFromBuffer = (len < bytesAvailable) ? len : bytesAvailable; @@ -142,7 +142,7 @@ void AudioFileSourceSPIRAMBuffer::fill() bytesAvailable+=cnt; writePtr = (writePtr + cnt) % ramSize; #ifdef SPIBUF_DEBUG - Serial.printf_P(PSTR("SockRead: %u | RamAvail: %u\n"), cnt, bytesAvailable); + audioLogger->printf_P(PSTR("SockRead: %u | RamAvail: %u\n"), cnt, bytesAvailable); #endif } return; diff --git a/src/AudioGeneratorAAC.cpp b/src/AudioGeneratorAAC.cpp index 7b72bb7..98f7207 100644 --- a/src/AudioGeneratorAAC.cpp +++ b/src/AudioGeneratorAAC.cpp @@ -34,13 +34,13 @@ AudioGeneratorAAC::AudioGeneratorAAC() buff = (uint8_t*)malloc(buffLen); outSample = (int16_t*)malloc(1024 * 2 * sizeof(uint16_t)); if (!buff || !outSample) { - Serial.printf_P(PSTR("ERROR: Out of memory in AAC\n")); + audioLogger->printf_P(PSTR("ERROR: Out of memory in AAC\n")); Serial.flush(); } hAACDecoder = AACInitDecoder(); if (!hAACDecoder) { - Serial.printf_P(PSTR("Out of memory error! hAACDecoder==NULL\n")); + audioLogger->printf_P(PSTR("Out of memory error! hAACDecoder==NULL\n")); Serial.flush(); } @@ -69,12 +69,12 @@ AudioGeneratorAAC::AudioGeneratorAAC(void *preallocateData, int preallocateSz) int used = p - (uint8_t*)preallocateSpace; int availSpace = preallocateSize - used; if (availSpace < 0 ) { - Serial.printf_P(PSTR("ERROR: Out of memory in AAC\n")); + audioLogger->printf_P(PSTR("ERROR: Out of memory in AAC\n")); } hAACDecoder = AACInitDecoderPre(p, availSpace); if (!hAACDecoder) { - Serial.printf_P(PSTR("Out of memory error! hAACDecoder==NULL\n")); + audioLogger->printf_P(PSTR("Out of memory error! hAACDecoder==NULL\n")); Serial.flush(); } buffValid = 0; diff --git a/src/AudioGeneratorFLAC.cpp b/src/AudioGeneratorFLAC.cpp index 3b21712..01f7e90 100644 --- a/src/AudioGeneratorFLAC.cpp +++ b/src/AudioGeneratorFLAC.cpp @@ -186,7 +186,7 @@ void AudioGeneratorFLAC::metadata_cb(const FLAC__StreamDecoder *decoder, const F { (void) decoder; (void) metadata; - Serial.printf_P(PSTR("Metadata\n")); + audioLogger->printf_P(PSTR("Metadata\n")); } void AudioGeneratorFLAC::error_cb(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status) { diff --git a/src/AudioGeneratorMIDI.cpp b/src/AudioGeneratorMIDI.cpp index bdc37a0..ba6d032 100644 --- a/src/AudioGeneratorMIDI.cpp +++ b/src/AudioGeneratorMIDI.cpp @@ -73,7 +73,7 @@ void AudioGeneratorMIDI::midi_error(const char *msg, int curpos) cb.st(curpos, msg); #if 0 int ptr; - Serial.printf("---> MIDI file error at position %04X (%d): %s\n", (uint16_t) curpos, (uint16_t) curpos, msg); + audioLogger->printf("---> MIDI file error at position %04X (%d): %s\n", (uint16_t) curpos, (uint16_t) curpos, msg); /* print some bytes surrounding the error */ ptr = curpos - 16; if (ptr < 0) ptr = 0; @@ -81,9 +81,9 @@ void AudioGeneratorMIDI::midi_error(const char *msg, int curpos) for (int i = 0; i < 32; i++) { char c; buffer.read (buffer.data, &c, 1); - Serial.printf((ptr + i) == curpos ? " [%02X] " : "%02X ", (int) c & 0xff); + audioLogger->printf((ptr + i) == curpos ? " [%02X] " : "%02X ", (int) c & 0xff); } - Serial.printf("\n"); + audioLogger->printf("\n"); #endif running = false; } diff --git a/src/AudioGeneratorMP3.cpp b/src/AudioGeneratorMP3.cpp index 538e5b9..6f19da9 100644 --- a/src/AudioGeneratorMP3.cpp +++ b/src/AudioGeneratorMP3.cpp @@ -121,7 +121,7 @@ enum mad_flow AudioGeneratorMP3::Input() int len = buffLen - unused; len = file->read(buff + unused, len); if (len == 0) { - Serial.printf_P(PSTR("MP3 stop, len==0\n")); + audioLogger->printf_P(PSTR("MP3 stop, len==0\n")); return MAD_FLOW_STOP; } @@ -162,7 +162,7 @@ bool AudioGeneratorMP3::GetOneSample(int16_t sample[2]) switch ( mad_synth_frame_onens(synth, frame, nsCount++) ) { case MAD_FLOW_STOP: - case MAD_FLOW_BREAK: Serial.printf_P(PSTR("msf1ns failed\n")); + case MAD_FLOW_BREAK: audioLogger->printf_P(PSTR("msf1ns failed\n")); return false; // Either way we're done default: break; // Do nothing @@ -201,7 +201,7 @@ retry: } if (!GetOneSample(lastSample)) { - Serial.printf_P(PSTR("G1S failed\n")); + audioLogger->printf_P(PSTR("G1S failed\n")); running = false; goto done; } @@ -223,7 +223,7 @@ bool AudioGeneratorMP3::begin(AudioFileSource *source, AudioOutput *output) if (!output) return false; this->output = output; if (!file->isOpen()) { - Serial.printf_P(PSTR("MP3 source file not open\n")); + audioLogger->printf_P(PSTR("MP3 source file not open\n")); return false; // Error } @@ -252,7 +252,7 @@ bool AudioGeneratorMP3::begin(AudioFileSource *source, AudioOutput *output) p += (sizeof(struct mad_synth)+7) & ~7; int neededBytes = p - reinterpret_cast(preallocateSpace); if (neededBytes > preallocateSize) { - Serial.printf_P("OOM error in MP3: Want %d bytes, have %d bytes preallocated.\n", neededBytes, preallocateSize); + audioLogger->printf_P("OOM error in MP3: Want %d bytes, have %d bytes preallocated.\n", neededBytes, preallocateSize); return false; } } else { @@ -313,13 +313,13 @@ extern "C" { if ((freestack < 512) || (freeheap < 5120)) { static int laststack, lastheap; if (laststack!=freestack|| lastheap !=freeheap) { - Serial.printf_P(PSTR("%s: FREESTACK=%d, FREEHEAP=%d\n"), s, /*t, i,*/ freestack, /*cont_get_free_stack(&g_cont),*/ freeheap); + audioLogger->printf_P(PSTR("%s: FREESTACK=%d, FREEHEAP=%d\n"), s, /*t, i,*/ freestack, /*cont_get_free_stack(&g_cont),*/ freeheap); } if (freestack < 256) { - Serial.printf_P(PSTR("out of stack!\n")); + audioLogger->printf_P(PSTR("out of stack!\n")); } if (freeheap < 1024) { - Serial.printf_P(PSTR("out of heap!\n")); + audioLogger->printf_P(PSTR("out of heap!\n")); } Serial.flush(); laststack = freestack; diff --git a/src/AudioGeneratorMP3a.cpp b/src/AudioGeneratorMP3a.cpp index d256822..f75846a 100644 --- a/src/AudioGeneratorMP3a.cpp +++ b/src/AudioGeneratorMP3a.cpp @@ -30,7 +30,7 @@ AudioGeneratorMP3a::AudioGeneratorMP3a() output = NULL; hMP3Decoder = MP3InitDecoder(); if (!hMP3Decoder) { - Serial.printf_P(PSTR("Out of memory error! hMP3Decoder==NULL\n")); + audioLogger->printf_P(PSTR("Out of memory error! hMP3Decoder==NULL\n")); Serial.flush(); } // For sanity's sake... diff --git a/src/AudioGeneratorRTTTL.cpp b/src/AudioGeneratorRTTTL.cpp index c30b895..87825bd 100644 --- a/src/AudioGeneratorRTTTL.cpp +++ b/src/AudioGeneratorRTTTL.cpp @@ -257,7 +257,7 @@ bool AudioGeneratorRTTTL::GetNextNote() } ttlSamples = (rate * dur ) / 1000; - //Serial.printf("%d %d %d %d %d\n", dur, note, scale, ttlSamplesPerWaveFP10, ttlSamples ); + //audioLogger->printf("%d %d %d %d %d\n", dur, note, scale, ttlSamplesPerWaveFP10, ttlSamples ); return true; } diff --git a/src/AudioLogger.cpp b/src/AudioLogger.cpp new file mode 100644 index 0000000..19135ae --- /dev/null +++ b/src/AudioLogger.cpp @@ -0,0 +1,5 @@ + +#include "AudioLogger.h" + +DevNullOut silencedLogger; +Print* audioLogger = &silencedLogger; diff --git a/src/AudioLogger.h b/src/AudioLogger.h new file mode 100644 index 0000000..3ea0898 --- /dev/null +++ b/src/AudioLogger.h @@ -0,0 +1,19 @@ + +#include + +#ifndef _AUDIOLOGGER_H +#define _AUDIOLOGGER_H + +class DevNullOut: public Print +{ +public: + virtual size_t write(uint8_t) { return 1; } +}; + +extern DevNullOut silencedLogger; + +// Global `audioLogger` is initialized to &silencedLogger +// It can be initialized anytime to &Serial or any other Print:: derivative instance. +extern Print* audioLogger; + +#endif diff --git a/src/AudioOutputI2S.cpp b/src/AudioOutputI2S.cpp index 6256cbe..71ac29e 100644 --- a/src/AudioOutputI2S.cpp +++ b/src/AudioOutputI2S.cpp @@ -69,9 +69,9 @@ AudioOutputI2S::AudioOutputI2S(int port, int output_mode, int dma_buf_count, int .dma_buf_len = 64, .use_apll = use_apll // Use audio PLL }; - Serial.printf("+%d %p\n", portNo, &i2s_config_dac); + audioLogger->printf("+%d %p\n", portNo, &i2s_config_dac); if (i2s_driver_install((i2s_port_t)portNo, &i2s_config_dac, 0, NULL) != ESP_OK) { - Serial.println("ERROR: Unable to install I2S drives\n"); + audioLogger->println("ERROR: Unable to install I2S drives\n"); } if (output_mode == INTERNAL_DAC || output_mode == INTERNAL_PDM) { i2s_set_pin((i2s_port_t)portNo, NULL); @@ -100,7 +100,7 @@ AudioOutputI2S::~AudioOutputI2S() { #ifdef ESP32 if (i2sOn) { - Serial.printf("UNINSTALL I2S\n"); + audioLogger->printf("UNINSTALL I2S\n"); i2s_driver_uninstall((i2s_port_t)portNo); //stop & destroy i2s driver } #else diff --git a/src/AudioOutputSerialWAV.cpp b/src/AudioOutputSerialWAV.cpp index 159d692..458860f 100644 --- a/src/AudioOutputSerialWAV.cpp +++ b/src/AudioOutputSerialWAV.cpp @@ -72,7 +72,7 @@ bool AudioOutputSerialWAV::ConsumeSample(int16_t sample[2]) bool AudioOutputSerialWAV::stop() { - Serial.printf_P(PSTR("\n\n\nEOF\n\n\n")); + audioLogger->printf_P(PSTR("\n\n\nEOF\n\n\n")); return true; } diff --git a/src/AudioStatus.h b/src/AudioStatus.h index c451065..62e663f 100644 --- a/src/AudioStatus.h +++ b/src/AudioStatus.h @@ -23,6 +23,8 @@ #include +#include "AudioLogger.h" + class AudioStatus { public: