correction

git-svn-id: svn://elaine/murooma/trunk@86 d8a302eb-03bc-478d-80e4-98257eca68ef
This commit is contained in:
(no author) 2014-07-09 22:31:04 +00:00
parent f418a144af
commit 1708157108
3 changed files with 48 additions and 26 deletions

View file

@ -6,12 +6,12 @@
#define CHANNELS (2) #define CHANNELS (2)
#define WIRE_CHUNK_MS (50) #define WIRE_CHUNK_MS (50)
#define WIRE_CHUNK_SIZE (SAMPLE_RATE*CHANNELS*WIRE_CHUNK_MS/1000) #define WIRE_CHUNK_SIZE ((SAMPLE_RATE*CHANNELS*WIRE_CHUNK_MS)/1000)
#define PLAYER_CHUNK_MS (10) #define PLAYER_CHUNK_MS (10)
#define PLAYER_CHUNK_SIZE (SAMPLE_RATE*CHANNELS*PLAYER_CHUNK_MS/1000) #define PLAYER_CHUNK_SIZE ((SAMPLE_RATE*CHANNELS*PLAYER_CHUNK_MS)/1000)
#define PLAYER_CHUNK_MS_SIZE (SAMPLE_RATE*CHANNELS/1000) #define PLAYER_CHUNK_MS_SIZE ((SAMPLE_RATE*CHANNELS)/1000)
#define FRAMES_PER_BUFFER (SAMPLE_RATE*PLAYER_CHUNK_MS/1000) #define FRAMES_PER_BUFFER ((SAMPLE_RATE*PLAYER_CHUNK_MS)/1000)
template <size_t T> template <size_t T>

View file

