From 18c057ef5e388449d00d75687328020d6bc63174 Mon Sep 17 00:00:00 2001 From: Philip Rosedale Date: Mon, 4 Feb 2013 18:39:59 -0800 Subject: [PATCH] rendering of remaining time for audio play out --- Source/Audio.cpp | 32 +++++++++++++++++++++++++++----- Source/AudioData.cpp | 1 + Source/AudioData.h | 3 +++ 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/Source/Audio.cpp b/Source/Audio.cpp index 501a5ccc47..86d153d6fd 100644 --- a/Source/Audio.cpp +++ b/Source/Audio.cpp @@ -28,7 +28,7 @@ const float AMPLITUDE_RATIO_AT_90 = 0.5; const short RING_BUFFER_FRAMES = 10; const short RING_BUFFER_SIZE_SAMPLES = RING_BUFFER_FRAMES * BUFFER_LENGTH_SAMPLES; -const short JITTER_BUFFER_LENGTH_MSECS = 26; +const short JITTER_BUFFER_LENGTH_MSECS = 50; const int SAMPLE_RATE = 22050; const short NUM_AUDIO_SOURCES = 2; @@ -188,6 +188,7 @@ int audioCallback (const void *inputBuffer, } } + gettimeofday(&data->lastCallback, NULL); return paContinue; } @@ -415,16 +416,36 @@ void Audio::render(int screenWidth, int screenHeight) glVertex2f(currentX, topY); glVertex2f(currentX, bottomY); } + glEnd(); // show the next audio buffer and end of last write position int scaleLength = currentX - startX; float nextOutputSampleOffset = data->ringBuffer->nextOutput - data->ringBuffer->buffer; - float nextOutputX = startX + (nextOutputSampleOffset / RING_BUFFER_SIZE_SAMPLES) * scaleLength; - glColor3f(1, 0, 0); - glVertex2f(nextOutputX, topY); - glVertex2f(nextOutputX, bottomY); + float remainingBuffer = 0; + timeval currentTime; + gettimeofday(¤tTime, NULL); + float timeLeftInCurrentBuffer = diffclock(currentTime, data->lastCallback)/(1000.0*(float)BUFFER_LENGTH_SAMPLES/(float)SAMPLE_RATE) * frameWidth; + //float timeLeftInCurrentBuffer = diffclock(currentTime, data->lastCallback)/23.22 * frameWidth; + + if (data->ringBuffer->endOfLastWrite != NULL) { + remainingBuffer = (data->ringBuffer->diffLastWriteNextOutput())/BUFFER_LENGTH_SAMPLES*frameWidth; + } + + glColor3f(1, 0, 0); + glBegin(GL_QUADS); + glVertex2f(startX, topY); + glVertex2f(startX + remainingBuffer + timeLeftInCurrentBuffer, topY); + glVertex2f(startX + remainingBuffer + timeLeftInCurrentBuffer, bottomY); + glVertex2f(startX, bottomY); + glEnd(); + + //glVertex2f(nextOutputX, topY); + //glVertex2f(nextOutputX, bottomY); + + /* + float nextOutputX = startX + (nextOutputSampleOffset / RING_BUFFER_SIZE_SAMPLES) * scaleLength; float endLastWriteSampleOffset = data->ringBuffer->endOfLastWrite - data->ringBuffer->buffer; if (data->ringBuffer->endOfLastWrite == NULL) { @@ -437,6 +458,7 @@ void Audio::render(int screenWidth, int screenHeight) glVertex2f(endLastWriteX, bottomY); glEnd(); + */ } } diff --git a/Source/AudioData.cpp b/Source/AudioData.cpp index 00f550af92..a36ec2eb17 100644 --- a/Source/AudioData.cpp +++ b/Source/AudioData.cpp @@ -24,6 +24,7 @@ AudioData::AudioData(int numberOfSources, int bufferLength) { } samplesToQueue = new int16_t[bufferLength / sizeof(int16_t)]; + averagedLatency = 0.0; } AudioData::~AudioData() { diff --git a/Source/AudioData.h b/Source/AudioData.h index 7e926f1049..e4ef322d31 100644 --- a/Source/AudioData.h +++ b/Source/AudioData.h @@ -26,6 +26,9 @@ class AudioData { int16_t *samplesToQueue; + timeval lastCallback; + float averagedLatency; + AudioData(int bufferLength); AudioData(int numberOfSources, int bufferLength); ~AudioData();