Merge pull request #342 from birarda/voxel-noise

use stream identifier to match injected audio across packets
This commit is contained in:
birarda 2013-05-17 15:07:29 -07:00
commit 67bab7e73d
14 changed files with 200 additions and 135 deletions

View file

@ -3,11 +3,18 @@ cmake_minimum_required(VERSION 2.8)
set(ROOT_DIR ..) set(ROOT_DIR ..)
set(MACRO_DIR ${ROOT_DIR}/cmake/macros) set(MACRO_DIR ${ROOT_DIR}/cmake/macros)
# setup for find modules
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/../cmake/modules/")
set(TARGET_NAME audio-mixer) set(TARGET_NAME audio-mixer)
include(${MACRO_DIR}/SetupHifiProject.cmake) include(${MACRO_DIR}/SetupHifiProject.cmake)
setup_hifi_project(${TARGET_NAME}) setup_hifi_project(${TARGET_NAME})
# set up the external glm library
include(${MACRO_DIR}/IncludeGLM.cmake)
include_glm(${TARGET_NAME} ${ROOT_DIR})
# link the shared hifi library # link the shared hifi library
include(${MACRO_DIR}/LinkHifiLibrary.cmake) include(${MACRO_DIR}/LinkHifiLibrary.cmake)
link_hifi_library(shared ${TARGET_NAME} ${ROOT_DIR}) link_hifi_library(shared ${TARGET_NAME} ${ROOT_DIR})

View file