@ -53,18 +53,12 @@ void Stream::getSilentPlayerChunk(short* outputBuffer)
timeval Stream::getNextPlayerChunk(short* outputBuffer, int correction) timeval Stream::getNextPlayerChunk(short* outputBuffer, int correction)
{ {
Chunk* chunk = getNextChunk(); Chunk* chunk = getNextChunk();
if (correction > PLAYER_CHUNK_MS / 2) timeval tv = getTimeval(chunk);
correction = PLAYER_CHUNK_MS/2;
else if (correction < -PLAYER_CHUNK_MS/2)
correction = -PLAYER_CHUNK_MS/2;
//std::cerr << "GetNextPlayerChunk: " << correction << "\n"; //std::cerr << "GetNextPlayerChunk: " << correction << "\n";
// int age(0); // int age(0);
// age = getAge(*chunk) + outputBufferDacTime*1000 - bufferMs; // age = getAge(*chunk) + outputBufferDacTime*1000 - bufferMs;
// std::cerr << "age: " << age << " \tidx: " << chunk->idx << "\n"; // std::cerr << "age: " << age << " \tidx: " << chunk->idx << "\n";
timeval tv;
tv.tv_sec = chunk->tv_sec;
tv.tv_usec = chunk->tv_usec;
size_t missing = PLAYER_CHUNK_SIZE;// + correction*PLAYER_CHUNK_MS_SIZE; size_t missing = PLAYER_CHUNK_SIZE;// + correction*PLAYER_CHUNK_MS_SIZE;
/* double factor = (double)PLAYER_CHUNK_MS / (double)(PLAYER_CHUNK_MS + correction); /* double factor = (double)PLAYER_CHUNK_MS / (double)(PLAYER_CHUNK_MS + correction);
@ -96,18 +90,30 @@ timeval Stream::getNextPlayerChunk(short* outputBuffer, int correction)
if (correction != 0) if (correction != 0)
{ {
std::cerr << "Correction: " << correction << "\n"; timeval nextTv = tv;
if (correction > 0)
{
addMs(nextTv, -PLAYER_CHUNK_MS * 1.5);
}
else if (correction < 0)
{
addMs(nextTv, -PLAYER_CHUNK_MS / 2);
}
// std::cerr << "Correction: " << correction << "\n";
// size_t idxCorrection(0); // size_t idxCorrection(0);
size_t idx(chunk->idx); size_t idx(chunk->idx);
for (size_t n=0; n<PLAYER_CHUNK_SIZE/2; ++n) for (size_t n=0; n<PLAYER_CHUNK_SIZE/2; ++n)
{ {
if (correction > 0) if (correction > 0)
idx += 4; idx += 4;
else if ((correction < 0) && (n % 4 == 0)) else if (correction < 0)
{
if (n % 3 != 0)
idx += 2; idx += 2;
}
if (idx >= WIRE_CHUNK_SIZE) if (idx >= WIRE_CHUNK_SIZE)
{ {
//std::cerr << "idx >= WIRE_CHUNK_SIZE: " << idx << "\t" << WIRE_CHUNK_SIZE << "\n";
chunks.pop_front(); chunks.pop_front();
delete chunk; delete chunk;
chunk = getNextChunk(); chunk = getNextChunk();
@ -115,16 +121,21 @@ timeval Stream::getNextPlayerChunk(short* outputBuffer, int correction)
} }
*(outputBuffer + 2*n) = chunk->payload[idx]; *(outputBuffer + 2*n) = chunk->payload[idx];
*(outputBuffer + 2*n+1) = chunk->payload[idx + 1]; *(outputBuffer + 2*n+1) = chunk->payload[idx + 1];
//std::cerr << 2*n << "\t" << idx << "\n";
} }
//std::cerr << "Idx: " << chunk->idx << " => " << idx+2 << "\t" << WIRE_CHUNK_SIZE << "\t" << PLAYER_CHUNK_SIZE/2 << "\n";
chunk->idx = idx+2; chunk->idx = idx+2;
addMs(chunk, -chunk->idx / PLAYER_CHUNK_MS_SIZE); setTimeval(chunk, nextTv);
//timeval tvLater = getTimeval(chunk);
if (chunk->idx >= WIRE_CHUNK_SIZE) if (chunk->idx >= WIRE_CHUNK_SIZE)
{ {
//std::cerr << "Pop" << "\n";
// mutex.lock(); // mutex.lock();
chunks.pop_front(); chunks.pop_front();
// mutex.unlock(); // mutex.unlock();
delete chunk; delete chunk;
} }
//std::cerr << "Diff: " << diff_ms(tv, tvLater) << "\n";
return tv; return tv;
} }
@ -220,6 +231,7 @@ void Stream::getChunk(short* outputBuffer, double outputBufferDacTime, unsigned
sleep = median; sleep = median;
// else if (pBuffer->full() && (median+1 < -floor(PLAYER_CHUNK_MS / 2))) // else if (pBuffer->full() && (median+1 < -floor(PLAYER_CHUNK_MS / 2)))
// sleep = median; // sleep = median;
//sleep = 0;
if (sleep != 0) if (sleep != 0)
std::cerr << "Sleep: " << sleep << "\n"; std::cerr << "Sleep: " << sleep << "\n";
std::cerr << "Chunk: " << age << "\t" << shortMedian << "\t" << median << "\t" << pBuffer->size() << "\t" << outputBufferDacTime*1000 << "\n"; std::cerr << "Chunk: " << age << "\t" << shortMedian << "\t" << median << "\t" << pBuffer->size() << "\t" << outputBufferDacTime*1000 << "\n";

View file

@ -19,11 +19,26 @@ std::string timeToStr(const timeval& timestamp)
template <typename T> template <typename T>
std::string chunkTime(const T& chunk) std::string chunkTime(const T& chunk)
{
return timeToStr(getTimeval(chunk));
}
template <typename T>
timeval getTimeval(const T* chunk)
{ {
timeval ts; timeval ts;
ts.tv_sec = chunk.tv_sec; ts.tv_sec = chunk->tv_sec;
ts.tv_usec = chunk.tv_usec; ts.tv_usec = chunk->tv_usec;
return timeToStr(ts); return ts;
}
template <typename T>
void setTimeval(T* chunk, timeval tv)
{
chunk->tv_sec = tv.tv_sec;
chunk->tv_usec = tv.tv_usec;
} }
@ -39,10 +54,7 @@ long getAge(const T* chunk)
{ {
timeval now; timeval now;
gettimeofday(&now, NULL); gettimeofday(&now, NULL);
timeval ts; return diff_ms(now, chunkTime(chunk));
ts.tv_sec = chunk->tv_sec;
ts.tv_usec = chunk->tv_usec;
return diff_ms(now, ts);
} }
@ -80,9 +92,7 @@ inline void addMs(timeval& tv, int ms)
template <typename T> template <typename T>
void addMs(T* chunk, int ms) void addMs(T* chunk, int ms)
{ {
timeval tv; timeval tv = getTimeval(chunk);
tv.tv_sec = chunk->tv_sec;
tv.tv_usec = chunk->tv_usec;
addMs(tv, ms); addMs(tv, ms);
chunk->tv_sec = tv.tv_sec; chunk->tv_sec = tv.tv_sec;
chunk->tv_usec = tv.tv_usec; chunk->tv_usec = tv.tv_usec;