mirror of
https://github.com/lubosz/overte.git
synced 2025-08-07 17:41:12 +02:00
Merge pull request #261 from birarda/mixer-crash
de-thread the audio mixer to hopefully resolve its constant restarts
This commit is contained in:
commit
a1a0bf320f
3 changed files with 211 additions and 272 deletions
|
@ -71,23 +71,42 @@ void plateauAdditionOfSamples(int16_t &mixSample, int16_t sampleToAdd) {
|
||||||
mixSample = normalizedSample;
|
mixSample = normalizedSample;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *sendBuffer(void *args) {
|
void attachNewBufferToAgent(Agent *newAgent) {
|
||||||
int sentBytes;
|
if (!newAgent->getLinkedData()) {
|
||||||
|
newAgent->setLinkedData(new AudioRingBuffer(RING_BUFFER_SAMPLES, BUFFER_LENGTH_SAMPLES_PER_CHANNEL));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, const char* argv[]) {
|
||||||
|
setvbuf(stdout, NULL, _IOLBF, 0);
|
||||||
|
|
||||||
|
AgentList* agentList = AgentList::createInstance(AGENT_TYPE_AUDIO_MIXER, MIXER_LISTEN_PORT);
|
||||||
|
|
||||||
|
ssize_t receivedBytes = 0;
|
||||||
|
|
||||||
|
agentList->linkedDataCreateCallback = attachNewBufferToAgent;
|
||||||
|
|
||||||
|
agentList->startSilentAgentRemovalThread();
|
||||||
|
agentList->startDomainServerCheckInThread();
|
||||||
|
|
||||||
|
unsigned char* packetData = new unsigned char[MAX_PACKET_SIZE];
|
||||||
|
|
||||||
|
sockaddr* agentAddress = new sockaddr;
|
||||||
|
|
||||||
|
// make sure our agent socket is non-blocking
|
||||||
|
agentList->getAgentSocket().setBlocking(false);
|
||||||
|
|
||||||
int nextFrame = 0;
|
int nextFrame = 0;
|
||||||
timeval startTime;
|
timeval startTime;
|
||||||
|
|
||||||
AgentList* agentList = AgentList::getInstance();
|
|
||||||
|
|
||||||
gettimeofday(&startTime, NULL);
|
gettimeofday(&startTime, NULL);
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
sentBytes = 0;
|
// enumerate the agents, check if we can add audio from the agent to current mix
|
||||||
|
|
||||||
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 && agentBuffer->getEndOfLastWrite() != NULL) {
|
if (agentBuffer->getEndOfLastWrite()) {
|
||||||
|
|
||||||
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) {
|
||||||
printf("Held back buffer for agent with ID %d.\n", agent->getAgentId());
|
printf("Held back buffer for agent with ID %d.\n", agent->getAgentId());
|
||||||
|
@ -107,132 +126,131 @@ void *sendBuffer(void *args) {
|
||||||
int numAgents = agentList->size();
|
int numAgents = agentList->size();
|
||||||
float distanceCoefficients[numAgents][numAgents];
|
float distanceCoefficients[numAgents][numAgents];
|
||||||
memset(distanceCoefficients, 0, sizeof(distanceCoefficients));
|
memset(distanceCoefficients, 0, sizeof(distanceCoefficients));
|
||||||
|
|
||||||
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) {
|
for (AgentList::iterator otherAgent = agentList->begin(); otherAgent != agentList->end(); otherAgent++) {
|
||||||
int16_t clientMix[BUFFER_LENGTH_SAMPLES_PER_CHANNEL * 2] = {};
|
if (otherAgent != agent || (otherAgent == agent && agentRingBuffer->shouldLoopbackForAgent())) {
|
||||||
|
AudioRingBuffer* otherAgentBuffer = (AudioRingBuffer*) otherAgent->getLinkedData();
|
||||||
for (AgentList::iterator otherAgent = agentList->begin(); otherAgent != agentList->end(); otherAgent++) {
|
|
||||||
if (otherAgent != agent || (otherAgent == agent && agentRingBuffer->shouldLoopbackForAgent())) {
|
if (otherAgentBuffer->shouldBeAddedToMix()) {
|
||||||
AudioRingBuffer* otherAgentBuffer = (AudioRingBuffer*) otherAgent->getLinkedData();
|
|
||||||
|
|
||||||
if (otherAgentBuffer->shouldBeAddedToMix()) {
|
float bearingRelativeAngleToSource = 0.f;
|
||||||
|
float attenuationCoefficient = 1.f;
|
||||||
|
int numSamplesDelay = 0;
|
||||||
|
float weakChannelAmplitudeRatio = 1.f;
|
||||||
|
|
||||||
|
if (otherAgent != agent) {
|
||||||
|
Position agentPosition = agentRingBuffer->getPosition();
|
||||||
|
Position otherAgentPosition = otherAgentBuffer->getPosition();
|
||||||
|
|
||||||
float bearingRelativeAngleToSource = 0.f;
|
// calculate the distance to the other agent
|
||||||
float attenuationCoefficient = 1.f;
|
|
||||||
int numSamplesDelay = 0;
|
|
||||||
float weakChannelAmplitudeRatio = 1.f;
|
|
||||||
|
|
||||||
if (otherAgent != agent) {
|
// use the distance to the other agent to calculate the change in volume for this frame
|
||||||
float *agentPosition = agentRingBuffer->getPosition();
|
int lowAgentIndex = std::min(agent.getAgentIndex(), otherAgent.getAgentIndex());
|
||||||
float *otherAgentPosition = otherAgentBuffer->getPosition();
|
int highAgentIndex = std::max(agent.getAgentIndex(), otherAgent.getAgentIndex());
|
||||||
|
|
||||||
|
if (distanceCoefficients[lowAgentIndex][highAgentIndex] == 0) {
|
||||||
|
float distanceToAgent = sqrtf(powf(agentPosition.x - otherAgentPosition.x, 2) +
|
||||||
|
powf(agentPosition.y - otherAgentPosition.y, 2) +
|
||||||
|
powf(agentPosition.z - otherAgentPosition.z, 2));
|
||||||
|
|
||||||
// calculate the distance to the other agent
|
float minCoefficient = std::min(1.0f,
|
||||||
|
powf(0.5,
|
||||||
// use the distance to the other agent to calculate the change in volume for this frame
|
(logf(DISTANCE_RATIO * distanceToAgent) / logf(3)) - 1));
|
||||||
int lowAgentIndex = std::min(agent.getAgentIndex(), otherAgent.getAgentIndex());
|
distanceCoefficients[lowAgentIndex][highAgentIndex] = minCoefficient;
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// 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;
|
|
||||||
} else {
|
|
||||||
absoluteAngleToSource = 90 + triangleAngle;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bearingRelativeAngleToSource = absoluteAngleToSource - agentRingBuffer->getBearing();
|
|
||||||
|
|
||||||
if (bearingRelativeAngleToSource > 180) {
|
|
||||||
bearingRelativeAngleToSource -= 360;
|
|
||||||
} else if (bearingRelativeAngleToSource < -180) {
|
|
||||||
bearingRelativeAngleToSource += 360;
|
|
||||||
}
|
|
||||||
|
|
||||||
float angleOfDelivery = absoluteAngleToSource - otherAgentBuffer->getBearing();
|
|
||||||
|
|
||||||
if (angleOfDelivery > 180) {
|
|
||||||
angleOfDelivery -= 360;
|
|
||||||
} else if (angleOfDelivery < -180) {
|
|
||||||
angleOfDelivery += 360;
|
|
||||||
}
|
|
||||||
|
|
||||||
float offAxisCoefficient = MAX_OFF_AXIS_ATTENUATION +
|
|
||||||
(OFF_AXIS_ATTENUATION_FORMULA_STEP * (fabsf(angleOfDelivery) / 90.0f));
|
|
||||||
|
|
||||||
attenuationCoefficient = distanceCoefficients[lowAgentIndex][highAgentIndex]
|
|
||||||
* otherAgentBuffer->getAttenuationRatio()
|
|
||||||
* offAxisCoefficient;
|
|
||||||
|
|
||||||
bearingRelativeAngleToSource *= (M_PI / 180);
|
|
||||||
|
|
||||||
float sinRatio = fabsf(sinf(bearingRelativeAngleToSource));
|
|
||||||
numSamplesDelay = PHASE_DELAY_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* delayedChannel = bearingRelativeAngleToSource > 0.0f
|
|
||||||
? clientMix
|
|
||||||
: clientMix + BUFFER_LENGTH_SAMPLES_PER_CHANNEL;
|
|
||||||
|
|
||||||
int16_t* delaySamplePointer = otherAgentBuffer->getNextOutput() == otherAgentBuffer->getBuffer()
|
// get the angle from the right-angle triangle
|
||||||
|
float triangleAngle = atan2f(fabsf(agentPosition.z - otherAgentPosition.z),
|
||||||
|
fabsf(agentPosition.x - otherAgentPosition.x)) * (180 / M_PI);
|
||||||
|
float absoluteAngleToSource = 0;
|
||||||
|
bearingRelativeAngleToSource = 0;
|
||||||
|
|
||||||
|
// find the angle we need for calculation based on the orientation of the triangle
|
||||||
|
if (otherAgentPosition.x > agentPosition.x) {
|
||||||
|
if (otherAgentPosition.z > agentPosition.z) {
|
||||||
|
absoluteAngleToSource = -90 + triangleAngle;
|
||||||
|
} else {
|
||||||
|
absoluteAngleToSource = -90 - triangleAngle;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (otherAgentPosition.z > agentPosition.z) {
|
||||||
|
absoluteAngleToSource = 90 - triangleAngle;
|
||||||
|
} else {
|
||||||
|
absoluteAngleToSource = 90 + triangleAngle;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bearingRelativeAngleToSource = absoluteAngleToSource - agentRingBuffer->getBearing();
|
||||||
|
|
||||||
|
if (bearingRelativeAngleToSource > 180) {
|
||||||
|
bearingRelativeAngleToSource -= 360;
|
||||||
|
} else if (bearingRelativeAngleToSource < -180) {
|
||||||
|
bearingRelativeAngleToSource += 360;
|
||||||
|
}
|
||||||
|
|
||||||
|
float angleOfDelivery = absoluteAngleToSource - otherAgentBuffer->getBearing();
|
||||||
|
|
||||||
|
if (angleOfDelivery > 180) {
|
||||||
|
angleOfDelivery -= 360;
|
||||||
|
} else if (angleOfDelivery < -180) {
|
||||||
|
angleOfDelivery += 360;
|
||||||
|
}
|
||||||
|
|
||||||
|
float offAxisCoefficient = MAX_OFF_AXIS_ATTENUATION +
|
||||||
|
(OFF_AXIS_ATTENUATION_FORMULA_STEP * (fabsf(angleOfDelivery) / 90.0f));
|
||||||
|
|
||||||
|
attenuationCoefficient = distanceCoefficients[lowAgentIndex][highAgentIndex]
|
||||||
|
* otherAgentBuffer->getAttenuationRatio()
|
||||||
|
* offAxisCoefficient;
|
||||||
|
|
||||||
|
bearingRelativeAngleToSource *= (M_PI / 180);
|
||||||
|
|
||||||
|
float sinRatio = fabsf(sinf(bearingRelativeAngleToSource));
|
||||||
|
numSamplesDelay = PHASE_DELAY_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* delayedChannel = bearingRelativeAngleToSource > 0.0f
|
||||||
|
? clientMix
|
||||||
|
: clientMix + BUFFER_LENGTH_SAMPLES_PER_CHANNEL;
|
||||||
|
|
||||||
|
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) {
|
||||||
|
// pull the earlier sample for the delayed channel
|
||||||
if (s < numSamplesDelay) {
|
int earlierSample = delaySamplePointer[s] * attenuationCoefficient;
|
||||||
// pull the earlier sample for the delayed channel
|
plateauAdditionOfSamples(delayedChannel[s], earlierSample * weakChannelAmplitudeRatio);
|
||||||
int earlierSample = delaySamplePointer[s] * attenuationCoefficient;
|
}
|
||||||
plateauAdditionOfSamples(delayedChannel[s], earlierSample * weakChannelAmplitudeRatio);
|
|
||||||
}
|
int16_t currentSample = (otherAgentBuffer->getNextOutput()[s] * attenuationCoefficient);
|
||||||
|
plateauAdditionOfSamples(goodChannel[s], currentSample);
|
||||||
int16_t currentSample = (otherAgentBuffer->getNextOutput()[s] * attenuationCoefficient);
|
|
||||||
plateauAdditionOfSamples(goodChannel[s], currentSample);
|
if (s + numSamplesDelay < BUFFER_LENGTH_SAMPLES_PER_CHANNEL) {
|
||||||
|
plateauAdditionOfSamples(delayedChannel[s + numSamplesDelay],
|
||||||
if (s + numSamplesDelay < BUFFER_LENGTH_SAMPLES_PER_CHANNEL) {
|
currentSample * weakChannelAmplitudeRatio);
|
||||||
plateauAdditionOfSamples(delayedChannel[s + numSamplesDelay],
|
|
||||||
currentSample * weakChannelAmplitudeRatio);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
agentList->getAgentSocket().send(agent->getPublicSocket(), clientMix, BUFFER_LENGTH_BYTES);
|
|
||||||
}
|
agentList->getAgentSocket().send(agent->getPublicSocket(), clientMix, BUFFER_LENGTH_BYTES);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// push forward the next output pointers for any audio buffers we used
|
||||||
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 && agentBuffer->shouldBeAddedToMix()) {
|
if (agentBuffer && agentBuffer->shouldBeAddedToMix()) {
|
||||||
|
@ -246,45 +264,8 @@ void *sendBuffer(void *args) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
double usecToSleep = usecTimestamp(&startTime) + (++nextFrame * BUFFER_SEND_INTERVAL_USECS) - usecTimestampNow();
|
// pull any new audio data from agents off of the network stack
|
||||||
|
while (agentList->getAgentSocket().receive(agentAddress, packetData, &receivedBytes)) {
|
||||||
if (usecToSleep > 0) {
|
|
||||||
usleep(usecToSleep);
|
|
||||||
} else {
|
|
||||||
std::cout << "Took too much time, not sleeping!\n";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pthread_exit(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void attachNewBufferToAgent(Agent *newAgent) {
|
|
||||||
if (newAgent->getLinkedData() == NULL) {
|
|
||||||
newAgent->setLinkedData(new AudioRingBuffer(RING_BUFFER_SAMPLES, BUFFER_LENGTH_SAMPLES_PER_CHANNEL));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, const char* argv[]) {
|
|
||||||
setvbuf(stdout, NULL, _IOLBF, 0);
|
|
||||||
|
|
||||||
AgentList* agentList = AgentList::createInstance(AGENT_TYPE_AUDIO_MIXER, MIXER_LISTEN_PORT);
|
|
||||||
|
|
||||||
ssize_t receivedBytes = 0;
|
|
||||||
|
|
||||||
agentList->linkedDataCreateCallback = attachNewBufferToAgent;
|
|
||||||
|
|
||||||
agentList->startSilentAgentRemovalThread();
|
|
||||||
agentList->startDomainServerCheckInThread();
|
|
||||||
|
|
||||||
unsigned char *packetData = new unsigned char[MAX_PACKET_SIZE];
|
|
||||||
|
|
||||||
pthread_t sendBufferThread;
|
|
||||||
pthread_create(&sendBufferThread, NULL, sendBuffer, NULL);
|
|
||||||
|
|
||||||
sockaddr *agentAddress = new sockaddr;
|
|
||||||
|
|
||||||
while (true) {
|
|
||||||
if(agentList->getAgentSocket().receive(agentAddress, packetData, &receivedBytes)) {
|
|
||||||
if (packetData[0] == PACKET_HEADER_INJECT_AUDIO) {
|
if (packetData[0] == PACKET_HEADER_INJECT_AUDIO) {
|
||||||
|
|
||||||
if (agentList->addOrUpdateAgent(agentAddress, agentAddress, packetData[0], agentList->getLastAgentID())) {
|
if (agentList->addOrUpdateAgent(agentAddress, agentAddress, packetData[0], agentList->getLastAgentID())) {
|
||||||
|
@ -294,9 +275,15 @@ int main(int argc, const char* argv[]) {
|
||||||
agentList->updateAgentWithData(agentAddress, packetData, receivedBytes);
|
agentList->updateAgentWithData(agentAddress, packetData, receivedBytes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
double usecToSleep = usecTimestamp(&startTime) + (++nextFrame * BUFFER_SEND_INTERVAL_USECS) - usecTimestampNow();
|
||||||
|
|
||||||
|
if (usecToSleep > 0) {
|
||||||
|
usleep(usecToSleep);
|
||||||
|
} else {
|
||||||
|
std::cout << "Took too much time, not sleeping!\n";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pthread_join(sendBufferThread, NULL);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,118 +10,63 @@
|
||||||
#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),
|
_endOfLastWrite(NULL),
|
||||||
started(false),
|
_started(false),
|
||||||
_shouldBeAddedToMix(false),
|
_shouldBeAddedToMix(false),
|
||||||
_shouldLoopbackForAgent(false) {
|
_shouldLoopbackForAgent(false) {
|
||||||
buffer = new int16_t[ringBufferLengthSamples];
|
|
||||||
nextOutput = buffer;
|
_buffer = new int16_t[_ringBufferLengthSamples];
|
||||||
|
_nextOutput = _buffer;
|
||||||
};
|
};
|
||||||
|
|
||||||
AudioRingBuffer::AudioRingBuffer(const AudioRingBuffer &otherRingBuffer) {
|
AudioRingBuffer::AudioRingBuffer(const AudioRingBuffer &otherRingBuffer) {
|
||||||
ringBufferLengthSamples = otherRingBuffer.ringBufferLengthSamples;
|
_ringBufferLengthSamples = otherRingBuffer._ringBufferLengthSamples;
|
||||||
bufferLengthSamples = otherRingBuffer.bufferLengthSamples;
|
_bufferLengthSamples = otherRingBuffer._bufferLengthSamples;
|
||||||
started = otherRingBuffer.started;
|
_started = otherRingBuffer._started;
|
||||||
_shouldBeAddedToMix = otherRingBuffer._shouldBeAddedToMix;
|
_shouldBeAddedToMix = otherRingBuffer._shouldBeAddedToMix;
|
||||||
_shouldLoopbackForAgent = otherRingBuffer._shouldLoopbackForAgent;
|
_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);
|
||||||
|
|
||||||
nextOutput = buffer + (otherRingBuffer.nextOutput - otherRingBuffer.buffer);
|
_nextOutput = _buffer + (otherRingBuffer._nextOutput - otherRingBuffer._buffer);
|
||||||
endOfLastWrite = buffer + (otherRingBuffer.endOfLastWrite - otherRingBuffer.buffer);
|
_endOfLastWrite = _buffer + (otherRingBuffer._endOfLastWrite - otherRingBuffer._buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
AudioRingBuffer::~AudioRingBuffer() {
|
AudioRingBuffer::~AudioRingBuffer() {
|
||||||
delete[] buffer;
|
delete[] _buffer;
|
||||||
};
|
};
|
||||||
|
|
||||||
AudioRingBuffer* AudioRingBuffer::clone() const {
|
AudioRingBuffer* AudioRingBuffer::clone() const {
|
||||||
return new AudioRingBuffer(*this);
|
return new AudioRingBuffer(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
int16_t* AudioRingBuffer::getNextOutput() {
|
|
||||||
return nextOutput;
|
|
||||||
}
|
|
||||||
|
|
||||||
void AudioRingBuffer::setNextOutput(int16_t *newPointer) {
|
|
||||||
nextOutput = newPointer;
|
|
||||||
}
|
|
||||||
|
|
||||||
int16_t* AudioRingBuffer::getEndOfLastWrite() {
|
|
||||||
return endOfLastWrite;
|
|
||||||
}
|
|
||||||
|
|
||||||
void AudioRingBuffer::setEndOfLastWrite(int16_t *newPointer) {
|
|
||||||
endOfLastWrite = newPointer;
|
|
||||||
}
|
|
||||||
|
|
||||||
int16_t* AudioRingBuffer::getBuffer() {
|
|
||||||
return buffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool AudioRingBuffer::isStarted() {
|
|
||||||
return started;
|
|
||||||
}
|
|
||||||
|
|
||||||
void AudioRingBuffer::setStarted(bool status) {
|
|
||||||
started = status;
|
|
||||||
}
|
|
||||||
|
|
||||||
float* AudioRingBuffer::getPosition() {
|
|
||||||
return position;
|
|
||||||
}
|
|
||||||
|
|
||||||
void AudioRingBuffer::setPosition(float *newPosition) {
|
|
||||||
position[0] = newPosition[0];
|
|
||||||
position[1] = newPosition[1];
|
|
||||||
position[2] = newPosition[2];
|
|
||||||
}
|
|
||||||
|
|
||||||
float AudioRingBuffer::getAttenuationRatio() {
|
|
||||||
return attenuationRatio;
|
|
||||||
}
|
|
||||||
|
|
||||||
void AudioRingBuffer::setAttenuationRatio(float newAttenuation) {
|
|
||||||
attenuationRatio = newAttenuation;
|
|
||||||
}
|
|
||||||
|
|
||||||
float AudioRingBuffer::getBearing() {
|
|
||||||
return bearing;
|
|
||||||
}
|
|
||||||
|
|
||||||
void AudioRingBuffer::setBearing(float newBearing) {
|
|
||||||
bearing = newBearing;
|
|
||||||
}
|
|
||||||
|
|
||||||
const int AGENT_LOOPBACK_MODIFIER = 307;
|
const int AGENT_LOOPBACK_MODIFIER = 307;
|
||||||
|
|
||||||
int AudioRingBuffer::parseData(unsigned char* sourceBuffer, int numBytes) {
|
int AudioRingBuffer::parseData(unsigned char* sourceBuffer, int numBytes) {
|
||||||
if (numBytes > (bufferLengthSamples * sizeof(int16_t))) {
|
if (numBytes > (_bufferLengthSamples * sizeof(int16_t))) {
|
||||||
|
|
||||||
unsigned char *dataPtr = sourceBuffer + 1;
|
unsigned char *dataPtr = sourceBuffer + 1;
|
||||||
|
|
||||||
for (int p = 0; p < 3; p ++) {
|
memcpy(&_position, dataPtr, sizeof(_position));
|
||||||
memcpy(&position[p], dataPtr, sizeof(float));
|
dataPtr += (sizeof(_position));
|
||||||
dataPtr += sizeof(float);
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int attenuationByte = *(dataPtr++);
|
unsigned int attenuationByte = *(dataPtr++);
|
||||||
attenuationRatio = attenuationByte / 255.0f;
|
_attenuationRatio = attenuationByte / 255.0f;
|
||||||
|
|
||||||
memcpy(&bearing, dataPtr, sizeof(float));
|
memcpy(&_bearing, dataPtr, sizeof(float));
|
||||||
dataPtr += sizeof(bearing);
|
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)
|
||||||
_shouldLoopbackForAgent = true;
|
_shouldLoopbackForAgent = true;
|
||||||
|
|
||||||
// correct the bearing
|
// correct the bearing
|
||||||
bearing = bearing > 0
|
_bearing = _bearing > 0
|
||||||
? bearing - AGENT_LOOPBACK_MODIFIER
|
? _bearing - AGENT_LOOPBACK_MODIFIER
|
||||||
: bearing + AGENT_LOOPBACK_MODIFIER;
|
: _bearing + AGENT_LOOPBACK_MODIFIER;
|
||||||
} else {
|
} else {
|
||||||
_shouldLoopbackForAgent = false;
|
_shouldLoopbackForAgent = false;
|
||||||
}
|
}
|
||||||
|
@ -129,33 +74,33 @@ int AudioRingBuffer::parseData(unsigned char* sourceBuffer, int numBytes) {
|
||||||
sourceBuffer = dataPtr;
|
sourceBuffer = dataPtr;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (endOfLastWrite == NULL) {
|
if (!_endOfLastWrite) {
|
||||||
endOfLastWrite = buffer;
|
_endOfLastWrite = _buffer;
|
||||||
} else if (diffLastWriteNextOutput() > ringBufferLengthSamples - bufferLengthSamples) {
|
} else if (diffLastWriteNextOutput() > _ringBufferLengthSamples - _bufferLengthSamples) {
|
||||||
endOfLastWrite = buffer;
|
_endOfLastWrite = _buffer;
|
||||||
nextOutput = buffer;
|
_nextOutput = _buffer;
|
||||||
started = false;
|
_started = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(endOfLastWrite, sourceBuffer, bufferLengthSamples * sizeof(int16_t));
|
memcpy(_endOfLastWrite, sourceBuffer, _bufferLengthSamples * sizeof(int16_t));
|
||||||
|
|
||||||
endOfLastWrite += bufferLengthSamples;
|
_endOfLastWrite += _bufferLengthSamples;
|
||||||
|
|
||||||
if (endOfLastWrite >= buffer + ringBufferLengthSamples) {
|
if (_endOfLastWrite >= _buffer + _ringBufferLengthSamples) {
|
||||||
endOfLastWrite = buffer;
|
_endOfLastWrite = _buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
return numBytes;
|
return numBytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
short AudioRingBuffer::diffLastWriteNextOutput() {
|
short AudioRingBuffer::diffLastWriteNextOutput() {
|
||||||
if (endOfLastWrite == NULL) {
|
if (!_endOfLastWrite) {
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
short sampleDifference = endOfLastWrite - nextOutput;
|
short sampleDifference = _endOfLastWrite - _nextOutput;
|
||||||
|
|
||||||
if (sampleDifference < 0) {
|
if (sampleDifference < 0) {
|
||||||
sampleDifference += ringBufferLengthSamples;
|
sampleDifference += _ringBufferLengthSamples;
|
||||||
}
|
}
|
||||||
|
|
||||||
return sampleDifference;
|
return sampleDifference;
|
||||||
|
|
|
@ -12,6 +12,12 @@
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include "AgentData.h"
|
#include "AgentData.h"
|
||||||
|
|
||||||
|
struct Position {
|
||||||
|
float x;
|
||||||
|
float y;
|
||||||
|
float z;
|
||||||
|
};
|
||||||
|
|
||||||
class AudioRingBuffer : public AgentData {
|
class AudioRingBuffer : public AgentData {
|
||||||
public:
|
public:
|
||||||
AudioRingBuffer(int ringSamples, int bufferSamples);
|
AudioRingBuffer(int ringSamples, int bufferSamples);
|
||||||
|
@ -21,35 +27,36 @@ public:
|
||||||
int parseData(unsigned char* sourceBuffer, int numBytes);
|
int parseData(unsigned char* sourceBuffer, int numBytes);
|
||||||
AudioRingBuffer* clone() const;
|
AudioRingBuffer* clone() const;
|
||||||
|
|
||||||
int16_t* getNextOutput();
|
int16_t* getNextOutput() const { return _nextOutput; }
|
||||||
void setNextOutput(int16_t *newPointer);
|
void setNextOutput(int16_t* nextOutput) { _nextOutput = nextOutput; }
|
||||||
int16_t* getEndOfLastWrite();
|
|
||||||
void setEndOfLastWrite(int16_t *newPointer);
|
int16_t* getEndOfLastWrite() const { return _endOfLastWrite; }
|
||||||
int16_t* getBuffer();
|
void setEndOfLastWrite(int16_t* endOfLastWrite) { _endOfLastWrite = endOfLastWrite; }
|
||||||
bool isStarted();
|
|
||||||
void setStarted(bool status);
|
int16_t* getBuffer() const { return _buffer; }
|
||||||
|
|
||||||
|
bool isStarted() const { return _started; }
|
||||||
|
void setStarted(bool started) { _started = started; }
|
||||||
|
|
||||||
bool shouldBeAddedToMix() const { return _shouldBeAddedToMix; }
|
bool shouldBeAddedToMix() const { return _shouldBeAddedToMix; }
|
||||||
void setShouldBeAddedToMix(bool shouldBeAddedToMix) { _shouldBeAddedToMix = shouldBeAddedToMix; }
|
void setShouldBeAddedToMix(bool shouldBeAddedToMix) { _shouldBeAddedToMix = shouldBeAddedToMix; }
|
||||||
float* getPosition();
|
|
||||||
void setPosition(float newPosition[]);
|
|
||||||
float getAttenuationRatio();
|
|
||||||
void setAttenuationRatio(float newAttenuation);
|
|
||||||
float getBearing();
|
|
||||||
void setBearing(float newBearing);
|
|
||||||
|
|
||||||
|
const Position& getPosition() const { return _position; }
|
||||||
|
float getAttenuationRatio() const { return _attenuationRatio; }
|
||||||
|
float getBearing() const { return _bearing; }
|
||||||
bool shouldLoopbackForAgent() const { return _shouldLoopbackForAgent; }
|
bool shouldLoopbackForAgent() const { return _shouldLoopbackForAgent; }
|
||||||
|
|
||||||
short diffLastWriteNextOutput();
|
short diffLastWriteNextOutput();
|
||||||
private:
|
private:
|
||||||
int ringBufferLengthSamples;
|
int _ringBufferLengthSamples;
|
||||||
int bufferLengthSamples;
|
int _bufferLengthSamples;
|
||||||
float position[3];
|
Position _position;
|
||||||
float attenuationRatio;
|
float _attenuationRatio;
|
||||||
float bearing;
|
float _bearing;
|
||||||
int16_t *nextOutput;
|
int16_t* _nextOutput;
|
||||||
int16_t *endOfLastWrite;
|
int16_t* _endOfLastWrite;
|
||||||
int16_t *buffer;
|
int16_t* _buffer;
|
||||||
bool started;
|
bool _started;
|
||||||
bool _shouldBeAddedToMix;
|
bool _shouldBeAddedToMix;
|
||||||
bool _shouldLoopbackForAgent;
|
bool _shouldLoopbackForAgent;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue