From 617698ae7e5f60c6555767c6329b2735f91e2cc6 Mon Sep 17 00:00:00 2001 From: Philip Rosedale Date: Tue, 5 Feb 2013 13:26:37 -0800 Subject: [PATCH] Added yellow line for average msecs latency --- Source/Audio.cpp | 44 ++++++++++++++++++++++------- Source/AudioData.cpp | 2 ++ Source/AudioData.h | 1 + Source/octal.cpp | 26 +++++++++++++++++ Source/octal.h | 14 +++++++++ interface.xcodeproj/project.pbxproj | 6 ++++ 6 files changed, 83 insertions(+), 10 deletions(-) create mode 100644 Source/octal.cpp create mode 100644 Source/octal.h diff --git a/Source/Audio.cpp b/Source/Audio.cpp index 374a2d4cb8..138962e501 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 = 50; +const short JITTER_BUFFER_LENGTH_MSECS = 40; const int SAMPLE_RATE = 22050; const short NUM_AUDIO_SOURCES = 2; @@ -43,7 +43,7 @@ int starve_counter = 0; StDev stdev; -#define LOG_SAMPLE_DELAY 1 +#define LOG_SAMPLE_DELAY 0 bool Audio::initialized; PaError Audio::err; @@ -127,6 +127,7 @@ int audioCallback (const void *inputBuffer, if (ringBuffer->diffLastWriteNextOutput() < BUFFER_LENGTH_SAMPLES) { starve_counter++; printf("Starved #%d\n", starve_counter); + data->wasStarved = 10; // Frames to render the indication that the system was starved. ringBuffer->endOfLastWrite = NULL; } } @@ -263,6 +264,7 @@ void *receiveAudioViaUDP(void *args) { // we'll need a jitter buffer // reset the ring buffer and write copyToPointer = ringBuffer->buffer; + std::cout << "Writing jitter buffer\n"; } else { copyToPointer = ringBuffer->endOfLastWrite; @@ -422,19 +424,41 @@ void Audio::render(int screenWidth, int screenHeight) 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; + float timeLeftInCurrentBuffer = 0; + if (data->lastCallback.tv_usec > 0) timeLeftInCurrentBuffer = diffclock(data->lastCallback, currentTime)/(1000.0*(float)BUFFER_LENGTH_SAMPLES/(float)SAMPLE_RATE) * frameWidth; - remainingBuffer = data->ringBuffer->diffLastWriteNextOutput() / BUFFER_LENGTH_SAMPLES * frameWidth; + if (data->ringBuffer->endOfLastWrite != NULL) + remainingBuffer = data->ringBuffer->diffLastWriteNextOutput() / BUFFER_LENGTH_SAMPLES * frameWidth; + + if (data->wasStarved == 0) glColor3f(0, 1, 0); + else { + glColor3f(0.5 + (float)data->wasStarved/20.0, 0, 0); + data->wasStarved--; + } - glColor3f(1, 0, 0); glBegin(GL_QUADS); - glVertex2f(startX, topY); - glVertex2f(startX + remainingBuffer + timeLeftInCurrentBuffer, topY); - glVertex2f(startX + remainingBuffer + timeLeftInCurrentBuffer, bottomY); - glVertex2f(startX, bottomY); + glVertex2f(startX, topY + 5); + glVertex2f(startX + remainingBuffer + timeLeftInCurrentBuffer, topY + 5); + glVertex2f(startX + remainingBuffer + timeLeftInCurrentBuffer, bottomY - 5); + glVertex2f(startX, bottomY - 5); glEnd(); + if (data->averagedLatency == 0.0) data->averagedLatency = remainingBuffer + timeLeftInCurrentBuffer; + else data->averagedLatency = 0.99*data->averagedLatency + 0.01*((float)remainingBuffer + (float)timeLeftInCurrentBuffer); + + glColor3f(1,1,0); + glBegin(GL_QUADS); + glLineWidth(4.0); + glVertex2f(startX + data->averagedLatency - 2, topY - 2); + glVertex2f(startX + data->averagedLatency + 2, topY - 2); + glVertex2f(startX + data->averagedLatency + 2, bottomY + 2); + glVertex2f(startX + data->averagedLatency - 2, bottomY + 2); + glEnd(); + + char out[20]; + sprintf(out, "%3.0f\n", data->averagedLatency/(float)frameWidth*(1000.0*(float)BUFFER_LENGTH_SAMPLES/(float)SAMPLE_RATE)); + drawtext(startX + data->averagedLatency, topY-10, 0.1, 0, 1, 0, out); + //glVertex2f(nextOutputX, topY); //glVertex2f(nextOutputX, bottomY); diff --git a/Source/AudioData.cpp b/Source/AudioData.cpp index a36ec2eb17..c86a3b6fdb 100644 --- a/Source/AudioData.cpp +++ b/Source/AudioData.cpp @@ -25,6 +25,8 @@ AudioData::AudioData(int numberOfSources, int bufferLength) { samplesToQueue = new int16_t[bufferLength / sizeof(int16_t)]; averagedLatency = 0.0; + lastCallback.tv_usec = 0; + wasStarved = 0; } AudioData::~AudioData() { diff --git a/Source/AudioData.h b/Source/AudioData.h index e4ef322d31..4a28b966f8 100644 --- a/Source/AudioData.h +++ b/Source/AudioData.h @@ -28,6 +28,7 @@ class AudioData { timeval lastCallback; float averagedLatency; + int wasStarved; AudioData(int bufferLength); AudioData(int numberOfSources, int bufferLength); diff --git a/Source/octal.cpp b/Source/octal.cpp new file mode 100644 index 0000000000..2c648102dc --- /dev/null +++ b/Source/octal.cpp @@ -0,0 +1,26 @@ +// +// octal.cpp +// interface +// +// Created by Philip on 2/4/13. +// Copyright (c) 2013 HighFidelity, Inc. All rights reserved. +// +// Various subroutines for converting between X,Y,Z coords and octree coordinates. +// + +const int X = 0; +const int Y = 1; +const int Z = 2; + +#include "util.h" +#include "octal.h" + +// Given a position vector between zero and one (but less than one), and a voxel scale 1/2^scale, +// returns the smallest voxel at that scale which encloses the given point. +void getVoxel(float * pos, int scale, float * vpos) { + float vscale = powf(2, scale); + vpos[X] = floor(pos[X]*vscale)/vscale; + vpos[Y] = floor(pos[Y]*vscale)/vscale; + vpos[Z] = floor(pos[Z]*vscale)/vscale; +} + diff --git a/Source/octal.h b/Source/octal.h new file mode 100644 index 0000000000..672ad32362 --- /dev/null +++ b/Source/octal.h @@ -0,0 +1,14 @@ +// +// octal.h +// interface +// +// Created by Philip on 2/4/13. +// Copyright (c) 2013 HighFidelity, Inc. All rights reserved. +// + +#ifndef __interface__octal__ +#define __interface__octal__ + +#include + +#endif /* defined(__interface__octal__) */ diff --git a/interface.xcodeproj/project.pbxproj b/interface.xcodeproj/project.pbxproj index d848191626..c62997a82a 100644 --- a/interface.xcodeproj/project.pbxproj +++ b/interface.xcodeproj/project.pbxproj @@ -46,6 +46,7 @@ B6BDADE415F44AC7002A07DF /* AudioUnit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B6BDADDC15F444D3002A07DF /* AudioUnit.framework */; }; D40BDFD513404BA300B0BE1F /* GLUT.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D40BDFD413404BA300B0BE1F /* GLUT.framework */; }; D40BDFD713404BB300B0BE1F /* OpenGL.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D40BDFD613404BB300B0BE1F /* OpenGL.framework */; }; + D4200F7816C0DBA3000F8D6D /* octal.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D4200F7616C0DBA3000F8D6D /* octal.cpp */; }; /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ @@ -357,6 +358,8 @@ B6BDADDE15F444DB002A07DF /* CoreServices.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreServices.framework; path = System/Library/Frameworks/CoreServices.framework; sourceTree = SDKROOT; }; D40BDFD413404BA300B0BE1F /* GLUT.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = GLUT.framework; path = /System/Library/Frameworks/GLUT.framework; sourceTree = ""; }; D40BDFD613404BB300B0BE1F /* OpenGL.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = OpenGL.framework; path = /System/Library/Frameworks/OpenGL.framework; sourceTree = ""; }; + D4200F7616C0DBA3000F8D6D /* octal.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = octal.cpp; sourceTree = ""; }; + D4200F7716C0DBA3000F8D6D /* octal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = octal.h; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -441,6 +444,8 @@ 53CF370516B9C039001FCB05 /* main.cpp */, 53CF370A16B9C039001FCB05 /* Network.cpp */, 53CF370B16B9C039001FCB05 /* Network.h */, + D4200F7616C0DBA3000F8D6D /* octal.cpp */, + D4200F7716C0DBA3000F8D6D /* octal.h */, 53ACC39616B9C59500ABD227 /* Oscilloscope.cpp */, 53ACC39716B9C59500ABD227 /* Oscilloscope.h */, 53CF370C16B9C039001FCB05 /* Particle.cpp */, @@ -874,6 +879,7 @@ 53CF372A16B9C039001FCB05 /* Util.cpp in Sources */, 53ACC39816B9C59500ABD227 /* Oscilloscope.cpp in Sources */, 53A4EAB216BC770A00F07F4C /* AudioRingBuffer.cpp in Sources */, + D4200F7816C0DBA3000F8D6D /* octal.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; };