mirror of
https://github.com/badaix/snapcast.git
synced 2025-06-03 11:21:41 +02:00
mean
git-svn-id: svn://elaine/murooma/trunk@160 d8a302eb-03bc-478d-80e4-98257eca68ef
This commit is contained in:
parent
47137cc436
commit
a0dc7f336f
2 changed files with 27 additions and 11 deletions
|
@ -28,6 +28,16 @@ public:
|
||||||
return tmpBuffer[tmpBuffer.size() / 2];
|
return tmpBuffer[tmpBuffer.size() / 2];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
T mean() const
|
||||||
|
{
|
||||||
|
if (buffer.empty())
|
||||||
|
return 0;
|
||||||
|
double mean = 0.;
|
||||||
|
for (size_t n=0; n<buffer.size(); ++n)
|
||||||
|
mean += (float)buffer[n] / (float)buffer.size();
|
||||||
|
return (T)mean;
|
||||||
|
}
|
||||||
|
|
||||||
inline bool full() const
|
inline bool full() const
|
||||||
{
|
{
|
||||||
return (buffer.size() == bufferSize);
|
return (buffer.size() == bufferSize);
|
||||||
|
|
28
stream.cpp
28
stream.cpp
|
@ -47,7 +47,12 @@ time_point_ms Stream::getNextPlayerChunk(short* outputBuffer, unsigned long fram
|
||||||
short* buffer;
|
short* buffer;
|
||||||
|
|
||||||
if (correction != 0)
|
if (correction != 0)
|
||||||
|
{
|
||||||
|
int msBuffer = floor(framesPerBuffer*2 / PLAYER_CHUNK_MS_SIZE);
|
||||||
|
if (abs(correction) > msBuffer / 2)
|
||||||
|
correction = copysign(msBuffer / 2, correction);
|
||||||
buffer = (short*)malloc(toRead * sizeof(short));
|
buffer = (short*)malloc(toRead * sizeof(short));
|
||||||
|
}
|
||||||
else
|
else
|
||||||
buffer = outputBuffer;
|
buffer = outputBuffer;
|
||||||
|
|
||||||
|
@ -93,7 +98,8 @@ void Stream::resetBuffers()
|
||||||
|
|
||||||
void Stream::getPlayerChunk(short* outputBuffer, double outputBufferDacTime, unsigned long framesPerBuffer)
|
void Stream::getPlayerChunk(short* outputBuffer, double outputBufferDacTime, unsigned long framesPerBuffer)
|
||||||
{
|
{
|
||||||
//cout << "framesPerBuffer: " << framesPerBuffer << "\t";
|
//cout << "framesPerBuffer: " << framesPerBuffer << "\tms: " << framesPerBuffer*2 / PLAYER_CHUNK_MS_SIZE << "\t" << PLAYER_CHUNK_SIZE << "\n";
|
||||||
|
int msBuffer = floor(framesPerBuffer*2 / PLAYER_CHUNK_MS_SIZE);
|
||||||
if (sleep != 0)
|
if (sleep != 0)
|
||||||
{
|
{
|
||||||
resetBuffers();
|
resetBuffers();
|
||||||
|
@ -112,7 +118,7 @@ void Stream::getPlayerChunk(short* outputBuffer, double outputBufferDacTime, uns
|
||||||
while (true)// (int i=0; i<(int)(round((float)sleep / (float)PLAYER_CHUNK_MS)) + 1; ++i)
|
while (true)// (int i=0; i<(int)(round((float)sleep / (float)PLAYER_CHUNK_MS)) + 1; ++i)
|
||||||
{
|
{
|
||||||
int age = Chunk::getAge(getNextPlayerChunk(outputBuffer, framesPerBuffer)) - bufferMs;
|
int age = Chunk::getAge(getNextPlayerChunk(outputBuffer, framesPerBuffer)) - bufferMs;
|
||||||
if (age < PLAYER_CHUNK_MS / 2)
|
if (age < msBuffer / 2)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
sleep = 0;
|
sleep = 0;
|
||||||
|
@ -121,13 +127,13 @@ void Stream::getPlayerChunk(short* outputBuffer, double outputBufferDacTime, uns
|
||||||
}
|
}
|
||||||
|
|
||||||
int correction(0);
|
int correction(0);
|
||||||
if ((pBuffer->full() && (abs(median) <= PLAYER_CHUNK_MS) && (abs(median) > 1)) ||
|
if ((pBuffer->full() && (abs(median) <= msBuffer) && (abs(median) > 1)) ||
|
||||||
(pShortBuffer->full() && (abs(shortMedian) <= PLAYER_CHUNK_MS) && (abs(shortMedian) > 10)))
|
(pShortBuffer->full() && (abs(shortMedian) <= msBuffer) && (abs(shortMedian) > max(7, msBuffer))))
|
||||||
{
|
{
|
||||||
correction = shortMedian;
|
correction = shortMedian;
|
||||||
resetBuffers();
|
resetBuffers();
|
||||||
}
|
}
|
||||||
correction = 0;
|
//correction = 0;
|
||||||
//if (time(NULL) != lastUpdate)
|
//if (time(NULL) != lastUpdate)
|
||||||
// correction = 2;
|
// correction = 2;
|
||||||
|
|
||||||
|
@ -137,14 +143,14 @@ correction = 0;
|
||||||
age += outputBufferDacTime*1000;
|
age += outputBufferDacTime*1000;
|
||||||
// cout << age << "\t" << outputBufferDacTime*1000 << "\n";
|
// cout << age << "\t" << outputBufferDacTime*1000 << "\n";
|
||||||
|
|
||||||
if (pShortBuffer->full() && (abs(shortMedian) > PLAYER_CHUNK_MS))
|
if (pShortBuffer->full() && (abs(shortMedian) > max(15, msBuffer)))
|
||||||
{
|
{
|
||||||
sleep = shortMedian;
|
sleep = shortMedian;
|
||||||
std::cerr << "Sleep: " << sleep << "\n";
|
std::cerr << "Sleep: " << sleep << "\n";
|
||||||
}
|
}
|
||||||
else if (pMiniBuffer->full() && (abs(age) > 50) && (abs(pMiniBuffer->median()) > 50))
|
else if (pMiniBuffer->full() && (abs(age) > 50) && (abs(pMiniBuffer->mean()) > 50))
|
||||||
{
|
{
|
||||||
sleep = pMiniBuffer->median();
|
sleep = pMiniBuffer->mean();
|
||||||
std::cerr << "Sleep: " << sleep << "\n";
|
std::cerr << "Sleep: " << sleep << "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -155,9 +161,9 @@ correction = 0;
|
||||||
if (now != lastUpdate)
|
if (now != lastUpdate)
|
||||||
{
|
{
|
||||||
lastUpdate = now;
|
lastUpdate = now;
|
||||||
median = pBuffer->median();
|
median = pBuffer->mean();
|
||||||
shortMedian = pShortBuffer->median();
|
shortMedian = pShortBuffer->mean();
|
||||||
std::cerr << "Chunk: " << age << "\t" << pMiniBuffer->median() << "\t" << shortMedian << "\t" << median << "\t" << pBuffer->size() << "\t" << outputBufferDacTime*1000 << "\n";
|
std::cerr << "Chunk: " << age << "\t" << pMiniBuffer->mean() << "\t" << shortMedian << "\t" << median << "\t" << pBuffer->size() << "\t" << outputBufferDacTime*1000 << "\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue