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.
This commit is contained in:
Earle F. Philhower, III committed 2017-11-24 10:35:20 -08:00
1 parent df4588197f
commit fece88a9be
10 files changed
+72 -23

No files matched your search

+1
View File
@@ -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
+20 -5
View File
@@ -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;
}
+4
View File
@@ -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;
+6 -2
View File
@@ -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;
}
+10 -5
View File
@@ -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;
}
+7 -2
View File
@@ -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;
}
+9 -4
View File
@@ -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)
+8 -3
View File
@@ -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;
}
+6 -2
View File
@@ -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;
}
+1
View File
@@ -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