refactoring in audio mixer in preparation for off axis attenuation

This commit is contained in:
Stephen Birarda 2013-05-06 17:07:57 -07:00
parent 1aeb448009
commit 81fade33dc
3 changed files with 50 additions and 85 deletions

View file

@ -60,12 +60,6 @@ const float DISTANCE_RATIO = 3.0f / 0.3f;
const float PHASE_AMPLITUDE_RATIO_AT_90 = 0.5;
const int PHASE_DELAY_AT_90 = 20;
const int AGENT_LOOPBACK_MODIFIER = 307;
const int LOOPBACK_SANITY_CHECK = 0;
StDev stdev;
void plateauAdditionOfSamples(int16_t &mixSample, int16_t sampleToAdd) {
long sumSample = sampleToAdd + mixSample;
@ -109,29 +103,17 @@ void *sendBuffer(void *args) {
}
int numAgents = agentList->size();
float distanceCoeffs[numAgents][numAgents];
memset(distanceCoeffs, 0, sizeof(distanceCoeffs));
float distanceCoefficients[numAgents][numAgents];
memset(distanceCoefficients, 0, sizeof(distanceCoefficients));
for (AgentList::iterator agent = agentList->begin(); agent != agentList->end(); agent++) {
AudioRingBuffer* agentRingBuffer = (AudioRingBuffer*) agent->getLinkedData();
float agentBearing = agentRingBuffer->getBearing();
bool agentWantsLoopback = false;
if (agentBearing > 180 || agentBearing < -180) {
// we were passed an invalid bearing because this agent wants loopback (pressed the H key)
agentWantsLoopback = true;
// correct the bearing
agentBearing = agentBearing > 0
? agentBearing - AGENT_LOOPBACK_MODIFIER
: agentBearing + AGENT_LOOPBACK_MODIFIER;
}
int16_t clientMix[BUFFER_LENGTH_SAMPLES_PER_CHANNEL * 2] = {};
for (AgentList::iterator otherAgent = agentList->begin(); otherAgent != agentList->end(); otherAgent++) {
if (otherAgent != agent || (otherAgent == agent && agentWantsLoopback)) {
if (otherAgent != agent || (otherAgent == agent && agentRingBuffer->shouldLoopbackForAgent())) {
AudioRingBuffer* otherAgentBuffer = (AudioRingBuffer*) otherAgent->getLinkedData();
if (otherAgentBuffer->shouldBeAddedToMix()) {
@ -144,80 +126,79 @@ void *sendBuffer(void *args) {
int lowAgentIndex = std::min(agent.getAgentIndex(), otherAgent.getAgentIndex());
int highAgentIndex = std::max(agent.getAgentIndex(), otherAgent.getAgentIndex());
if (distanceCoeffs[lowAgentIndex][highAgentIndex] == 0) {
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));
distanceCoeffs[lowAgentIndex][highAgentIndex] = minCoefficient;
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 angleToSource;
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]) {
angleToSource = -90 + triangleAngle - agentBearing;
absoluteAngleToSource = -90 + triangleAngle;
} else {
angleToSource = -90 - triangleAngle - agentBearing;
absoluteAngleToSource = -90 - triangleAngle;
}
} else {
if (otherAgentPosition[2] > agentPosition[2]) {
angleToSource = 90 - triangleAngle - agentBearing;
absoluteAngleToSource = 90 - triangleAngle;
} else {
angleToSource = 90 + triangleAngle - agentBearing;
absoluteAngleToSource = 90 + triangleAngle;
}
}
if (angleToSource > 180) {
angleToSource -= 360;
} else if (angleToSource < -180) {
angleToSource += 360;
if (absoluteAngleToSource > 180) {
absoluteAngleToSource -= 360;
} else if (absoluteAngleToSource < -180) {
absoluteAngleToSource += 360;
}
angleToSource *= (M_PI / 180);
bearingRelativeAngleToSource = absoluteAngleToSource - agentBearing;
bearingRelativeAngleToSource *= (M_PI / 180);
float sinRatio = fabsf(sinf(angleToSource));
float attenuationCoefficient = distanceCoefficients[lowAgentIndex][highAgentIndex] *
otherAgentBuffer->getAttenuationRatio();
float sinRatio = fabsf(sinf(bearingRelativeAngleToSource));
int numSamplesDelay = PHASE_DELAY_AT_90 * sinRatio;
float weakChannelAmplitudeRatio = 1 - (PHASE_AMPLITUDE_RATIO_AT_90 * sinRatio);
int16_t *goodChannel = angleToSource > 0 ? clientMix + BUFFER_LENGTH_SAMPLES_PER_CHANNEL : clientMix;
int16_t *delayedChannel = angleToSource > 0 ? clientMix : clientMix + BUFFER_LENGTH_SAMPLES_PER_CHANNEL;
int16_t *delaySamplePointer = otherAgentBuffer->getNextOutput() == otherAgentBuffer->getBuffer()
? otherAgentBuffer->getBuffer() + RING_BUFFER_SAMPLES - numSamplesDelay
: otherAgentBuffer->getNextOutput() - numSamplesDelay;
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* 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] *
distanceCoeffs[lowAgentIndex][highAgentIndex] *
otherAgentBuffer->getAttenuationRatio();
int earlierSample = delaySamplePointer[s] * attenuationCoefficient;
plateauAdditionOfSamples(delayedChannel[s], earlierSample * weakChannelAmplitudeRatio);
}
int16_t currentSample = (otherAgentBuffer->getNextOutput()[s] *
distanceCoeffs[lowAgentIndex][highAgentIndex] *
otherAgentBuffer->getAttenuationRatio());
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 *
otherAgentBuffer->getAttenuationRatio());
weakChannelAmplitudeRatio);
}
}
}
@ -276,49 +257,17 @@ int main(int argc, const char* argv[]) {
pthread_t sendBufferThread;
pthread_create(&sendBufferThread, NULL, sendBuffer, NULL);
int16_t *loopbackAudioPacket;
if (LOOPBACK_SANITY_CHECK) {
loopbackAudioPacket = new int16_t[1024];
}
sockaddr *agentAddress = new sockaddr;
timeval lastReceive;
gettimeofday(&lastReceive, NULL);
bool firstSample = true;
while (true) {
if(agentList->getAgentSocket().receive(agentAddress, packetData, &receivedBytes)) {
if (packetData[0] == PACKET_HEADER_INJECT_AUDIO) {
// Compute and report standard deviation for jitter calculation
if (firstSample) {
stdev.reset();
firstSample = false;
} else {
double tDiff = (usecTimestampNow() - usecTimestamp(&lastReceive)) / 1000;
stdev.addValue(tDiff);
if (stdev.getSamples() > 500) {
//printf("Avg: %4.2f, Stdev: %4.2f\n", stdev.getAverage(), stdev.getStDev());
stdev.reset();
}
if (agentList->addOrUpdateAgent(agentAddress, agentAddress, packetData[0], agentList->getLastAgentId())) {
agentList->increaseAgentId();
}
gettimeofday(&lastReceive, NULL);
// add or update the existing interface agent
if (!LOOPBACK_SANITY_CHECK) {
if (agentList->addOrUpdateAgent(agentAddress, agentAddress, packetData[0], agentList->getLastAgentId())) {
agentList->increaseAgentId();
}
agentList->updateAgentWithData(agentAddress, packetData, receivedBytes);
} else {
memcpy(loopbackAudioPacket, packetData + 1 + (sizeof(float) * 4), 1024);
agentList->getAgentSocket().send(agentAddress, loopbackAudioPacket, 1024);
}
agentList->updateAgentWithData(agentAddress, packetData, receivedBytes);
}
}
}

View file

@ -97,6 +97,8 @@ void AudioRingBuffer::setBearing(float newBearing) {
bearing = newBearing;
}
const int AGENT_LOOPBACK_MODIFIER = 307;
int AudioRingBuffer::parseData(unsigned char* sourceBuffer, int numBytes) {
if (numBytes > (bufferLengthSamples * sizeof(int16_t))) {
@ -111,6 +113,17 @@ int AudioRingBuffer::parseData(unsigned char* sourceBuffer, int numBytes) {
attenuationRatio = attenuationByte / 255.0f;
memcpy(&bearing, dataPtr, sizeof(float));
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;
}
dataPtr += sizeof(float);
sourceBuffer = dataPtr;

View file

@ -36,6 +36,8 @@ public:
void setAttenuationRatio(float newAttenuation);
float getBearing();
void setBearing(float newBearing);
bool shouldLoopbackForAgent() const { return _shouldLoopbackForAgent; }
short diffLastWriteNextOutput();
private:
@ -49,6 +51,7 @@ private:
int16_t *buffer;
bool started;
bool _shouldBeAddedToMix;
bool _shouldLoopbackForAgent;
};
#endif /* defined(__interface__AudioRingBuffer__) */