mirror of
https://github.com/overte-org/overte.git
synced 2025-08-08 18:16:45 +02:00
add specialized parsing of audio data to ARB subclasses
This commit is contained in:
parent
4cb00ad54b
commit
d64b46a6b2
7 changed files with 73 additions and 27 deletions
|
@ -6,6 +6,8 @@
|
||||||
// Copyright (c) 2013 HighFidelity, Inc. All rights reserved.
|
// Copyright (c) 2013 HighFidelity, Inc. All rights reserved.
|
||||||
//
|
//
|
||||||
|
|
||||||
|
#include <PacketHeaders.h>
|
||||||
|
|
||||||
#include "InjectedAudioRingBuffer.h"
|
#include "InjectedAudioRingBuffer.h"
|
||||||
|
|
||||||
InjectedAudioRingBuffer::InjectedAudioRingBuffer() :
|
InjectedAudioRingBuffer::InjectedAudioRingBuffer() :
|
||||||
|
@ -14,4 +16,26 @@ InjectedAudioRingBuffer::InjectedAudioRingBuffer() :
|
||||||
_streamIdentifier()
|
_streamIdentifier()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int InjectedAudioRingBuffer::parseData(unsigned char* sourceBuffer, int numBytes) {
|
||||||
|
unsigned char* currentBuffer = sourceBuffer + sizeof(PACKET_HEADER_INJECT_AUDIO);
|
||||||
|
|
||||||
|
// pull stream identifier from the packet
|
||||||
|
memcpy(&_streamIdentifier, currentBuffer, sizeof(_streamIdentifier));
|
||||||
|
currentBuffer += sizeof(_streamIdentifier);
|
||||||
|
|
||||||
|
// use parsePositionalData in parent PostionalAudioRingBuffer class to pull common positional data
|
||||||
|
currentBuffer += parsePositionalData(currentBuffer, numBytes - (currentBuffer - sourceBuffer));
|
||||||
|
|
||||||
|
// pull out the radius for this injected source - if it's zero this is a point source
|
||||||
|
memcpy(&_radius, currentBuffer, sizeof(_radius));
|
||||||
|
currentBuffer += sizeof(_radius);
|
||||||
|
|
||||||
|
unsigned int attenuationByte = *(currentBuffer++);
|
||||||
|
_attenuationRatio = attenuationByte / MAX_INJECTOR_VOLUME;
|
||||||
|
|
||||||
|
currentBuffer += parseAudioSamples(currentBuffer, numBytes - (currentBuffer - sourceBuffer));
|
||||||
|
|
||||||
|
return currentBuffer - sourceBuffer;
|
||||||
}
|
}
|
|
@ -17,6 +17,8 @@ class InjectedAudioRingBuffer : public PositionalAudioRingBuffer {
|
||||||
public:
|
public:
|
||||||
InjectedAudioRingBuffer();
|
InjectedAudioRingBuffer();
|
||||||
|
|
||||||
|
int parseData(unsigned char* sourceBuffer, int numBytes);
|
||||||
|
|
||||||
float getRadius() const { return _radius; }
|
float getRadius() const { return _radius; }
|
||||||
float getAttenuationRatio() const { return _attenuationRatio; }
|
float getAttenuationRatio() const { return _attenuationRatio; }
|
||||||
const unsigned char* getStreamIdentifier() const { return _streamIdentifier; }
|
const unsigned char* getStreamIdentifier() const { return _streamIdentifier; }
|
||||||
|
|
|
@ -6,6 +6,8 @@
|
||||||
// Copyright (c) 2013 HighFidelity, Inc. All rights reserved.
|
// Copyright (c) 2013 HighFidelity, Inc. All rights reserved.
|
||||||
//
|
//
|
||||||
|
|
||||||
|
#include <PacketHeaders.h>
|
||||||
|
|
||||||
#include "PositionalAudioRingBuffer.h"
|
#include "PositionalAudioRingBuffer.h"
|
||||||
|
|
||||||
PositionalAudioRingBuffer::PositionalAudioRingBuffer() :
|
PositionalAudioRingBuffer::PositionalAudioRingBuffer() :
|
||||||
|
@ -18,6 +20,33 @@ PositionalAudioRingBuffer::PositionalAudioRingBuffer() :
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int PositionalAudioRingBuffer::parseData(unsigned char* sourceBuffer, int numBytes) {
|
||||||
|
unsigned char* currentBuffer = sourceBuffer + sizeof(PACKET_HEADER);
|
||||||
|
currentBuffer += parsePositionalData(currentBuffer, numBytes - (currentBuffer - sourceBuffer));
|
||||||
|
currentBuffer += parseAudioSamples(currentBuffer, numBytes - (currentBuffer - sourceBuffer));
|
||||||
|
|
||||||
|
return currentBuffer - sourceBuffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
int PositionalAudioRingBuffer::parsePositionalData(unsigned char* sourceBuffer, int numBytes) {
|
||||||
|
unsigned char* currentBuffer = sourceBuffer;
|
||||||
|
|
||||||
|
memcpy(&_position, currentBuffer, sizeof(_position));
|
||||||
|
currentBuffer += sizeof(_position);
|
||||||
|
|
||||||
|
memcpy(&_orientation, sourceBuffer, sizeof(_orientation));
|
||||||
|
currentBuffer += sizeof(_orientation);
|
||||||
|
|
||||||
|
// if this agent sent us a NaN for first float in orientation then don't consider this good audio and bail
|
||||||
|
if (std::isnan(_orientation.x)) {
|
||||||
|
_endOfLastWrite = _nextOutput = _buffer;
|
||||||
|
_isStarted = false;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return currentBuffer - sourceBuffer;
|
||||||
|
}
|
||||||
|
|
||||||
bool PositionalAudioRingBuffer::shouldBeAddedToMix(int numJitterBufferSamples) {
|
bool PositionalAudioRingBuffer::shouldBeAddedToMix(int numJitterBufferSamples) {
|
||||||
if (_endOfLastWrite) {
|
if (_endOfLastWrite) {
|
||||||
if (!_isStarted && diffLastWriteNextOutput() <= BUFFER_LENGTH_SAMPLES_PER_CHANNEL + numJitterBufferSamples) {
|
if (!_isStarted && diffLastWriteNextOutput() <= BUFFER_LENGTH_SAMPLES_PER_CHANNEL + numJitterBufferSamples) {
|
||||||
|
|
|
@ -17,6 +17,9 @@ class PositionalAudioRingBuffer : public AudioRingBuffer {
|
||||||
public:
|
public:
|
||||||
PositionalAudioRingBuffer();
|
PositionalAudioRingBuffer();
|
||||||
|
|
||||||
|
int parseData(unsigned char* sourceBuffer, int numBytes);
|
||||||
|
int parsePositionalData(unsigned char* sourceBuffer, int numBytes);
|
||||||
|
|
||||||
bool shouldBeAddedToMix(int numJitterBufferSamples);
|
bool shouldBeAddedToMix(int numJitterBufferSamples);
|
||||||
|
|
||||||
bool wasAddedToMix() const { return _wasAddedToMix; }
|
bool wasAddedToMix() const { return _wasAddedToMix; }
|
||||||
|
|
|
@ -142,9 +142,6 @@ int audioCallback (const void* inputBuffer,
|
||||||
memcpy(currentPacketPtr, &headPosition, sizeof(headPosition));
|
memcpy(currentPacketPtr, &headPosition, sizeof(headPosition));
|
||||||
currentPacketPtr += (sizeof(headPosition));
|
currentPacketPtr += (sizeof(headPosition));
|
||||||
|
|
||||||
// tell the mixer not to add additional attenuation to our source
|
|
||||||
*(currentPacketPtr++) = 255;
|
|
||||||
|
|
||||||
// memcpy our orientation
|
// memcpy our orientation
|
||||||
memcpy(currentPacketPtr, &headOrientation, sizeof(headOrientation));
|
memcpy(currentPacketPtr, &headOrientation, sizeof(headOrientation));
|
||||||
currentPacketPtr += sizeof(headOrientation);
|
currentPacketPtr += sizeof(headOrientation);
|
||||||
|
|
|
@ -15,8 +15,6 @@
|
||||||
|
|
||||||
#include "AudioInjector.h"
|
#include "AudioInjector.h"
|
||||||
|
|
||||||
const int MAX_INJECTOR_VOLUME = 0xFF;
|
|
||||||
|
|
||||||
AudioInjector::AudioInjector(const char* filename) :
|
AudioInjector::AudioInjector(const char* filename) :
|
||||||
_position(0.0f, 0.0f, 0.0f),
|
_position(0.0f, 0.0f, 0.0f),
|
||||||
_orientation(0.0f, 0.0f, 0.0f, 0.0f),
|
_orientation(0.0f, 0.0f, 0.0f, 0.0f),
|
||||||
|
@ -72,20 +70,17 @@ void AudioInjector::injectAudio(UDPSocket* injectorSocket, sockaddr* destination
|
||||||
timeval startTime;
|
timeval startTime;
|
||||||
|
|
||||||
// calculate the number of bytes required for additional data
|
// calculate the number of bytes required for additional data
|
||||||
int leadingBytes = sizeof(PACKET_HEADER) + sizeof(INJECT_AUDIO_AT_POINT_COMMAND) + sizeof(_streamIdentifier)
|
int leadingBytes = sizeof(PACKET_HEADER)
|
||||||
+ sizeof(_position) + sizeof(_orientation) + sizeof(_volume);
|
+ sizeof(_streamIdentifier)
|
||||||
|
+ sizeof(_position)
|
||||||
if (_radius > 0) {
|
+ sizeof(_orientation)
|
||||||
// we'll need 4 extra bytes if the cube side length is being sent as well
|
+ sizeof(_radius)
|
||||||
leadingBytes += sizeof(_radius);
|
+ sizeof(_volume);
|
||||||
}
|
|
||||||
|
|
||||||
unsigned char dataPacket[BUFFER_LENGTH_BYTES + leadingBytes];
|
unsigned char dataPacket[BUFFER_LENGTH_BYTES + leadingBytes];
|
||||||
|
|
||||||
dataPacket[0] = PACKET_HEADER_INJECT_AUDIO;
|
dataPacket[0] = PACKET_HEADER_INJECT_AUDIO;
|
||||||
// add the correct command for point source or cube of sound
|
unsigned char *currentPacketPtr = dataPacket + sizeof(PACKET_HEADER_INJECT_AUDIO);
|
||||||
dataPacket[1] = (_radius > 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
|
// copy the identifier for this injector
|
||||||
memcpy(currentPacketPtr, &_streamIdentifier, sizeof(_streamIdentifier));
|
memcpy(currentPacketPtr, &_streamIdentifier, sizeof(_streamIdentifier));
|
||||||
|
@ -94,19 +89,15 @@ void AudioInjector::injectAudio(UDPSocket* injectorSocket, sockaddr* destination
|
||||||
memcpy(currentPacketPtr, &_position, sizeof(_position));
|
memcpy(currentPacketPtr, &_position, sizeof(_position));
|
||||||
currentPacketPtr += sizeof(_position);
|
currentPacketPtr += sizeof(_position);
|
||||||
|
|
||||||
if (_radius > 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, &_radius, sizeof(_radius));
|
|
||||||
currentPacketPtr += sizeof(_radius);
|
|
||||||
}
|
|
||||||
|
|
||||||
*currentPacketPtr = _volume;
|
|
||||||
currentPacketPtr++;
|
|
||||||
|
|
||||||
memcpy(currentPacketPtr, &_orientation, sizeof(_orientation));
|
memcpy(currentPacketPtr, &_orientation, sizeof(_orientation));
|
||||||
currentPacketPtr += sizeof(_orientation);
|
currentPacketPtr += sizeof(_orientation);
|
||||||
|
|
||||||
|
memcpy(currentPacketPtr, &_radius, sizeof(_radius));
|
||||||
|
currentPacketPtr += sizeof(_radius);
|
||||||
|
|
||||||
|
*currentPacketPtr = _volume;
|
||||||
|
currentPacketPtr++;
|
||||||
|
|
||||||
gettimeofday(&startTime, NULL);
|
gettimeofday(&startTime, NULL);
|
||||||
int nextFrame = 0;
|
int nextFrame = 0;
|
||||||
|
|
||||||
|
|
|
@ -17,8 +17,8 @@
|
||||||
#include "AudioRingBuffer.h"
|
#include "AudioRingBuffer.h"
|
||||||
|
|
||||||
const int STREAM_IDENTIFIER_NUM_BYTES = 8;
|
const int STREAM_IDENTIFIER_NUM_BYTES = 8;
|
||||||
const char INJECT_AUDIO_AT_POINT_COMMAND = 'P';
|
|
||||||
const char INJECT_AUDIO_AT_CUBE_COMMAND = 'C';
|
const int MAX_INJECTOR_VOLUME = 0xFF;
|
||||||
|
|
||||||
const float INJECT_INTERVAL_USECS = (BUFFER_LENGTH_SAMPLES_PER_CHANNEL / SAMPLE_RATE) * 1000000;
|
const float INJECT_INTERVAL_USECS = (BUFFER_LENGTH_SAMPLES_PER_CHANNEL / SAMPLE_RATE) * 1000000;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue