From cd69297af236ef7b0085a0763330f4ef4bf0528c Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Thu, 9 May 2013 13:04:42 -0700 Subject: [PATCH] some refactoring for member variables in AudioRingBuffer --- audio-mixer/src/main.cpp | 20 ++-- libraries/shared/src/AudioRingBuffer.cpp | 117 ++++++----------------- libraries/shared/src/AudioRingBuffer.h | 47 +++++---- 3 files changed, 68 insertions(+), 116 deletions(-) diff --git a/audio-mixer/src/main.cpp b/audio-mixer/src/main.cpp index 3610efa7cc..88cb867df4 100644 --- a/audio-mixer/src/main.cpp +++ b/audio-mixer/src/main.cpp @@ -144,8 +144,8 @@ int main(int argc, const char* argv[]) { float weakChannelAmplitudeRatio = 1.f; if (otherAgent != agent) { - float *agentPosition = agentRingBuffer->getPosition(); - float *otherAgentPosition = otherAgentBuffer->getPosition(); + Position agentPosition = agentRingBuffer->getPosition(); + Position otherAgentPosition = otherAgentBuffer->getPosition(); // 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()); 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 distanceToAgent = sqrtf(powf(agentPosition.x - otherAgentPosition.x, 2) + + powf(agentPosition.y - otherAgentPosition.y, 2) + + powf(agentPosition.z - otherAgentPosition.z, 2)); float minCoefficient = std::min(1.0f, powf(0.5, @@ -166,20 +166,20 @@ int main(int argc, const char* argv[]) { // 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 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[0] > agentPosition[0]) { - if (otherAgentPosition[2] > agentPosition[2]) { + if (otherAgentPosition.x > agentPosition.x) { + if (otherAgentPosition.z > agentPosition.z) { absoluteAngleToSource = -90 + triangleAngle; } else { absoluteAngleToSource = -90 - triangleAngle; } } else { - if (otherAgentPosition[2] > agentPosition[2]) { + if (otherAgentPosition.z > agentPosition.z) { absoluteAngleToSource = 90 - triangleAngle; } else { absoluteAngleToSource = 90 + triangleAngle; diff --git a/libraries/shared/src/AudioRingBuffer.cpp b/libraries/shared/src/AudioRingBuffer.cpp index 072b085c5d..fb8ecdac86 100644 --- a/libraries/shared/src/AudioRingBuffer.cpp +++ b/libraries/shared/src/AudioRingBuffer.cpp @@ -12,90 +12,37 @@ AudioRingBuffer::AudioRingBuffer(int ringSamples, int bufferSamples) : _ringBufferLengthSamples(ringSamples), _bufferLengthSamples(bufferSamples), - endOfLastWrite(NULL), - started(false), + _endOfLastWrite(NULL), + _started(false), _shouldBeAddedToMix(false), _shouldLoopbackForAgent(false) { - buffer = new int16_t[_ringBufferLengthSamples]; - nextOutput = buffer; + + _buffer = new int16_t[_ringBufferLengthSamples]; + _nextOutput = _buffer; }; AudioRingBuffer::AudioRingBuffer(const AudioRingBuffer &otherRingBuffer) { _ringBufferLengthSamples = otherRingBuffer._ringBufferLengthSamples; _bufferLengthSamples = otherRingBuffer._bufferLengthSamples; - started = otherRingBuffer.started; + _started = otherRingBuffer._started; _shouldBeAddedToMix = otherRingBuffer._shouldBeAddedToMix; _shouldLoopbackForAgent = otherRingBuffer._shouldLoopbackForAgent; - buffer = new int16_t[_ringBufferLengthSamples]; - memcpy(buffer, otherRingBuffer.buffer, sizeof(int16_t) * _ringBufferLengthSamples); + _buffer = new int16_t[_ringBufferLengthSamples]; + memcpy(_buffer, otherRingBuffer._buffer, sizeof(int16_t) * _ringBufferLengthSamples); - nextOutput = buffer + (otherRingBuffer.nextOutput - otherRingBuffer.buffer); - endOfLastWrite = buffer + (otherRingBuffer.endOfLastWrite - otherRingBuffer.buffer); + _nextOutput = _buffer + (otherRingBuffer._nextOutput - otherRingBuffer._buffer); + _endOfLastWrite = _buffer + (otherRingBuffer._endOfLastWrite - otherRingBuffer._buffer); } AudioRingBuffer::~AudioRingBuffer() { - delete[] buffer; + delete[] _buffer; }; AudioRingBuffer* AudioRingBuffer::clone() const { 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; 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; - for (int p = 0; p < 3; p ++) { - memcpy(&position[p], dataPtr, sizeof(float)); - dataPtr += sizeof(float); - } + memcpy(&_position, dataPtr, sizeof(_position.x) * 3); + dataPtr += (sizeof(_position.x) * 3); unsigned int attenuationByte = *(dataPtr++); - attenuationRatio = attenuationByte / 255.0f; + _attenuationRatio = attenuationByte / 255.0f; - memcpy(&bearing, dataPtr, sizeof(float)); - dataPtr += sizeof(bearing); + memcpy(&_bearing, dataPtr, sizeof(float)); + 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) _shouldLoopbackForAgent = true; // correct the bearing - bearing = bearing > 0 - ? bearing - AGENT_LOOPBACK_MODIFIER - : bearing + AGENT_LOOPBACK_MODIFIER; + _bearing = _bearing > 0 + ? _bearing - AGENT_LOOPBACK_MODIFIER + : _bearing + AGENT_LOOPBACK_MODIFIER; } else { _shouldLoopbackForAgent = false; } @@ -129,30 +74,30 @@ int AudioRingBuffer::parseData(unsigned char* sourceBuffer, int numBytes) { sourceBuffer = dataPtr; } - if (endOfLastWrite == NULL) { - endOfLastWrite = buffer; + if (_endOfLastWrite == NULL) { + _endOfLastWrite = _buffer; } else if (diffLastWriteNextOutput() > _ringBufferLengthSamples - _bufferLengthSamples) { - endOfLastWrite = buffer; - nextOutput = buffer; - started = false; + _endOfLastWrite = _buffer; + _nextOutput = _buffer; + _started = false; } - memcpy(endOfLastWrite, sourceBuffer, _bufferLengthSamples * sizeof(int16_t)); + memcpy(_endOfLastWrite, sourceBuffer, _bufferLengthSamples * sizeof(int16_t)); - endOfLastWrite += _bufferLengthSamples; + _endOfLastWrite += _bufferLengthSamples; - if (endOfLastWrite >= buffer + _ringBufferLengthSamples) { - endOfLastWrite = buffer; + if (_endOfLastWrite >= _buffer + _ringBufferLengthSamples) { + _endOfLastWrite = _buffer; } return numBytes; } short AudioRingBuffer::diffLastWriteNextOutput() { - if (endOfLastWrite == NULL) { + if (_endOfLastWrite == NULL) { return 0; } else { - short sampleDifference = endOfLastWrite - nextOutput; + short sampleDifference = _endOfLastWrite - _nextOutput; if (sampleDifference < 0) { sampleDifference += _ringBufferLengthSamples; diff --git a/libraries/shared/src/AudioRingBuffer.h b/libraries/shared/src/AudioRingBuffer.h index d939c215ff..0e0e1ca8bc 100644 --- a/libraries/shared/src/AudioRingBuffer.h +++ b/libraries/shared/src/AudioRingBuffer.h @@ -12,6 +12,12 @@ #include #include "AgentData.h" +struct Position { + float x; + float y; + float z; +}; + class AudioRingBuffer : public AgentData { public: AudioRingBuffer(int ringSamples, int bufferSamples); @@ -21,35 +27,36 @@ public: int parseData(unsigned char* sourceBuffer, int numBytes); AudioRingBuffer* clone() const; - int16_t* getNextOutput(); - void setNextOutput(int16_t *newPointer); - int16_t* getEndOfLastWrite(); - void setEndOfLastWrite(int16_t *newPointer); - int16_t* getBuffer(); - bool isStarted(); - void setStarted(bool status); + int16_t* getNextOutput() const { return _nextOutput; } + void setNextOutput(int16_t *nextOutput) { _nextOutput = nextOutput; } + + int16_t* getEndOfLastWrite() const { return _endOfLastWrite; } + void setEndOfLastWrite(int16_t *endOfLastWrite) { _endOfLastWrite = endOfLastWrite; } + + int16_t* getBuffer() const { return _buffer; } + + bool isStarted() const { return _started; } + void setStarted(bool started) { _started = started; } + bool shouldBeAddedToMix() const { return _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; } short diffLastWriteNextOutput(); private: int _ringBufferLengthSamples; int _bufferLengthSamples; - float position[3]; - float attenuationRatio; - float bearing; - int16_t *nextOutput; - int16_t *endOfLastWrite; - int16_t *buffer; - bool started; + Position _position; + float _attenuationRatio; + float _bearing; + int16_t* _nextOutput; + int16_t* _endOfLastWrite; + int16_t* _buffer; + bool _started; bool _shouldBeAddedToMix; bool _shouldLoopbackForAgent; };