Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finish bufferred samples #520

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/AudioGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class AudioGenerator

protected:
bool running;
bool finishing;
AudioFileSource *file;
AudioOutput *output;
int16_t lastSample[2];
Expand Down
15 changes: 13 additions & 2 deletions src/AudioGeneratorMP3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
AudioGeneratorMP3::AudioGeneratorMP3()
{
running = false;
finishing = false;
file = NULL;
output = NULL;
buff = NULL;
Expand All @@ -34,6 +35,7 @@ AudioGeneratorMP3::AudioGeneratorMP3()
AudioGeneratorMP3::AudioGeneratorMP3(void *space, int size): preallocateSpace(space), preallocateSize(size)
{
running = false;
finishing = false;
file = NULL;
output = NULL;
buff = NULL;
Expand All @@ -48,6 +50,7 @@ AudioGeneratorMP3::AudioGeneratorMP3(void *buff, int buffSize, void *stream, int
preallocateSynthSpace(synth), preallocateSynthSize(synthSize)
{
running = false;
finishing = false;
file = NULL;
output = NULL;
buff = NULL;
Expand Down Expand Up @@ -88,13 +91,14 @@ bool AudioGeneratorMP3::stop()
stream = NULL;

running = false;
finishing = false;
output->stop();
return file->close();
}

bool AudioGeneratorMP3::isRunning()
{
return running;
return running || finishing;
}

enum mad_flow AudioGeneratorMP3::ErrorToFlow()
Expand Down Expand Up @@ -209,6 +213,11 @@ bool AudioGeneratorMP3::GetOneSample(int16_t sample[2])

bool AudioGeneratorMP3::loop()
{
if ( finishing )
{
return output->finish() == false;
}

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
Expand All @@ -221,7 +230,9 @@ bool AudioGeneratorMP3::loop()
if ( (samplePtr >= synth->pcm.length) && (nsCount >= nsCountMax) ) {
retry:
if (Input() == MAD_FLOW_STOP) {
return false;
running = false;
finishing = true;
return true;
}

if (!DecodeNextFrame()) {
Expand Down
25 changes: 20 additions & 5 deletions src/AudioGeneratorWAV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
AudioGeneratorWAV::AudioGeneratorWAV()
{
running = false;
finishing = false;
file = NULL;
output = NULL;
buffSize = 128;
Expand All @@ -42,6 +43,7 @@ bool AudioGeneratorWAV::stop()
{
if (!running) return true;
running = false;
finishing = false;
free(buff);
buff = NULL;
output->stop();
Expand All @@ -50,7 +52,7 @@ bool AudioGeneratorWAV::stop()

bool AudioGeneratorWAV::isRunning()
{
return running;
return running || finishing;
}


Expand All @@ -76,6 +78,19 @@ bool AudioGeneratorWAV::GetBufferedData(int bytes, void *dest)

bool AudioGeneratorWAV::loop()
{
if ( finishing )
{
if ( output->finish() )
{
stop();
return false;
}
else
{
return true;
}
}

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
Expand All @@ -86,18 +101,18 @@ bool AudioGeneratorWAV::loop()
{
if (bitsPerSample == 8) {
uint8_t l, r;
if (!GetBufferedData(1, &l)) stop();
if (!GetBufferedData(1, &l)) finishing = true;
if (channels == 2) {
if (!GetBufferedData(1, &r)) stop();
if (!GetBufferedData(1, &r)) finishing = true;
} else {
r = 0;
}
lastSample[AudioOutput::LEFTCHANNEL] = l;
lastSample[AudioOutput::RIGHTCHANNEL] = r;
} else if (bitsPerSample == 16) {
if (!GetBufferedData(2, &lastSample[AudioOutput::LEFTCHANNEL])) stop();
if (!GetBufferedData(2, &lastSample[AudioOutput::LEFTCHANNEL])) finishing = true;
if (channels == 2) {
if (!GetBufferedData(2, &lastSample[AudioOutput::RIGHTCHANNEL])) stop();
if (!GetBufferedData(2, &lastSample[AudioOutput::RIGHTCHANNEL])) finishing = true;
} else {
lastSample[AudioOutput::RIGHTCHANNEL] = 0;
}
Expand Down
1 change: 1 addition & 0 deletions src/AudioOutput.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class AudioOutput
}
return count;
}
virtual bool finish() { return true; }
virtual bool stop() { return false; }
virtual void flush() { return; }
virtual bool loop() { return true; }
Expand Down
16 changes: 16 additions & 0 deletions src/AudioOutputI2S.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ bool AudioOutputI2S::begin(bool txDAC)
}
#endif
i2sOn = true;
finalSamples = 2*128*dma_buf_count;
SetRate(hertz); // Default
return true;
}
Expand Down Expand Up @@ -338,6 +339,21 @@ void AudioOutputI2S::flush()
#endif
}

bool AudioOutputI2S::finish()
{
if (!i2sOn)
return true;

int16_t sample[2];
sample[0] = 0;
sample[1] = 0;

while ( finalSamples > 0 && ConsumeSample(sample)) finalSamples--;

return finalSamples == 0;
}


bool AudioOutputI2S::stop()
{
if (!i2sOn)
Expand Down
2 changes: 2 additions & 0 deletions src/AudioOutputI2S.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class AudioOutputI2S : public AudioOutput
virtual bool begin() override { return begin(true); }
virtual bool ConsumeSample(int16_t sample[2]) override;
virtual void flush() override;
virtual bool finish() override;
virtual bool stop() override;

bool begin(bool txDAC);
Expand All @@ -63,4 +64,5 @@ class AudioOutputI2S : public AudioOutput
uint8_t bclkPin;
uint8_t wclkPin;
uint8_t doutPin;
unsigned long finalSamples;
};