mirror of
https://github.com/Armored-Dragon/overte.git
synced 2025-03-11 16:13:16 +01:00
some refactoring for member variables in AudioRingBuffer
This commit is contained in:
parent
a8eb9187f1
commit
cd69297af2
3 changed files with 68 additions and 116 deletions
|
@ -144,8 +144,8 @@ int main(int argc, const char* argv[]) {
|
||||||
float weakChannelAmplitudeRatio = 1.f;
|
float weakChannelAmplitudeRatio = 1.f;
|
||||||
|
|
||||||
if (otherAgent != agent) {
|
if (otherAgent != agent) {
|
||||||
float *agentPosition = agentRingBuffer->getPosition();
|
Position agentPosition = agentRingBuffer->getPosition();
|
||||||
float *otherAgentPosition = otherAgentBuffer->getPosition();
|
Position otherAgentPosition = otherAgentBuffer->getPosition();
|
||||||
|
|
||||||
// calculate the distance to the other agent
|
// calculate the distance to the other agent
|
||||||
|
|
||||||
|
@ -154,9 +154,9 @@ int main(int argc, const char* argv[]) {
|
||||||
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.x - otherAgentPosition.x, 2) +
|
||||||
powf(agentPosition[1] - otherAgentPosition[1], 2) +
|
powf(agentPosition.y - otherAgentPosition.y, 2) +
|
||||||
powf(agentPosition[2] - otherAgentPosition[2], 2));
|
powf(agentPosition.z - otherAgentPosition.z, 2));
|
||||||
|
|
||||||
float minCoefficient = std::min(1.0f,
|
float minCoefficient = std::min(1.0f,
|
||||||
powf(0.5,
|
powf(0.5,
|
||||||
|
@ -166,20 +166,20 @@ int main(int argc, const char* argv[]) {
|
||||||
|
|
||||||
|
|
||||||
// get the angle from the right-angle triangle
|
// get the angle from the right-angle triangle
|
||||||
float triangleAngle = atan2f(fabsf(agentPosition[2] - otherAgentPosition[2]),
|
float triangleAngle = atan2f(fabsf(agentPosition.z - otherAgentPosition.z),
|
||||||
fabsf(agentPosition[0] - otherAgentPosition[0])) * (180 / M_PI);
|
fabsf(agentPosition.x - otherAgentPosition.x)) * (180 / M_PI);
|
||||||
float absoluteAngleToSource = 0;
|
float absoluteAngleToSource = 0;
|
||||||
bearingRelativeAngleToSource = 0;
|
bearingRelativeAngleToSource = 0;
|
||||||
|
|
||||||
// find the angle we need for calculation based on the orientation of the triangle
|
// find the angle we need for calculation based on the orientation of the triangle
|
||||||
if (otherAgentPosition[0] > agentPosition[0]) {
|
if (otherAgentPosition.x > agentPosition.x) {
|
||||||
if (otherAgentPosition[2] > agentPosition[2]) {
|
if (otherAgentPosition.z > agentPosition.z) {
|
||||||
absoluteAngleToSource = -90 + triangleAngle;
|
absoluteAngleToSource = -90 + triangleAngle;
|
||||||
} else {
|
} else {
|
||||||
absoluteAngleToSource = -90 - triangleAngle;
|
absoluteAngleToSource = -90 - triangleAngle;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (otherAgentPosition[2] > agentPosition[2]) {
|
if (otherAgentPosition.z > agentPosition.z) {
|
||||||
absoluteAngleToSource = 90 - triangleAngle;
|
absoluteAngleToSource = 90 - triangleAngle;
|
||||||
} else {
|
} else {
|
||||||
absoluteAngleToSource = 90 + triangleAngle;
|
absoluteAngleToSource = 90 + triangleAngle;
|
||||||
|
|
|
@ -12,90 +12,37 @@
|
||||||
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) {
|
||||||
|
@ -103,25 +50,23 @@ int AudioRingBuffer::parseData(unsigned char* sourceBuffer, int numBytes) {
|
||||||
|
|
||||||
unsigned char *dataPtr = sourceBuffer + 1;
|
unsigned char *dataPtr = sourceBuffer + 1;
|
||||||
|
|
||||||
for (int p = 0; p < 3; p ++) {
|
memcpy(&_position, dataPtr, sizeof(_position.x) * 3);
|
||||||
memcpy(&position[p], dataPtr, sizeof(float));
|
dataPtr += (sizeof(_position.x) * 3);
|
||||||
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,30 +74,30 @@ int AudioRingBuffer::parseData(unsigned char* sourceBuffer, int numBytes) {
|
||||||
sourceBuffer = dataPtr;
|
sourceBuffer = dataPtr;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (endOfLastWrite == NULL) {
|
if (_endOfLastWrite == NULL) {
|
||||||
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 == NULL) {
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
short sampleDifference = endOfLastWrite - nextOutput;
|
short sampleDifference = _endOfLastWrite - _nextOutput;
|
||||||
|
|
||||||
if (sampleDifference < 0) {
|
if (sampleDifference < 0) {
|
||||||
sampleDifference += _ringBufferLengthSamples;
|
sampleDifference += _ringBufferLengthSamples;
|
||||||
|
|
|
@ -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