Playing with some buffering improvements -- try to buffer samples in

chunks, instead of one at a time. Increased the default stream buffer
size.
This commit is contained in:
casey langen 2017-01-04 01:32:49 -08:00
parent 165b5aeb32
commit 9af25a1541
2 changed files with 22 additions and 2 deletions

View File

@ -61,6 +61,7 @@ Stream::Stream(int samplesPerChannel, int bufferCount, unsigned int options)
, decoderChannels(0)
, decoderSamplePosition(0)
, done(false)
, flags(FlagNone)
, remainder(nullptr)
, rawBuffer(nullptr) {
if ((this->options & NoDSP) == 0) {
@ -216,7 +217,23 @@ Buffer* Stream::GetNextProcessedOutputBuffer() {
}
void Stream::RefillInternalBuffers() {
int count = this->bufferCount - this->filledBuffers.size();
int recycled = this->recycledBuffers.size();
int count = 0;
if (!this->rawBuffer) { /* not initialized */
this->flags |= FlagFilling;
recycled = this->bufferCount;
}
else if (recycled > (this->bufferCount * 2) / 3) { /* dwindling */
this->flags |= FlagFilling;
}
if ((this->flags & FlagFilling) > 0) {
count = std::min(recycled, this->bufferCount / 5);
if (count <= 1) {
this->flags &= ~FlagFilling; /* full again */
}
}
while (!this->done && count > 0) {
--count;

View File

@ -56,7 +56,7 @@ namespace musik { namespace core { namespace audio {
memory. if we need more buffers they will be allocated dynamically */
static StreamPtr Create(
int samplesPerChannel = 2048,
int bufferCount = 32,
int bufferCount = 128,
unsigned int options = 0);
private:
@ -72,6 +72,8 @@ namespace musik { namespace core { namespace audio {
virtual bool OpenStream(std::string uri);
private:
enum Flags { FlagNone = 0, FlagFilling = 1 };
bool GetNextBufferFromDecoder();
Buffer* GetEmptyBuffer();
void ApplyDsp(Buffer* buffer);
@ -96,6 +98,7 @@ namespace musik { namespace core { namespace audio {
unsigned int options;
int samplesPerChannel;
int bufferCount;
int flags;
bool done;
float* rawBuffer;