@ -18,6 +18,7 @@
#include <signal.h> #include <signal.h>
#include <AgentList.h> #include <AgentList.h>
#include <Agent.h>
#include <AgentTypes.h> #include <AgentTypes.h>
#include <SharedUtil.h> #include <SharedUtil.h>
#include <StdDev.h> #include <StdDev.h>
@ -132,6 +133,7 @@ int main(int argc, const char* argv[]) {
memset(distanceCoefficients, 0, sizeof(distanceCoefficients)); memset(distanceCoefficients, 0, sizeof(distanceCoefficients));
for (AgentList::iterator agent = agentList->begin(); agent != agentList->end(); agent++) { for (AgentList::iterator agent = agentList->begin(); agent != agentList->end(); agent++) {
if (agent->getType() == AGENT_TYPE_AVATAR) {
AudioRingBuffer* agentRingBuffer = (AudioRingBuffer*) agent->getLinkedData(); AudioRingBuffer* agentRingBuffer = (AudioRingBuffer*) agent->getLinkedData();
// zero out the client mix for this agent // zero out the client mix for this agent
@ -149,8 +151,8 @@ int main(int argc, const char* argv[]) {
float weakChannelAmplitudeRatio = 1.f; float weakChannelAmplitudeRatio = 1.f;
if (otherAgent != agent) { if (otherAgent != agent) {
Position agentPosition = agentRingBuffer->getPosition(); glm::vec3 agentPosition = agentRingBuffer->getPosition();
Position otherAgentPosition = otherAgentBuffer->getPosition(); glm::vec3 otherAgentPosition = otherAgentBuffer->getPosition();
// calculate the distance to the other agent // calculate the distance to the other agent
@ -255,6 +257,7 @@ int main(int argc, const char* argv[]) {
memcpy(clientPacket + 1, clientSamples, sizeof(clientSamples)); memcpy(clientPacket + 1, clientSamples, sizeof(clientSamples));
agentList->getAgentSocket()->send(agent->getPublicSocket(), clientPacket, BUFFER_LENGTH_BYTES + 1); agentList->getAgentSocket()->send(agent->getPublicSocket(), clientPacket, BUFFER_LENGTH_BYTES + 1);
} }
}
// push forward the next output pointers for any audio buffers we used // push forward the next output pointers for any audio buffers we used
for (AgentList::iterator agent = agentList->begin(); agent != agentList->end(); agent++) { for (AgentList::iterator agent = agentList->begin(); agent != agentList->end(); agent++) {
@ -272,16 +275,42 @@ int main(int argc, const char* argv[]) {
// pull any new audio data from agents off of the network stack // pull any new audio data from agents off of the network stack
while (agentList->getAgentSocket()->receive(agentAddress, packetData, &receivedBytes)) { while (agentList->getAgentSocket()->receive(agentAddress, packetData, &receivedBytes)) {
if (packetData[0] == PACKET_HEADER_INJECT_AUDIO || packetData[0] == PACKET_HEADER_MICROPHONE_AUDIO) { if (packetData[0] == PACKET_HEADER_MICROPHONE_AUDIO) {
char agentType = (packetData[0] == PACKET_HEADER_MICROPHONE_AUDIO) Agent* avatarAgent = agentList->addOrUpdateAgent(agentAddress,
? AGENT_TYPE_AVATAR agentAddress,
: AGENT_TYPE_AUDIO_INJECTOR; AGENT_TYPE_AVATAR,
agentList->getLastAgentID());
if (agentList->addOrUpdateAgent(agentAddress, agentAddress, agentType, agentList->getLastAgentID())) { if (avatarAgent->getAgentID() == agentList->getLastAgentID()) {
agentList->increaseAgentID(); agentList->increaseAgentID();
} }
agentList->updateAgentWithData(agentAddress, packetData, receivedBytes); agentList->updateAgentWithData(agentAddress, packetData, receivedBytes);
} else if (packetData[0] == PACKET_HEADER_INJECT_AUDIO) {
Agent* matchingInjector = NULL;
for (AgentList::iterator agent = agentList->begin(); agent != agentList->end(); agent++) {
if (agent->getLinkedData()) {
AudioRingBuffer* ringBuffer = (AudioRingBuffer*) agent->getLinkedData();
if (memcmp(ringBuffer->getStreamIdentifier(), packetData + 1, sizeof(STREAM_IDENTIFIER_NUM_BYTES))) {
// this is the matching stream, assign to matchingInjector and stop looking
matchingInjector = &*agent;
break;
}
}
}
if (!matchingInjector) {
matchingInjector = agentList->addOrUpdateAgent(NULL,
NULL,
AGENT_TYPE_AUDIO_INJECTOR,
agentList->getLastAgentID());
agentList->increaseAgentID();
}
// give the new audio data to the matching injector agent
agentList->updateAgentWithData(matchingInjector, packetData, receivedBytes);
} }
} }

View file

@ -70,6 +70,7 @@ int main(int argc, const char* argv[]) {
unsigned char* currentBufferPosition = NULL; unsigned char* currentBufferPosition = NULL;
uint16_t agentID = 0; uint16_t agentID = 0;
Agent* avatarAgent = NULL;
while (true) { while (true) {
if (agentList->getAgentSocket()->receive(agentAddress, packetData, &receivedBytes)) { if (agentList->getAgentSocket()->receive(agentAddress, packetData, &receivedBytes)) {
@ -79,10 +80,10 @@ int main(int argc, const char* argv[]) {
unpackAgentId(packetData + 1, &agentID); unpackAgentId(packetData + 1, &agentID);
// add or update the agent in our list // add or update the agent in our list
agentList->addOrUpdateAgent(agentAddress, agentAddress, AGENT_TYPE_AVATAR, agentID); avatarAgent = agentList->addOrUpdateAgent(agentAddress, agentAddress, AGENT_TYPE_AVATAR, agentID);
// parse positional data from an agent // parse positional data from an agent
agentList->updateAgentWithData(agentAddress, packetData, receivedBytes); agentList->updateAgentWithData(avatarAgent, packetData, receivedBytes);
currentBufferPosition = broadcastPacket + 1; currentBufferPosition = broadcastPacket + 1;

View file

@ -115,10 +115,12 @@ int main(int argc, const char * argv[])
} }
} }
if (agentList->addOrUpdateAgent((sockaddr*) &agentPublicAddress, Agent* newAgent = agentList->addOrUpdateAgent((sockaddr*) &agentPublicAddress,
(sockaddr*) &agentLocalAddress, (sockaddr*) &agentLocalAddress,
agentType, agentType,
agentList->getLastAgentID())) { agentList->getLastAgentID());
if (newAgent->getAgentID() == agentList->getLastAgentID()) {
agentList->increaseAgentID(); agentList->increaseAgentID();
} }

View file

@ -24,6 +24,8 @@ AudioInjector::AudioInjector(const char* filename) :
_indexOfNextSlot(0), _indexOfNextSlot(0),
_isInjectingAudio(false) _isInjectingAudio(false)
{ {
loadRandomIdentifier(_streamIdentifier, STREAM_IDENTIFIER_NUM_BYTES);
std::fstream sourceFile; std::fstream sourceFile;
sourceFile.open(filename, std::ios::in | std::ios::binary); sourceFile.open(filename, std::ios::in | std::ios::binary);
@ -51,6 +53,8 @@ AudioInjector::AudioInjector(int maxNumSamples) :
_indexOfNextSlot(0), _indexOfNextSlot(0),
_isInjectingAudio(false) _isInjectingAudio(false)
{ {
loadRandomIdentifier(_streamIdentifier, STREAM_IDENTIFIER_NUM_BYTES);
_audioSampleArray = new int16_t[maxNumSamples]; _audioSampleArray = new int16_t[maxNumSamples];
memset(_audioSampleArray, 0, _numTotalSamples * sizeof(int16_t)); memset(_audioSampleArray, 0, _numTotalSamples * sizeof(int16_t));
} }
@ -72,6 +76,10 @@ void AudioInjector::injectAudio(UDPSocket* injectorSocket, sockaddr* destination
dataPacket[0] = PACKET_HEADER_INJECT_AUDIO; dataPacket[0] = PACKET_HEADER_INJECT_AUDIO;
unsigned char *currentPacketPtr = dataPacket + 1; unsigned char *currentPacketPtr = dataPacket + 1;
// copy the identifier for this injector
memcpy(currentPacketPtr, &_streamIdentifier, sizeof(_streamIdentifier));
currentPacketPtr += sizeof(_streamIdentifier);
memcpy(currentPacketPtr, &_position, sizeof(_position)); memcpy(currentPacketPtr, &_position, sizeof(_position));
currentPacketPtr += sizeof(_position); currentPacketPtr += sizeof(_position);

View file

@ -13,14 +13,14 @@
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include "AudioRingBuffer.h"
const int BUFFER_LENGTH_BYTES = 512; const int BUFFER_LENGTH_BYTES = 512;
const int BUFFER_LENGTH_SAMPLES = BUFFER_LENGTH_BYTES / sizeof(int16_t); const int BUFFER_LENGTH_SAMPLES = BUFFER_LENGTH_BYTES / sizeof(int16_t);
const float SAMPLE_RATE = 22050.0f; const float SAMPLE_RATE = 22050.0f;
const float BUFFER_SEND_INTERVAL_USECS = (BUFFER_LENGTH_SAMPLES / SAMPLE_RATE) * 1000000; const float BUFFER_SEND_INTERVAL_USECS = (BUFFER_LENGTH_SAMPLES / SAMPLE_RATE) * 1000000;
class AudioInjector { class AudioInjector {
friend class AudioInjectionManager;
public: public:
AudioInjector(const char* filename); AudioInjector(const char* filename);
AudioInjector(int maxNumSamples); AudioInjector(int maxNumSamples);
@ -43,6 +43,7 @@ public:
void addSample(const int16_t sample); void addSample(const int16_t sample);
void addSamples(int16_t* sampleBuffer, int numSamples); void addSamples(int16_t* sampleBuffer, int numSamples);
private: private:
unsigned char _streamIdentifier[STREAM_IDENTIFIER_NUM_BYTES];
int16_t* _audioSampleArray; int16_t* _audioSampleArray;
int _numTotalSamples; int _numTotalSamples;
glm::vec3 _position; glm::vec3 _position;

View file

@ -18,8 +18,9 @@ AudioRingBuffer::AudioRingBuffer(int ringSamples, int bufferSamples) :
_endOfLastWrite(NULL), _endOfLastWrite(NULL),
_started(false), _started(false),
_shouldBeAddedToMix(false), _shouldBeAddedToMix(false),
_shouldLoopbackForAgent(false) { _shouldLoopbackForAgent(false),
_streamIdentifier()
{
_buffer = new int16_t[_ringBufferLengthSamples]; _buffer = new int16_t[_ringBufferLengthSamples];
_nextOutput = _buffer; _nextOutput = _buffer;
}; };
@ -39,6 +40,12 @@ int AudioRingBuffer::parseData(unsigned char* sourceBuffer, int numBytes) {
// if this came from an injector or interface client // if this came from an injector or interface client
// there's data required for spatialization to pull out // there's data required for spatialization to pull out
if (sourceBuffer[0] == PACKET_HEADER_INJECT_AUDIO) {
// we've got a stream identifier to pull from the packet
memcpy(&_streamIdentifier, dataBuffer, sizeof(_streamIdentifier));
dataBuffer += sizeof(_streamIdentifier);
}
memcpy(&_position, dataBuffer, sizeof(_position)); memcpy(&_position, dataBuffer, sizeof(_position));
dataBuffer += (sizeof(_position)); dataBuffer += (sizeof(_position));

View file

@ -10,13 +10,12 @@
#define __interface__AudioRingBuffer__ #define __interface__AudioRingBuffer__
#include <stdint.h> #include <stdint.h>
#include <glm/glm.hpp>
#include "AgentData.h" #include "AgentData.h"
struct Position { const int STREAM_IDENTIFIER_NUM_BYTES = 8;
float x;
float y;
float z;
};
class AudioRingBuffer : public AgentData { class AudioRingBuffer : public AgentData {
public: public:
@ -39,10 +38,11 @@ public:
bool shouldBeAddedToMix() const { return _shouldBeAddedToMix; } bool shouldBeAddedToMix() const { return _shouldBeAddedToMix; }
void setShouldBeAddedToMix(bool shouldBeAddedToMix) { _shouldBeAddedToMix = shouldBeAddedToMix; } void setShouldBeAddedToMix(bool shouldBeAddedToMix) { _shouldBeAddedToMix = shouldBeAddedToMix; }
const Position& getPosition() const { return _position; } const glm::vec3& getPosition() const { return _position; }
float getAttenuationRatio() const { return _attenuationRatio; } float getAttenuationRatio() const { return _attenuationRatio; }
float getBearing() const { return _bearing; } float getBearing() const { return _bearing; }
bool shouldLoopbackForAgent() const { return _shouldLoopbackForAgent; } bool shouldLoopbackForAgent() const { return _shouldLoopbackForAgent; }
const unsigned char* getStreamIdentifier() const { return _streamIdentifier; }
short diffLastWriteNextOutput(); short diffLastWriteNextOutput();
private: private:
@ -52,7 +52,7 @@ private:
int _ringBufferLengthSamples; int _ringBufferLengthSamples;
int _bufferLengthSamples; int _bufferLengthSamples;
Position _position; glm::vec3 _position;
float _attenuationRatio; float _attenuationRatio;
float _bearing; float _bearing;
int16_t* _nextOutput; int16_t* _nextOutput;
@ -61,6 +61,7 @@ private:
bool _started; bool _started;
bool _shouldBeAddedToMix; bool _shouldBeAddedToMix;
bool _shouldLoopbackForAgent; bool _shouldLoopbackForAgent;
unsigned char _streamIdentifier[STREAM_IDENTIFIER_NUM_BYTES];
}; };
#endif /* defined(__interface__AudioRingBuffer__) */ #endif /* defined(__interface__AudioRingBuffer__) */

View file

@ -19,7 +19,6 @@
using namespace std; using namespace std;
using avatars_lib::printLog; using avatars_lib::printLog;
int packFloatAngleToTwoByte(unsigned char* buffer, float angle) { int packFloatAngleToTwoByte(unsigned char* buffer, float angle) {
const float ANGLE_CONVERSION_RATIO = (std::numeric_limits<uint16_t>::max() / 360.0); const float ANGLE_CONVERSION_RATIO = (std::numeric_limits<uint16_t>::max() / 360.0);

View file

@ -126,11 +126,7 @@ void AgentList::processBulkAgentData(sockaddr *senderAddress, unsigned char *pac
if (!matchingAgent) { if (!matchingAgent) {
// we're missing this agent, we need to add it to the list // we're missing this agent, we need to add it to the list
addOrUpdateAgent(NULL, NULL, AGENT_TYPE_AVATAR, agentID); matchingAgent = addOrUpdateAgent(NULL, NULL, AGENT_TYPE_AVATAR, agentID);
// TODO: this is a really stupid way to do this
// Add a reverse iterator and go from the end of the list
matchingAgent = agentWithID(agentID);
} }
currentPosition += updateAgentWithData(matchingAgent, currentPosition += updateAgentWithData(matchingAgent,
@ -218,7 +214,7 @@ int AgentList::processDomainServerList(unsigned char *packetData, size_t dataByt
return readAgents; return readAgents;
} }
bool AgentList::addOrUpdateAgent(sockaddr *publicSocket, sockaddr *localSocket, char agentType, uint16_t agentId) { Agent* AgentList::addOrUpdateAgent(sockaddr* publicSocket, sockaddr* localSocket, char agentType, uint16_t agentId) {
AgentList::iterator agent = end(); AgentList::iterator agent = end();
if (publicSocket != NULL) { if (publicSocket != NULL) {
@ -250,7 +246,7 @@ bool AgentList::addOrUpdateAgent(sockaddr *publicSocket, sockaddr *localSocket,
addAgentToList(newAgent); addAgentToList(newAgent);
return true; return newAgent;
} else { } else {
if (agent->getType() == AGENT_TYPE_AUDIO_MIXER || agent->getType() == AGENT_TYPE_VOXEL) { if (agent->getType() == AGENT_TYPE_AUDIO_MIXER || agent->getType() == AGENT_TYPE_VOXEL) {
@ -260,7 +256,7 @@ bool AgentList::addOrUpdateAgent(sockaddr *publicSocket, sockaddr *localSocket,
} }
// we had this agent already, do nothing for now // we had this agent already, do nothing for now
return false; return &*agent;
} }
} }

View file

@ -59,7 +59,7 @@ public:
Agent* agentWithAddress(sockaddr *senderAddress); Agent* agentWithAddress(sockaddr *senderAddress);
Agent* agentWithID(uint16_t agentID); Agent* agentWithID(uint16_t agentID);
bool addOrUpdateAgent(sockaddr *publicSocket, sockaddr *localSocket, char agentType, uint16_t agentId); Agent* addOrUpdateAgent(sockaddr* publicSocket, sockaddr* localSocket, char agentType, uint16_t agentId);
void processAgentData(sockaddr *senderAddress, unsigned char *packetData, size_t dataBytes); void processAgentData(sockaddr *senderAddress, unsigned char *packetData, size_t dataBytes);
void processBulkAgentData(sockaddr *senderAddress, unsigned char *packetData, int numTotalBytes); void processBulkAgentData(sockaddr *senderAddress, unsigned char *packetData, int numTotalBytes);

View file

@ -114,6 +114,15 @@ void switchToResourcesParentIfRequired() {
#endif #endif
} }
void loadRandomIdentifier(unsigned char* identifierBuffer, int numBytes) {
// seed the the random number generator
srand(time(NULL));
for (int i = 0; i < numBytes; i++) {
identifierBuffer[i] = rand() % 256;
}
}
////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////
// Function: getCmdOption() // Function: getCmdOption()
// Description: Handy little function to tell you if a command line flag and option was // Description: Handy little function to tell you if a command line flag and option was

View file

@ -53,6 +53,8 @@ void setAtBit(unsigned char& byte, int bitIndex);
void switchToResourcesParentIfRequired(); void switchToResourcesParentIfRequired();
void loadRandomIdentifier(unsigned char* identifierBuffer, int numBytes);
const char* getCmdOption(int argc, const char * argv[],const char* option); const char* getCmdOption(int argc, const char * argv[],const char* option);
bool cmdOptionExists(int argc, const char * argv[],const char* option); bool cmdOptionExists(int argc, const char * argv[],const char* option);

View file

@ -614,9 +614,12 @@ int main(int argc, const char * argv[])
if (packetData[0] == PACKET_HEADER_HEAD_DATA) { if (packetData[0] == PACKET_HEADER_HEAD_DATA) {
uint16_t agentID = 0; uint16_t agentID = 0;
unpackAgentId(packetData + sizeof(PACKET_HEADER_HEAD_DATA), &agentID); unpackAgentId(packetData + sizeof(PACKET_HEADER_HEAD_DATA), &agentID);
agentList->addOrUpdateAgent(&agentPublicAddress, &agentPublicAddress, AGENT_TYPE_AVATAR, agentID); Agent* agent = agentList->addOrUpdateAgent(&agentPublicAddress,
&agentPublicAddress,
AGENT_TYPE_AVATAR,
agentID);
agentList->updateAgentWithData(&agentPublicAddress, packetData, receivedBytes); agentList->updateAgentWithData(agent, packetData, receivedBytes);
} }
} }
} }