From 76952a9d13508441c6064d6401384e5e40556eff Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Sun, 21 Jul 2013 16:24:23 -0700 Subject: [PATCH 01/14] testing --- libraries/shared/src/PacketHeaders.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libraries/shared/src/PacketHeaders.h b/libraries/shared/src/PacketHeaders.h index 041303f2b5..00c19e657a 100644 --- a/libraries/shared/src/PacketHeaders.h +++ b/libraries/shared/src/PacketHeaders.h @@ -34,6 +34,8 @@ const PACKET_TYPE PACKET_TYPE_ENVIRONMENT_DATA = 'e'; const PACKET_TYPE PACKET_TYPE_DOMAIN_LIST_REQUEST = 'L'; const PACKET_TYPE PACKET_TYPE_DOMAIN_REPORT_FOR_DUTY = 'C'; +// this is a test... + typedef char PACKET_VERSION; PACKET_VERSION versionForPacketType(PACKET_TYPE type); From c301b799c5bb215d1195e5d8cd90b8bae7bf3a94 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Mon, 22 Jul 2013 13:54:47 -0700 Subject: [PATCH 02/14] first cut at selecting audio sources --- audio-mixer/src/PositionalAudioRingBuffer.cpp | 49 +++++++++++- audio-mixer/src/PositionalAudioRingBuffer.h | 10 +++ interface/src/Application.cpp | 12 ++- interface/src/Audio.cpp | 76 ++++++++++++++++++- interface/src/Audio.h | 13 ++++ libraries/audio/src/AudioRingBuffer.h | 8 ++ libraries/shared/src/PacketHeaders.cpp | 6 ++ libraries/shared/src/PacketHeaders.h | 2 - 8 files changed, 171 insertions(+), 5 deletions(-) diff --git a/audio-mixer/src/PositionalAudioRingBuffer.cpp b/audio-mixer/src/PositionalAudioRingBuffer.cpp index 60ec9a425e..936b71fb79 100644 --- a/audio-mixer/src/PositionalAudioRingBuffer.cpp +++ b/audio-mixer/src/PositionalAudioRingBuffer.cpp @@ -16,19 +16,66 @@ PositionalAudioRingBuffer::PositionalAudioRingBuffer() : AudioRingBuffer(false), _position(0.0f, 0.0f, 0.0f), _orientation(0.0f, 0.0f, 0.0f, 0.0f), - _willBeAddedToMix(false) + _willBeAddedToMix(false), + _sourceID(-1), + _listenMode(AudioRingBuffer::NORMAL), + _listenRadius(0.0f), + _listenSourceCount(0), + _listenSources(NULL) { } +PositionalAudioRingBuffer::~PositionalAudioRingBuffer() { + if (_listenSources) { + delete[] _listenSources; + } +} + int PositionalAudioRingBuffer::parseData(unsigned char* sourceBuffer, int numBytes) { unsigned char* currentBuffer = sourceBuffer + numBytesForPacketHeader(sourceBuffer); + currentBuffer += parseSourceData(currentBuffer, numBytes - (currentBuffer - sourceBuffer)); + currentBuffer += parseListenModeData(currentBuffer, numBytes - (currentBuffer - sourceBuffer)); currentBuffer += parsePositionalData(currentBuffer, numBytes - (currentBuffer - sourceBuffer)); currentBuffer += parseAudioSamples(currentBuffer, numBytes - (currentBuffer - sourceBuffer)); return currentBuffer - sourceBuffer; } +int PositionalAudioRingBuffer::parseSourceData(unsigned char* sourceBuffer, int numBytes) { + unsigned char* currentBuffer = sourceBuffer; + + memcpy(&_sourceID, currentBuffer, sizeof(_sourceID)); + currentBuffer += sizeof(_sourceID); + + return currentBuffer - sourceBuffer; +} + +int PositionalAudioRingBuffer::parseListenModeData(unsigned char* sourceBuffer, int numBytes) { + unsigned char* currentBuffer = sourceBuffer; + + memcpy(&_listenMode, currentBuffer, sizeof(_listenMode)); + currentBuffer += sizeof(_listenMode); + + if (_listenMode == AudioRingBuffer::OMNI_DIRECTIONAL_POINT) { + memcpy(&_listenRadius, currentBuffer, sizeof(_listenRadius)); + currentBuffer += sizeof(_listenRadius); + } else if (_listenMode == AudioRingBuffer::SELECTED_SOURCES) { + memcpy(&_listenSourceCount, currentBuffer, sizeof(_listenSourceCount)); + currentBuffer += sizeof(_listenSourceCount); + + if (_listenSources) { + delete[] _listenSources; + } + _listenSources = new int[_listenSourceCount]; + memcpy(_listenSources, currentBuffer, sizeof(int) * _listenSourceCount); + currentBuffer += sizeof(int) * _listenSourceCount; + + } + + return currentBuffer - sourceBuffer; +} + int PositionalAudioRingBuffer::parsePositionalData(unsigned char* sourceBuffer, int numBytes) { unsigned char* currentBuffer = sourceBuffer; diff --git a/audio-mixer/src/PositionalAudioRingBuffer.h b/audio-mixer/src/PositionalAudioRingBuffer.h index 41e775e74a..9fd6921617 100644 --- a/audio-mixer/src/PositionalAudioRingBuffer.h +++ b/audio-mixer/src/PositionalAudioRingBuffer.h @@ -16,9 +16,12 @@ class PositionalAudioRingBuffer : public AudioRingBuffer { public: PositionalAudioRingBuffer(); + ~PositionalAudioRingBuffer(); int parseData(unsigned char* sourceBuffer, int numBytes); int parsePositionalData(unsigned char* sourceBuffer, int numBytes); + int parseSourceData(unsigned char* sourceBuffer, int numBytes); + int parseListenModeData(unsigned char* sourceBuffer, int numBytes); bool shouldBeAddedToMix(int numJitterBufferSamples); @@ -36,6 +39,13 @@ protected: glm::vec3 _position; glm::quat _orientation; bool _willBeAddedToMix; + + int _sourceID; + int _listenMode; + float _listenRadius; + int _listenSourceCount; + int* _listenSources; + }; #endif /* defined(__hifi__PositionalAudioRingBuffer__) */ diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 9571b44839..d13d69ffb9 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -3447,9 +3447,19 @@ void* Application::networkReceive(void* args) { case PACKET_TYPE_AVATAR_FACE_VIDEO: processAvatarFaceVideoMessage(app->_incomingPacket, bytesReceived); break; - default: + default: { NodeList::getInstance()->processNodeData(&senderAddress, app->_incomingPacket, bytesReceived); + + // Check to see if we have our ownerID + uint16_t ownerID = NodeList::getInstance()->getOwnerID(); + + if (ownerID != app->_audio.getSourceID()) { + app->_audio.setSourceID(ownerID); + } + + break; + } } } } else if (!app->_enableNetworkThread) { diff --git a/interface/src/Audio.cpp b/interface/src/Audio.cpp index 41090584f3..6bc2528bf0 100644 --- a/interface/src/Audio.cpp +++ b/interface/src/Audio.cpp @@ -120,6 +120,27 @@ inline void Audio::performIO(int16_t* inputLeft, int16_t* outputLeft, int16_t* o unsigned char* currentPacketPtr = dataPacket + populateTypeAndVersion(dataPacket, packetType); + // pack Source Data + memcpy(currentPacketPtr, &_sourceID, sizeof(_sourceID)); + currentPacketPtr += (sizeof(_sourceID)); + + // pack Listen Mode Data + memcpy(currentPacketPtr, &_listenMode, sizeof(_listenMode)); + currentPacketPtr += (sizeof(_listenMode)); + + if (_listenMode == AudioRingBuffer::OMNI_DIRECTIONAL_POINT) { + memcpy(currentPacketPtr, &_listenRadius, sizeof(_listenRadius)); + currentPacketPtr += (sizeof(_listenRadius)); + } else if (_listenMode == AudioRingBuffer::SELECTED_SOURCES) { + memcpy(currentPacketPtr, &_listenSourceCount, sizeof(_listenSourceCount)); + currentPacketPtr += (sizeof(_listenSourceCount)); + + if (_listenSources) { + memcpy(currentPacketPtr, &_listenSources, sizeof(int) * _listenSourceCount); + currentPacketPtr += (sizeof(int) * _listenSourceCount); + } + } + // memcpy the three float positions memcpy(currentPacketPtr, &headPosition, sizeof(headPosition)); currentPacketPtr += (sizeof(headPosition)); @@ -309,6 +330,49 @@ void Audio::reset() { _ringBuffer.reset(); } +void Audio::addListenSource(int sourceID) { + + // If we don't yet have a list of listen sources, make one + if (!_listenSources) { + _listenSources = new int[AudioRingBuffer::DEFAULT_LISTEN_LIST_SIZE]; + } + + // First check to see if the source is already in our list + for (int i = 0; i < _listenSourceCount; i++) { + if (_listenSources[i] == sourceID) { + return; // already in list + } + } + + // we know it's not in the list, check to see if we have room to add our source + if (_listenSourceCount + 1 < _listenSourcesArraySize) { + int* newList = new int[_listenSourcesArraySize + AudioRingBuffer::DEFAULT_LISTEN_LIST_SIZE]; + memmove(newList, _listenSources, _listenSourcesArraySize); + delete[] _listenSources; + _listenSources = newList; + _listenSourcesArraySize += AudioRingBuffer::DEFAULT_LISTEN_LIST_SIZE; + } + _listenSources[_listenSourceCount] = sourceID; + _listenSourceCount++; +} + +void Audio::removeListenSource(int sourceID) { + // If we don't yet have a list of listen sources, make one + if (_listenSources) { + // First check to see if the source is already in our list + for (int i = 0; i < _listenSourceCount; i++) { + if (_listenSources[i] == sourceID) { + + // found it, so, move the items forward in list + memmove(&_listenSources[i], &_listenSources[i+1], _listenSourceCount - i); + _listenSourceCount--; + return; + } + } + } +} + + Audio::Audio(Oscilloscope* scope, int16_t initialJitterBufferSamples) : _stream(NULL), _ringBuffer(true), @@ -338,7 +402,13 @@ Audio::Audio(Oscilloscope* scope, int16_t initialJitterBufferSamples) : _collisionSoundNoise(0.0f), _collisionSoundDuration(0.0f), _proceduralEffectSample(0), - _heartbeatMagnitude(0.0f) + _heartbeatMagnitude(0.0f), + _sourceID(UNKNOWN_NODE_ID), + _listenMode(AudioRingBuffer::NORMAL), + _listenRadius(0.0f), + _listenSourceCount(0), + _listenSourcesArraySize(0), + _listenSources(NULL) { outputPortAudioError(Pa_Initialize()); @@ -407,6 +477,10 @@ Audio::~Audio() { outputPortAudioError(Pa_Terminate()); } delete[] _echoSamplesLeft; + + if (_listenSources) { + delete[] _listenSources; + } } void Audio::addReceivedAudioToBuffer(unsigned char* receivedData, int receivedBytes) { diff --git a/interface/src/Audio.h b/interface/src/Audio.h index 37db447381..ca665c66f0 100644 --- a/interface/src/Audio.h +++ b/interface/src/Audio.h @@ -54,6 +54,12 @@ public: // The results of the analysis are written to the log. bool eventuallyAnalyzePing(); + int getSourceID() const { return _sourceID; }; + void setSourceID(int sourceID) { _sourceID = sourceID; }; + void setListenMode(int mode) { _listenMode = mode; }; + void setListenRadius(float radius) { _listenRadius = radius; }; + void addListenSource(int sourceID); + void removeListenSource(int sourceID); private: PaStream* _stream; @@ -90,6 +96,13 @@ private: float _collisionSoundDuration; int _proceduralEffectSample; float _heartbeatMagnitude; + + int _sourceID; + int _listenMode; + float _listenRadius; + int _listenSourceCount; + int _listenSourcesArraySize; + int* _listenSources; // Audio callback in class context. inline void performIO(int16_t* inputLeft, int16_t* outputLeft, int16_t* outputRight); diff --git a/libraries/audio/src/AudioRingBuffer.h b/libraries/audio/src/AudioRingBuffer.h index dea4a13fec..be87558010 100644 --- a/libraries/audio/src/AudioRingBuffer.h +++ b/libraries/audio/src/AudioRingBuffer.h @@ -27,6 +27,14 @@ const short RING_BUFFER_LENGTH_SAMPLES = RING_BUFFER_LENGTH_FRAMES * BUFFER_LENG class AudioRingBuffer : public NodeData { public: + + static int const DEFAULT_LISTEN_LIST_SIZE = 100; + enum { + NORMAL, + OMNI_DIRECTIONAL_POINT, + SELECTED_SOURCES + }; + AudioRingBuffer(bool isStereo); ~AudioRingBuffer(); diff --git a/libraries/shared/src/PacketHeaders.cpp b/libraries/shared/src/PacketHeaders.cpp index 821a2d0247..292c4bbc0a 100644 --- a/libraries/shared/src/PacketHeaders.cpp +++ b/libraries/shared/src/PacketHeaders.cpp @@ -14,6 +14,12 @@ PACKET_VERSION versionForPacketType(PACKET_TYPE type) { switch (type) { + + case PACKET_TYPE_MICROPHONE_AUDIO_NO_ECHO: + case PACKET_TYPE_MICROPHONE_AUDIO_WITH_ECHO: + return 1; + break; + case PACKET_TYPE_HEAD_DATA: return 2; break; diff --git a/libraries/shared/src/PacketHeaders.h b/libraries/shared/src/PacketHeaders.h index c62abb85e9..9652ac14e7 100644 --- a/libraries/shared/src/PacketHeaders.h +++ b/libraries/shared/src/PacketHeaders.h @@ -35,8 +35,6 @@ const PACKET_TYPE PACKET_TYPE_ENVIRONMENT_DATA = 'e'; const PACKET_TYPE PACKET_TYPE_DOMAIN_LIST_REQUEST = 'L'; const PACKET_TYPE PACKET_TYPE_DOMAIN_REPORT_FOR_DUTY = 'C'; -// this is a test... - typedef char PACKET_VERSION; PACKET_VERSION versionForPacketType(PACKET_TYPE type); From 0f1ac2b2267b86e033220f26c61865813b440fa2 Mon Sep 17 00:00:00 2001 From: Mark Peng Date: Mon, 22 Jul 2013 16:46:06 -0700 Subject: [PATCH 03/14] Fix eyePosition so that lookatVectors point directly to eyes, even when looking at someone from above. --- interface/src/Application.cpp | 9 ++++++--- interface/src/avatar/Head.cpp | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 9571b44839..0aee4a726c 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -1953,10 +1953,13 @@ bool Application::isLookingAtOtherAvatar(glm::vec3& mouseRayOrigin, glm::vec3& m void Application::renderLookatIndicator(glm::vec3 pointOfInterest, Camera& whichCamera) { const float DISTANCE_FROM_HEAD_SPHERE = 0.1f; + const float INDICATOR_RADIUS = 0.1f; const float YELLOW[] = { 1.0f, 1.0f, 0.0f }; + const int NUM_SEGMENTS = 30; glm::vec3 haloOrigin(pointOfInterest.x, pointOfInterest.y + DISTANCE_FROM_HEAD_SPHERE, pointOfInterest.z); glColor3f(YELLOW[0], YELLOW[1], YELLOW[2]); - renderCircle(haloOrigin, 0.1f, glm::vec3(0.0f, 1.0f, 0.0f), 30); + glm::vec3 normalToFloor(0.0f, 1.0f, 0.0f); + renderCircle(haloOrigin, INDICATOR_RADIUS, normalToFloor, NUM_SEGMENTS); } void Application::update(float deltaTime) { @@ -2003,7 +2006,7 @@ void Application::update(float deltaTime) { glm::vec3 front = orientation * IDENTITY_FRONT; glm::vec3 up = orientation * IDENTITY_UP; glm::vec3 towardVoxel = getMouseVoxelWorldCoordinates(_mouseVoxelDragging) - - _myAvatar.getCameraPosition(); // is this an error? getCameraPosition dne + - _myAvatar.getCameraPosition(); towardVoxel = front * glm::length(towardVoxel); glm::vec3 lateralToVoxel = glm::cross(up, glm::normalize(towardVoxel)) * glm::length(towardVoxel); _voxelThrust = glm::vec3(0, 0, 0); @@ -2276,7 +2279,7 @@ void Application::updateAvatar(float deltaTime) { // actually need to calculate the view frustum planes to send these details // to the server. loadViewFrustum(_myCamera, _viewFrustum); - _myAvatar.setCameraPosition(_viewFrustum.getPosition()); // setCameraPosition() dne + _myAvatar.setCameraPosition(_viewFrustum.getPosition()); _myAvatar.setCameraOrientation(_viewFrustum.getOrientation()); _myAvatar.setCameraFov(_viewFrustum.getFieldOfView()); _myAvatar.setCameraAspectRatio(_viewFrustum.getAspectRatio()); diff --git a/interface/src/avatar/Head.cpp b/interface/src/avatar/Head.cpp index b8393e26ab..76cf13a44d 100644 --- a/interface/src/avatar/Head.cpp +++ b/interface/src/avatar/Head.cpp @@ -272,7 +272,7 @@ void Head::calculateGeometry() { + up * _scale * BODY_BALL_RADIUS_HEAD_BASE * EYE_UP_OFFSET + front * _scale * BODY_BALL_RADIUS_HEAD_BASE * EYE_FRONT_OFFSET; - _eyeLevelPosition = _position + up * _scale * BODY_BALL_RADIUS_HEAD_BASE * EYE_UP_OFFSET; + _eyeLevelPosition = _rightEyePosition - right * _scale * BODY_BALL_RADIUS_HEAD_BASE * EYE_RIGHT_OFFSET; //calculate the eyebrow positions _leftEyeBrowPosition = _leftEyePosition; From 4e641eed2fd804a53111ef8216c42fc36dee8ab5 Mon Sep 17 00:00:00 2001 From: Mark Peng Date: Mon, 22 Jul 2013 17:11:11 -0700 Subject: [PATCH 04/14] LookatIndicator now scales with the avatar's scale. --- interface/src/Application.cpp | 6 ++++-- interface/src/Application.h | 1 + 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 0aee4a726c..c0fe2b360b 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -200,6 +200,7 @@ Application::Application(int& argc, char** argv, timeval &startup_time) : _mouseVoxelScale(1.0f / 1024.0f), _justEditedVoxel(false), _isLookingAtOtherAvatar(false), + _lookatIndicatorScale(1.0f), _paintOn(false), _dominantColor(0), _perfStatsOn(false), @@ -1942,6 +1943,7 @@ bool Application::isLookingAtOtherAvatar(glm::vec3& mouseRayOrigin, glm::vec3& m glm::vec3 headPosition = avatar->getHead().getPosition(); if (rayIntersectsSphere(mouseRayOrigin, mouseRayDirection, headPosition, HEAD_SPHERE_RADIUS)) { eyePosition = avatar->getHead().getEyeLevelPosition(); + _lookatIndicatorScale = avatar->getScale(); _lookatOtherPosition = headPosition; return true; } @@ -1952,8 +1954,8 @@ bool Application::isLookingAtOtherAvatar(glm::vec3& mouseRayOrigin, glm::vec3& m void Application::renderLookatIndicator(glm::vec3 pointOfInterest, Camera& whichCamera) { - const float DISTANCE_FROM_HEAD_SPHERE = 0.1f; - const float INDICATOR_RADIUS = 0.1f; + const float DISTANCE_FROM_HEAD_SPHERE = 0.1f * _lookatIndicatorScale; + const float INDICATOR_RADIUS = 0.1f * _lookatIndicatorScale; const float YELLOW[] = { 1.0f, 1.0f, 0.0f }; const int NUM_SEGMENTS = 30; glm::vec3 haloOrigin(pointOfInterest.x, pointOfInterest.y + DISTANCE_FROM_HEAD_SPHERE, pointOfInterest.z); diff --git a/interface/src/Application.h b/interface/src/Application.h index d29328c0b5..85c8334b3a 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -373,6 +373,7 @@ private: bool _isLookingAtOtherAvatar; glm::vec3 _lookatOtherPosition; + float _lookatIndicatorScale; bool _paintOn; // Whether to paint voxels as you fly around unsigned char _dominantColor; // The dominant color of the voxel we're painting From c969570e8c8fb64c2f86ed6e4c3bfe105c64b979 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Mon, 22 Jul 2013 23:05:42 -0700 Subject: [PATCH 05/14] working on selected audio --- audio-mixer/src/PositionalAudioRingBuffer.cpp | 32 +- audio-mixer/src/PositionalAudioRingBuffer.h | 4 + audio-mixer/src/main.cpp | 281 +++++++++--------- interface/src/Application.cpp | 36 +++ interface/src/Application.h | 4 + interface/src/Audio.cpp | 16 +- interface/src/Audio.h | 1 + interface/src/avatar/Avatar.h | 4 + 8 files changed, 233 insertions(+), 145 deletions(-) diff --git a/audio-mixer/src/PositionalAudioRingBuffer.cpp b/audio-mixer/src/PositionalAudioRingBuffer.cpp index 936b71fb79..26961c76e8 100644 --- a/audio-mixer/src/PositionalAudioRingBuffer.cpp +++ b/audio-mixer/src/PositionalAudioRingBuffer.cpp @@ -32,6 +32,32 @@ PositionalAudioRingBuffer::~PositionalAudioRingBuffer() { } } +bool PositionalAudioRingBuffer::isListeningToSource(PositionalAudioRingBuffer* other) { + switch (_listenMode) { + default: + case AudioRingBuffer::NORMAL: + return true; + break; + + case AudioRingBuffer::OMNI_DIRECTIONAL_POINT: { + float distance = glm::distance(_position, other->_position); + return distance <= _listenRadius; + break; + } + case AudioRingBuffer::SELECTED_SOURCES: + if (_listenSources) { + for (int i = 0; i < _listenSourceCount; i++) { + if (other->_sourceID == _listenSources[i]) { + return true; + } + } + } + return false; + break; + } +} + + int PositionalAudioRingBuffer::parseData(unsigned char* sourceBuffer, int numBytes) { unsigned char* currentBuffer = sourceBuffer + numBytesForPacketHeader(sourceBuffer); currentBuffer += parseSourceData(currentBuffer, numBytes - (currentBuffer - sourceBuffer)); @@ -56,21 +82,19 @@ int PositionalAudioRingBuffer::parseListenModeData(unsigned char* sourceBuffer, memcpy(&_listenMode, currentBuffer, sizeof(_listenMode)); currentBuffer += sizeof(_listenMode); - + if (_listenMode == AudioRingBuffer::OMNI_DIRECTIONAL_POINT) { memcpy(&_listenRadius, currentBuffer, sizeof(_listenRadius)); currentBuffer += sizeof(_listenRadius); } else if (_listenMode == AudioRingBuffer::SELECTED_SOURCES) { memcpy(&_listenSourceCount, currentBuffer, sizeof(_listenSourceCount)); currentBuffer += sizeof(_listenSourceCount); - if (_listenSources) { delete[] _listenSources; } _listenSources = new int[_listenSourceCount]; memcpy(_listenSources, currentBuffer, sizeof(int) * _listenSourceCount); currentBuffer += sizeof(int) * _listenSourceCount; - } return currentBuffer - sourceBuffer; @@ -81,7 +105,7 @@ int PositionalAudioRingBuffer::parsePositionalData(unsigned char* sourceBuffer, memcpy(&_position, currentBuffer, sizeof(_position)); currentBuffer += sizeof(_position); - + memcpy(&_orientation, currentBuffer, sizeof(_orientation)); currentBuffer += sizeof(_orientation); diff --git a/audio-mixer/src/PositionalAudioRingBuffer.h b/audio-mixer/src/PositionalAudioRingBuffer.h index 9fd6921617..eb6f0414e7 100644 --- a/audio-mixer/src/PositionalAudioRingBuffer.h +++ b/audio-mixer/src/PositionalAudioRingBuffer.h @@ -30,6 +30,10 @@ public: const glm::vec3& getPosition() const { return _position; } const glm::quat& getOrientation() const { return _orientation; } + + bool isListeningToSource(PositionalAudioRingBuffer* other); + int getSourceID() const { return _sourceID; } + int getListeningMode() const { return _listenMode; } protected: // disallow copying of PositionalAudioRingBuffer objects diff --git a/audio-mixer/src/main.cpp b/audio-mixer/src/main.cpp index 239eb7a32d..2f17113ba4 100644 --- a/audio-mixer/src/main.cpp +++ b/audio-mixer/src/main.cpp @@ -159,168 +159,174 @@ int main(int argc, const char* argv[]) { // zero out the client mix for this node memset(clientSamples, 0, sizeof(clientSamples)); + // loop through all other nodes that have sufficient audio to mix for (NodeList::iterator otherNode = nodeList->begin(); otherNode != nodeList->end(); otherNode++) { + if (((PositionalAudioRingBuffer*) otherNode->getLinkedData())->willBeAddedToMix() && (otherNode != node || (otherNode == node && nodeRingBuffer->shouldLoopbackForNode()))) { PositionalAudioRingBuffer* otherNodeBuffer = (PositionalAudioRingBuffer*) otherNode->getLinkedData(); + + // based on our listen mode we will do this mixing... + if (nodeRingBuffer->isListeningToSource(otherNodeBuffer)) { + float bearingRelativeAngleToSource = 0.0f; + float attenuationCoefficient = 1.0f; + int numSamplesDelay = 0; + float weakChannelAmplitudeRatio = 1.0f; - float bearingRelativeAngleToSource = 0.0f; - float attenuationCoefficient = 1.0f; - int numSamplesDelay = 0; - float weakChannelAmplitudeRatio = 1.0f; + stk::TwoPole* otherNodeTwoPole = NULL; - stk::TwoPole* otherNodeTwoPole = NULL; - - if (otherNode != node) { + // only do axis/distance attenuation when in normal mode + if (otherNode != node && nodeRingBuffer->getListeningMode() == AudioRingBuffer::NORMAL) { - glm::vec3 listenerPosition = nodeRingBuffer->getPosition(); - glm::vec3 relativePosition = otherNodeBuffer->getPosition() - nodeRingBuffer->getPosition(); - glm::quat inverseOrientation = glm::inverse(nodeRingBuffer->getOrientation()); + glm::vec3 listenerPosition = nodeRingBuffer->getPosition(); + glm::vec3 relativePosition = otherNodeBuffer->getPosition() - nodeRingBuffer->getPosition(); + glm::quat inverseOrientation = glm::inverse(nodeRingBuffer->getOrientation()); - float distanceSquareToSource = glm::dot(relativePosition, relativePosition); - float radius = 0.0f; + float distanceSquareToSource = glm::dot(relativePosition, relativePosition); + float radius = 0.0f; - if (otherNode->getType() == NODE_TYPE_AUDIO_INJECTOR) { - InjectedAudioRingBuffer* injectedBuffer = (InjectedAudioRingBuffer*) otherNodeBuffer; - radius = injectedBuffer->getRadius(); - attenuationCoefficient *= injectedBuffer->getAttenuationRatio(); - } + if (otherNode->getType() == NODE_TYPE_AUDIO_INJECTOR) { + InjectedAudioRingBuffer* injectedBuffer = (InjectedAudioRingBuffer*) otherNodeBuffer; + radius = injectedBuffer->getRadius(); + attenuationCoefficient *= injectedBuffer->getAttenuationRatio(); + } - if (radius == 0 || (distanceSquareToSource > radius * radius)) { - // this is either not a spherical source, or the listener is outside the sphere + if (radius == 0 || (distanceSquareToSource > radius * radius)) { + // this is either not a spherical source, or the listener is outside the sphere - if (radius > 0) { - // this is a spherical source - the distance used for the coefficient - // needs to be the closest point on the boundary to the source + if (radius > 0) { + // this is a spherical source - the distance used for the coefficient + // needs to be the closest point on the boundary to the source - // ovveride the distance to the node with the distance to the point on the - // boundary of the sphere - distanceSquareToSource -= (radius * radius); + // ovveride the distance to the node with the distance to the point on the + // boundary of the sphere + distanceSquareToSource -= (radius * radius); - } else { - // calculate the angle delivery for off-axis attenuation - glm::vec3 rotatedListenerPosition = glm::inverse(otherNodeBuffer->getOrientation()) - * relativePosition; + } else { + // calculate the angle delivery for off-axis attenuation + glm::vec3 rotatedListenerPosition = glm::inverse(otherNodeBuffer->getOrientation()) + * relativePosition; - float angleOfDelivery = glm::angle(glm::vec3(0.0f, 0.0f, -1.0f), - glm::normalize(rotatedListenerPosition)); + float angleOfDelivery = glm::angle(glm::vec3(0.0f, 0.0f, -1.0f), + glm::normalize(rotatedListenerPosition)); - const float MAX_OFF_AXIS_ATTENUATION = 0.2f; - const float OFF_AXIS_ATTENUATION_FORMULA_STEP = (1 - MAX_OFF_AXIS_ATTENUATION) / 2.0f; + const float MAX_OFF_AXIS_ATTENUATION = 0.2f; + const float OFF_AXIS_ATTENUATION_FORMULA_STEP = (1 - MAX_OFF_AXIS_ATTENUATION) / 2.0f; - float offAxisCoefficient = MAX_OFF_AXIS_ATTENUATION + - (OFF_AXIS_ATTENUATION_FORMULA_STEP * (angleOfDelivery / 90.0f)); + float offAxisCoefficient = MAX_OFF_AXIS_ATTENUATION + + (OFF_AXIS_ATTENUATION_FORMULA_STEP * (angleOfDelivery / 90.0f)); - // multiply the current attenuation coefficient by the calculated off axis coefficient - attenuationCoefficient *= offAxisCoefficient; + // multiply the current attenuation coefficient by the calculated off axis coefficient + attenuationCoefficient *= offAxisCoefficient; + } + + glm::vec3 rotatedSourcePosition = inverseOrientation * relativePosition; + + const float DISTANCE_SCALE = 2.5f; + const float GEOMETRIC_AMPLITUDE_SCALAR = 0.3f; + const float DISTANCE_LOG_BASE = 2.5f; + const float DISTANCE_SCALE_LOG = logf(DISTANCE_SCALE) / logf(DISTANCE_LOG_BASE); + + // calculate the distance coefficient using the distance to this node + float distanceCoefficient = powf(GEOMETRIC_AMPLITUDE_SCALAR, + DISTANCE_SCALE_LOG + + (0.5f * logf(distanceSquareToSource) / logf(DISTANCE_LOG_BASE)) - 1); + distanceCoefficient = std::min(1.0f, distanceCoefficient); + + // multiply the current attenuation coefficient by the distance coefficient + attenuationCoefficient *= distanceCoefficient; + + // project the rotated source position vector onto the XZ plane + rotatedSourcePosition.y = 0.0f; + + // produce an oriented angle about the y-axis + bearingRelativeAngleToSource = glm::orientedAngle(glm::vec3(0.0f, 0.0f, -1.0f), + glm::normalize(rotatedSourcePosition), + glm::vec3(0.0f, 1.0f, 0.0f)); + + const float PHASE_AMPLITUDE_RATIO_AT_90 = 0.5; + + // figure out the number of samples of delay and the ratio of the amplitude + // in the weak channel for audio spatialization + float sinRatio = fabsf(sinf(glm::radians(bearingRelativeAngleToSource))); + numSamplesDelay = PHASE_DELAY_AT_90 * sinRatio; + weakChannelAmplitudeRatio = 1 - (PHASE_AMPLITUDE_RATIO_AT_90 * sinRatio); + + // grab the TwoPole object for this source, add it if it doesn't exist + TwoPoleNodeMap& nodeTwoPoles = nodeRingBuffer->getTwoPoles(); + TwoPoleNodeMap::iterator twoPoleIterator = nodeTwoPoles.find(otherNode->getNodeID()); + + if (twoPoleIterator == nodeTwoPoles.end()) { + // setup the freeVerb effect for this source for this client + otherNodeTwoPole = nodeTwoPoles[otherNode->getNodeID()] = new stk::TwoPole; + } else { + otherNodeTwoPole = twoPoleIterator->second; + } + + // calculate the reasonance for this TwoPole based on angle to source + float TWO_POLE_CUT_OFF_FREQUENCY = 800.0f; + float TWO_POLE_MAX_FILTER_STRENGTH = 0.4f; + + otherNodeTwoPole->setResonance(TWO_POLE_CUT_OFF_FREQUENCY, + TWO_POLE_MAX_FILTER_STRENGTH + * fabsf(bearingRelativeAngleToSource) / 180.0f, + true); } + } + + int16_t* sourceBuffer = otherNodeBuffer->getNextOutput(); + + int16_t* goodChannel = (bearingRelativeAngleToSource > 0.0f) + ? clientSamples + : clientSamples + BUFFER_LENGTH_SAMPLES_PER_CHANNEL; + int16_t* delayedChannel = (bearingRelativeAngleToSource > 0.0f) + ? clientSamples + BUFFER_LENGTH_SAMPLES_PER_CHANNEL + : clientSamples; + + int16_t* delaySamplePointer = otherNodeBuffer->getNextOutput() == otherNodeBuffer->getBuffer() + ? otherNodeBuffer->getBuffer() + RING_BUFFER_LENGTH_SAMPLES - numSamplesDelay + : otherNodeBuffer->getNextOutput() - numSamplesDelay; + + for (int s = 0; s < BUFFER_LENGTH_SAMPLES_PER_CHANNEL; s++) { + // load up the stkFrameBuffer with this source's samples + stkFrameBuffer[s] = (stk::StkFloat) sourceBuffer[s]; + } + + // perform the TwoPole effect on the stkFrameBuffer + if (otherNodeTwoPole) { + otherNodeTwoPole->tick(stkFrameBuffer); + } + + 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] * attenuationCoefficient * weakChannelAmplitudeRatio; - glm::vec3 rotatedSourcePosition = inverseOrientation * relativePosition; - - const float DISTANCE_SCALE = 2.5f; - const float GEOMETRIC_AMPLITUDE_SCALAR = 0.3f; - const float DISTANCE_LOG_BASE = 2.5f; - const float DISTANCE_SCALE_LOG = logf(DISTANCE_SCALE) / logf(DISTANCE_LOG_BASE); - - // calculate the distance coefficient using the distance to this node - float distanceCoefficient = powf(GEOMETRIC_AMPLITUDE_SCALAR, - DISTANCE_SCALE_LOG + - (0.5f * logf(distanceSquareToSource) / logf(DISTANCE_LOG_BASE)) - 1); - distanceCoefficient = std::min(1.0f, distanceCoefficient); - - // multiply the current attenuation coefficient by the distance coefficient - attenuationCoefficient *= distanceCoefficient; - - // project the rotated source position vector onto the XZ plane - rotatedSourcePosition.y = 0.0f; - - // produce an oriented angle about the y-axis - bearingRelativeAngleToSource = glm::orientedAngle(glm::vec3(0.0f, 0.0f, -1.0f), - glm::normalize(rotatedSourcePosition), - glm::vec3(0.0f, 1.0f, 0.0f)); - - const float PHASE_AMPLITUDE_RATIO_AT_90 = 0.5; - - // figure out the number of samples of delay and the ratio of the amplitude - // in the weak channel for audio spatialization - float sinRatio = fabsf(sinf(glm::radians(bearingRelativeAngleToSource))); - numSamplesDelay = PHASE_DELAY_AT_90 * sinRatio; - weakChannelAmplitudeRatio = 1 - (PHASE_AMPLITUDE_RATIO_AT_90 * sinRatio); - - // grab the TwoPole object for this source, add it if it doesn't exist - TwoPoleNodeMap& nodeTwoPoles = nodeRingBuffer->getTwoPoles(); - TwoPoleNodeMap::iterator twoPoleIterator = nodeTwoPoles.find(otherNode->getNodeID()); - - if (twoPoleIterator == nodeTwoPoles.end()) { - // setup the freeVerb effect for this source for this client - otherNodeTwoPole = nodeTwoPoles[otherNode->getNodeID()] = new stk::TwoPole; - } else { - otherNodeTwoPole = twoPoleIterator->second; + delayedChannel[s] = glm::clamp(delayedChannel[s] + earlierSample, + MIN_SAMPLE_VALUE, + MAX_SAMPLE_VALUE); } - - // calculate the reasonance for this TwoPole based on angle to source - float TWO_POLE_CUT_OFF_FREQUENCY = 800.0f; - float TWO_POLE_MAX_FILTER_STRENGTH = 0.4f; - - otherNodeTwoPole->setResonance(TWO_POLE_CUT_OFF_FREQUENCY, - TWO_POLE_MAX_FILTER_STRENGTH - * fabsf(bearingRelativeAngleToSource) / 180.0f, - true); - } - } - - int16_t* sourceBuffer = otherNodeBuffer->getNextOutput(); - - int16_t* goodChannel = (bearingRelativeAngleToSource > 0.0f) - ? clientSamples - : clientSamples + BUFFER_LENGTH_SAMPLES_PER_CHANNEL; - int16_t* delayedChannel = (bearingRelativeAngleToSource > 0.0f) - ? clientSamples + BUFFER_LENGTH_SAMPLES_PER_CHANNEL - : clientSamples; - - int16_t* delaySamplePointer = otherNodeBuffer->getNextOutput() == otherNodeBuffer->getBuffer() - ? otherNodeBuffer->getBuffer() + RING_BUFFER_LENGTH_SAMPLES - numSamplesDelay - : otherNodeBuffer->getNextOutput() - numSamplesDelay; - - for (int s = 0; s < BUFFER_LENGTH_SAMPLES_PER_CHANNEL; s++) { - // load up the stkFrameBuffer with this source's samples - stkFrameBuffer[s] = (stk::StkFloat) sourceBuffer[s]; - } - - // perform the TwoPole effect on the stkFrameBuffer - if (otherNodeTwoPole) { - otherNodeTwoPole->tick(stkFrameBuffer); - } - - 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] * attenuationCoefficient * weakChannelAmplitudeRatio; - - delayedChannel[s] = glm::clamp(delayedChannel[s] + earlierSample, - MIN_SAMPLE_VALUE, - MAX_SAMPLE_VALUE); - } - int16_t currentSample = stkFrameBuffer[s] * attenuationCoefficient; + int16_t currentSample = stkFrameBuffer[s] * attenuationCoefficient; - goodChannel[s] = glm::clamp(goodChannel[s] + currentSample, - MIN_SAMPLE_VALUE, - MAX_SAMPLE_VALUE); + goodChannel[s] = glm::clamp(goodChannel[s] + currentSample, + MIN_SAMPLE_VALUE, + MAX_SAMPLE_VALUE); - if (s + numSamplesDelay < BUFFER_LENGTH_SAMPLES_PER_CHANNEL) { - int sumSample = delayedChannel[s + numSamplesDelay] - + (currentSample * weakChannelAmplitudeRatio); - delayedChannel[s + numSamplesDelay] = glm::clamp(sumSample, - MIN_SAMPLE_VALUE, - MAX_SAMPLE_VALUE); - } + if (s + numSamplesDelay < BUFFER_LENGTH_SAMPLES_PER_CHANNEL) { + int sumSample = delayedChannel[s + numSamplesDelay] + + (currentSample * weakChannelAmplitudeRatio); + delayedChannel[s + numSamplesDelay] = glm::clamp(sumSample, + MIN_SAMPLE_VALUE, + MAX_SAMPLE_VALUE); + } - if (s >= BUFFER_LENGTH_SAMPLES_PER_CHANNEL - PHASE_DELAY_AT_90) { - // this could be a delayed sample on the next pass - // so store the affected back in the ARB - otherNodeBuffer->getNextOutput()[s] = (int16_t) stkFrameBuffer[s]; + if (s >= BUFFER_LENGTH_SAMPLES_PER_CHANNEL - PHASE_DELAY_AT_90) { + // this could be a delayed sample on the next pass + // so store the affected back in the ARB + otherNodeBuffer->getNextOutput()[s] = (int16_t) stkFrameBuffer[s]; + } } } } @@ -340,7 +346,6 @@ int main(int argc, const char* argv[]) { if (nodeBuffer->getNextOutput() >= nodeBuffer->getBuffer() + RING_BUFFER_LENGTH_SAMPLES) { nodeBuffer->setNextOutput(nodeBuffer->getBuffer()); } - nodeBuffer->setWillBeAddedToMix(false); } } diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index d13d69ffb9..c1aa032542 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -1835,6 +1835,11 @@ void Application::initMenu() { (_simulateLeapHand = debugMenu->addAction("Simulate Leap Hand"))->setCheckable(true); (_testRaveGlove = debugMenu->addAction("Test RaveGlove"))->setCheckable(true); + QMenu* audioDebugMenu = debugMenu->addMenu("Audio Debugging Tools"); + audioDebugMenu->addAction("Listen Mode Normal", this, SLOT(setListenModeNormal()), Qt::CTRL | Qt::Key_1); + audioDebugMenu->addAction("Listen Mode Point/Radius", this, SLOT(setListenModePoint()), Qt::CTRL | Qt::Key_2); + audioDebugMenu->addAction("Listen Mode Single Source", this, SLOT(setListenModeSingleSource()), Qt::CTRL | Qt::Key_3); + QMenu* settingsMenu = menuBar->addMenu("Settings"); (_settingsAutosave = settingsMenu->addAction("Autosave"))->setCheckable(true); _settingsAutosave->setChecked(true); @@ -1846,6 +1851,37 @@ void Application::initMenu() { _networkAccessManager = new QNetworkAccessManager(this); } +void Application::setListenModeNormal() { + _audio.setListenMode(AudioRingBuffer::NORMAL); +} + +void Application::setListenModePoint() { + _audio.setListenMode(AudioRingBuffer::OMNI_DIRECTIONAL_POINT); + _audio.setListenRadius(1.0); +} + +void Application::setListenModeSingleSource() { + _audio.setListenMode(AudioRingBuffer::SELECTED_SOURCES); + _audio.clearListenSources(); + + NodeList* nodeList = NodeList::getInstance(); + for (NodeList::iterator node = nodeList->begin(); node != nodeList->end(); node++) { + if (node->getLinkedData() != NULL && node->getType() == NODE_TYPE_AGENT) { + Avatar* avatar = (Avatar *) node->getLinkedData(); + glm::vec3 headPosition = avatar->getHead().getPosition(); + glm::vec3 mouseRayOrigin = _myAvatar.getMouseRayOrigin(); + glm::vec3 mouseRayDirection = _myAvatar.getMouseRayDirection(); + const float HEAD_SPHERE_RADIUS = 0.07; + + if (rayIntersectsSphere(mouseRayOrigin, mouseRayDirection, headPosition, HEAD_SPHERE_RADIUS)) { + int sourceID = avatar->getOwningNode()->getNodeID(); + _audio.addListenSource(sourceID); + } + } + } +} + + void Application::updateFrustumRenderModeAction() { switch (_frustumDrawingMode) { default: diff --git a/interface/src/Application.h b/interface/src/Application.h index d29328c0b5..d462e88b98 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -170,6 +170,10 @@ private slots: void copyVoxels(); void pasteVoxels(); void runTests(); + void setListenModeNormal(); + void setListenModePoint(); + void setListenModeSingleSource(); + void renderCoverageMap(); void renderCoverageMapsRecursively(CoverageMap* map); diff --git a/interface/src/Audio.cpp b/interface/src/Audio.cpp index 6bc2528bf0..9119d3984f 100644 --- a/interface/src/Audio.cpp +++ b/interface/src/Audio.cpp @@ -112,7 +112,7 @@ inline void Audio::performIO(int16_t* inputLeft, int16_t* outputLeft, int16_t* o // we need the amount of bytes in the buffer + 1 for type // + 12 for 3 floats for position + float for bearing + 1 attenuation byte - unsigned char dataPacket[BUFFER_LENGTH_BYTES_PER_CHANNEL + leadingBytes]; + unsigned char dataPacket[MAX_PACKET_SIZE]; PACKET_TYPE packetType = (Application::getInstance()->shouldEchoAudio()) ? PACKET_TYPE_MICROPHONE_AUDIO_WITH_ECHO @@ -123,21 +123,25 @@ inline void Audio::performIO(int16_t* inputLeft, int16_t* outputLeft, int16_t* o // pack Source Data memcpy(currentPacketPtr, &_sourceID, sizeof(_sourceID)); currentPacketPtr += (sizeof(_sourceID)); + leadingBytes += (sizeof(_sourceID)); // pack Listen Mode Data memcpy(currentPacketPtr, &_listenMode, sizeof(_listenMode)); currentPacketPtr += (sizeof(_listenMode)); + leadingBytes += (sizeof(_listenMode)); if (_listenMode == AudioRingBuffer::OMNI_DIRECTIONAL_POINT) { memcpy(currentPacketPtr, &_listenRadius, sizeof(_listenRadius)); currentPacketPtr += (sizeof(_listenRadius)); + leadingBytes += (sizeof(_listenRadius)); } else if (_listenMode == AudioRingBuffer::SELECTED_SOURCES) { memcpy(currentPacketPtr, &_listenSourceCount, sizeof(_listenSourceCount)); currentPacketPtr += (sizeof(_listenSourceCount)); - + leadingBytes += (sizeof(_listenSourceCount)); if (_listenSources) { - memcpy(currentPacketPtr, &_listenSources, sizeof(int) * _listenSourceCount); + memcpy(currentPacketPtr, _listenSources, sizeof(int) * _listenSourceCount); currentPacketPtr += (sizeof(int) * _listenSourceCount); + leadingBytes += (sizeof(int) * _listenSourceCount); } } @@ -356,6 +360,12 @@ void Audio::addListenSource(int sourceID) { _listenSourceCount++; } +void Audio::clearListenSources() { + delete[] _listenSources; + _listenSources = NULL; + _listenSourceCount = 0; +} + void Audio::removeListenSource(int sourceID) { // If we don't yet have a list of listen sources, make one if (_listenSources) { diff --git a/interface/src/Audio.h b/interface/src/Audio.h index ca665c66f0..facc6a1caf 100644 --- a/interface/src/Audio.h +++ b/interface/src/Audio.h @@ -60,6 +60,7 @@ public: void setListenRadius(float radius) { _listenRadius = radius; }; void addListenSource(int sourceID); void removeListenSource(int sourceID); + void clearListenSources(); private: PaStream* _stream; diff --git a/interface/src/avatar/Avatar.h b/interface/src/avatar/Avatar.h index 75e0ab2f9b..ec0d5a2ce5 100755 --- a/interface/src/avatar/Avatar.h +++ b/interface/src/avatar/Avatar.h @@ -164,6 +164,10 @@ public: glm::quat getOrientation () const; glm::quat getWorldAlignedOrientation() const; + const glm::vec3& getMouseRayOrigin() const { return _mouseRayOrigin; } + const glm::vec3& getMouseRayDirection() const { return _mouseRayDirection; } + + glm::vec3 getGravity () const { return _gravity; } glm::vec3 getUprightHeadPosition() const; From 229d16b9c749a1e0671bb8ca3c79dfdfa51c289e Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Mon, 22 Jul 2013 23:06:08 -0700 Subject: [PATCH 06/14] added local mode to injector --- injector/src/main.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/injector/src/main.cpp b/injector/src/main.cpp index e2c1effc15..0c3ea2bfe6 100644 --- a/injector/src/main.cpp +++ b/injector/src/main.cpp @@ -40,11 +40,13 @@ bool hasInjectedAudioOnce = false; float sleepIntervalMin = 1.00; float sleepIntervalMax = 2.00; char *sourceAudioFile = NULL; -const char *allowedParameters = ":sc::a::f::t::r:"; +const char *allowedParameters = ":sc::a::f::t::r:l"; float floatArguments[4] = {0.0f, 0.0f, 0.0f, 0.0f}; unsigned char volume = DEFAULT_INJECTOR_VOLUME; float triggerDistance = 0.0f; float radius = 0.0f; +bool wantLocalDomain = false; + void usage(void) { std::cout << "High Fidelity - Interface audio injector" << std::endl; @@ -54,6 +56,7 @@ void usage(void) { std::cout << " -f FILENAME Name of audio source file. Required - RAW format, 22050hz 16bit signed mono" << std::endl; std::cout << " -t FLOAT Trigger distance for injection. If not specified will loop constantly" << std::endl; std::cout << " -r FLOAT Radius for spherical source. If not specified injected audio is point source" << std::endl; + std::cout << " -l Local domain mode." << std::endl; } bool processParameters(int parameterCount, char* parameterData[]) { @@ -96,6 +99,9 @@ bool processParameters(int parameterCount, char* parameterData[]) { ::radius = atof(optarg); std::cout << "[DEBUG] Injector radius: " << optarg << std::endl; break; + case 'l': + ::wantLocalDomain = true; + break; default: usage(); return false; @@ -111,6 +117,7 @@ void createAvatarDataForNode(Node* node) { } int main(int argc, char* argv[]) { + // new seed for random audio sleep times srand(time(0)); @@ -126,6 +133,11 @@ int main(int argc, char* argv[]) { // create an NodeList instance to handle communication with other nodes NodeList* nodeList = NodeList::createInstance(NODE_TYPE_AUDIO_INJECTOR, AUDIO_UDP_SEND_PORT); + if (::wantLocalDomain) { + printf("Local Domain MODE!\n"); + nodeList->setDomainIPToLocalhost(); + } + // start the node list thread that will kill off nodes when they stop talking nodeList->startSilentNodeRemovalThread(); From a12ebc63c3bdbe627768b5611065f8bcab5da569 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Tue, 23 Jul 2013 18:26:10 -0700 Subject: [PATCH 07/14] made listen mode a typedef --- audio-mixer/src/PositionalAudioRingBuffer.h | 12 ++++++------ libraries/audio/src/AudioRingBuffer.h | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/audio-mixer/src/PositionalAudioRingBuffer.h b/audio-mixer/src/PositionalAudioRingBuffer.h index eb6f0414e7..58a080872d 100644 --- a/audio-mixer/src/PositionalAudioRingBuffer.h +++ b/audio-mixer/src/PositionalAudioRingBuffer.h @@ -33,7 +33,7 @@ public: bool isListeningToSource(PositionalAudioRingBuffer* other); int getSourceID() const { return _sourceID; } - int getListeningMode() const { return _listenMode; } + ListenMode getListeningMode() const { return _listenMode; } protected: // disallow copying of PositionalAudioRingBuffer objects @@ -44,11 +44,11 @@ protected: glm::quat _orientation; bool _willBeAddedToMix; - int _sourceID; - int _listenMode; - float _listenRadius; - int _listenSourceCount; - int* _listenSources; + int _sourceID; + ListenMode _listenMode; + float _listenRadius; + int _listenSourceCount; + int* _listenSources; }; diff --git a/libraries/audio/src/AudioRingBuffer.h b/libraries/audio/src/AudioRingBuffer.h index be87558010..4c5374f032 100644 --- a/libraries/audio/src/AudioRingBuffer.h +++ b/libraries/audio/src/AudioRingBuffer.h @@ -29,11 +29,11 @@ class AudioRingBuffer : public NodeData { public: static int const DEFAULT_LISTEN_LIST_SIZE = 100; - enum { + typedef enum { NORMAL, OMNI_DIRECTIONAL_POINT, SELECTED_SOURCES - }; + } ListenMode; AudioRingBuffer(bool isStereo); ~AudioRingBuffer(); From 13ba913fabe58595dcc1114613f724ac2c4d513b Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Tue, 23 Jul 2013 18:28:30 -0700 Subject: [PATCH 08/14] made listen mode a typedef --- interface/src/Audio.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/interface/src/Audio.h b/interface/src/Audio.h index facc6a1caf..9552d6e2ec 100644 --- a/interface/src/Audio.h +++ b/interface/src/Audio.h @@ -56,7 +56,7 @@ public: int getSourceID() const { return _sourceID; }; void setSourceID(int sourceID) { _sourceID = sourceID; }; - void setListenMode(int mode) { _listenMode = mode; }; + void setListenMode(AudioRingBuffer::ListenMode mode) { _listenMode = mode; }; void setListenRadius(float radius) { _listenRadius = radius; }; void addListenSource(int sourceID); void removeListenSource(int sourceID); @@ -98,12 +98,12 @@ private: int _proceduralEffectSample; float _heartbeatMagnitude; - int _sourceID; - int _listenMode; - float _listenRadius; - int _listenSourceCount; - int _listenSourcesArraySize; - int* _listenSources; + int _sourceID; + AudioRingBuffer::ListenMode _listenMode; + float _listenRadius; + int _listenSourceCount; + int _listenSourcesArraySize; + int* _listenSources; // Audio callback in class context. inline void performIO(int16_t* inputLeft, int16_t* outputLeft, int16_t* outputRight); From 927b0b6708e131e6985d04c7a6a5ac8a18526659 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Tue, 23 Jul 2013 18:31:57 -0700 Subject: [PATCH 09/14] style fixes --- audio-mixer/src/PositionalAudioRingBuffer.cpp | 8 ++++---- injector/src/main.cpp | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/audio-mixer/src/PositionalAudioRingBuffer.cpp b/audio-mixer/src/PositionalAudioRingBuffer.cpp index 26961c76e8..55622566a5 100644 --- a/audio-mixer/src/PositionalAudioRingBuffer.cpp +++ b/audio-mixer/src/PositionalAudioRingBuffer.cpp @@ -37,13 +37,13 @@ bool PositionalAudioRingBuffer::isListeningToSource(PositionalAudioRingBuffer* o default: case AudioRingBuffer::NORMAL: return true; - break; + break; case AudioRingBuffer::OMNI_DIRECTIONAL_POINT: { float distance = glm::distance(_position, other->_position); return distance <= _listenRadius; - break; - } + break; + } case AudioRingBuffer::SELECTED_SOURCES: if (_listenSources) { for (int i = 0; i < _listenSourceCount; i++) { @@ -53,7 +53,7 @@ bool PositionalAudioRingBuffer::isListeningToSource(PositionalAudioRingBuffer* o } } return false; - break; + break; } } diff --git a/injector/src/main.cpp b/injector/src/main.cpp index 0c3ea2bfe6..9caad19e60 100644 --- a/injector/src/main.cpp +++ b/injector/src/main.cpp @@ -45,7 +45,7 @@ float floatArguments[4] = {0.0f, 0.0f, 0.0f, 0.0f}; unsigned char volume = DEFAULT_INJECTOR_VOLUME; float triggerDistance = 0.0f; float radius = 0.0f; -bool wantLocalDomain = false; +bool wantsLocalDomain = false; void usage(void) { @@ -100,7 +100,7 @@ bool processParameters(int parameterCount, char* parameterData[]) { std::cout << "[DEBUG] Injector radius: " << optarg << std::endl; break; case 'l': - ::wantLocalDomain = true; + ::wantsLocalDomain = true; break; default: usage(); @@ -133,7 +133,7 @@ int main(int argc, char* argv[]) { // create an NodeList instance to handle communication with other nodes NodeList* nodeList = NodeList::createInstance(NODE_TYPE_AUDIO_INJECTOR, AUDIO_UDP_SEND_PORT); - if (::wantLocalDomain) { + if (::wantsLocalDomain) { printf("Local Domain MODE!\n"); nodeList->setDomainIPToLocalhost(); } From 686bd6c5d103d9eea0963a8b4fb3de7069b8998e Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Tue, 23 Jul 2013 19:06:47 -0700 Subject: [PATCH 10/14] removed _sourceID, using getNodeID() feature of Node class --- audio-mixer/src/PositionalAudioRingBuffer.cpp | 21 ++++++------------- audio-mixer/src/PositionalAudioRingBuffer.h | 5 +---- audio-mixer/src/main.cpp | 17 +++++++-------- 3 files changed, 14 insertions(+), 29 deletions(-) diff --git a/audio-mixer/src/PositionalAudioRingBuffer.cpp b/audio-mixer/src/PositionalAudioRingBuffer.cpp index 55622566a5..4dc409628f 100644 --- a/audio-mixer/src/PositionalAudioRingBuffer.cpp +++ b/audio-mixer/src/PositionalAudioRingBuffer.cpp @@ -8,6 +8,7 @@ #include +#include #include #include "PositionalAudioRingBuffer.h" @@ -17,7 +18,6 @@ PositionalAudioRingBuffer::PositionalAudioRingBuffer() : _position(0.0f, 0.0f, 0.0f), _orientation(0.0f, 0.0f, 0.0f, 0.0f), _willBeAddedToMix(false), - _sourceID(-1), _listenMode(AudioRingBuffer::NORMAL), _listenRadius(0.0f), _listenSourceCount(0), @@ -32,7 +32,7 @@ PositionalAudioRingBuffer::~PositionalAudioRingBuffer() { } } -bool PositionalAudioRingBuffer::isListeningToSource(PositionalAudioRingBuffer* other) { +bool PositionalAudioRingBuffer::isListeningToNode(Node& other) const { switch (_listenMode) { default: case AudioRingBuffer::NORMAL: @@ -40,14 +40,15 @@ bool PositionalAudioRingBuffer::isListeningToSource(PositionalAudioRingBuffer* o break; case AudioRingBuffer::OMNI_DIRECTIONAL_POINT: { - float distance = glm::distance(_position, other->_position); + PositionalAudioRingBuffer* otherNodeBuffer = (PositionalAudioRingBuffer*) other.getLinkedData(); + float distance = glm::distance(_position, otherNodeBuffer->_position); return distance <= _listenRadius; break; } case AudioRingBuffer::SELECTED_SOURCES: if (_listenSources) { for (int i = 0; i < _listenSourceCount; i++) { - if (other->_sourceID == _listenSources[i]) { + if (other.getNodeID() == _listenSources[i]) { return true; } } @@ -60,7 +61,7 @@ bool PositionalAudioRingBuffer::isListeningToSource(PositionalAudioRingBuffer* o int PositionalAudioRingBuffer::parseData(unsigned char* sourceBuffer, int numBytes) { unsigned char* currentBuffer = sourceBuffer + numBytesForPacketHeader(sourceBuffer); - currentBuffer += parseSourceData(currentBuffer, numBytes - (currentBuffer - sourceBuffer)); + currentBuffer += sizeof(int); // the source ID currentBuffer += parseListenModeData(currentBuffer, numBytes - (currentBuffer - sourceBuffer)); currentBuffer += parsePositionalData(currentBuffer, numBytes - (currentBuffer - sourceBuffer)); currentBuffer += parseAudioSamples(currentBuffer, numBytes - (currentBuffer - sourceBuffer)); @@ -68,15 +69,6 @@ int PositionalAudioRingBuffer::parseData(unsigned char* sourceBuffer, int numByt return currentBuffer - sourceBuffer; } -int PositionalAudioRingBuffer::parseSourceData(unsigned char* sourceBuffer, int numBytes) { - unsigned char* currentBuffer = sourceBuffer; - - memcpy(&_sourceID, currentBuffer, sizeof(_sourceID)); - currentBuffer += sizeof(_sourceID); - - return currentBuffer - sourceBuffer; -} - int PositionalAudioRingBuffer::parseListenModeData(unsigned char* sourceBuffer, int numBytes) { unsigned char* currentBuffer = sourceBuffer; @@ -134,6 +126,5 @@ bool PositionalAudioRingBuffer::shouldBeAddedToMix(int numJitterBufferSamples) { return true; } } - return false; } diff --git a/audio-mixer/src/PositionalAudioRingBuffer.h b/audio-mixer/src/PositionalAudioRingBuffer.h index 58a080872d..44d8a2b4f8 100644 --- a/audio-mixer/src/PositionalAudioRingBuffer.h +++ b/audio-mixer/src/PositionalAudioRingBuffer.h @@ -20,7 +20,6 @@ public: int parseData(unsigned char* sourceBuffer, int numBytes); int parsePositionalData(unsigned char* sourceBuffer, int numBytes); - int parseSourceData(unsigned char* sourceBuffer, int numBytes); int parseListenModeData(unsigned char* sourceBuffer, int numBytes); bool shouldBeAddedToMix(int numJitterBufferSamples); @@ -31,8 +30,7 @@ public: const glm::vec3& getPosition() const { return _position; } const glm::quat& getOrientation() const { return _orientation; } - bool isListeningToSource(PositionalAudioRingBuffer* other); - int getSourceID() const { return _sourceID; } + bool isListeningToNode(Node& other) const; ListenMode getListeningMode() const { return _listenMode; } protected: @@ -44,7 +42,6 @@ protected: glm::quat _orientation; bool _willBeAddedToMix; - int _sourceID; ListenMode _listenMode; float _listenRadius; int _listenSourceCount; diff --git a/audio-mixer/src/main.cpp b/audio-mixer/src/main.cpp index 2f17113ba4..6cfa106e5b 100644 --- a/audio-mixer/src/main.cpp +++ b/audio-mixer/src/main.cpp @@ -141,7 +141,6 @@ int main(int argc, const char* argv[]) { for (NodeList::iterator node = nodeList->begin(); node != nodeList->end(); node++) { PositionalAudioRingBuffer* positionalRingBuffer = (PositionalAudioRingBuffer*) node->getLinkedData(); - if (positionalRingBuffer && positionalRingBuffer->shouldBeAddedToMix(JITTER_BUFFER_SAMPLES)) { // this is a ring buffer that is ready to go // set its flag so we know to push its buffer when all is said and done @@ -161,14 +160,11 @@ int main(int argc, const char* argv[]) { // loop through all other nodes that have sufficient audio to mix for (NodeList::iterator otherNode = nodeList->begin(); otherNode != nodeList->end(); otherNode++) { - if (((PositionalAudioRingBuffer*) otherNode->getLinkedData())->willBeAddedToMix() && (otherNode != node || (otherNode == node && nodeRingBuffer->shouldLoopbackForNode()))) { - PositionalAudioRingBuffer* otherNodeBuffer = (PositionalAudioRingBuffer*) otherNode->getLinkedData(); - // based on our listen mode we will do this mixing... - if (nodeRingBuffer->isListeningToSource(otherNodeBuffer)) { + if (nodeRingBuffer->isListeningToNode(*otherNode)) { float bearingRelativeAngleToSource = 0.0f; float attenuationCoefficient = 1.0f; int numSamplesDelay = 0; @@ -355,14 +351,15 @@ int main(int argc, const char* argv[]) { packetVersionMatch(packetData)) { if (packetData[0] == PACKET_TYPE_MICROPHONE_AUDIO_NO_ECHO || packetData[0] == PACKET_TYPE_MICROPHONE_AUDIO_WITH_ECHO) { + + unsigned char* currentBuffer = packetData + numBytesForPacketHeader(packetData); + int sourceID; + memcpy(&sourceID, currentBuffer, sizeof(sourceID)); + Node* avatarNode = nodeList->addOrUpdateNode(nodeAddress, nodeAddress, NODE_TYPE_AGENT, - nodeList->getLastNodeID()); - - if (avatarNode->getNodeID() == nodeList->getLastNodeID()) { - nodeList->increaseNodeID(); - } + sourceID); nodeList->updateNodeWithData(nodeAddress, packetData, receivedBytes); From a162643e1e5af883426c586c47c74567c5abda77 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Tue, 23 Jul 2013 19:21:43 -0700 Subject: [PATCH 11/14] more removing of SourceID from classes --- audio-mixer/src/PositionalAudioRingBuffer.cpp | 3 ++- audio-mixer/src/main.cpp | 2 +- interface/src/Application.cpp | 12 +----------- interface/src/Audio.cpp | 8 ++++---- interface/src/Audio.h | 3 --- 5 files changed, 8 insertions(+), 20 deletions(-) diff --git a/audio-mixer/src/PositionalAudioRingBuffer.cpp b/audio-mixer/src/PositionalAudioRingBuffer.cpp index 4dc409628f..f0a3d41be5 100644 --- a/audio-mixer/src/PositionalAudioRingBuffer.cpp +++ b/audio-mixer/src/PositionalAudioRingBuffer.cpp @@ -61,7 +61,7 @@ bool PositionalAudioRingBuffer::isListeningToNode(Node& other) const { int PositionalAudioRingBuffer::parseData(unsigned char* sourceBuffer, int numBytes) { unsigned char* currentBuffer = sourceBuffer + numBytesForPacketHeader(sourceBuffer); - currentBuffer += sizeof(int); // the source ID + currentBuffer += sizeof(uint16_t); // the source ID currentBuffer += parseListenModeData(currentBuffer, numBytes - (currentBuffer - sourceBuffer)); currentBuffer += parsePositionalData(currentBuffer, numBytes - (currentBuffer - sourceBuffer)); currentBuffer += parseAudioSamples(currentBuffer, numBytes - (currentBuffer - sourceBuffer)); @@ -126,5 +126,6 @@ bool PositionalAudioRingBuffer::shouldBeAddedToMix(int numJitterBufferSamples) { return true; } } + printf("packet mismatch...\n"); return false; } diff --git a/audio-mixer/src/main.cpp b/audio-mixer/src/main.cpp index 6cfa106e5b..9fc088a8bf 100644 --- a/audio-mixer/src/main.cpp +++ b/audio-mixer/src/main.cpp @@ -353,7 +353,7 @@ int main(int argc, const char* argv[]) { packetData[0] == PACKET_TYPE_MICROPHONE_AUDIO_WITH_ECHO) { unsigned char* currentBuffer = packetData + numBytesForPacketHeader(packetData); - int sourceID; + uint16_t sourceID; memcpy(&sourceID, currentBuffer, sizeof(sourceID)); Node* avatarNode = nodeList->addOrUpdateNode(nodeAddress, diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index c1aa032542..14156b7aee 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -3483,19 +3483,9 @@ void* Application::networkReceive(void* args) { case PACKET_TYPE_AVATAR_FACE_VIDEO: processAvatarFaceVideoMessage(app->_incomingPacket, bytesReceived); break; - default: { + default: NodeList::getInstance()->processNodeData(&senderAddress, app->_incomingPacket, bytesReceived); - - // Check to see if we have our ownerID - uint16_t ownerID = NodeList::getInstance()->getOwnerID(); - - if (ownerID != app->_audio.getSourceID()) { - app->_audio.setSourceID(ownerID); - } - - break; - } } } } else if (!app->_enableNetworkThread) { diff --git a/interface/src/Audio.cpp b/interface/src/Audio.cpp index 9119d3984f..96c5db4d8c 100644 --- a/interface/src/Audio.cpp +++ b/interface/src/Audio.cpp @@ -121,9 +121,10 @@ inline void Audio::performIO(int16_t* inputLeft, int16_t* outputLeft, int16_t* o unsigned char* currentPacketPtr = dataPacket + populateTypeAndVersion(dataPacket, packetType); // pack Source Data - memcpy(currentPacketPtr, &_sourceID, sizeof(_sourceID)); - currentPacketPtr += (sizeof(_sourceID)); - leadingBytes += (sizeof(_sourceID)); + uint16_t ownerID = NodeList::getInstance()->getOwnerID(); + memcpy(currentPacketPtr, &ownerID, sizeof(ownerID)); + currentPacketPtr += (sizeof(ownerID)); + leadingBytes += (sizeof(ownerID)); // pack Listen Mode Data memcpy(currentPacketPtr, &_listenMode, sizeof(_listenMode)); @@ -413,7 +414,6 @@ Audio::Audio(Oscilloscope* scope, int16_t initialJitterBufferSamples) : _collisionSoundDuration(0.0f), _proceduralEffectSample(0), _heartbeatMagnitude(0.0f), - _sourceID(UNKNOWN_NODE_ID), _listenMode(AudioRingBuffer::NORMAL), _listenRadius(0.0f), _listenSourceCount(0), diff --git a/interface/src/Audio.h b/interface/src/Audio.h index 9552d6e2ec..3886f9d6fc 100644 --- a/interface/src/Audio.h +++ b/interface/src/Audio.h @@ -54,8 +54,6 @@ public: // The results of the analysis are written to the log. bool eventuallyAnalyzePing(); - int getSourceID() const { return _sourceID; }; - void setSourceID(int sourceID) { _sourceID = sourceID; }; void setListenMode(AudioRingBuffer::ListenMode mode) { _listenMode = mode; }; void setListenRadius(float radius) { _listenRadius = radius; }; void addListenSource(int sourceID); @@ -98,7 +96,6 @@ private: int _proceduralEffectSample; float _heartbeatMagnitude; - int _sourceID; AudioRingBuffer::ListenMode _listenMode; float _listenRadius; int _listenSourceCount; From 9507cd8955d372199f4cdcd07cdaf6dba81df293 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Tue, 23 Jul 2013 19:54:29 -0700 Subject: [PATCH 12/14] switched to vector --- audio-mixer/src/PositionalAudioRingBuffer.cpp | 30 ++++----- audio-mixer/src/PositionalAudioRingBuffer.h | 9 ++- interface/src/Audio.cpp | 67 +++++-------------- interface/src/Audio.h | 5 +- 4 files changed, 33 insertions(+), 78 deletions(-) diff --git a/audio-mixer/src/PositionalAudioRingBuffer.cpp b/audio-mixer/src/PositionalAudioRingBuffer.cpp index f0a3d41be5..9980b48a3e 100644 --- a/audio-mixer/src/PositionalAudioRingBuffer.cpp +++ b/audio-mixer/src/PositionalAudioRingBuffer.cpp @@ -19,17 +19,12 @@ PositionalAudioRingBuffer::PositionalAudioRingBuffer() : _orientation(0.0f, 0.0f, 0.0f, 0.0f), _willBeAddedToMix(false), _listenMode(AudioRingBuffer::NORMAL), - _listenRadius(0.0f), - _listenSourceCount(0), - _listenSources(NULL) + _listenRadius(0.0f) { } PositionalAudioRingBuffer::~PositionalAudioRingBuffer() { - if (_listenSources) { - delete[] _listenSources; - } } bool PositionalAudioRingBuffer::isListeningToNode(Node& other) const { @@ -46,11 +41,9 @@ bool PositionalAudioRingBuffer::isListeningToNode(Node& other) const { break; } case AudioRingBuffer::SELECTED_SOURCES: - if (_listenSources) { - for (int i = 0; i < _listenSourceCount; i++) { - if (other.getNodeID() == _listenSources[i]) { - return true; - } + for (int i = 0; i < _listenSources.size(); i++) { + if (other.getNodeID() == _listenSources[i]) { + return true; } } return false; @@ -79,14 +72,15 @@ int PositionalAudioRingBuffer::parseListenModeData(unsigned char* sourceBuffer, memcpy(&_listenRadius, currentBuffer, sizeof(_listenRadius)); currentBuffer += sizeof(_listenRadius); } else if (_listenMode == AudioRingBuffer::SELECTED_SOURCES) { - memcpy(&_listenSourceCount, currentBuffer, sizeof(_listenSourceCount)); - currentBuffer += sizeof(_listenSourceCount); - if (_listenSources) { - delete[] _listenSources; + int listenSourcesCount; + memcpy(&listenSourcesCount, currentBuffer, sizeof(listenSourcesCount)); + currentBuffer += sizeof(listenSourcesCount); + for (int i = 0; i < listenSourcesCount; i++) { + int sourceID; + memcpy(&sourceID, currentBuffer, sizeof(sourceID)); + currentBuffer += sizeof(sourceID); + _listenSources.push_back(sourceID); } - _listenSources = new int[_listenSourceCount]; - memcpy(_listenSources, currentBuffer, sizeof(int) * _listenSourceCount); - currentBuffer += sizeof(int) * _listenSourceCount; } return currentBuffer - sourceBuffer; diff --git a/audio-mixer/src/PositionalAudioRingBuffer.h b/audio-mixer/src/PositionalAudioRingBuffer.h index 44d8a2b4f8..6c7ee9ce3f 100644 --- a/audio-mixer/src/PositionalAudioRingBuffer.h +++ b/audio-mixer/src/PositionalAudioRingBuffer.h @@ -9,6 +9,7 @@ #ifndef __hifi__PositionalAudioRingBuffer__ #define __hifi__PositionalAudioRingBuffer__ +#include #include #include @@ -42,11 +43,9 @@ protected: glm::quat _orientation; bool _willBeAddedToMix; - ListenMode _listenMode; - float _listenRadius; - int _listenSourceCount; - int* _listenSources; - + ListenMode _listenMode; + float _listenRadius; + std::vector _listenSources; }; #endif /* defined(__hifi__PositionalAudioRingBuffer__) */ diff --git a/interface/src/Audio.cpp b/interface/src/Audio.cpp index 96c5db4d8c..a9aa61a34e 100644 --- a/interface/src/Audio.cpp +++ b/interface/src/Audio.cpp @@ -136,13 +136,14 @@ inline void Audio::performIO(int16_t* inputLeft, int16_t* outputLeft, int16_t* o currentPacketPtr += (sizeof(_listenRadius)); leadingBytes += (sizeof(_listenRadius)); } else if (_listenMode == AudioRingBuffer::SELECTED_SOURCES) { - memcpy(currentPacketPtr, &_listenSourceCount, sizeof(_listenSourceCount)); - currentPacketPtr += (sizeof(_listenSourceCount)); - leadingBytes += (sizeof(_listenSourceCount)); - if (_listenSources) { - memcpy(currentPacketPtr, _listenSources, sizeof(int) * _listenSourceCount); - currentPacketPtr += (sizeof(int) * _listenSourceCount); - leadingBytes += (sizeof(int) * _listenSourceCount); + int listenSourceCount = _listenSources.size(); + memcpy(currentPacketPtr, &listenSourceCount, sizeof(listenSourceCount)); + currentPacketPtr += (sizeof(listenSourceCount)); + leadingBytes += (sizeof(listenSourceCount)); + for (int i = 0; i < listenSourceCount; i++) { + memcpy(currentPacketPtr, &_listenSources[i], sizeof(_listenSources[i])); + currentPacketPtr += sizeof(_listenSources[i]); + leadingBytes += sizeof(_listenSources[i]); } } @@ -336,49 +337,18 @@ void Audio::reset() { } void Audio::addListenSource(int sourceID) { - - // If we don't yet have a list of listen sources, make one - if (!_listenSources) { - _listenSources = new int[AudioRingBuffer::DEFAULT_LISTEN_LIST_SIZE]; - } - - // First check to see if the source is already in our list - for (int i = 0; i < _listenSourceCount; i++) { - if (_listenSources[i] == sourceID) { - return; // already in list - } - } - - // we know it's not in the list, check to see if we have room to add our source - if (_listenSourceCount + 1 < _listenSourcesArraySize) { - int* newList = new int[_listenSourcesArraySize + AudioRingBuffer::DEFAULT_LISTEN_LIST_SIZE]; - memmove(newList, _listenSources, _listenSourcesArraySize); - delete[] _listenSources; - _listenSources = newList; - _listenSourcesArraySize += AudioRingBuffer::DEFAULT_LISTEN_LIST_SIZE; - } - _listenSources[_listenSourceCount] = sourceID; - _listenSourceCount++; + _listenSources.push_back(sourceID); } void Audio::clearListenSources() { - delete[] _listenSources; - _listenSources = NULL; - _listenSourceCount = 0; + _listenSources.clear(); } void Audio::removeListenSource(int sourceID) { - // If we don't yet have a list of listen sources, make one - if (_listenSources) { - // First check to see if the source is already in our list - for (int i = 0; i < _listenSourceCount; i++) { - if (_listenSources[i] == sourceID) { - - // found it, so, move the items forward in list - memmove(&_listenSources[i], &_listenSources[i+1], _listenSourceCount - i); - _listenSourceCount--; - return; - } + for (int i = 0; i < _listenSources.size(); i++) { + if (_listenSources[i] == sourceID) { + _listenSources.erase(_listenSources.begin() + i); + return; } } } @@ -415,10 +385,7 @@ Audio::Audio(Oscilloscope* scope, int16_t initialJitterBufferSamples) : _proceduralEffectSample(0), _heartbeatMagnitude(0.0f), _listenMode(AudioRingBuffer::NORMAL), - _listenRadius(0.0f), - _listenSourceCount(0), - _listenSourcesArraySize(0), - _listenSources(NULL) + _listenRadius(0.0f) { outputPortAudioError(Pa_Initialize()); @@ -487,10 +454,6 @@ Audio::~Audio() { outputPortAudioError(Pa_Terminate()); } delete[] _echoSamplesLeft; - - if (_listenSources) { - delete[] _listenSources; - } } void Audio::addReceivedAudioToBuffer(unsigned char* receivedData, int receivedBytes) { diff --git a/interface/src/Audio.h b/interface/src/Audio.h index 3886f9d6fc..2eb4e7ef70 100644 --- a/interface/src/Audio.h +++ b/interface/src/Audio.h @@ -9,6 +9,7 @@ #ifndef __interface__Audio__ #define __interface__Audio__ +#include #include #include #include @@ -98,9 +99,7 @@ private: AudioRingBuffer::ListenMode _listenMode; float _listenRadius; - int _listenSourceCount; - int _listenSourcesArraySize; - int* _listenSources; + std::vector _listenSources; // Audio callback in class context. inline void performIO(int16_t* inputLeft, int16_t* outputLeft, int16_t* outputRight); From eb8c5653500df4df5a9cd50e09a006e965cd6c94 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Tue, 23 Jul 2013 20:08:57 -0700 Subject: [PATCH 13/14] dry up code --- interface/src/Application.cpp | 33 +++++++++++++++++---------------- interface/src/Application.h | 4 +++- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 14156b7aee..9784a8a7e6 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -1864,20 +1864,13 @@ void Application::setListenModeSingleSource() { _audio.setListenMode(AudioRingBuffer::SELECTED_SOURCES); _audio.clearListenSources(); - NodeList* nodeList = NodeList::getInstance(); - for (NodeList::iterator node = nodeList->begin(); node != nodeList->end(); node++) { - if (node->getLinkedData() != NULL && node->getType() == NODE_TYPE_AGENT) { - Avatar* avatar = (Avatar *) node->getLinkedData(); - glm::vec3 headPosition = avatar->getHead().getPosition(); - glm::vec3 mouseRayOrigin = _myAvatar.getMouseRayOrigin(); - glm::vec3 mouseRayDirection = _myAvatar.getMouseRayDirection(); - const float HEAD_SPHERE_RADIUS = 0.07; + glm::vec3 mouseRayOrigin = _myAvatar.getMouseRayOrigin(); + glm::vec3 mouseRayDirection = _myAvatar.getMouseRayDirection(); + glm::vec3 eyePositionIgnored; + uint16_t nodeID; - if (rayIntersectsSphere(mouseRayOrigin, mouseRayDirection, headPosition, HEAD_SPHERE_RADIUS)) { - int sourceID = avatar->getOwningNode()->getNodeID(); - _audio.addListenSource(sourceID); - } - } + if (isLookingAtOtherAvatar(mouseRayOrigin, mouseRayDirection, eyePositionIgnored, nodeID)) { + _audio.addListenSource(nodeID); } } @@ -1970,7 +1963,10 @@ const float MAX_AVATAR_EDIT_VELOCITY = 1.0f; const float MAX_VOXEL_EDIT_DISTANCE = 20.0f; const float HEAD_SPHERE_RADIUS = 0.07; -bool Application::isLookingAtOtherAvatar(glm::vec3& mouseRayOrigin, glm::vec3& mouseRayDirection, glm::vec3& eyePosition) { + +bool Application::isLookingAtOtherAvatar(glm::vec3& mouseRayOrigin, glm::vec3& mouseRayDirection, + glm::vec3& eyePosition, uint16_t& nodeID) { + NodeList* nodeList = NodeList::getInstance(); for (NodeList::iterator node = nodeList->begin(); node != nodeList->end(); node++) { if (node->getLinkedData() != NULL && node->getType() == NODE_TYPE_AGENT) { @@ -1979,6 +1975,7 @@ bool Application::isLookingAtOtherAvatar(glm::vec3& mouseRayOrigin, glm::vec3& m if (rayIntersectsSphere(mouseRayOrigin, mouseRayDirection, headPosition, HEAD_SPHERE_RADIUS)) { eyePosition = avatar->getHead().getEyeLevelPosition(); _lookatOtherPosition = headPosition; + nodeID = avatar->getOwningNode()->getNodeID(); return true; } } @@ -2022,7 +2019,9 @@ void Application::update(float deltaTime) { // Set where I am looking based on my mouse ray (so that other people can see) glm::vec3 eyePosition; - if (_isLookingAtOtherAvatar = isLookingAtOtherAvatar(mouseRayOrigin, mouseRayDirection, eyePosition)) { + uint16_t ignored; + _isLookingAtOtherAvatar = isLookingAtOtherAvatar(mouseRayOrigin, mouseRayDirection, eyePosition, ignored); + if (_isLookingAtOtherAvatar) { // If the mouse is over another avatar's head... glm::vec3 myLookAtFromMouse(eyePosition); _myAvatar.getHead().setLookAtPosition(myLookAtFromMouse); @@ -2284,7 +2283,9 @@ void Application::updateAvatar(float deltaTime) { _viewFrustum.computePickRay(MIDPOINT_OF_SCREEN, MIDPOINT_OF_SCREEN, screenCenterRayOrigin, screenCenterRayDirection); glm::vec3 eyePosition; - if (_isLookingAtOtherAvatar = isLookingAtOtherAvatar(screenCenterRayOrigin, screenCenterRayDirection, eyePosition)) { + uint16_t ignored; + _isLookingAtOtherAvatar = isLookingAtOtherAvatar(screenCenterRayOrigin, screenCenterRayDirection, eyePosition, ignored); + if (_isLookingAtOtherAvatar) { glm::vec3 myLookAtFromMouse(eyePosition); _myAvatar.getHead().setLookAtPosition(myLookAtFromMouse); } diff --git a/interface/src/Application.h b/interface/src/Application.h index d462e88b98..da8cf96189 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -203,7 +203,9 @@ private: void init(); void update(float deltaTime); - bool isLookingAtOtherAvatar(glm::vec3& mouseRayOrigin, glm::vec3& mouseRayDirection, glm::vec3& eyePosition); + bool isLookingAtOtherAvatar(glm::vec3& mouseRayOrigin, glm::vec3& mouseRayDirection, + glm::vec3& eyePosition, uint16_t& nodeID); + void renderLookatIndicator(glm::vec3 pointOfInterest, Camera& whichCamera); void updateAvatar(float deltaTime); void loadViewFrustum(Camera& camera, ViewFrustum& viewFrustum); From 7177b71bc77f29b635787d2e1048ecf36c5f6e5e Mon Sep 17 00:00:00 2001 From: Mark Peng Date: Wed, 24 Jul 2013 00:19:25 -0700 Subject: [PATCH 14/14] Replace var in renderLookatIndicator with const IDENTITY_UP --- interface/src/Application.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index c0fe2b360b..c5f12822fe 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -1960,8 +1960,7 @@ void Application::renderLookatIndicator(glm::vec3 pointOfInterest, Camera& which const int NUM_SEGMENTS = 30; glm::vec3 haloOrigin(pointOfInterest.x, pointOfInterest.y + DISTANCE_FROM_HEAD_SPHERE, pointOfInterest.z); glColor3f(YELLOW[0], YELLOW[1], YELLOW[2]); - glm::vec3 normalToFloor(0.0f, 1.0f, 0.0f); - renderCircle(haloOrigin, INDICATOR_RADIUS, normalToFloor, NUM_SEGMENTS); + renderCircle(haloOrigin, INDICATOR_RADIUS, IDENTITY_UP, NUM_SEGMENTS); } void Application::update(float deltaTime) {