From 82cff262b1906d79dae0479e97fe5d913b3da5ca Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Tue, 7 May 2013 14:28:48 -0700 Subject: [PATCH] for agent loopback perform no distance and angle calculations --- audio-mixer/src/main.cpp | 127 ++++++++++++----------- libraries/shared/src/AudioRingBuffer.cpp | 5 +- 2 files changed, 68 insertions(+), 64 deletions(-) diff --git a/audio-mixer/src/main.cpp b/audio-mixer/src/main.cpp index 1427a4097b..82f307f8d4 100644 --- a/audio-mixer/src/main.cpp +++ b/audio-mixer/src/main.cpp @@ -120,73 +120,78 @@ void *sendBuffer(void *args) { AudioRingBuffer* otherAgentBuffer = (AudioRingBuffer*) otherAgent->getLinkedData(); if (otherAgentBuffer->shouldBeAddedToMix()) { - float *agentPosition = agentRingBuffer->getPosition(); - float *otherAgentPosition = otherAgentBuffer->getPosition(); + + float bearingRelativeAngleToSource = 0.f; + float attenuationCoefficient = 1.f; - // 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)); + if (otherAgent != agent) { + float *agentPosition = agentRingBuffer->getPosition(); + float *otherAgentPosition = otherAgentBuffer->getPosition(); - 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; - float 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; + // 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; } - } else { - if (otherAgentPosition[2] > agentPosition[2]) { - absoluteAngleToSource = 90 - triangleAngle; + + + // 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; + float 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 { - absoluteAngleToSource = 90 + triangleAngle; + if (otherAgentPosition[2] > agentPosition[2]) { + absoluteAngleToSource = 90 - triangleAngle; + } else { + absoluteAngleToSource = 90 + triangleAngle; + } } - } - - if (absoluteAngleToSource > 180) { - absoluteAngleToSource -= 360; - } else if (absoluteAngleToSource < -180) { - absoluteAngleToSource += 360; - } - - bearingRelativeAngleToSource = absoluteAngleToSource - agentBearing; - bearingRelativeAngleToSource *= (M_PI / 180); - - float angleOfDelivery = absoluteAngleToSource - otherAgentBuffer->getBearing(); - - if (angleOfDelivery < -180) { - angleOfDelivery += 360; - } - - float offAxisCoefficient = MAX_OFF_AXIS_ATTENUATION + + + if (absoluteAngleToSource > 180) { + absoluteAngleToSource -= 360; + } else if (absoluteAngleToSource < -180) { + absoluteAngleToSource += 360; + } + + bearingRelativeAngleToSource = absoluteAngleToSource - agentBearing; + bearingRelativeAngleToSource *= (M_PI / 180); + + float angleOfDelivery = absoluteAngleToSource - otherAgentBuffer->getBearing(); + + if (angleOfDelivery < -180) { + angleOfDelivery += 360; + } + + float offAxisCoefficient = MAX_OFF_AXIS_ATTENUATION + (OFF_AXIS_ATTENUATION_FORMULA_STEP * (fabsf(angleOfDelivery) / 90.0f)); - - float attenuationCoefficient = distanceCoefficients[lowAgentIndex][highAgentIndex] - * otherAgentBuffer->getAttenuationRatio() - * offAxisCoefficient; - - float sinRatio = fabsf(sinf(bearingRelativeAngleToSource)); - int numSamplesDelay = PHASE_DELAY_AT_90 * sinRatio; - float weakChannelAmplitudeRatio = 1 - (PHASE_AMPLITUDE_RATIO_AT_90 * sinRatio); + + attenuationCoefficient = distanceCoefficients[lowAgentIndex][highAgentIndex] + * otherAgentBuffer->getAttenuationRatio() + * offAxisCoefficient; + + float sinRatio = fabsf(sinf(bearingRelativeAngleToSource)); + int numSamplesDelay = PHASE_DELAY_AT_90 * sinRatio; + float weakChannelAmplitudeRatio = 1 - (PHASE_AMPLITUDE_RATIO_AT_90 * sinRatio); + } int16_t* goodChannel = bearingRelativeAngleToSource > 0 ? clientMix + BUFFER_LENGTH_SAMPLES_PER_CHANNEL : clientMix; int16_t* delayedChannel = bearingRelativeAngleToSource > 0 ? clientMix : clientMix + BUFFER_LENGTH_SAMPLES_PER_CHANNEL; diff --git a/libraries/shared/src/AudioRingBuffer.cpp b/libraries/shared/src/AudioRingBuffer.cpp index d48f95c754..5079582544 100644 --- a/libraries/shared/src/AudioRingBuffer.cpp +++ b/libraries/shared/src/AudioRingBuffer.cpp @@ -113,6 +113,7 @@ int AudioRingBuffer::parseData(unsigned char* sourceBuffer, int numBytes) { attenuationRatio = attenuationByte / 255.0f; memcpy(&bearing, dataPtr, sizeof(float)); + dataPtr += sizeof(bearing); if (bearing > 180 || bearing < -180) { // we were passed an invalid bearing because this agent wants loopback (pressed the H key) @@ -124,9 +125,7 @@ int AudioRingBuffer::parseData(unsigned char* sourceBuffer, int numBytes) { : bearing + AGENT_LOOPBACK_MODIFIER; } else { _shouldLoopbackForAgent = false; - } - - dataPtr += sizeof(float); + } sourceBuffer = dataPtr; }