From fece88a9beb13f2461626400071d9abe19fe92b9 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Fri, 24 Nov 2017 10:27:33 -0800 Subject: [PATCH] Add loop method to all input and outputs Adds a virtual loop() method to the file input and audio output objects. By default does nothing, but it can be used to prefill buffers/etc. in the case of a buffered input, or to update a volume meter on an output, etc. The AudioGenerators all now call it as part of their own loop()s, so there is no user intervention required to make it work. Thanks to @armSeb for the idea with his SPI RAM code. --- src/AudioFileSource.h | 1 + src/AudioFileSourceBuffer.cpp | 25 ++++++++++++++++++++----- src/AudioFileSourceBuffer.h | 4 ++++ src/AudioGeneratorAAC.cpp | 8 ++++++-- src/AudioGeneratorFLAC.cpp | 15 ++++++++++----- src/AudioGeneratorMIDI.cpp | 9 +++++++-- src/AudioGeneratorMOD.cpp | 13 +++++++++---- src/AudioGeneratorMP3.cpp | 11 ++++++++--- src/AudioGeneratorWAV.cpp | 8 ++++++-- src/AudioOutput.h | 1 + 10 files changed, 72 insertions(+), 23 deletions(-) diff --git a/src/AudioFileSource.h b/src/AudioFileSource.h index 16b79d6..c8890ae 100644 --- a/src/AudioFileSource.h +++ b/src/AudioFileSource.h @@ -36,6 +36,7 @@ class AudioFileSource virtual bool isOpen() { return false; }; virtual uint32_t getSize() { return 0; }; virtual uint32_t getPos() { return 0; }; + virtual bool loop() { return true; }; }; #endif diff --git a/src/AudioFileSourceBuffer.cpp b/src/AudioFileSourceBuffer.cpp index 56a23a2..132bde8 100644 --- a/src/AudioFileSourceBuffer.cpp +++ b/src/AudioFileSourceBuffer.cpp @@ -115,9 +115,17 @@ uint32_t AudioFileSourceBuffer::read(void *data, uint32_t len) writePtr = 0; length = 0; filled = false; - return bytes; } + fill(); + + return bytes; +} + +void AudioFileSourceBuffer::fill() +{ + if (!buffer) return; + if (length < buffSize) { // Now try and opportunistically fill the buffer if (readPtr > writePtr) { @@ -127,7 +135,7 @@ uint32_t AudioFileSourceBuffer::read(void *data, uint32_t len) length += cnt; writePtr = (writePtr + cnt) % buffSize; } - return bytes; + return; } int bytesAvailEnd = buffSize - writePtr; @@ -135,7 +143,7 @@ uint32_t AudioFileSourceBuffer::read(void *data, uint32_t len) int cnt = src->readNonBlock(&buffer[writePtr], bytesAvailEnd); length += cnt; writePtr = (writePtr + cnt) % buffSize; - if (cnt != bytesAvailEnd) return bytes; + if (cnt != bytesAvailEnd) return; } int bytesAvailStart = readPtr - 1; if (bytesAvailStart > 0) { @@ -144,7 +152,14 @@ uint32_t AudioFileSourceBuffer::read(void *data, uint32_t len) writePtr = (writePtr + cnt) % buffSize; } } - - return bytes; } + + +bool AudioFileSourceBuffer::loop() +{ + if (!src->loop()) return false; + fill(); + return true; +} + diff --git a/src/AudioFileSourceBuffer.h b/src/AudioFileSourceBuffer.h index d79254f..7f4b5f5 100644 --- a/src/AudioFileSourceBuffer.h +++ b/src/AudioFileSourceBuffer.h @@ -36,6 +36,10 @@ class AudioFileSourceBuffer : public AudioFileSource virtual bool isOpen() override; virtual uint32_t getSize() override; virtual uint32_t getPos() override; + virtual bool loop() override; + + private: + virtual void fill(); private: AudioFileSource *src; diff --git a/src/AudioGeneratorAAC.cpp b/src/AudioGeneratorAAC.cpp index 720142a..e5f20c8 100644 --- a/src/AudioGeneratorAAC.cpp +++ b/src/AudioGeneratorAAC.cpp @@ -89,13 +89,13 @@ bool AudioGeneratorAAC::FillBufferWithValidFrame() bool AudioGeneratorAAC::loop() { - if (!running) return true; // Nothing to do here! + if (!running) goto done; // Nothing to do here! // If we've got data, try and pump it out... while (validSamples) { lastSample[0] = outSample[curSample*2]; lastSample[1] = outSample[curSample*2 + 1]; - if (!output->ConsumeSample(lastSample)) return true; // Can't send, but no error detected + if (!output->ConsumeSample(lastSample)) goto done; // Can't send, but no error detected validSamples--; curSample++; } @@ -128,6 +128,10 @@ bool AudioGeneratorAAC::loop() running = false; // No more data, we're done here... } +done: + file->loop(); + output->loop(); + return running; } diff --git a/src/AudioGeneratorFLAC.cpp b/src/AudioGeneratorFLAC.cpp index 8ec418a..33045d2 100644 --- a/src/AudioGeneratorFLAC.cpp +++ b/src/AudioGeneratorFLAC.cpp @@ -67,21 +67,22 @@ bool AudioGeneratorFLAC::begin(AudioFileSource *source, AudioOutput *output) bool AudioGeneratorFLAC::loop() { FLAC__bool ret; - if (!running) return false; - if (!output->ConsumeSample(lastSample)) return true; // Try and send last buffered sample + if (!running) goto done; + + if (!output->ConsumeSample(lastSample)) goto done; // Try and send last buffered sample do { if (buffPtr == buffLen) { ret = FLAC__stream_decoder_process_single(flac); if (!ret) { running = false; - return false; + goto done; } else { // We might be done... if (FLAC__stream_decoder_get_state(flac)==FLAC__STREAM_DECODER_END_OF_STREAM) { running = false; - return false; + goto done; } unsigned newsr = FLAC__stream_decoder_get_sample_rate(flac); unsigned newch = FLAC__stream_decoder_get_channels(flac); @@ -94,7 +95,7 @@ bool AudioGeneratorFLAC::loop() // Check for some weird case where above didn't give any data if (buffPtr == buffLen) { - return true; // At some point the flac better error and we'll retudn + goto done; // At some point the flac better error and we'll retudn } if (bitsPerSample <= 16) { lastSample[AudioOutput::LEFTCHANNEL] = buff[0][buffPtr] & 0xffff; @@ -112,6 +113,10 @@ bool AudioGeneratorFLAC::loop() buffPtr++; } while (running && output->ConsumeSample(lastSample)); +done: + file->loop(); + output->loop(); + return running; } diff --git a/src/AudioGeneratorMIDI.cpp b/src/AudioGeneratorMIDI.cpp index 734423c..92a600b 100644 --- a/src/AudioGeneratorMIDI.cpp +++ b/src/AudioGeneratorMIDI.cpp @@ -530,10 +530,11 @@ bool AudioGeneratorMIDI::begin(AudioFileSource *src, AudioOutput *out) bool AudioGeneratorMIDI::loop() { static int c = 0; - if (!running) return true; // Nothing to do here! + + if (!running) goto done; // Nothing to do here! // First, try and push in the stored sample. If we can't, then punt and try later - if (!output->ConsumeSample(lastSample)) return true; // Can't send, but no error detected + if (!output->ConsumeSample(lastSample)) goto done; // Can't send, but no error detected // Try and stuff the buffer one sample at a time do { @@ -570,6 +571,10 @@ play: } } while (running && output->ConsumeSample(lastSample)); +done: + file->loop(); + output->loop(); + return running; } diff --git a/src/AudioGeneratorMOD.cpp b/src/AudioGeneratorMOD.cpp index cf1fd83..c9b3ebd 100644 --- a/src/AudioGeneratorMOD.cpp +++ b/src/AudioGeneratorMOD.cpp @@ -75,10 +75,10 @@ bool AudioGeneratorMOD::stop() bool AudioGeneratorMOD::loop() { - if (!running) return false; // Easy-peasy + if (!running) goto done; // Easy-peasy // First, try and push in the stored sample. If we can't, then punt and try later - if (!output->ConsumeSample(lastSample)) return true; // FIFO full, wait... + if (!output->ConsumeSample(lastSample)) goto done; // FIFO full, wait... // Now advance enough times to fill the i2s buffer do { @@ -86,15 +86,20 @@ bool AudioGeneratorMOD::loop() running = RunPlayer(); if (!running) { stop(); - return false; + goto done; } mixerTick = Player.samplesPerTick; } GetSample( lastSample ); mixerTick--; } while (output->ConsumeSample(lastSample)); + +done: + file->loop(); + output->loop(); + // We'll be left with one sample still in our buffer because it couldn't fit in the FIFO - return true; + return running; } bool AudioGeneratorMOD::begin(AudioFileSource *source, AudioOutput *out) diff --git a/src/AudioGeneratorMP3.cpp b/src/AudioGeneratorMP3.cpp index 451ebb2..d3946e5 100644 --- a/src/AudioGeneratorMP3.cpp +++ b/src/AudioGeneratorMP3.cpp @@ -167,20 +167,25 @@ bool AudioGeneratorMP3::GetOneSample(int16_t sample[2]) bool AudioGeneratorMP3::loop() { - if (!running) return true; // Nothing to do here! + if (!running) goto done; // Nothing to do here! // First, try and push in the stored sample. If we can't, then punt and try later - if (!output->ConsumeSample(lastSample)) return true; // Can't send, but no error detected + if (!output->ConsumeSample(lastSample)) goto done; // Can't send, but no error detected + // Try and stuff the buffer one sample at a time do { if (!GetOneSample(lastSample)) { Serial.println("G1S failed\n"); running = false; - return false; + goto done; } } while (running && output->ConsumeSample(lastSample)); +done: + file->loop(); + output->loop(); + return running; } diff --git a/src/AudioGeneratorWAV.cpp b/src/AudioGeneratorWAV.cpp index 753ea31..1dcf4a6 100644 --- a/src/AudioGeneratorWAV.cpp +++ b/src/AudioGeneratorWAV.cpp @@ -73,10 +73,10 @@ bool AudioGeneratorWAV::GetBufferedData(int bytes, void *dest) bool AudioGeneratorWAV::loop() { - if (!running) return true; // Nothing to do here! + if (!running) goto done; // Nothing to do here! // First, try and push in the stored sample. If we can't, then punt and try later - if (!output->ConsumeSample(lastSample)) return true; // Can't send, but no error detected + if (!output->ConsumeSample(lastSample)) goto done; // Can't send, but no error detected // Try and stuff the buffer one sample at a time do @@ -101,6 +101,10 @@ bool AudioGeneratorWAV::loop() } } while (running && output->ConsumeSample(lastSample)); +done: + file->loop(); + output->loop(); + return running; } diff --git a/src/AudioOutput.h b/src/AudioOutput.h index 9fb4bb3..67621d4 100644 --- a/src/AudioOutput.h +++ b/src/AudioOutput.h @@ -36,6 +36,7 @@ class AudioOutput typedef enum { LEFTCHANNEL=0, RIGHTCHANNEL=1 } SampleIndex; virtual bool ConsumeSample(int16_t sample[2]) { (void)sample; return false; }; virtual bool stop() { return false; }; + virtual bool loop() { return true; }; void MakeSampleStereo16(int16_t sample[2]) { // Mono to "stereo" conversion