Add double buffer and FastROMFS classes

AudioFileSourceFastROMFS connects with the new FastROMFS to enable
higher speed read from SPI flash for files.  Requires
https://github.com/earlephilhower/ESP8266FastROMFS to be installed.

AudioOutputBuffer implements a 2nd-level memory buffer for output
dat to hopefully allow playback w/o hiccups, but needs to be vetted.
Without being able to call it from an interrupt, not sure it can actually
improve output performance.
This commit is contained in:
Earle F. Philhower, III committed 2017-10-27 21:14:14 -07:00
1 parent c5da8c3062
commit bf4f3d3257
5 files changed
+262

No files matched your search

+2
View File
@@ -1,5 +1,6 @@
AudioFileSource KEYWORD1
AudioFileSourceSPIFFS KEYWORD1
AudioFileSourceFastROMFS KEYWORD1
AudioFileSourcePROGMEM KEYWORD1
AudioFileSourceHTTPStream KEYWORD1
AudioFileSourceSD KEYWORD1
@@ -12,5 +13,6 @@ AudioOutput KEYWORD1
AudioOutputI2SDAC KEYWORD1
AudioOutputI2SNoDAC KEYWORD1
AudioOutputNull KEYWORD1
AudioOutputBuffer KEYWORD1
AudioOutputSerialWAV KEYWORD1
AudioOutputSPIFFSWAV KEYWORD1
+73
View File
@@ -0,0 +1,73 @@
/*
AudioFileSourceFastROMFS
Input FastROMFS "file" to be used by AudioGenerator
Copyright (C) 2017 Earle F. Philhower, III
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "AudioFileSourceFastROMFS.h"
AudioFileSourceFastROMFS::AudioFileSourceFastROMFS()
{
}
AudioFileSourceFastROMFS::AudioFileSourceFastROMFS(FastROMFilesystem *fs, const char *filename)
{
this->fs = fs;
open(filename);
}
bool AudioFileSourceFastROMFS::open(const char *filename)
{
f = fs->open(filename, "r");
return f;
}
AudioFileSourceFastROMFS::~AudioFileSourceFastROMFS()
{
if (f) f->close();
f = NULL;
}
uint32_t AudioFileSourceFastROMFS::read(void *data, uint32_t len)
{
return f->read(reinterpret_cast<uint8_t*>(data), len);
}
bool AudioFileSourceFastROMFS::seek(int32_t pos, int dir)
{
return f->seek(pos, dir);
}
bool AudioFileSourceFastROMFS::close()
{
f->close();
return true;
}
bool AudioFileSourceFastROMFS::isOpen()
{
return f?true:false;
}
uint32_t AudioFileSourceFastROMFS::getSize()
{
if (!f) return 0;
return f->size();
}
+49
View File
@@ -0,0 +1,49 @@
/*
AudioFileSourceFastROMFS
Input FastROMFS "file" to be used by AudioGenerator
Copyright (C) 2017 Earle F. Philhower, III
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _AUDIOFILESOURCEFASTROMFS_H
#define _AUDIOFILESOURCEFASTROMFS_H
#include <Arduino.h>
#include <ESP8266FastROMFS.h>
#include "AudioFileSource.h"
class AudioFileSourceFastROMFS : public AudioFileSource
{
public:
AudioFileSourceFastROMFS();
AudioFileSourceFastROMFS(FastROMFilesystem *fs, const char *filename);
virtual ~AudioFileSourceFastROMFS() override;
virtual bool open(const char *filename) override;
virtual uint32_t read(void *data, uint32_t len) override;
virtual bool seek(int32_t pos, int dir) override;
virtual bool close() override;
virtual bool isOpen() override;
virtual uint32_t getSize() override;
virtual uint32_t getPos() override { if (!f) return 0; else return f->position(); };
private:
FastROMFilesystem *fs;
FastROMFile *f;
};
#endif
+89
View File
@@ -0,0 +1,89 @@
/*
AudioOutputBuffer
Adds additional bufferspace to the output chain
Copyright (C) 2017 Earle F. Philhower, III
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <Arduino.h>
#include "AudioOutputBuffer.h"
AudioOutputBuffer::AudioOutputBuffer(int buffSizeSamples, AudioOutput *dest)
{
buffSize = buffSizeSamples;
leftSample = (int16_t*)malloc(sizeof(int16_t) * buffSize);
rightSample = (int16_t*)malloc(sizeof(int16_t) * buffSize);
writePtr = 0;
readPtr = 0;
sink = dest;
}
AudioOutputBuffer::~AudioOutputBuffer()
{
free(leftSample);
free(rightSample);
}
bool AudioOutputBuffer::SetRate(int hz)
{
return sink->SetRate(hz);
}
bool AudioOutputBuffer::SetBitsPerSample(int bits)
{
return sink->SetBitsPerSample(bits);
}
bool AudioOutputBuffer::SetChannels(int channels)
{
return sink->SetChannels(channels);
}
bool AudioOutputBuffer::begin()
{
filled = false;
return sink->begin();
}
bool AudioOutputBuffer::ConsumeSample(int16_t sample[2])
{
// First, try and fill I2S...
if (filled) {
while (readPtr != writePtr) {
int16_t s[2] = {leftSample[readPtr], rightSample[readPtr]};
if (!sink->ConsumeSample(s)) break; // Can't stuff any more in I2S...
readPtr = (readPtr + 1) % buffSize;
}
}
// Now, do we have space for a new sample?
int nextWritePtr = (writePtr + 1) % buffSize;
if (nextWritePtr == readPtr) {
filled = true;
return false;
}
leftSample[writePtr] = sample[LEFTCHANNEL];
rightSample[writePtr] = sample[RIGHTCHANNEL];
writePtr = nextWritePtr;
return true;
}
bool AudioOutputBuffer::stop()
{
return sink->stop();
}
+49
View File
@@ -0,0 +1,49 @@
/*
AudioOutputBuffer
Adds additional bufferspace to the output chain
Copyright (C) 2017 Earle F. Philhower, III
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _AUDIOOUTPUTBUFFER_H
#define _AUDIOOUTPUTBUFFER_H
#include "AudioOutput.h"
class AudioOutputBuffer : public AudioOutput
{
public:
AudioOutputBuffer(int bufferSizeSamples, AudioOutput *dest);
virtual ~AudioOutputBuffer() override;
virtual bool SetRate(int hz) override;
virtual bool SetBitsPerSample(int bits) override;
virtual bool SetChannels(int channels) override;
virtual bool begin() override;
virtual bool ConsumeSample(int16_t sample[2]) override;
virtual bool stop() override;
protected:
AudioOutput *sink;
int buffSize;
int16_t *leftSample;
int16_t *rightSample;
int writePtr;
int readPtr;
bool filled;
};
#endif