From bb93c64d8d0bfcd8781444a26add7dfe5f1e440c Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 16 Dec 2013 16:21:50 -0800 Subject: [PATCH] some initial audio stat render fixes --- interface/src/Application.cpp | 2 +- interface/src/Audio.cpp | 18 +++++++++--------- interface/src/Audio.h | 6 ------ libraries/audio/src/AudioRingBuffer.cpp | 2 -- libraries/audio/src/AudioRingBuffer.h | 2 ++ 5 files changed, 12 insertions(+), 18 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index e75c708841..36dccb061a 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -71,7 +71,7 @@ const int IDLE_SIMULATE_MSECS = 16; // How often should call simul // in the idle loop? (60 FPS is default) static QTimer* idleTimer = NULL; -const int STARTUP_JITTER_SAMPLES = PACKET_LENGTH_SAMPLES_PER_CHANNEL / 2; +const int STARTUP_JITTER_SAMPLES = NETWORK_BUFFER_LENGTH_SAMPLES_PER_CHANNEL / 2; // Startup optimistically with small jitter buffer that // will start playback on the second received audio packet. diff --git a/interface/src/Audio.cpp b/interface/src/Audio.cpp index 6114ce4984..add99ec4f2 100644 --- a/interface/src/Audio.cpp +++ b/interface/src/Audio.cpp @@ -451,6 +451,7 @@ void Audio::addReceivedAudioToBuffer(const QByteArray& audioByteArray) { _desiredOutputFormat, _outputFormat); if (_outputDevice) { + _outputDevice->write(outputBuffer); // add output (@speakers) data just written to the scope @@ -500,7 +501,7 @@ void Audio::render(int screenWidth, int screenHeight) { glVertex2f(currentX, topY); glVertex2f(currentX, bottomY); - for (int i = 0; i < _ringBuffer.getSampleCapacity() / 2; i++) { + for (int i = 0; i < RING_BUFFER_LENGTH_FRAMES; i++) { glVertex2f(currentX, halfY); glVertex2f(currentX + frameWidth, halfY); currentX += frameWidth; @@ -512,14 +513,12 @@ void Audio::render(int screenWidth, int screenHeight) { // Show a bar with the amount of audio remaining in ring buffer beyond current playback float remainingBuffer = 0; - timeval currentTime; - gettimeofday(¤tTime, NULL); - float timeLeftInCurrentBuffer = 0; - if (_lastCallbackTime.tv_usec > 0) { - timeLeftInCurrentBuffer = AUDIO_CALLBACK_MSECS - diffclock(&_lastCallbackTime, ¤tTime); - } - remainingBuffer = PACKET_LENGTH_SAMPLES / PACKET_LENGTH_SAMPLES * AUDIO_CALLBACK_MSECS; + int bytesLeftInAudioOutput = _audioOutput->bufferSize() - _audioOutput->bytesFree(); + float secondsLeftForAudioOutput = (bytesLeftInAudioOutput / sizeof(int16_t)) / _outputFormat.sampleRate(); + float timeLeftInCurrentBuffer = AUDIO_CALLBACK_MSECS - (secondsLeftForAudioOutput * 1000); + + remainingBuffer = 1 / AUDIO_CALLBACK_MSECS; if (_numFramesDisplayStarve == 0) { glColor3f(0, 1, 0); @@ -557,7 +556,8 @@ void Audio::render(int screenWidth, int screenHeight) { // Show a red bar with the 'start' point of one frame plus the jitter buffer glColor3f(1, 0, 0); - int jitterBufferPels = (1.f + (float)getJitterBufferSamples() / (float) PACKET_LENGTH_SAMPLES_PER_CHANNEL) * frameWidth; + int jitterBufferPels = (1.f + (float)getJitterBufferSamples() + / (float) NETWORK_BUFFER_LENGTH_SAMPLES_PER_CHANNEL) * frameWidth; sprintf(out, "%.0f\n", getJitterBufferSamples() / SAMPLE_RATE * 1000.f); drawtext(startX + jitterBufferPels - 5, topY - 9, 0.10, 0, 1, 0, out, 1, 0, 0); sprintf(out, "j %.1f\n", _measuredJitter); diff --git a/interface/src/Audio.h b/interface/src/Audio.h index f0c71189dd..eff1be345b 100644 --- a/interface/src/Audio.h +++ b/interface/src/Audio.h @@ -26,11 +26,6 @@ static const int NUM_AUDIO_CHANNELS = 2; -static const int PACKET_LENGTH_BYTES = 1024; -static const int PACKET_LENGTH_BYTES_PER_CHANNEL = PACKET_LENGTH_BYTES / 2; -static const int PACKET_LENGTH_SAMPLES = PACKET_LENGTH_BYTES / sizeof(int16_t); -static const int PACKET_LENGTH_SAMPLES_PER_CHANNEL = PACKET_LENGTH_SAMPLES / 2; - class QAudioInput; class QAudioOutput; class QIODevice; @@ -86,7 +81,6 @@ private: AudioRingBuffer _ringBuffer; Oscilloscope* _scope; StDev _stdev; - timeval _lastCallbackTime; timeval _lastReceiveTime; float _averagedLatency; float _measuredJitter; diff --git a/libraries/audio/src/AudioRingBuffer.cpp b/libraries/audio/src/AudioRingBuffer.cpp index 83c2fe59a0..5e9abf38b7 100644 --- a/libraries/audio/src/AudioRingBuffer.cpp +++ b/libraries/audio/src/AudioRingBuffer.cpp @@ -15,8 +15,6 @@ #include "AudioRingBuffer.h" -const short RING_BUFFER_LENGTH_FRAMES = 10; - AudioRingBuffer::AudioRingBuffer(int numFrameSamples) : NodeData(NULL), _sampleCapacity(numFrameSamples * RING_BUFFER_LENGTH_FRAMES), diff --git a/libraries/audio/src/AudioRingBuffer.h b/libraries/audio/src/AudioRingBuffer.h index 3e6917456f..addad13146 100644 --- a/libraries/audio/src/AudioRingBuffer.h +++ b/libraries/audio/src/AudioRingBuffer.h @@ -25,6 +25,8 @@ const int NETWORK_BUFFER_LENGTH_SAMPLES_STEREO = NETWORK_BUFFER_LENGTH_BYTES_STE const int NETWORK_BUFFER_LENGTH_BYTES_PER_CHANNEL = 512; const int NETWORK_BUFFER_LENGTH_SAMPLES_PER_CHANNEL = NETWORK_BUFFER_LENGTH_BYTES_PER_CHANNEL / sizeof(int16_t); +const short RING_BUFFER_LENGTH_FRAMES = 10; + const int MAX_SAMPLE_VALUE = std::numeric_limits::max(); const int MIN_SAMPLE_VALUE = std::numeric_limits::min();