diff --git a/doubleBuffer.h b/doubleBuffer.h index 27255c22..e9f2579a 100644 --- a/doubleBuffer.h +++ b/doubleBuffer.h @@ -28,6 +28,16 @@ public: return tmpBuffer[tmpBuffer.size() / 2]; } + T mean() const + { + if (buffer.empty()) + return 0; + double mean = 0.; + for (size_t n=0; n msBuffer / 2) + correction = copysign(msBuffer / 2, correction); buffer = (short*)malloc(toRead * sizeof(short)); + } else buffer = outputBuffer; @@ -93,7 +98,8 @@ void Stream::resetBuffers() 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) { 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) { int age = Chunk::getAge(getNextPlayerChunk(outputBuffer, framesPerBuffer)) - bufferMs; - if (age < PLAYER_CHUNK_MS / 2) + if (age < msBuffer / 2) break; } sleep = 0; @@ -121,13 +127,13 @@ void Stream::getPlayerChunk(short* outputBuffer, double outputBufferDacTime, uns } int correction(0); - if ((pBuffer->full() && (abs(median) <= PLAYER_CHUNK_MS) && (abs(median) > 1)) || - (pShortBuffer->full() && (abs(shortMedian) <= PLAYER_CHUNK_MS) && (abs(shortMedian) > 10))) + if ((pBuffer->full() && (abs(median) <= msBuffer) && (abs(median) > 1)) || + (pShortBuffer->full() && (abs(shortMedian) <= msBuffer) && (abs(shortMedian) > max(7, msBuffer)))) { correction = shortMedian; resetBuffers(); } -correction = 0; +//correction = 0; //if (time(NULL) != lastUpdate) // correction = 2; @@ -137,14 +143,14 @@ correction = 0; age += outputBufferDacTime*1000; // cout << age << "\t" << outputBufferDacTime*1000 << "\n"; - if (pShortBuffer->full() && (abs(shortMedian) > PLAYER_CHUNK_MS)) + if (pShortBuffer->full() && (abs(shortMedian) > max(15, msBuffer))) { sleep = shortMedian; 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"; } @@ -155,9 +161,9 @@ correction = 0; if (now != lastUpdate) { lastUpdate = now; - median = pBuffer->median(); - shortMedian = pShortBuffer->median(); - std::cerr << "Chunk: " << age << "\t" << pMiniBuffer->median() << "\t" << shortMedian << "\t" << median << "\t" << pBuffer->size() << "\t" << outputBufferDacTime*1000 << "\n"; + median = pBuffer->mean(); + shortMedian = pShortBuffer->mean(); + std::cerr << "Chunk: " << age << "\t" << pMiniBuffer->mean() << "\t" << shortMedian << "\t" << median << "\t" << pBuffer->size() << "\t" << outputBufferDacTime*1000 << "\n"; } }