mirror of
https://github.com/overte-org/overte.git
synced 2025-08-09 12:08:54 +02:00
Merge pull request #231 from birarda/master
fix loopback from audio mixer
This commit is contained in:
commit
68ead2f309
2 changed files with 68 additions and 64 deletions
|
@ -120,73 +120,78 @@ void *sendBuffer(void *args) {
|
||||||
AudioRingBuffer* otherAgentBuffer = (AudioRingBuffer*) otherAgent->getLinkedData();
|
AudioRingBuffer* otherAgentBuffer = (AudioRingBuffer*) otherAgent->getLinkedData();
|
||||||
|
|
||||||
if (otherAgentBuffer->shouldBeAddedToMix()) {
|
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
|
if (otherAgent != agent) {
|
||||||
|
float *agentPosition = agentRingBuffer->getPosition();
|
||||||
// use the distance to the other agent to calculate the change in volume for this frame
|
float *otherAgentPosition = otherAgentBuffer->getPosition();
|
||||||
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,
|
// calculate the distance to the other agent
|
||||||
powf(0.5, (logf(DISTANCE_RATIO * distanceToAgent) / logf(3)) - 1));
|
|
||||||
distanceCoefficients[lowAgentIndex][highAgentIndex] = minCoefficient;
|
// 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());
|
||||||
|
|
||||||
// get the angle from the right-angle triangle
|
if (distanceCoefficients[lowAgentIndex][highAgentIndex] == 0) {
|
||||||
float triangleAngle = atan2f(fabsf(agentPosition[2] - otherAgentPosition[2]),
|
float distanceToAgent = sqrtf(powf(agentPosition[0] - otherAgentPosition[0], 2) +
|
||||||
fabsf(agentPosition[0] - otherAgentPosition[0])) * (180 / M_PI);
|
powf(agentPosition[1] - otherAgentPosition[1], 2) +
|
||||||
float absoluteAngleToSource = 0;
|
powf(agentPosition[2] - otherAgentPosition[2], 2));
|
||||||
float bearingRelativeAngleToSource = 0;
|
|
||||||
|
float minCoefficient = std::min(1.0f,
|
||||||
|
powf(0.5, (logf(DISTANCE_RATIO * distanceToAgent) / logf(3)) - 1));
|
||||||
// find the angle we need for calculation based on the orientation of the triangle
|
distanceCoefficients[lowAgentIndex][highAgentIndex] = minCoefficient;
|
||||||
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;
|
// 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 {
|
} else {
|
||||||
absoluteAngleToSource = 90 + triangleAngle;
|
if (otherAgentPosition[2] > agentPosition[2]) {
|
||||||
|
absoluteAngleToSource = 90 - triangleAngle;
|
||||||
|
} else {
|
||||||
|
absoluteAngleToSource = 90 + triangleAngle;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
if (absoluteAngleToSource > 180) {
|
||||||
if (absoluteAngleToSource > 180) {
|
absoluteAngleToSource -= 360;
|
||||||
absoluteAngleToSource -= 360;
|
} else if (absoluteAngleToSource < -180) {
|
||||||
} else if (absoluteAngleToSource < -180) {
|
absoluteAngleToSource += 360;
|
||||||
absoluteAngleToSource += 360;
|
}
|
||||||
}
|
|
||||||
|
bearingRelativeAngleToSource = absoluteAngleToSource - agentBearing;
|
||||||
bearingRelativeAngleToSource = absoluteAngleToSource - agentBearing;
|
bearingRelativeAngleToSource *= (M_PI / 180);
|
||||||
bearingRelativeAngleToSource *= (M_PI / 180);
|
|
||||||
|
float angleOfDelivery = absoluteAngleToSource - otherAgentBuffer->getBearing();
|
||||||
float angleOfDelivery = absoluteAngleToSource - otherAgentBuffer->getBearing();
|
|
||||||
|
if (angleOfDelivery < -180) {
|
||||||
if (angleOfDelivery < -180) {
|
angleOfDelivery += 360;
|
||||||
angleOfDelivery += 360;
|
}
|
||||||
}
|
|
||||||
|
float offAxisCoefficient = MAX_OFF_AXIS_ATTENUATION +
|
||||||
float offAxisCoefficient = MAX_OFF_AXIS_ATTENUATION +
|
|
||||||
(OFF_AXIS_ATTENUATION_FORMULA_STEP * (fabsf(angleOfDelivery) / 90.0f));
|
(OFF_AXIS_ATTENUATION_FORMULA_STEP * (fabsf(angleOfDelivery) / 90.0f));
|
||||||
|
|
||||||
float attenuationCoefficient = distanceCoefficients[lowAgentIndex][highAgentIndex]
|
attenuationCoefficient = distanceCoefficients[lowAgentIndex][highAgentIndex]
|
||||||
* otherAgentBuffer->getAttenuationRatio()
|
* otherAgentBuffer->getAttenuationRatio()
|
||||||
* offAxisCoefficient;
|
* offAxisCoefficient;
|
||||||
|
|
||||||
float sinRatio = fabsf(sinf(bearingRelativeAngleToSource));
|
float sinRatio = fabsf(sinf(bearingRelativeAngleToSource));
|
||||||
int numSamplesDelay = PHASE_DELAY_AT_90 * sinRatio;
|
int numSamplesDelay = PHASE_DELAY_AT_90 * sinRatio;
|
||||||
float weakChannelAmplitudeRatio = 1 - (PHASE_AMPLITUDE_RATIO_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* goodChannel = bearingRelativeAngleToSource > 0 ? clientMix + BUFFER_LENGTH_SAMPLES_PER_CHANNEL : clientMix;
|
||||||
int16_t* delayedChannel = bearingRelativeAngleToSource > 0 ? clientMix : clientMix + BUFFER_LENGTH_SAMPLES_PER_CHANNEL;
|
int16_t* delayedChannel = bearingRelativeAngleToSource > 0 ? clientMix : clientMix + BUFFER_LENGTH_SAMPLES_PER_CHANNEL;
|
||||||
|
|
|
@ -113,6 +113,7 @@ int AudioRingBuffer::parseData(unsigned char* sourceBuffer, int numBytes) {
|
||||||
attenuationRatio = attenuationByte / 255.0f;
|
attenuationRatio = attenuationByte / 255.0f;
|
||||||
|
|
||||||
memcpy(&bearing, dataPtr, sizeof(float));
|
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)
|
// 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;
|
: bearing + AGENT_LOOPBACK_MODIFIER;
|
||||||
} else {
|
} else {
|
||||||
_shouldLoopbackForAgent = false;
|
_shouldLoopbackForAgent = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
dataPtr += sizeof(float);
|
|
||||||
|
|
||||||
sourceBuffer = dataPtr;
|
sourceBuffer = dataPtr;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue