diff --git a/Source/Audio.cpp b/Source/Audio.cpp index 0eccdad0fb..d88e887194 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 = 1; +const short JITTER_BUFFER_LENGTH_MSECS = 26; const int SAMPLE_RATE = 22050; const short NUM_AUDIO_SOURCES = 2; @@ -39,6 +39,9 @@ char EC2_WEST_AUDIO_SERVER[] = "54.241.92.53"; const int AUDIO_UDP_LISTEN_PORT = 55444; +int starve_counter = 0; + +StDev stdev; #define LOG_SAMPLE_DELAY 1 @@ -81,7 +84,7 @@ int audioCallback (const void *inputBuffer, // int16_t *inputRight = ((int16_t **) inputBuffer)[1]; if (inputLeft != NULL) { - data->audioSocket->send((char *) EC2_WEST_AUDIO_SERVER, 55443, (void *)inputLeft, BUFFER_LENGTH_BYTES); + data->audioSocket->send((char *) WORKCLUB_AUDIO_SERVER, 55443, (void *)inputLeft, BUFFER_LENGTH_BYTES); } int16_t *outputLeft = ((int16_t **) outputBuffer)[0]; @@ -122,8 +125,8 @@ int audioCallback (const void *inputBuffer, } if (ringBuffer->diffLastWriteNextOutput() < BUFFER_LENGTH_SAMPLES) { - std::cout << "Starved\n"; - ringBuffer->nextOutput = ringBuffer->buffer; + starve_counter++; + printf("Starved #%d\n", starve_counter); ringBuffer->endOfLastWrite = NULL; } } @@ -221,18 +224,29 @@ void *receiveAudioViaUDP(void *args) { while (true) { if (sharedAudioData->audioSocket->receive((void *)receivedData, receivedBytes)) { + bool firstSample = (currentReceiveTime.tv_sec == 0); + + gettimeofday(¤tReceiveTime, NULL); + if (LOG_SAMPLE_DELAY) { - if (currentReceiveTime.tv_sec == 0) { - gettimeofday(¤tReceiveTime, NULL); - } else { - gettimeofday(¤tReceiveTime, NULL); - + if (!firstSample) { // write time difference (in microseconds) between packet receipts to file double timeDiff = diffclock(previousReceiveTime, currentReceiveTime); logFile << timeDiff << std::endl; } } + // Compute and report standard deviation for jitter calculation + if (firstSample) { + stdev.reset(); + } else { + stdev.addValue(diffclock(previousReceiveTime, currentReceiveTime)); + if (stdev.getSamples() > 500) { + printf("Avg: %4.2f, Stdev: %4.2f\n", stdev.getAverage(), stdev.getStDev()); + stdev.reset(); + } + } + AudioRingBuffer *ringBuffer = sharedAudioData->ringBuffer; int16_t *copyToPointer; diff --git a/Source/Util.cpp b/Source/Util.cpp index 74cfb4b41d..2fb5ad14eb 100644 --- a/Source/Util.cpp +++ b/Source/Util.cpp @@ -14,6 +14,51 @@ #include #include "world.h" #include "glm.hpp" +#include "util.h" + + +// +// Standard Deviation Object +// + +const int MAX_STDEV_SAMPLES = 1000; // Don't add more than this number of samples. + +StDev::StDev() { + data = new float[MAX_STDEV_SAMPLES]; + sampleCount = 0; +} + +void StDev::reset() { + sampleCount = 0; +} + +void StDev::addValue(float v) { + data[sampleCount++] = v; + if (sampleCount == MAX_STDEV_SAMPLES) sampleCount = 0; +} + +float StDev::getAverage() { + float average = 0; + for (int i = 0; i < sampleCount; i++) { + average += data[i]; + } + if (sampleCount > 0) + return average/(float)sampleCount; + else return 0; +} + +float StDev::getStDev() { + float average = getAverage(); + float stdev = 0; + for (int i = 0; i < sampleCount; i++) { + stdev += powf(data[i] - average, 2); + } + if (sampleCount > 1) + return sqrt(stdev/(float)(sampleCount - 1.0)); + else + return 0; +} + float randFloat () { return (rand()%10000)/10000.f; @@ -101,7 +146,7 @@ double diffclock(timeval clock1,timeval clock2) } void drawtext(int x, int y, float scale, float rotate, float thick, int mono, char *string, - float r=1.0, float g=1.0, float b=1.0) + float r, float g, float b) { // // Draws text on screen as stroked so it can be resized @@ -126,7 +171,7 @@ void drawtext(int x, int y, float scale, float rotate, float thick, int mono, ch void drawvec3(int x, int y, float scale, float rotate, float thick, int mono, glm::vec3 vec, - float r=1.0, float g=1.0, float b=1.0) + float r, float g, float b) { // // Draws text on screen as stroked so it can be resized diff --git a/Source/Util.h b/Source/Util.h index 0074ff9994..594983972b 100644 --- a/Source/Util.h +++ b/Source/Util.h @@ -24,4 +24,17 @@ void drawvec3(int x, int y, float scale, float rotate, float thick, int mono, gl float r=1.0, float g=1.0, float b=1.0); double diffclock(timeval clock1,timeval clock2); +class StDev { +public: + StDev(); + void reset(); + void addValue(float v); + float getAverage(); + float getStDev(); + int getSamples() {return sampleCount;}; +private: + float * data; + int sampleCount = 0; +}; + #endif diff --git a/Source/main.cpp b/Source/main.cpp index 49e9bc6829..1dc89ffeee 100644 --- a/Source/main.cpp +++ b/Source/main.cpp @@ -903,8 +903,35 @@ int main(int argc, char** argv) int test_recv = network_receive(UDP_socket, &from_addr, incoming_packet, delay); printf("Received %i bytes\n", test_recv); - // Load textures - //Img.Load("/Users/philip/Downloads/galaxy1.tga"); + // + printf("Testing math... standard deviation.\n"); + StDev stdevtest; + stdevtest.reset(); + stdevtest.addValue(1345); + stdevtest.addValue(1301); + stdevtest.addValue(1368); + stdevtest.addValue(1322); + stdevtest.addValue(1310); + stdevtest.addValue(1370); + stdevtest.addValue(1318); + stdevtest.addValue(1350); + stdevtest.addValue(1303); + stdevtest.addValue(1299); + + if (stdevtest.getSamples() == 10) + printf("Samples=PASS "); + else + printf("Samples=FAIL "); + + if (floor(stdevtest.getAverage()*100.0) == 132859.0) + printf("Average=PASS "); + else + printf("Average=FAIL, avg reported = %5.3f ", floor(stdevtest.getAverage()*100.0)); + + if (floor(stdevtest.getStDev()*100.0) == 2746.0) + printf("Stdev=PASS \n"); + else + printf("Stdev=FAIL \n"); // // Try to connect the serial port I/O