From 1783ee5f85f193b75d73f48cf7bc03c920f880a6 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Tue, 26 Feb 2013 13:13:40 -0800 Subject: [PATCH] send interface client position to audio mixer --- domain/src/main.cpp | 2 -- interface/src/Audio.cpp | 24 +++++++++++++++++++++++- interface/src/Audio.h | 3 ++- interface/src/AudioData.h | 10 ++++++---- interface/src/main.cpp | 2 +- mixer/src/main.cpp | 11 +++-------- shared/src/AgentList.cpp | 2 +- shared/src/AgentList.h | 1 + shared/src/AudioRingBuffer.cpp | 16 ++++++++++++++-- shared/src/UDPSocket.h | 2 +- 10 files changed, 52 insertions(+), 21 deletions(-) diff --git a/domain/src/main.cpp b/domain/src/main.cpp index 7b6f75ad3f..9a8b6bc64f 100644 --- a/domain/src/main.cpp +++ b/domain/src/main.cpp @@ -32,8 +32,6 @@ #include "SharedUtil.h" const int DOMAIN_LISTEN_PORT = 40102; - -const int MAX_PACKET_SIZE = 1500; unsigned char packetData[MAX_PACKET_SIZE]; const int LOGOFF_CHECK_INTERVAL = 5000; diff --git a/interface/src/Audio.cpp b/interface/src/Audio.cpp index 6e9136b4b5..4990e4f5de 100644 --- a/interface/src/Audio.cpp +++ b/interface/src/Audio.cpp @@ -84,7 +84,25 @@ int audioCallback (const void *inputBuffer, audioMixerSocket.sin_family = AF_INET; audioMixerSocket.sin_addr.s_addr = data->mixerAddress; audioMixerSocket.sin_port = data->mixerPort; - data->audioSocket->send((sockaddr *)&audioMixerSocket, (void *)inputLeft, BUFFER_LENGTH_BYTES); + + int leadingBytes = 1 + (sizeof(float) * 3); + + // we need the amount of bytes in the buffer + 1 for type + 12 for 3 floats for position + unsigned char *dataPacket = new unsigned char[BUFFER_LENGTH_BYTES + leadingBytes]; + + dataPacket[0] = 'I'; + + // memcpy the three float positions + for (int p = 0; p < 3; p++) { + memcpy(dataPacket + 1 + (p * sizeof(float)), &data->sourcePosition[p], sizeof(float)); + } + + // copy the audio data to the last 1024 bytes of the data packet + memcpy(dataPacket + leadingBytes, inputLeft, BUFFER_LENGTH_BYTES); + + data->audioSocket->send((sockaddr *)&audioMixerSocket, dataPacket, BUFFER_LENGTH_BYTES + leadingBytes); + + delete dataPacket; } // @@ -236,6 +254,10 @@ void *receiveAudioViaUDP(void *args) { pthread_exit(0); } +void Audio::setSourcePosition(glm::vec3 newPosition) { + audioData->sourcePosition = newPosition; +} + /** * Initialize portaudio and start an audio stream. * Should be called at the beginning of program exection. diff --git a/interface/src/Audio.h b/interface/src/Audio.h index 7f1fa9143f..0798486106 100644 --- a/interface/src/Audio.h +++ b/interface/src/Audio.h @@ -25,11 +25,12 @@ public: void getInputLoudness(float * lastLoudness, float * averageLoudness); void updateMixerParams(in_addr_t mixerAddress, in_port_t mixerPort); + void setSourcePosition(glm::vec3 position); + // terminates audio I/O bool terminate(); private: bool initialized; - AudioData *audioData; // protects constructor so that public init method is used diff --git a/interface/src/AudioData.h b/interface/src/AudioData.h index e5094f8263..4efe39884a 100644 --- a/interface/src/AudioData.h +++ b/interface/src/AudioData.h @@ -11,17 +11,22 @@ #include #include +#include #include "AudioRingBuffer.h" #include "UDPSocket.h" class AudioData { - public: + public: + AudioData(int bufferLength); + ~AudioData(); AudioRingBuffer *ringBuffer; UDPSocket *audioSocket; int16_t *samplesToQueue; + glm::vec3 sourcePosition; + // store current mixer address and port in_addr_t mixerAddress; in_port_t mixerPort; @@ -34,9 +39,6 @@ class AudioData { float lastInputLoudness; float averagedInputLoudness; - - AudioData(int bufferLength); - ~AudioData(); }; #endif /* defined(__interface__AudioData__) */ diff --git a/interface/src/main.cpp b/interface/src/main.cpp index e6901a2829..c86d3c179d 100644 --- a/interface/src/main.cpp +++ b/interface/src/main.cpp @@ -53,7 +53,6 @@ int simulate_on = 1; // Network Socket and network constants // -const int MAX_PACKET_SIZE = 1500; char DOMAIN_HOSTNAME[] = "highfidelity.below92.com"; char DOMAIN_IP[100] = ""; // IP Address will be used first if not empty string const int DOMAINSERVER_PORT = 40102; @@ -513,6 +512,7 @@ void simulateHead(float frametime) myHead.setRenderYaw(myHead.getRenderYaw() + render_yaw_rate); myHead.setRenderPitch(render_pitch); myHead.setPos(glm::vec3(location[0], location[1], location[2])); + audio.setSourcePosition(glm::vec3(location[0], location[1], location[2])); // Get audio loudness data from audio input device float loudness, averageLoudness; diff --git a/mixer/src/main.cpp b/mixer/src/main.cpp index 379298b828..37093396ce 100644 --- a/mixer/src/main.cpp +++ b/mixer/src/main.cpp @@ -16,12 +16,8 @@ #include #include -const int MAX_AGENTS = 1000; -const int LOGOFF_CHECK_INTERVAL = 1000; - const unsigned short MIXER_LISTEN_PORT = 55443; - const float SAMPLE_RATE = 22050.0; const float BUFFER_SEND_INTERVAL_USECS = (BUFFER_LENGTH_SAMPLES/SAMPLE_RATE) * 1000000; @@ -35,7 +31,6 @@ char DOMAIN_HOSTNAME[] = "highfidelity.below92.com"; char DOMAIN_IP[100] = ""; // IP Address will be re-set by lookup on startup const int DOMAINSERVER_PORT = 40102; -const int MAX_SOURCE_BUFFERS = 20; AgentList agentList(MIXER_LISTEN_PORT); @@ -182,7 +177,7 @@ int main(int argc, const char * argv[]) printf("Using static domainserver IP: %s\n", DOMAIN_IP); } - int16_t *packetData = new int16_t[BUFFER_LENGTH_SAMPLES]; + unsigned char *packetData = new unsigned char[MAX_PACKET_SIZE]; pthread_t sendBufferThread; pthread_create(&sendBufferThread, NULL, sendBuffer, NULL); @@ -191,9 +186,9 @@ int main(int argc, const char * argv[]) while (true) { if(agentList.getAgentSocket().receive(agentAddress, packetData, &receivedBytes)) { - if (receivedBytes == BUFFER_LENGTH_BYTES) { + if (receivedBytes > BUFFER_LENGTH_BYTES) { // add or update the existing interface agent - agentList.addOrUpdateAgent(agentAddress, agentAddress, 'I'); + agentList.addOrUpdateAgent(agentAddress, agentAddress, packetData[0]); agentList.updateAgentWithData(agentAddress, (void *)packetData, receivedBytes); } } diff --git a/shared/src/AgentList.cpp b/shared/src/AgentList.cpp index d1c4db24ce..fe280894ac 100644 --- a/shared/src/AgentList.cpp +++ b/shared/src/AgentList.cpp @@ -147,7 +147,7 @@ bool AgentList::addOrUpdateAgent(sockaddr *publicSocket, sockaddr *localSocket, // this is an audio mixer // for now that means we need to tell the audio class // to use the local socket information the domain server gave us - sockaddr_in *localSocketIn = (sockaddr_in *)localSocket; + sockaddr_in *localSocketIn = (sockaddr_in *)publicSocket; audioMixerSocketUpdate(localSocketIn->sin_addr.s_addr, localSocketIn->sin_port); } diff --git a/shared/src/AgentList.h b/shared/src/AgentList.h index 0741e63d7b..4fb6a2d989 100644 --- a/shared/src/AgentList.h +++ b/shared/src/AgentList.h @@ -14,6 +14,7 @@ #include "Agent.h" #include "UDPSocket.h" +const int MAX_PACKET_SIZE = 1500; const unsigned short AGENT_SOCKET_LISTEN_PORT = 40103; const int AGENT_SILENCE_THRESHOLD_USECS = 2 * 1000000; extern const char *SOLO_AGENT_TYPES_STRING; diff --git a/shared/src/AudioRingBuffer.cpp b/shared/src/AudioRingBuffer.cpp index d234a405e3..e20860a7bf 100644 --- a/shared/src/AudioRingBuffer.cpp +++ b/shared/src/AudioRingBuffer.cpp @@ -75,8 +75,20 @@ void AudioRingBuffer::setAddedToMix(bool added) { } void AudioRingBuffer::parseData(void *data, int size) { - int16_t *audioData = (int16_t *)data; + int16_t *audioDataStart = (int16_t *) data; + if (size > BUFFER_LENGTH_BYTES) { + float position[3]; + unsigned char *charData = (unsigned char *) data; + + for (int p = 0; p < 3; p ++) { + memcpy(&position[p], charData + 1 + (sizeof(float) * p), sizeof(float)); + } + + audioDataStart = (int16_t *) charData + 1 + (sizeof(float) * 3); + } + + if (endOfLastWrite == NULL) { endOfLastWrite = buffer; } else if (diffLastWriteNextOutput() > RING_BUFFER_SAMPLES - BUFFER_LENGTH_SAMPLES) { @@ -85,7 +97,7 @@ void AudioRingBuffer::parseData(void *data, int size) { started = false; } - memcpy(endOfLastWrite, audioData, BUFFER_LENGTH_BYTES); + memcpy(endOfLastWrite, audioDataStart, BUFFER_LENGTH_BYTES); endOfLastWrite += BUFFER_LENGTH_SAMPLES; if (endOfLastWrite >= buffer + RING_BUFFER_SAMPLES) { diff --git a/shared/src/UDPSocket.h b/shared/src/UDPSocket.h index 27a3c48ad3..b2389ad73f 100644 --- a/shared/src/UDPSocket.h +++ b/shared/src/UDPSocket.h @@ -13,7 +13,7 @@ #include #include -#define MAX_BUFFER_LENGTH_BYTES 1024 +#define MAX_BUFFER_LENGTH_BYTES 1500 class UDPSocket { public: