mirror of
https://github.com/lubosz/overte.git
synced 2025-04-25 00:03:16 +02:00
Merge pull request #217 from birarda/off-axis
add off-axis attenuation to audio
This commit is contained in:
commit
ef68ef9c5d
3 changed files with 62 additions and 84 deletions
|
@ -60,11 +60,8 @@ 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;
|
||||
const float MAX_OFF_AXIS_ATTENUATION = 0.5f;
|
||||
const float OFF_AXIS_ATTENUATION_FORMULA_STEP = (1 - MAX_OFF_AXIS_ATTENUATION) / 2.0f;
|
||||
|
||||
void plateauAdditionOfSamples(int16_t &mixSample, int16_t sampleToAdd) {
|
||||
long sumSample = sampleToAdd + mixSample;
|
||||
|
@ -109,29 +106,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 +129,89 @@ 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 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);
|
||||
|
||||
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 +270,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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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__) */
|
||||
|
|
Loading…
Reference in a new issue