prepend audio packets from the mixer with a packet header

This commit is contained in:
Stephen Birarda 2013-05-15 12:00:04 -07:00
parent ce34a8f3e0
commit e83710e45e
3 changed files with 33 additions and 18 deletions

View file

@ -99,6 +99,11 @@ int main(int argc, const char* argv[]) {
int nextFrame = 0;
timeval startTime;
unsigned char clientPacket[BUFFER_LENGTH_BYTES + 1];
clientPacket[0] = PACKET_HEADER_MIXED_AUDIO;
int16_t clientSamples[BUFFER_LENGTH_SAMPLES_PER_CHANNEL * 2] = {};
gettimeofday(&startTime, NULL);
while (true) {
@ -129,8 +134,9 @@ int main(int argc, const char* argv[]) {
for (AgentList::iterator agent = agentList->begin(); agent != agentList->end(); agent++) {
AudioRingBuffer* agentRingBuffer = (AudioRingBuffer*) agent->getLinkedData();
int16_t clientMix[BUFFER_LENGTH_SAMPLES_PER_CHANNEL * 2] = {};
// zero out the client mix for this agent
memset(clientSamples, 0, sizeof(clientSamples));
for (AgentList::iterator otherAgent = agentList->begin(); otherAgent != agentList->end(); otherAgent++) {
if (otherAgent != agent || (otherAgent == agent && agentRingBuffer->shouldLoopbackForAgent())) {
@ -219,11 +225,11 @@ int main(int argc, const char* argv[]) {
}
int16_t* goodChannel = bearingRelativeAngleToSource > 0.0f
? clientMix + BUFFER_LENGTH_SAMPLES_PER_CHANNEL
: clientMix;
? clientSamples + BUFFER_LENGTH_SAMPLES_PER_CHANNEL
: clientSamples;
int16_t* delayedChannel = bearingRelativeAngleToSource > 0.0f
? clientMix
: clientMix + BUFFER_LENGTH_SAMPLES_PER_CHANNEL;
? clientSamples
: clientSamples + BUFFER_LENGTH_SAMPLES_PER_CHANNEL;
int16_t* delaySamplePointer = otherAgentBuffer->getNextOutput() == otherAgentBuffer->getBuffer()
? otherAgentBuffer->getBuffer() + RING_BUFFER_SAMPLES - numSamplesDelay
@ -249,7 +255,8 @@ int main(int argc, const char* argv[]) {
}
}
agentList->getAgentSocket().send(agent->getPublicSocket(), clientMix, BUFFER_LENGTH_BYTES);
memcpy(clientPacket + 1, clientSamples, sizeof(clientSamples));
agentList->getAgentSocket().send(agent->getPublicSocket(), clientSamples, BUFFER_LENGTH_BYTES + 1);
}
// push forward the next output pointers for any audio buffers we used

View file

@ -1995,6 +1995,9 @@ void* Application::networkReceive(void* args) {
printf("The rotation: %f, %f, %f\n", rotationRates[0], rotationRates[1], rotationRates[2]);
break;
case PACKET_HEADER_MIXED_AUDIO:
app->_audio.addReceivedAudioToBuffer(app->_incomingPacket, bytesReceived);
break;
case PACKET_HEADER_VOXEL_DATA:
case PACKET_HEADER_VOXEL_DATA_MONOCHROME:
case PACKET_HEADER_Z_COMMAND:

View file

@ -7,6 +7,9 @@
//
#include <cstring>
#include "PacketHeaders.h"
#include "AudioRingBuffer.h"
AudioRingBuffer::AudioRingBuffer(int ringSamples, int bufferSamples) :
@ -46,18 +49,22 @@ AudioRingBuffer* AudioRingBuffer::clone() const {
const int AGENT_LOOPBACK_MODIFIER = 307;
int AudioRingBuffer::parseData(unsigned char* sourceBuffer, int numBytes) {
if (numBytes > (_bufferLengthSamples * sizeof(int16_t))) {
unsigned char* dataBuffer = sourceBuffer + 1;
if (sourceBuffer[0] == PACKET_HEADER_INJECT_AUDIO ||
sourceBuffer[0] == PACKET_HEADER_MICROPHONE_AUDIO) {
// if this came from an injector or interface client
// there's data required for spatialization to pull out
unsigned char *dataPtr = sourceBuffer + 1;
memcpy(&_position, dataBuffer, sizeof(_position));
dataBuffer += (sizeof(_position));
memcpy(&_position, dataPtr, sizeof(_position));
dataPtr += (sizeof(_position));
unsigned int attenuationByte = *(dataPtr++);
unsigned int attenuationByte = *(dataBuffer++);
_attenuationRatio = attenuationByte / 255.0f;
memcpy(&_bearing, dataPtr, sizeof(float));
dataPtr += sizeof(_bearing);
memcpy(&_bearing, dataBuffer, sizeof(float));
dataBuffer += sizeof(_bearing);
if (_bearing > 180 || _bearing < -180) {
// we were passed an invalid bearing because this agent wants loopback (pressed the H key)
@ -70,8 +77,6 @@ int AudioRingBuffer::parseData(unsigned char* sourceBuffer, int numBytes) {
} else {
_shouldLoopbackForAgent = false;
}
sourceBuffer = dataPtr;
}
if (!_endOfLastWrite) {
@ -82,7 +87,7 @@ int AudioRingBuffer::parseData(unsigned char* sourceBuffer, int numBytes) {
_started = false;
}
memcpy(_endOfLastWrite, sourceBuffer, _bufferLengthSamples * sizeof(int16_t));
memcpy(_endOfLastWrite, dataBuffer, _bufferLengthSamples * sizeof(int16_t));
_endOfLastWrite += _bufferLengthSamples;