Merge remote-tracking branch 'upstream/master'

This commit is contained in:
Jeffrey Ventrella 2013-05-09 13:04:45 -07:00
commit 07eba1ae88
2 changed files with 114 additions and 109 deletions

View file

@ -86,7 +86,7 @@ void *sendBuffer(void *args) {
for (AgentList::iterator agent = agentList->begin(); agent != agentList->end(); agent++) { for (AgentList::iterator agent = agentList->begin(); agent != agentList->end(); agent++) {
AudioRingBuffer* agentBuffer = (AudioRingBuffer*) agent->getLinkedData(); AudioRingBuffer* agentBuffer = (AudioRingBuffer*) agent->getLinkedData();
if (agentBuffer != NULL && agentBuffer->getEndOfLastWrite() != NULL) { if (agentBuffer && agentBuffer->getEndOfLastWrite() != NULL) {
if (!agentBuffer->isStarted() if (!agentBuffer->isStarted()
&& agentBuffer->diffLastWriteNextOutput() <= BUFFER_LENGTH_SAMPLES_PER_CHANNEL + JITTER_BUFFER_SAMPLES) { && agentBuffer->diffLastWriteNextOutput() <= BUFFER_LENGTH_SAMPLES_PER_CHANNEL + JITTER_BUFFER_SAMPLES) {
@ -111,124 +111,131 @@ void *sendBuffer(void *args) {
for (AgentList::iterator agent = agentList->begin(); agent != agentList->end(); agent++) { for (AgentList::iterator agent = agentList->begin(); agent != agentList->end(); agent++) {
AudioRingBuffer* agentRingBuffer = (AudioRingBuffer*) agent->getLinkedData(); AudioRingBuffer* agentRingBuffer = (AudioRingBuffer*) agent->getLinkedData();
int16_t clientMix[BUFFER_LENGTH_SAMPLES_PER_CHANNEL * 2] = {}; if (agentRingBuffer) {
int16_t clientMix[BUFFER_LENGTH_SAMPLES_PER_CHANNEL * 2] = {};
for (AgentList::iterator otherAgent = agentList->begin(); otherAgent != agentList->end(); otherAgent++) { for (AgentList::iterator otherAgent = agentList->begin(); otherAgent != agentList->end(); otherAgent++) {
if (otherAgent != agent || (otherAgent == agent && agentRingBuffer->shouldLoopbackForAgent())) { if (otherAgent != agent || (otherAgent == agent && agentRingBuffer->shouldLoopbackForAgent())) {
AudioRingBuffer* otherAgentBuffer = (AudioRingBuffer*) otherAgent->getLinkedData(); AudioRingBuffer* otherAgentBuffer = (AudioRingBuffer*) otherAgent->getLinkedData();
if (otherAgentBuffer->shouldBeAddedToMix()) { if (otherAgentBuffer->shouldBeAddedToMix()) {
float bearingRelativeAngleToSource = 0.f; float bearingRelativeAngleToSource = 0.f;
float attenuationCoefficient = 1.f; float attenuationCoefficient = 1.f;
int numSamplesDelay = 0; int numSamplesDelay = 0;
float weakChannelAmplitudeRatio = 1.f; float weakChannelAmplitudeRatio = 1.f;
if (otherAgent != agent) { if (otherAgent != agent) {
float *agentPosition = agentRingBuffer->getPosition(); float *agentPosition = agentRingBuffer->getPosition();
float *otherAgentPosition = otherAgentBuffer->getPosition(); float *otherAgentPosition = otherAgentBuffer->getPosition();
// calculate the distance to the other agent // calculate the distance to the other agent
// use the distance to the other agent to calculate the change in volume for this frame // 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 lowAgentIndex = std::min(agent.getAgentIndex(), otherAgent.getAgentIndex());
int highAgentIndex = std::max(agent.getAgentIndex(), otherAgent.getAgentIndex()); int highAgentIndex = std::max(agent.getAgentIndex(), otherAgent.getAgentIndex());
if (distanceCoefficients[lowAgentIndex][highAgentIndex] == 0) { if (distanceCoefficients[lowAgentIndex][highAgentIndex] == 0) {
float distanceToAgent = sqrtf(powf(agentPosition[0] - otherAgentPosition[0], 2) + float distanceToAgent = sqrtf(powf(agentPosition[0] - otherAgentPosition[0], 2) +
powf(agentPosition[1] - otherAgentPosition[1], 2) + powf(agentPosition[1] - otherAgentPosition[1], 2) +
powf(agentPosition[2] - otherAgentPosition[2], 2)); powf(agentPosition[2] - otherAgentPosition[2], 2));
float minCoefficient = std::min(1.0f, float minCoefficient = std::min(1.0f,
powf(0.5, (logf(DISTANCE_RATIO * distanceToAgent) / logf(3)) - 1)); powf(0.5,
distanceCoefficients[lowAgentIndex][highAgentIndex] = minCoefficient; (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; // 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 { } else {
absoluteAngleToSource = 90 + triangleAngle; if (otherAgentPosition[2] > agentPosition[2]) {
absoluteAngleToSource = 90 - triangleAngle;
} else {
absoluteAngleToSource = 90 + triangleAngle;
}
} }
}
bearingRelativeAngleToSource = absoluteAngleToSource - agentRingBuffer->getBearing(); bearingRelativeAngleToSource = absoluteAngleToSource - agentRingBuffer->getBearing();
if (bearingRelativeAngleToSource > 180) { if (bearingRelativeAngleToSource > 180) {
bearingRelativeAngleToSource -= 360; bearingRelativeAngleToSource -= 360;
} else if (bearingRelativeAngleToSource < -180) { } else if (bearingRelativeAngleToSource < -180) {
bearingRelativeAngleToSource += 360; bearingRelativeAngleToSource += 360;
} }
float angleOfDelivery = absoluteAngleToSource - otherAgentBuffer->getBearing(); float angleOfDelivery = absoluteAngleToSource - otherAgentBuffer->getBearing();
if (angleOfDelivery > 180) { if (angleOfDelivery > 180) {
angleOfDelivery -= 360; angleOfDelivery -= 360;
} else if (angleOfDelivery < -180) { } else 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));
attenuationCoefficient = distanceCoefficients[lowAgentIndex][highAgentIndex] attenuationCoefficient = distanceCoefficients[lowAgentIndex][highAgentIndex]
* otherAgentBuffer->getAttenuationRatio() * otherAgentBuffer->getAttenuationRatio()
* offAxisCoefficient; * offAxisCoefficient;
bearingRelativeAngleToSource *= (M_PI / 180); bearingRelativeAngleToSource *= (M_PI / 180);
float sinRatio = fabsf(sinf(bearingRelativeAngleToSource)); float sinRatio = fabsf(sinf(bearingRelativeAngleToSource));
numSamplesDelay = PHASE_DELAY_AT_90 * sinRatio; numSamplesDelay = PHASE_DELAY_AT_90 * sinRatio;
weakChannelAmplitudeRatio = 1 - (PHASE_AMPLITUDE_RATIO_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* goodChannel = bearingRelativeAngleToSource > 0.0f
int16_t* delayedChannel = bearingRelativeAngleToSource > 0.0f ? clientMix : clientMix + BUFFER_LENGTH_SAMPLES_PER_CHANNEL; ? 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() int16_t* delaySamplePointer = otherAgentBuffer->getNextOutput() == otherAgentBuffer->getBuffer()
? otherAgentBuffer->getBuffer() + RING_BUFFER_SAMPLES - numSamplesDelay ? otherAgentBuffer->getBuffer() + RING_BUFFER_SAMPLES - numSamplesDelay
: otherAgentBuffer->getNextOutput() - numSamplesDelay; : otherAgentBuffer->getNextOutput() - numSamplesDelay;
for (int s = 0; s < BUFFER_LENGTH_SAMPLES_PER_CHANNEL; s++) { for (int s = 0; s < BUFFER_LENGTH_SAMPLES_PER_CHANNEL; s++) {
if (s < numSamplesDelay) { if (s < numSamplesDelay) {
// pull the earlier sample for the delayed channel // pull the earlier sample for the delayed channel
int earlierSample = delaySamplePointer[s] * attenuationCoefficient; int earlierSample = delaySamplePointer[s] * attenuationCoefficient;
plateauAdditionOfSamples(delayedChannel[s], earlierSample * weakChannelAmplitudeRatio); plateauAdditionOfSamples(delayedChannel[s], earlierSample * weakChannelAmplitudeRatio);
} }
int16_t currentSample = (otherAgentBuffer->getNextOutput()[s] * attenuationCoefficient); int16_t currentSample = (otherAgentBuffer->getNextOutput()[s] * attenuationCoefficient);
plateauAdditionOfSamples(goodChannel[s], currentSample); plateauAdditionOfSamples(goodChannel[s], currentSample);
if (s + numSamplesDelay < BUFFER_LENGTH_SAMPLES_PER_CHANNEL) { if (s + numSamplesDelay < BUFFER_LENGTH_SAMPLES_PER_CHANNEL) {
plateauAdditionOfSamples(delayedChannel[s + numSamplesDelay], plateauAdditionOfSamples(delayedChannel[s + numSamplesDelay],
currentSample * weakChannelAmplitudeRatio); currentSample * weakChannelAmplitudeRatio);
}
} }
} }
} }
} }
}
agentList->getAgentSocket().send(agent->getPublicSocket(), clientMix, BUFFER_LENGTH_BYTES); agentList->getAgentSocket().send(agent->getPublicSocket(), clientMix, BUFFER_LENGTH_BYTES);
}
} }
for (AgentList::iterator agent = agentList->begin(); agent != agentList->end(); agent++) { for (AgentList::iterator agent = agentList->begin(); agent != agentList->end(); agent++) {
AudioRingBuffer* agentBuffer = (AudioRingBuffer*) agent->getLinkedData(); AudioRingBuffer* agentBuffer = (AudioRingBuffer*) agent->getLinkedData();
if (agentBuffer->shouldBeAddedToMix()) { if (agentBuffer && agentBuffer->shouldBeAddedToMix()) {
agentBuffer->setNextOutput(agentBuffer->getNextOutput() + BUFFER_LENGTH_SAMPLES_PER_CHANNEL); agentBuffer->setNextOutput(agentBuffer->getNextOutput() + BUFFER_LENGTH_SAMPLES_PER_CHANNEL);
if (agentBuffer->getNextOutput() >= agentBuffer->getBuffer() + RING_BUFFER_SAMPLES) { if (agentBuffer->getNextOutput() >= agentBuffer->getBuffer() + RING_BUFFER_SAMPLES) {

View file

@ -9,15 +9,13 @@
#include <cstring> #include <cstring>
#include "AudioRingBuffer.h" #include "AudioRingBuffer.h"
AudioRingBuffer::AudioRingBuffer(int ringSamples, int bufferSamples) { AudioRingBuffer::AudioRingBuffer(int ringSamples, int bufferSamples) :
ringBufferLengthSamples = ringSamples; ringBufferLengthSamples(ringSamples),
bufferLengthSamples = bufferSamples; bufferLengthSamples(bufferSamples),
endOfLastWrite(NULL),
started = false; started(false),
_shouldBeAddedToMix = false; _shouldBeAddedToMix(false),
_shouldLoopbackForAgent(false) {
endOfLastWrite = NULL;
buffer = new int16_t[ringBufferLengthSamples]; buffer = new int16_t[ringBufferLengthSamples];
nextOutput = buffer; nextOutput = buffer;
}; };
@ -27,6 +25,7 @@ AudioRingBuffer::AudioRingBuffer(const AudioRingBuffer &otherRingBuffer) {
bufferLengthSamples = otherRingBuffer.bufferLengthSamples; bufferLengthSamples = otherRingBuffer.bufferLengthSamples;
started = otherRingBuffer.started; started = otherRingBuffer.started;
_shouldBeAddedToMix = otherRingBuffer._shouldBeAddedToMix; _shouldBeAddedToMix = otherRingBuffer._shouldBeAddedToMix;
_shouldLoopbackForAgent = otherRingBuffer._shouldLoopbackForAgent;
buffer = new int16_t[ringBufferLengthSamples]; buffer = new int16_t[ringBufferLengthSamples];
memcpy(buffer, otherRingBuffer.buffer, sizeof(int16_t) * ringBufferLengthSamples); memcpy(buffer, otherRingBuffer.buffer, sizeof(int16_t) * ringBufferLengthSamples);
@ -149,8 +148,7 @@ int AudioRingBuffer::parseData(unsigned char* sourceBuffer, int numBytes) {
return numBytes; return numBytes;
} }
short AudioRingBuffer::diffLastWriteNextOutput() short AudioRingBuffer::diffLastWriteNextOutput() {
{
if (endOfLastWrite == NULL) { if (endOfLastWrite == NULL) {
return 0; return 0;
} else { } else {