mirror of
https://github.com/badaix/snapcast.git
synced 2025-05-30 01:16:16 +02:00
doubleBuffer
git-svn-id: svn://elaine/murooma/trunk@34 d8a302eb-03bc-478d-80e4-98257eca68ef
This commit is contained in:
parent
fcf5250a47
commit
07d2ef5794
2 changed files with 45 additions and 1 deletions
|
@ -18,8 +18,10 @@
|
|||
#include <condition_variable>
|
||||
#include <portaudio.h>
|
||||
#include "chunk.h"
|
||||
#include "doubleBuffer.h"
|
||||
|
||||
|
||||
DoubleBuffer<int> buffer(5000 / MS);
|
||||
std::deque<Chunk*> chunks;
|
||||
std::deque<int> timeDiffs;
|
||||
std::mutex mtx;
|
||||
|
@ -184,7 +186,9 @@ static int patestCallback( const void *inputBuffer, void *outputBuffer,
|
|||
// std::cerr << "Chunks: " << chunks->size() << "\n";
|
||||
mutex.unlock();
|
||||
age = getAge(*chunk) + timeInfo->outputBufferDacTime*1000;
|
||||
std::cerr << "age: " << getAge(*chunk) << "\t" << age << "\t" << timeInfo->outputBufferDacTime*1000 << "\n";
|
||||
int median = buffer.median();
|
||||
std::cerr << "age: " << getAge(*chunk) << "\t" << age << "\t" << median << "\t" << timeInfo->outputBufferDacTime*1000 << "\n";
|
||||
|
||||
if (age > bufferMs + 2*MS)
|
||||
{
|
||||
chunks->pop_front();
|
||||
|
@ -202,6 +206,7 @@ static int patestCallback( const void *inputBuffer, void *outputBuffer,
|
|||
}
|
||||
else
|
||||
{
|
||||
buffer.add(age);
|
||||
chunks->pop_front();
|
||||
break;
|
||||
}
|
||||
|
|
39
doubleBuffer.h
Normal file
39
doubleBuffer.h
Normal file
|
@ -0,0 +1,39 @@
|
|||
#ifndef DOUBLE_BUFFER_H
|
||||
#define DOUBLE_BUFFER_H
|
||||
|
||||
#include<deque>
|
||||
#include<algorithm>
|
||||
|
||||
template <class T>
|
||||
class DoubleBuffer
|
||||
{
|
||||
public:
|
||||
DoubleBuffer(size_t bufferSize) : size(bufferSize)
|
||||
{
|
||||
}
|
||||
|
||||
void add(const T& element)
|
||||
{
|
||||
buffer.push_back(element);
|
||||
if (buffer.size() > size)
|
||||
buffer.pop_front();
|
||||
}
|
||||
|
||||
T median()
|
||||
{
|
||||
if (buffer.empty())
|
||||
return 0;
|
||||
std::deque<T> tmpBuffer(buffer.begin(), buffer.end());
|
||||
std::sort(tmpBuffer.begin(), tmpBuffer.end());
|
||||
return tmpBuffer[tmpBuffer.size() / 2];
|
||||
}
|
||||
|
||||
private:
|
||||
size_t size;
|
||||
std::deque<T> buffer;
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue