From 9ef409b2efbaab554fcc928854a96c5884ad53c5 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Thu, 9 May 2013 12:34:14 -0700 Subject: [PATCH 1/6] perform send of audio buffer on main thread --- audio-mixer/src/main.cpp | 370 +++++++++++++++++++-------------------- 1 file changed, 177 insertions(+), 193 deletions(-) diff --git a/audio-mixer/src/main.cpp b/audio-mixer/src/main.cpp index 5c6781b46e..994fbdf474 100644 --- a/audio-mixer/src/main.cpp +++ b/audio-mixer/src/main.cpp @@ -72,193 +72,6 @@ void plateauAdditionOfSamples(int16_t &mixSample, int16_t sampleToAdd) { mixSample = normalizedSample; } -void *sendBuffer(void *args) { - int sentBytes; - int nextFrame = 0; - timeval startTime; - - AgentList* agentList = AgentList::getInstance(); - - gettimeofday(&startTime, NULL); - - while (true) { - sentBytes = 0; - - for (AgentList::iterator agent = agentList->begin(); agent != agentList->end(); agent++) { - AudioRingBuffer* agentBuffer = (AudioRingBuffer*) agent->getLinkedData(); - - if (agentBuffer && agentBuffer->getEndOfLastWrite() != NULL) { - - if (!agentBuffer->isStarted() - && agentBuffer->diffLastWriteNextOutput() <= BUFFER_LENGTH_SAMPLES_PER_CHANNEL + JITTER_BUFFER_SAMPLES) { - printf("Held back buffer for agent with ID %d.\n", agent->getAgentId()); - agentBuffer->setShouldBeAddedToMix(false); - } else if (agentBuffer->diffLastWriteNextOutput() < BUFFER_LENGTH_SAMPLES_PER_CHANNEL) { - printf("Buffer from agent with ID %d starved.\n", agent->getAgentId()); - agentBuffer->setStarted(false); - agentBuffer->setShouldBeAddedToMix(false); - } else { - // good buffer, add this to the mix - agentBuffer->setStarted(true); - agentBuffer->setShouldBeAddedToMix(true); - } - } - } - - int numAgents = agentList->size(); - float distanceCoefficients[numAgents][numAgents]; - memset(distanceCoefficients, 0, sizeof(distanceCoefficients)); - - for (AgentList::iterator agent = agentList->begin(); agent != agentList->end(); agent++) { - AudioRingBuffer* agentRingBuffer = (AudioRingBuffer*) agent->getLinkedData(); - - if (agentRingBuffer) { - int16_t clientMix[BUFFER_LENGTH_SAMPLES_PER_CHANNEL * 2] = {}; - - for (AgentList::iterator otherAgent = agentList->begin(); otherAgent != agentList->end(); otherAgent++) { - if (otherAgent != agent || (otherAgent == agent && agentRingBuffer->shouldLoopbackForAgent())) { - AudioRingBuffer* otherAgentBuffer = (AudioRingBuffer*) otherAgent->getLinkedData(); - - if (otherAgentBuffer->shouldBeAddedToMix()) { - - float bearingRelativeAngleToSource = 0.f; - float attenuationCoefficient = 1.f; - int numSamplesDelay = 0; - float weakChannelAmplitudeRatio = 1.f; - - if (otherAgent != agent) { - float *agentPosition = agentRingBuffer->getPosition(); - float *otherAgentPosition = otherAgentBuffer->getPosition(); - - // calculate the distance to the other agent - - // use the distance to the other agent to calculate the change in volume for this frame - int lowAgentIndex = std::min(agent.getAgentIndex(), otherAgent.getAgentIndex()); - int highAgentIndex = std::max(agent.getAgentIndex(), otherAgent.getAgentIndex()); - - if (distanceCoefficients[lowAgentIndex][highAgentIndex] == 0) { - float distanceToAgent = sqrtf(powf(agentPosition[0] - otherAgentPosition[0], 2) + - powf(agentPosition[1] - otherAgentPosition[1], 2) + - powf(agentPosition[2] - otherAgentPosition[2], 2)); - - float minCoefficient = std::min(1.0f, - powf(0.5, - (logf(DISTANCE_RATIO * distanceToAgent) / logf(3)) - 1)); - distanceCoefficients[lowAgentIndex][highAgentIndex] = minCoefficient; - } - - - // get the angle from the right-angle triangle - float triangleAngle = atan2f(fabsf(agentPosition[2] - otherAgentPosition[2]), - fabsf(agentPosition[0] - otherAgentPosition[0])) * (180 / M_PI); - float absoluteAngleToSource = 0; - bearingRelativeAngleToSource = 0; - - // find the angle we need for calculation based on the orientation of the triangle - if (otherAgentPosition[0] > agentPosition[0]) { - if (otherAgentPosition[2] > agentPosition[2]) { - absoluteAngleToSource = -90 + triangleAngle; - } else { - absoluteAngleToSource = -90 - triangleAngle; - } - } else { - if (otherAgentPosition[2] > agentPosition[2]) { - absoluteAngleToSource = 90 - triangleAngle; - } else { - absoluteAngleToSource = 90 + triangleAngle; - } - } - - bearingRelativeAngleToSource = absoluteAngleToSource - agentRingBuffer->getBearing(); - - if (bearingRelativeAngleToSource > 180) { - bearingRelativeAngleToSource -= 360; - } else if (bearingRelativeAngleToSource < -180) { - bearingRelativeAngleToSource += 360; - } - - float angleOfDelivery = absoluteAngleToSource - otherAgentBuffer->getBearing(); - - if (angleOfDelivery > 180) { - angleOfDelivery -= 360; - } else if (angleOfDelivery < -180) { - angleOfDelivery += 360; - } - - float offAxisCoefficient = MAX_OFF_AXIS_ATTENUATION + - (OFF_AXIS_ATTENUATION_FORMULA_STEP * (fabsf(angleOfDelivery) / 90.0f)); - - attenuationCoefficient = distanceCoefficients[lowAgentIndex][highAgentIndex] - * otherAgentBuffer->getAttenuationRatio() - * offAxisCoefficient; - - bearingRelativeAngleToSource *= (M_PI / 180); - - float sinRatio = fabsf(sinf(bearingRelativeAngleToSource)); - numSamplesDelay = PHASE_DELAY_AT_90 * sinRatio; - weakChannelAmplitudeRatio = 1 - (PHASE_AMPLITUDE_RATIO_AT_90 * sinRatio); - } - - int16_t* goodChannel = bearingRelativeAngleToSource > 0.0f - ? clientMix + BUFFER_LENGTH_SAMPLES_PER_CHANNEL - : clientMix; - int16_t* delayedChannel = bearingRelativeAngleToSource > 0.0f - ? clientMix - : clientMix + BUFFER_LENGTH_SAMPLES_PER_CHANNEL; - - int16_t* delaySamplePointer = otherAgentBuffer->getNextOutput() == otherAgentBuffer->getBuffer() - ? otherAgentBuffer->getBuffer() + RING_BUFFER_SAMPLES - numSamplesDelay - : otherAgentBuffer->getNextOutput() - numSamplesDelay; - - for (int s = 0; s < BUFFER_LENGTH_SAMPLES_PER_CHANNEL; s++) { - - if (s < numSamplesDelay) { - // pull the earlier sample for the delayed channel - int earlierSample = delaySamplePointer[s] * attenuationCoefficient; - plateauAdditionOfSamples(delayedChannel[s], earlierSample * weakChannelAmplitudeRatio); - } - - int16_t currentSample = (otherAgentBuffer->getNextOutput()[s] * attenuationCoefficient); - plateauAdditionOfSamples(goodChannel[s], currentSample); - - if (s + numSamplesDelay < BUFFER_LENGTH_SAMPLES_PER_CHANNEL) { - plateauAdditionOfSamples(delayedChannel[s + numSamplesDelay], - currentSample * weakChannelAmplitudeRatio); - } - } - } - } - } - - agentList->getAgentSocket().send(agent->getPublicSocket(), clientMix, BUFFER_LENGTH_BYTES); - } - } - - for (AgentList::iterator agent = agentList->begin(); agent != agentList->end(); agent++) { - AudioRingBuffer* agentBuffer = (AudioRingBuffer*) agent->getLinkedData(); - if (agentBuffer && agentBuffer->shouldBeAddedToMix()) { - agentBuffer->setNextOutput(agentBuffer->getNextOutput() + BUFFER_LENGTH_SAMPLES_PER_CHANNEL); - - if (agentBuffer->getNextOutput() >= agentBuffer->getBuffer() + RING_BUFFER_SAMPLES) { - agentBuffer->setNextOutput(agentBuffer->getBuffer()); - } - - agentBuffer->setShouldBeAddedToMix(false); - } - } - - double usecToSleep = usecTimestamp(&startTime) + (++nextFrame * BUFFER_SEND_INTERVAL_USECS) - usecTimestampNow(); - - if (usecToSleep > 0) { - usleep(usecToSleep); - } else { - std::cout << "Took too much time, not sleeping!\n"; - } - } - - pthread_exit(0); -} - void attachNewBufferToAgent(Agent *newAgent) { if (newAgent->getLinkedData() == NULL) { newAgent->setLinkedData(new AudioRingBuffer(RING_BUFFER_SAMPLES, BUFFER_LENGTH_SAMPLES_PER_CHANNEL)); @@ -280,13 +93,178 @@ int main(int argc, const char* argv[]) { unsigned char *packetData = new unsigned char[MAX_PACKET_SIZE]; - pthread_t sendBufferThread; - pthread_create(&sendBufferThread, NULL, sendBuffer, NULL); - sockaddr *agentAddress = new sockaddr; + + int nextFrame = 0; + timeval startTime; + + gettimeofday(&startTime, NULL); while (true) { - if(agentList->getAgentSocket().receive(agentAddress, packetData, &receivedBytes)) { + // enumerate the agents, check if we can add audio from the agent to current mix + for (AgentList::iterator agent = agentList->begin(); agent != agentList->end(); agent++) { + AudioRingBuffer* agentBuffer = (AudioRingBuffer*) agent->getLinkedData(); + + if (agentBuffer->getEndOfLastWrite() != NULL) { + if (!agentBuffer->isStarted() + && agentBuffer->diffLastWriteNextOutput() <= BUFFER_LENGTH_SAMPLES_PER_CHANNEL + JITTER_BUFFER_SAMPLES) { + printf("Held back buffer for agent with ID %d.\n", agent->getAgentId()); + agentBuffer->setShouldBeAddedToMix(false); + } else if (agentBuffer->diffLastWriteNextOutput() < BUFFER_LENGTH_SAMPLES_PER_CHANNEL) { + printf("Buffer from agent with ID %d starved.\n", agent->getAgentId()); + agentBuffer->setStarted(false); + agentBuffer->setShouldBeAddedToMix(false); + } else { + // good buffer, add this to the mix + agentBuffer->setStarted(true); + agentBuffer->setShouldBeAddedToMix(true); + } + } + } + + int numAgents = agentList->size(); + float distanceCoefficients[numAgents][numAgents]; + memset(distanceCoefficients, 0, sizeof(distanceCoefficients)); + + for (AgentList::iterator agent = agentList->begin(); agent != agentList->end(); agent++) { + AudioRingBuffer* agentRingBuffer = (AudioRingBuffer*) agent->getLinkedData(); + + int16_t clientMix[BUFFER_LENGTH_SAMPLES_PER_CHANNEL * 2] = {}; + + for (AgentList::iterator otherAgent = agentList->begin(); otherAgent != agentList->end(); otherAgent++) { + if (otherAgent != agent || (otherAgent == agent && agentRingBuffer->shouldLoopbackForAgent())) { + AudioRingBuffer* otherAgentBuffer = (AudioRingBuffer*) otherAgent->getLinkedData(); + + if (otherAgentBuffer->shouldBeAddedToMix()) { + + float bearingRelativeAngleToSource = 0.f; + float attenuationCoefficient = 1.f; + int numSamplesDelay = 0; + float weakChannelAmplitudeRatio = 1.f; + + if (otherAgent != agent) { + float *agentPosition = agentRingBuffer->getPosition(); + float *otherAgentPosition = otherAgentBuffer->getPosition(); + + // calculate the distance to the other agent + + // use the distance to the other agent to calculate the change in volume for this frame + int lowAgentIndex = std::min(agent.getAgentIndex(), otherAgent.getAgentIndex()); + int highAgentIndex = std::max(agent.getAgentIndex(), otherAgent.getAgentIndex()); + + if (distanceCoefficients[lowAgentIndex][highAgentIndex] == 0) { + float distanceToAgent = sqrtf(powf(agentPosition[0] - otherAgentPosition[0], 2) + + powf(agentPosition[1] - otherAgentPosition[1], 2) + + powf(agentPosition[2] - otherAgentPosition[2], 2)); + + float minCoefficient = std::min(1.0f, + powf(0.5, + (logf(DISTANCE_RATIO * distanceToAgent) / logf(3)) - 1)); + distanceCoefficients[lowAgentIndex][highAgentIndex] = minCoefficient; + } + + + // get the angle from the right-angle triangle + float triangleAngle = atan2f(fabsf(agentPosition[2] - otherAgentPosition[2]), + fabsf(agentPosition[0] - otherAgentPosition[0])) * (180 / M_PI); + float absoluteAngleToSource = 0; + bearingRelativeAngleToSource = 0; + + // find the angle we need for calculation based on the orientation of the triangle + if (otherAgentPosition[0] > agentPosition[0]) { + if (otherAgentPosition[2] > agentPosition[2]) { + absoluteAngleToSource = -90 + triangleAngle; + } else { + absoluteAngleToSource = -90 - triangleAngle; + } + } else { + if (otherAgentPosition[2] > agentPosition[2]) { + absoluteAngleToSource = 90 - triangleAngle; + } else { + absoluteAngleToSource = 90 + triangleAngle; + } + } + + bearingRelativeAngleToSource = absoluteAngleToSource - agentRingBuffer->getBearing(); + + if (bearingRelativeAngleToSource > 180) { + bearingRelativeAngleToSource -= 360; + } else if (bearingRelativeAngleToSource < -180) { + bearingRelativeAngleToSource += 360; + } + + float angleOfDelivery = absoluteAngleToSource - otherAgentBuffer->getBearing(); + + if (angleOfDelivery > 180) { + angleOfDelivery -= 360; + } else if (angleOfDelivery < -180) { + angleOfDelivery += 360; + } + + float offAxisCoefficient = MAX_OFF_AXIS_ATTENUATION + + (OFF_AXIS_ATTENUATION_FORMULA_STEP * (fabsf(angleOfDelivery) / 90.0f)); + + attenuationCoefficient = distanceCoefficients[lowAgentIndex][highAgentIndex] + * otherAgentBuffer->getAttenuationRatio() + * offAxisCoefficient; + + bearingRelativeAngleToSource *= (M_PI / 180); + + float sinRatio = fabsf(sinf(bearingRelativeAngleToSource)); + numSamplesDelay = PHASE_DELAY_AT_90 * sinRatio; + weakChannelAmplitudeRatio = 1 - (PHASE_AMPLITUDE_RATIO_AT_90 * sinRatio); + } + + int16_t* goodChannel = bearingRelativeAngleToSource > 0.0f + ? clientMix + BUFFER_LENGTH_SAMPLES_PER_CHANNEL + : clientMix; + int16_t* delayedChannel = bearingRelativeAngleToSource > 0.0f + ? clientMix + : clientMix + BUFFER_LENGTH_SAMPLES_PER_CHANNEL; + + int16_t* delaySamplePointer = otherAgentBuffer->getNextOutput() == otherAgentBuffer->getBuffer() + ? otherAgentBuffer->getBuffer() + RING_BUFFER_SAMPLES - numSamplesDelay + : otherAgentBuffer->getNextOutput() - numSamplesDelay; + + for (int s = 0; s < BUFFER_LENGTH_SAMPLES_PER_CHANNEL; s++) { + + if (s < numSamplesDelay) { + // pull the earlier sample for the delayed channel + int earlierSample = delaySamplePointer[s] * attenuationCoefficient; + plateauAdditionOfSamples(delayedChannel[s], earlierSample * weakChannelAmplitudeRatio); + } + + int16_t currentSample = (otherAgentBuffer->getNextOutput()[s] * attenuationCoefficient); + plateauAdditionOfSamples(goodChannel[s], currentSample); + + if (s + numSamplesDelay < BUFFER_LENGTH_SAMPLES_PER_CHANNEL) { + plateauAdditionOfSamples(delayedChannel[s + numSamplesDelay], + currentSample * weakChannelAmplitudeRatio); + } + } + } + } + } + + agentList->getAgentSocket().send(agent->getPublicSocket(), clientMix, BUFFER_LENGTH_BYTES); + } + + // push forward the next output pointers for any audio buffers we used + for (AgentList::iterator agent = agentList->begin(); agent != agentList->end(); agent++) { + AudioRingBuffer* agentBuffer = (AudioRingBuffer*) agent->getLinkedData(); + if (agentBuffer && agentBuffer->shouldBeAddedToMix()) { + agentBuffer->setNextOutput(agentBuffer->getNextOutput() + BUFFER_LENGTH_SAMPLES_PER_CHANNEL); + + if (agentBuffer->getNextOutput() >= agentBuffer->getBuffer() + RING_BUFFER_SAMPLES) { + agentBuffer->setNextOutput(agentBuffer->getBuffer()); + } + + agentBuffer->setShouldBeAddedToMix(false); + } + } + + // pull any new audio data from agents off of the network stack + while (agentList->getAgentSocket().receive(agentAddress, packetData, &receivedBytes)) { if (packetData[0] == PACKET_HEADER_INJECT_AUDIO) { if (agentList->addOrUpdateAgent(agentAddress, agentAddress, packetData[0], agentList->getLastAgentID())) { @@ -296,9 +274,15 @@ int main(int argc, const char* argv[]) { agentList->updateAgentWithData(agentAddress, packetData, receivedBytes); } } + + double usecToSleep = usecTimestamp(&startTime) + (++nextFrame * BUFFER_SEND_INTERVAL_USECS) - usecTimestampNow(); + + if (usecToSleep > 0) { + usleep(usecToSleep); + } else { + std::cout << "Took too much time, not sleeping!\n"; + } } - pthread_join(sendBufferThread, NULL); - return 0; } From 812f87798432f6b4c1ea3a58fb2d7942e802a4c7 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Thu, 9 May 2013 12:37:39 -0700 Subject: [PATCH 2/6] resolve conflicts on merge with upstream master --- audio-mixer/src/main.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/audio-mixer/src/main.cpp b/audio-mixer/src/main.cpp index 31da0d5b47..aa8d7a4fee 100644 --- a/audio-mixer/src/main.cpp +++ b/audio-mixer/src/main.cpp @@ -78,7 +78,6 @@ void attachNewBufferToAgent(Agent *newAgent) { } int main(int argc, const char* argv[]) { - signal(SIGSEGV, printStacktrace); setvbuf(stdout, NULL, _IOLBF, 0); AgentList* agentList = AgentList::createInstance(AGENT_TYPE_AUDIO_MIXER, MIXER_LISTEN_PORT); From 0a6adc7ed3d5796020ab8b3f56f876b646b5c77c Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Thu, 9 May 2013 12:46:59 -0700 Subject: [PATCH 3/6] make the audio mixer socket non-blocking so it doesn't halt waiting for data --- audio-mixer/src/main.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/audio-mixer/src/main.cpp b/audio-mixer/src/main.cpp index aa8d7a4fee..3610efa7cc 100644 --- a/audio-mixer/src/main.cpp +++ b/audio-mixer/src/main.cpp @@ -93,6 +93,9 @@ int main(int argc, const char* argv[]) { sockaddr *agentAddress = new sockaddr; + // make sure our agent socket is non-blocking + agentList->getAgentSocket().setBlocking(false); + int nextFrame = 0; timeval startTime; From a8eb9187f17ce922bcb7049142a56ae1c9700650 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Thu, 9 May 2013 12:48:20 -0700 Subject: [PATCH 4/6] underscore the sample length variables in AudioRingBuffer --- libraries/shared/src/AudioRingBuffer.cpp | 26 ++++++++++++------------ libraries/shared/src/AudioRingBuffer.h | 4 ++-- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/libraries/shared/src/AudioRingBuffer.cpp b/libraries/shared/src/AudioRingBuffer.cpp index a399852950..072b085c5d 100644 --- a/libraries/shared/src/AudioRingBuffer.cpp +++ b/libraries/shared/src/AudioRingBuffer.cpp @@ -10,25 +10,25 @@ #include "AudioRingBuffer.h" AudioRingBuffer::AudioRingBuffer(int ringSamples, int bufferSamples) : - ringBufferLengthSamples(ringSamples), - bufferLengthSamples(bufferSamples), + _ringBufferLengthSamples(ringSamples), + _bufferLengthSamples(bufferSamples), endOfLastWrite(NULL), started(false), _shouldBeAddedToMix(false), _shouldLoopbackForAgent(false) { - buffer = new int16_t[ringBufferLengthSamples]; + buffer = new int16_t[_ringBufferLengthSamples]; nextOutput = buffer; }; AudioRingBuffer::AudioRingBuffer(const AudioRingBuffer &otherRingBuffer) { - ringBufferLengthSamples = otherRingBuffer.ringBufferLengthSamples; - bufferLengthSamples = otherRingBuffer.bufferLengthSamples; + _ringBufferLengthSamples = otherRingBuffer._ringBufferLengthSamples; + _bufferLengthSamples = otherRingBuffer._bufferLengthSamples; started = otherRingBuffer.started; _shouldBeAddedToMix = otherRingBuffer._shouldBeAddedToMix; _shouldLoopbackForAgent = otherRingBuffer._shouldLoopbackForAgent; - buffer = new int16_t[ringBufferLengthSamples]; - memcpy(buffer, otherRingBuffer.buffer, sizeof(int16_t) * ringBufferLengthSamples); + buffer = new int16_t[_ringBufferLengthSamples]; + memcpy(buffer, otherRingBuffer.buffer, sizeof(int16_t) * _ringBufferLengthSamples); nextOutput = buffer + (otherRingBuffer.nextOutput - otherRingBuffer.buffer); endOfLastWrite = buffer + (otherRingBuffer.endOfLastWrite - otherRingBuffer.buffer); @@ -99,7 +99,7 @@ void AudioRingBuffer::setBearing(float newBearing) { const int AGENT_LOOPBACK_MODIFIER = 307; int AudioRingBuffer::parseData(unsigned char* sourceBuffer, int numBytes) { - if (numBytes > (bufferLengthSamples * sizeof(int16_t))) { + if (numBytes > (_bufferLengthSamples * sizeof(int16_t))) { unsigned char *dataPtr = sourceBuffer + 1; @@ -131,17 +131,17 @@ int AudioRingBuffer::parseData(unsigned char* sourceBuffer, int numBytes) { if (endOfLastWrite == NULL) { endOfLastWrite = buffer; - } else if (diffLastWriteNextOutput() > ringBufferLengthSamples - bufferLengthSamples) { + } else if (diffLastWriteNextOutput() > _ringBufferLengthSamples - _bufferLengthSamples) { endOfLastWrite = buffer; nextOutput = buffer; started = false; } - memcpy(endOfLastWrite, sourceBuffer, bufferLengthSamples * sizeof(int16_t)); + memcpy(endOfLastWrite, sourceBuffer, _bufferLengthSamples * sizeof(int16_t)); - endOfLastWrite += bufferLengthSamples; + endOfLastWrite += _bufferLengthSamples; - if (endOfLastWrite >= buffer + ringBufferLengthSamples) { + if (endOfLastWrite >= buffer + _ringBufferLengthSamples) { endOfLastWrite = buffer; } @@ -155,7 +155,7 @@ short AudioRingBuffer::diffLastWriteNextOutput() { short sampleDifference = endOfLastWrite - nextOutput; if (sampleDifference < 0) { - sampleDifference += ringBufferLengthSamples; + sampleDifference += _ringBufferLengthSamples; } return sampleDifference; diff --git a/libraries/shared/src/AudioRingBuffer.h b/libraries/shared/src/AudioRingBuffer.h index 32e4e0c8a9..d939c215ff 100644 --- a/libraries/shared/src/AudioRingBuffer.h +++ b/libraries/shared/src/AudioRingBuffer.h @@ -41,8 +41,8 @@ public: short diffLastWriteNextOutput(); private: - int ringBufferLengthSamples; - int bufferLengthSamples; + int _ringBufferLengthSamples; + int _bufferLengthSamples; float position[3]; float attenuationRatio; float bearing; From cd69297af236ef7b0085a0763330f4ef4bf0528c Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Thu, 9 May 2013 13:04:42 -0700 Subject: [PATCH 5/6] some refactoring for member variables in AudioRingBuffer --- audio-mixer/src/main.cpp | 20 ++-- libraries/shared/src/AudioRingBuffer.cpp | 117 ++++++----------------- libraries/shared/src/AudioRingBuffer.h | 47 +++++---- 3 files changed, 68 insertions(+), 116 deletions(-) diff --git a/audio-mixer/src/main.cpp b/audio-mixer/src/main.cpp index 3610efa7cc..88cb867df4 100644 --- a/audio-mixer/src/main.cpp +++ b/audio-mixer/src/main.cpp @@ -144,8 +144,8 @@ int main(int argc, const char* argv[]) { float weakChannelAmplitudeRatio = 1.f; if (otherAgent != agent) { - float *agentPosition = agentRingBuffer->getPosition(); - float *otherAgentPosition = otherAgentBuffer->getPosition(); + Position agentPosition = agentRingBuffer->getPosition(); + Position otherAgentPosition = otherAgentBuffer->getPosition(); // calculate the distance to the other agent @@ -154,9 +154,9 @@ int main(int argc, const char* argv[]) { int highAgentIndex = std::max(agent.getAgentIndex(), otherAgent.getAgentIndex()); if (distanceCoefficients[lowAgentIndex][highAgentIndex] == 0) { - float distanceToAgent = sqrtf(powf(agentPosition[0] - otherAgentPosition[0], 2) + - powf(agentPosition[1] - otherAgentPosition[1], 2) + - powf(agentPosition[2] - otherAgentPosition[2], 2)); + float distanceToAgent = sqrtf(powf(agentPosition.x - otherAgentPosition.x, 2) + + powf(agentPosition.y - otherAgentPosition.y, 2) + + powf(agentPosition.z - otherAgentPosition.z, 2)); float minCoefficient = std::min(1.0f, powf(0.5, @@ -166,20 +166,20 @@ int main(int argc, const char* argv[]) { // get the angle from the right-angle triangle - float triangleAngle = atan2f(fabsf(agentPosition[2] - otherAgentPosition[2]), - fabsf(agentPosition[0] - otherAgentPosition[0])) * (180 / M_PI); + float triangleAngle = atan2f(fabsf(agentPosition.z - otherAgentPosition.z), + fabsf(agentPosition.x - otherAgentPosition.x)) * (180 / M_PI); float absoluteAngleToSource = 0; bearingRelativeAngleToSource = 0; // find the angle we need for calculation based on the orientation of the triangle - if (otherAgentPosition[0] > agentPosition[0]) { - if (otherAgentPosition[2] > agentPosition[2]) { + if (otherAgentPosition.x > agentPosition.x) { + if (otherAgentPosition.z > agentPosition.z) { absoluteAngleToSource = -90 + triangleAngle; } else { absoluteAngleToSource = -90 - triangleAngle; } } else { - if (otherAgentPosition[2] > agentPosition[2]) { + if (otherAgentPosition.z > agentPosition.z) { absoluteAngleToSource = 90 - triangleAngle; } else { absoluteAngleToSource = 90 + triangleAngle; diff --git a/libraries/shared/src/AudioRingBuffer.cpp b/libraries/shared/src/AudioRingBuffer.cpp index 072b085c5d..fb8ecdac86 100644 --- a/libraries/shared/src/AudioRingBuffer.cpp +++ b/libraries/shared/src/AudioRingBuffer.cpp @@ -12,90 +12,37 @@ AudioRingBuffer::AudioRingBuffer(int ringSamples, int bufferSamples) : _ringBufferLengthSamples(ringSamples), _bufferLengthSamples(bufferSamples), - endOfLastWrite(NULL), - started(false), + _endOfLastWrite(NULL), + _started(false), _shouldBeAddedToMix(false), _shouldLoopbackForAgent(false) { - buffer = new int16_t[_ringBufferLengthSamples]; - nextOutput = buffer; + + _buffer = new int16_t[_ringBufferLengthSamples]; + _nextOutput = _buffer; }; AudioRingBuffer::AudioRingBuffer(const AudioRingBuffer &otherRingBuffer) { _ringBufferLengthSamples = otherRingBuffer._ringBufferLengthSamples; _bufferLengthSamples = otherRingBuffer._bufferLengthSamples; - started = otherRingBuffer.started; + _started = otherRingBuffer._started; _shouldBeAddedToMix = otherRingBuffer._shouldBeAddedToMix; _shouldLoopbackForAgent = otherRingBuffer._shouldLoopbackForAgent; - buffer = new int16_t[_ringBufferLengthSamples]; - memcpy(buffer, otherRingBuffer.buffer, sizeof(int16_t) * _ringBufferLengthSamples); + _buffer = new int16_t[_ringBufferLengthSamples]; + memcpy(_buffer, otherRingBuffer._buffer, sizeof(int16_t) * _ringBufferLengthSamples); - nextOutput = buffer + (otherRingBuffer.nextOutput - otherRingBuffer.buffer); - endOfLastWrite = buffer + (otherRingBuffer.endOfLastWrite - otherRingBuffer.buffer); + _nextOutput = _buffer + (otherRingBuffer._nextOutput - otherRingBuffer._buffer); + _endOfLastWrite = _buffer + (otherRingBuffer._endOfLastWrite - otherRingBuffer._buffer); } AudioRingBuffer::~AudioRingBuffer() { - delete[] buffer; + delete[] _buffer; }; AudioRingBuffer* AudioRingBuffer::clone() const { return new AudioRingBuffer(*this); } -int16_t* AudioRingBuffer::getNextOutput() { - return nextOutput; -} - -void AudioRingBuffer::setNextOutput(int16_t *newPointer) { - nextOutput = newPointer; -} - -int16_t* AudioRingBuffer::getEndOfLastWrite() { - return endOfLastWrite; -} - -void AudioRingBuffer::setEndOfLastWrite(int16_t *newPointer) { - endOfLastWrite = newPointer; -} - -int16_t* AudioRingBuffer::getBuffer() { - return buffer; -} - -bool AudioRingBuffer::isStarted() { - return started; -} - -void AudioRingBuffer::setStarted(bool status) { - started = status; -} - -float* AudioRingBuffer::getPosition() { - return position; -} - -void AudioRingBuffer::setPosition(float *newPosition) { - position[0] = newPosition[0]; - position[1] = newPosition[1]; - position[2] = newPosition[2]; -} - -float AudioRingBuffer::getAttenuationRatio() { - return attenuationRatio; -} - -void AudioRingBuffer::setAttenuationRatio(float newAttenuation) { - attenuationRatio = newAttenuation; -} - -float AudioRingBuffer::getBearing() { - return bearing; -} - -void AudioRingBuffer::setBearing(float newBearing) { - bearing = newBearing; -} - const int AGENT_LOOPBACK_MODIFIER = 307; int AudioRingBuffer::parseData(unsigned char* sourceBuffer, int numBytes) { @@ -103,25 +50,23 @@ int AudioRingBuffer::parseData(unsigned char* sourceBuffer, int numBytes) { unsigned char *dataPtr = sourceBuffer + 1; - for (int p = 0; p < 3; p ++) { - memcpy(&position[p], dataPtr, sizeof(float)); - dataPtr += sizeof(float); - } + memcpy(&_position, dataPtr, sizeof(_position.x) * 3); + dataPtr += (sizeof(_position.x) * 3); unsigned int attenuationByte = *(dataPtr++); - attenuationRatio = attenuationByte / 255.0f; + _attenuationRatio = attenuationByte / 255.0f; - memcpy(&bearing, dataPtr, sizeof(float)); - dataPtr += sizeof(bearing); + memcpy(&_bearing, dataPtr, sizeof(float)); + dataPtr += sizeof(_bearing); - if (bearing > 180 || bearing < -180) { + if (_bearing > 180 || _bearing < -180) { // we were passed an invalid bearing because this agent wants loopback (pressed the H key) _shouldLoopbackForAgent = true; // correct the bearing - bearing = bearing > 0 - ? bearing - AGENT_LOOPBACK_MODIFIER - : bearing + AGENT_LOOPBACK_MODIFIER; + _bearing = _bearing > 0 + ? _bearing - AGENT_LOOPBACK_MODIFIER + : _bearing + AGENT_LOOPBACK_MODIFIER; } else { _shouldLoopbackForAgent = false; } @@ -129,30 +74,30 @@ int AudioRingBuffer::parseData(unsigned char* sourceBuffer, int numBytes) { sourceBuffer = dataPtr; } - if (endOfLastWrite == NULL) { - endOfLastWrite = buffer; + if (_endOfLastWrite == NULL) { + _endOfLastWrite = _buffer; } else if (diffLastWriteNextOutput() > _ringBufferLengthSamples - _bufferLengthSamples) { - endOfLastWrite = buffer; - nextOutput = buffer; - started = false; + _endOfLastWrite = _buffer; + _nextOutput = _buffer; + _started = false; } - memcpy(endOfLastWrite, sourceBuffer, _bufferLengthSamples * sizeof(int16_t)); + memcpy(_endOfLastWrite, sourceBuffer, _bufferLengthSamples * sizeof(int16_t)); - endOfLastWrite += _bufferLengthSamples; + _endOfLastWrite += _bufferLengthSamples; - if (endOfLastWrite >= buffer + _ringBufferLengthSamples) { - endOfLastWrite = buffer; + if (_endOfLastWrite >= _buffer + _ringBufferLengthSamples) { + _endOfLastWrite = _buffer; } return numBytes; } short AudioRingBuffer::diffLastWriteNextOutput() { - if (endOfLastWrite == NULL) { + if (_endOfLastWrite == NULL) { return 0; } else { - short sampleDifference = endOfLastWrite - nextOutput; + short sampleDifference = _endOfLastWrite - _nextOutput; if (sampleDifference < 0) { sampleDifference += _ringBufferLengthSamples; diff --git a/libraries/shared/src/AudioRingBuffer.h b/libraries/shared/src/AudioRingBuffer.h index d939c215ff..0e0e1ca8bc 100644 --- a/libraries/shared/src/AudioRingBuffer.h +++ b/libraries/shared/src/AudioRingBuffer.h @@ -12,6 +12,12 @@ #include #include "AgentData.h" +struct Position { + float x; + float y; + float z; +}; + class AudioRingBuffer : public AgentData { public: AudioRingBuffer(int ringSamples, int bufferSamples); @@ -21,35 +27,36 @@ public: int parseData(unsigned char* sourceBuffer, int numBytes); AudioRingBuffer* clone() const; - int16_t* getNextOutput(); - void setNextOutput(int16_t *newPointer); - int16_t* getEndOfLastWrite(); - void setEndOfLastWrite(int16_t *newPointer); - int16_t* getBuffer(); - bool isStarted(); - void setStarted(bool status); + int16_t* getNextOutput() const { return _nextOutput; } + void setNextOutput(int16_t *nextOutput) { _nextOutput = nextOutput; } + + int16_t* getEndOfLastWrite() const { return _endOfLastWrite; } + void setEndOfLastWrite(int16_t *endOfLastWrite) { _endOfLastWrite = endOfLastWrite; } + + int16_t* getBuffer() const { return _buffer; } + + bool isStarted() const { return _started; } + void setStarted(bool started) { _started = started; } + bool shouldBeAddedToMix() const { return _shouldBeAddedToMix; } void setShouldBeAddedToMix(bool shouldBeAddedToMix) { _shouldBeAddedToMix = shouldBeAddedToMix; } - float* getPosition(); - void setPosition(float newPosition[]); - float getAttenuationRatio(); - void setAttenuationRatio(float newAttenuation); - float getBearing(); - void setBearing(float newBearing); + const Position& getPosition() const { return _position; } + float getAttenuationRatio() const { return _attenuationRatio; } + float getBearing() const { return _bearing; } bool shouldLoopbackForAgent() const { return _shouldLoopbackForAgent; } short diffLastWriteNextOutput(); private: int _ringBufferLengthSamples; int _bufferLengthSamples; - float position[3]; - float attenuationRatio; - float bearing; - int16_t *nextOutput; - int16_t *endOfLastWrite; - int16_t *buffer; - bool started; + Position _position; + float _attenuationRatio; + float _bearing; + int16_t* _nextOutput; + int16_t* _endOfLastWrite; + int16_t* _buffer; + bool _started; bool _shouldBeAddedToMix; bool _shouldLoopbackForAgent; }; From 23f861f5e2e27937aa131e5e646e9bca8302d24b Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Thu, 9 May 2013 13:30:11 -0700 Subject: [PATCH 6/6] fixes per code review comments --- audio-mixer/src/main.cpp | 20 ++++++++++---------- libraries/shared/src/AudioRingBuffer.cpp | 8 ++++---- libraries/shared/src/AudioRingBuffer.h | 4 ++-- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/audio-mixer/src/main.cpp b/audio-mixer/src/main.cpp index 88cb867df4..52889cee8e 100644 --- a/audio-mixer/src/main.cpp +++ b/audio-mixer/src/main.cpp @@ -72,7 +72,7 @@ void plateauAdditionOfSamples(int16_t &mixSample, int16_t sampleToAdd) { } void attachNewBufferToAgent(Agent *newAgent) { - if (newAgent->getLinkedData() == NULL) { + if (!newAgent->getLinkedData()) { newAgent->setLinkedData(new AudioRingBuffer(RING_BUFFER_SAMPLES, BUFFER_LENGTH_SAMPLES_PER_CHANNEL)); } } @@ -89,9 +89,9 @@ int main(int argc, const char* argv[]) { agentList->startSilentAgentRemovalThread(); agentList->startDomainServerCheckInThread(); - unsigned char *packetData = new unsigned char[MAX_PACKET_SIZE]; + unsigned char* packetData = new unsigned char[MAX_PACKET_SIZE]; - sockaddr *agentAddress = new sockaddr; + sockaddr* agentAddress = new sockaddr; // make sure our agent socket is non-blocking agentList->getAgentSocket().setBlocking(false); @@ -106,7 +106,7 @@ int main(int argc, const char* argv[]) { for (AgentList::iterator agent = agentList->begin(); agent != agentList->end(); agent++) { AudioRingBuffer* agentBuffer = (AudioRingBuffer*) agent->getLinkedData(); - if (agentBuffer->getEndOfLastWrite() != NULL) { + if (agentBuffer->getEndOfLastWrite()) { if (!agentBuffer->isStarted() && agentBuffer->diffLastWriteNextOutput() <= BUFFER_LENGTH_SAMPLES_PER_CHANNEL + JITTER_BUFFER_SAMPLES) { printf("Held back buffer for agent with ID %d.\n", agent->getAgentId()); @@ -217,15 +217,15 @@ int main(int argc, const char* argv[]) { } int16_t* goodChannel = bearingRelativeAngleToSource > 0.0f - ? clientMix + BUFFER_LENGTH_SAMPLES_PER_CHANNEL - : clientMix; + ? clientMix + BUFFER_LENGTH_SAMPLES_PER_CHANNEL + : clientMix; int16_t* delayedChannel = bearingRelativeAngleToSource > 0.0f - ? clientMix - : clientMix + BUFFER_LENGTH_SAMPLES_PER_CHANNEL; + ? clientMix + : clientMix + BUFFER_LENGTH_SAMPLES_PER_CHANNEL; int16_t* delaySamplePointer = otherAgentBuffer->getNextOutput() == otherAgentBuffer->getBuffer() - ? otherAgentBuffer->getBuffer() + RING_BUFFER_SAMPLES - numSamplesDelay - : otherAgentBuffer->getNextOutput() - numSamplesDelay; + ? otherAgentBuffer->getBuffer() + RING_BUFFER_SAMPLES - numSamplesDelay + : otherAgentBuffer->getNextOutput() - numSamplesDelay; for (int s = 0; s < BUFFER_LENGTH_SAMPLES_PER_CHANNEL; s++) { diff --git a/libraries/shared/src/AudioRingBuffer.cpp b/libraries/shared/src/AudioRingBuffer.cpp index fb8ecdac86..b49b65945c 100644 --- a/libraries/shared/src/AudioRingBuffer.cpp +++ b/libraries/shared/src/AudioRingBuffer.cpp @@ -50,8 +50,8 @@ int AudioRingBuffer::parseData(unsigned char* sourceBuffer, int numBytes) { unsigned char *dataPtr = sourceBuffer + 1; - memcpy(&_position, dataPtr, sizeof(_position.x) * 3); - dataPtr += (sizeof(_position.x) * 3); + memcpy(&_position, dataPtr, sizeof(_position)); + dataPtr += (sizeof(_position)); unsigned int attenuationByte = *(dataPtr++); _attenuationRatio = attenuationByte / 255.0f; @@ -74,7 +74,7 @@ int AudioRingBuffer::parseData(unsigned char* sourceBuffer, int numBytes) { sourceBuffer = dataPtr; } - if (_endOfLastWrite == NULL) { + if (!_endOfLastWrite) { _endOfLastWrite = _buffer; } else if (diffLastWriteNextOutput() > _ringBufferLengthSamples - _bufferLengthSamples) { _endOfLastWrite = _buffer; @@ -94,7 +94,7 @@ int AudioRingBuffer::parseData(unsigned char* sourceBuffer, int numBytes) { } short AudioRingBuffer::diffLastWriteNextOutput() { - if (_endOfLastWrite == NULL) { + if (!_endOfLastWrite) { return 0; } else { short sampleDifference = _endOfLastWrite - _nextOutput; diff --git a/libraries/shared/src/AudioRingBuffer.h b/libraries/shared/src/AudioRingBuffer.h index 0e0e1ca8bc..fb739629c9 100644 --- a/libraries/shared/src/AudioRingBuffer.h +++ b/libraries/shared/src/AudioRingBuffer.h @@ -28,10 +28,10 @@ public: AudioRingBuffer* clone() const; int16_t* getNextOutput() const { return _nextOutput; } - void setNextOutput(int16_t *nextOutput) { _nextOutput = nextOutput; } + void setNextOutput(int16_t* nextOutput) { _nextOutput = nextOutput; } int16_t* getEndOfLastWrite() const { return _endOfLastWrite; } - void setEndOfLastWrite(int16_t *endOfLastWrite) { _endOfLastWrite = endOfLastWrite; } + void setEndOfLastWrite(int16_t* endOfLastWrite) { _endOfLastWrite = endOfLastWrite; } int16_t* getBuffer() const { return _buffer; }