From c55b6a20d77de398351e6d3d0982f5f4b92b4295 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 3 Jun 2013 15:38:12 -0700 Subject: [PATCH] parse the desired cube side length for injected audio source --- injector/src/main.cpp | 15 +++++++++++++-- libraries/audio/src/AudioInjector.cpp | 13 ++++++++++++- libraries/audio/src/AudioInjector.h | 4 ++++ libraries/audio/src/AudioRingBuffer.cpp | 11 +++++++++++ libraries/audio/src/AudioRingBuffer.h | 4 ++++ 5 files changed, 44 insertions(+), 3 deletions(-) diff --git a/injector/src/main.cpp b/injector/src/main.cpp index 32a8442d8f..512882c374 100644 --- a/injector/src/main.cpp +++ b/injector/src/main.cpp @@ -32,10 +32,11 @@ bool loopAudio = true; float sleepIntervalMin = 1.00; float sleepIntervalMax = 2.00; char *sourceAudioFile = NULL; -const char *allowedParameters = ":rb::t::c::a::f::d:"; +const char *allowedParameters = ":rb::t::c::a::f::d::s:"; float floatArguments[4] = {0.0f, 0.0f, 0.0f, 0.0f}; unsigned char volume = DEFAULT_INJECTOR_VOLUME; -float triggerDistance = 0; +float triggerDistance = 0.0f; +float cubeSideLength = 0.0f; void usage(void) { std::cout << "High Fidelity - Interface audio injector" << std::endl; @@ -46,6 +47,7 @@ void usage(void) { std::cout << " -a 0-255 Attenuation curve modifier, defaults to 255" << std::endl; std::cout << " -f FILENAME Name of audio source file. Required - RAW format, 22050hz 16bit signed mono" << std::endl; std::cout << " -d FLOAT Trigger distance for injection. If not specified will loop constantly" << std::endl; + std::cout << " -s FLOAT Length of side of cube audio source. If not specified injected audio is point source" << std::endl; } bool processParameters(int parameterCount, char* parameterData[]) { @@ -92,6 +94,10 @@ bool processParameters(int parameterCount, char* parameterData[]) { ::triggerDistance = atof(optarg); std::cout << "[DEBUG] Trigger distance: " << optarg << std::endl; break; + case 's': + ::cubeSideLength = atof(optarg); + std::cout << "[DEBUG] Cube side length: " << optarg << std::endl; + break; default: usage(); return false; @@ -163,6 +169,11 @@ int main(int argc, char* argv[]) { injector.setPosition(glm::vec3(::floatArguments[0], ::floatArguments[1], ::floatArguments[2])); injector.setBearing(*(::floatArguments + 3)); injector.setVolume(::volume); + + if (::cubeSideLength > 0) { + // if we were passed a cube side length, give that to the injector + injector.setCubeSideLength(::cubeSideLength); + } // register the callback for agent data creation agentList->linkedDataCreateCallback = createAvatarDataForAgent; diff --git a/libraries/audio/src/AudioInjector.cpp b/libraries/audio/src/AudioInjector.cpp index 8e540c9735..e1de27ef6d 100644 --- a/libraries/audio/src/AudioInjector.cpp +++ b/libraries/audio/src/AudioInjector.cpp @@ -19,6 +19,7 @@ const int MAX_INJECTOR_VOLUME = 0xFF; AudioInjector::AudioInjector(const char* filename) : _position(), + _cubeSideLength(0.0f), _bearing(0), _volume(MAX_INJECTOR_VOLUME), _indexOfNextSlot(0), @@ -48,6 +49,7 @@ AudioInjector::AudioInjector(const char* filename) : AudioInjector::AudioInjector(int maxNumSamples) : _numTotalSamples(maxNumSamples), _position(), + _cubeSideLength(0.0f), _bearing(0), _volume(MAX_INJECTOR_VOLUME), _indexOfNextSlot(0), @@ -75,7 +77,9 @@ void AudioInjector::injectAudio(UDPSocket* injectorSocket, sockaddr* destination unsigned char dataPacket[BUFFER_LENGTH_BYTES + leadingBytes]; dataPacket[0] = PACKET_HEADER_INJECT_AUDIO; - unsigned char *currentPacketPtr = dataPacket + 1; + // add the correct command for point source or cube of sound + dataPacket[1] = (_cubeSideLength > 0) ? INJECT_AUDIO_AT_CUBE_COMMAND : INJECT_AUDIO_AT_POINT_COMMAND; + unsigned char *currentPacketPtr = dataPacket + sizeof(PACKET_HEADER) + sizeof(INJECT_AUDIO_AT_POINT_COMMAND); // copy the identifier for this injector memcpy(currentPacketPtr, &_streamIdentifier, sizeof(_streamIdentifier)); @@ -84,6 +88,13 @@ void AudioInjector::injectAudio(UDPSocket* injectorSocket, sockaddr* destination memcpy(currentPacketPtr, &_position, sizeof(_position)); currentPacketPtr += sizeof(_position); + if (_cubeSideLength > 0) { + // if we have a cube half height we need to send it here + // this tells the mixer how much volume the injected audio will occupy + memcpy(currentPacketPtr, &_cubeSideLength, sizeof(_cubeSideLength)); + currentPacketPtr += sizeof(_cubeSideLength); + } + *currentPacketPtr = _volume; currentPacketPtr++; diff --git a/libraries/audio/src/AudioInjector.h b/libraries/audio/src/AudioInjector.h index 7db9398fc9..0269931568 100644 --- a/libraries/audio/src/AudioInjector.h +++ b/libraries/audio/src/AudioInjector.h @@ -39,6 +39,9 @@ public: float getBearing() const { return _bearing; } void setBearing(float bearing) { _bearing = bearing; } + float getCubeSideLength() const { return _cubeSideLength; } + void setCubeSideLength(float cubeSideLength) { _cubeSideLength = cubeSideLength; } + void addSample(const int16_t sample); void addSamples(int16_t* sampleBuffer, int numSamples); private: @@ -46,6 +49,7 @@ private: int16_t* _audioSampleArray; int _numTotalSamples; glm::vec3 _position; + float _cubeSideLength; float _bearing; unsigned char _volume; int _indexOfNextSlot; diff --git a/libraries/audio/src/AudioRingBuffer.cpp b/libraries/audio/src/AudioRingBuffer.cpp index 006dd825bf..39cdc43287 100644 --- a/libraries/audio/src/AudioRingBuffer.cpp +++ b/libraries/audio/src/AudioRingBuffer.cpp @@ -17,6 +17,7 @@ AudioRingBuffer::AudioRingBuffer(int ringSamples, int bufferSamples) : AgentData(NULL), _ringBufferLengthSamples(ringSamples), _bufferLengthSamples(bufferSamples), + _cubeSideLength(0.0f), _endOfLastWrite(NULL), _started(false), _shouldBeAddedToMix(false), @@ -46,11 +47,21 @@ int AudioRingBuffer::parseData(unsigned char* sourceBuffer, int numBytes) { // we've got a stream identifier to pull from the packet memcpy(&_streamIdentifier, dataBuffer, sizeof(_streamIdentifier)); dataBuffer += sizeof(_streamIdentifier); + + // push past the injection command + dataBuffer += sizeof(INJECT_AUDIO_AT_POINT_COMMAND); } memcpy(&_position, dataBuffer, sizeof(_position)); dataBuffer += (sizeof(_position)); + if (sourceBuffer[0] == PACKET_HEADER_INJECT_AUDIO && sourceBuffer[1] == INJECT_AUDIO_AT_CUBE_COMMAND) { + // this is audio that needs to be injected as a volume (cube) + // parse out the cubeHalfHeight sent by the client + memcpy(&_cubeSideLength, dataBuffer, sizeof(_cubeSideLength)); + dataBuffer += (sizeof(_cubeSideLength)); + } + unsigned int attenuationByte = *(dataBuffer++); _attenuationRatio = attenuationByte / 255.0f; diff --git a/libraries/audio/src/AudioRingBuffer.h b/libraries/audio/src/AudioRingBuffer.h index b448f93cee..3554e13d46 100644 --- a/libraries/audio/src/AudioRingBuffer.h +++ b/libraries/audio/src/AudioRingBuffer.h @@ -17,6 +17,9 @@ const int STREAM_IDENTIFIER_NUM_BYTES = 8; +const char INJECT_AUDIO_AT_POINT_COMMAND = 'P'; +const char INJECT_AUDIO_AT_CUBE_COMMAND = 'C'; + class AudioRingBuffer : public AgentData { public: AudioRingBuffer(int ringSamples, int bufferSamples); @@ -53,6 +56,7 @@ private: int _ringBufferLengthSamples; int _bufferLengthSamples; glm::vec3 _position; + float _cubeSideLength; float _attenuationRatio; float _bearing; int16_t* _nextOutput;