mirror of
https://github.com/overte-org/overte.git
synced 2025-07-10 20:58:36 +02:00
Merge pull request #317 from birarda/audio-refactor
refactoring Audio in interface and audio-mixer
This commit is contained in:
commit
ab3ee22dc9
13 changed files with 262 additions and 441 deletions
|
@ -99,6 +99,11 @@ int main(int argc, const char* argv[]) {
|
||||||
int nextFrame = 0;
|
int nextFrame = 0;
|
||||||
timeval startTime;
|
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);
|
gettimeofday(&startTime, NULL);
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
|
@ -129,8 +134,9 @@ int main(int argc, const char* argv[]) {
|
||||||
|
|
||||||
for (AgentList::iterator agent = agentList->begin(); agent != agentList->end(); agent++) {
|
for (AgentList::iterator agent = agentList->begin(); agent != agentList->end(); agent++) {
|
||||||
AudioRingBuffer* agentRingBuffer = (AudioRingBuffer*) agent->getLinkedData();
|
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++) {
|
for (AgentList::iterator otherAgent = agentList->begin(); otherAgent != agentList->end(); otherAgent++) {
|
||||||
if (otherAgent != agent || (otherAgent == agent && agentRingBuffer->shouldLoopbackForAgent())) {
|
if (otherAgent != agent || (otherAgent == agent && agentRingBuffer->shouldLoopbackForAgent())) {
|
||||||
|
@ -219,11 +225,11 @@ int main(int argc, const char* argv[]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int16_t* goodChannel = bearingRelativeAngleToSource > 0.0f
|
int16_t* goodChannel = bearingRelativeAngleToSource > 0.0f
|
||||||
? clientMix + BUFFER_LENGTH_SAMPLES_PER_CHANNEL
|
? clientSamples + BUFFER_LENGTH_SAMPLES_PER_CHANNEL
|
||||||
: clientMix;
|
: clientSamples;
|
||||||
int16_t* delayedChannel = bearingRelativeAngleToSource > 0.0f
|
int16_t* delayedChannel = bearingRelativeAngleToSource > 0.0f
|
||||||
? clientMix
|
? clientSamples
|
||||||
: clientMix + BUFFER_LENGTH_SAMPLES_PER_CHANNEL;
|
: clientSamples + BUFFER_LENGTH_SAMPLES_PER_CHANNEL;
|
||||||
|
|
||||||
int16_t* delaySamplePointer = otherAgentBuffer->getNextOutput() == otherAgentBuffer->getBuffer()
|
int16_t* delaySamplePointer = otherAgentBuffer->getNextOutput() == otherAgentBuffer->getBuffer()
|
||||||
? otherAgentBuffer->getBuffer() + RING_BUFFER_SAMPLES - numSamplesDelay
|
? 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(), 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
|
||||||
|
@ -268,9 +275,12 @@ 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) {
|
if (packetData[0] == PACKET_HEADER_INJECT_AUDIO || packetData[0] == PACKET_HEADER_MICROPHONE_AUDIO) {
|
||||||
|
char agentType = (packetData[0] == PACKET_HEADER_MICROPHONE_AUDIO)
|
||||||
|
? AGENT_TYPE_AVATAR
|
||||||
|
: AGENT_TYPE_AUDIO_INJECTOR;
|
||||||
|
|
||||||
if (agentList->addOrUpdateAgent(agentAddress, agentAddress, packetData[0], agentList->getLastAgentID())) {
|
if (agentList->addOrUpdateAgent(agentAddress, agentAddress, agentType, agentList->getLastAgentID())) {
|
||||||
agentList->increaseAgentID();
|
agentList->increaseAgentID();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -154,7 +154,7 @@ Application::Application(int& argc, char** argv) :
|
||||||
_oculusProgram(0),
|
_oculusProgram(0),
|
||||||
_oculusDistortionScale(1.25),
|
_oculusDistortionScale(1.25),
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
_audio(&_audioScope, &_myAvatar),
|
_audio(&_audioScope),
|
||||||
#endif
|
#endif
|
||||||
_stopNetworkReceiveThread(false),
|
_stopNetworkReceiveThread(false),
|
||||||
_packetCount(0),
|
_packetCount(0),
|
||||||
|
@ -202,10 +202,6 @@ Application::Application(int& argc, char** argv) :
|
||||||
// the callback for our instance of AgentList is attachNewHeadToAgent
|
// the callback for our instance of AgentList is attachNewHeadToAgent
|
||||||
AgentList::getInstance()->linkedDataCreateCallback = &attachNewHeadToAgent;
|
AgentList::getInstance()->linkedDataCreateCallback = &attachNewHeadToAgent;
|
||||||
|
|
||||||
#ifndef _WIN32
|
|
||||||
AgentList::getInstance()->audioMixerSocketUpdate = &audioMixerUpdate;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
WSADATA WsaData;
|
WSADATA WsaData;
|
||||||
int wsaresult = WSAStartup(MAKEWORD(2,2), &WsaData);
|
int wsaresult = WSAStartup(MAKEWORD(2,2), &WsaData);
|
||||||
|
@ -913,11 +909,7 @@ void Application::terminate() {
|
||||||
// Close serial port
|
// Close serial port
|
||||||
// close(serial_fd);
|
// close(serial_fd);
|
||||||
|
|
||||||
_myAvatar.writeAvatarDataToFile();
|
_myAvatar.writeAvatarDataToFile();
|
||||||
|
|
||||||
#ifndef _WIN32
|
|
||||||
_audio.terminate();
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if (_enableNetworkThread) {
|
if (_enableNetworkThread) {
|
||||||
_stopNetworkReceiveThread = true;
|
_stopNetworkReceiveThread = true;
|
||||||
|
@ -1278,7 +1270,7 @@ void Application::updateAvatar(float deltaTime) {
|
||||||
|
|
||||||
// Get audio loudness data from audio input device
|
// Get audio loudness data from audio input device
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
_myAvatar.setLoudness(_audio.getInputLoudness());
|
_myAvatar.setLoudness(_audio.getLastInputLoudness());
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Update Avatar with latest camera and view frustum data...
|
// Update Avatar with latest camera and view frustum data...
|
||||||
|
@ -1972,12 +1964,6 @@ void Application::attachNewHeadToAgent(Agent *newAgent) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef _WIN32
|
|
||||||
void Application::audioMixerUpdate(in_addr_t newMixerAddress, in_port_t newMixerPort) {
|
|
||||||
static_cast<Application*>(QCoreApplication::instance())->_audio.updateMixerParams(newMixerAddress, newMixerPort);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Receive packets from other agents/servers and decide what to do with them!
|
// Receive packets from other agents/servers and decide what to do with them!
|
||||||
void* Application::networkReceive(void* args) {
|
void* Application::networkReceive(void* args) {
|
||||||
sockaddr senderAddress;
|
sockaddr senderAddress;
|
||||||
|
@ -2009,6 +1995,9 @@ void* Application::networkReceive(void* args) {
|
||||||
|
|
||||||
printf("The rotation: %f, %f, %f\n", rotationRates[0], rotationRates[1], rotationRates[2]);
|
printf("The rotation: %f, %f, %f\n", rotationRates[0], rotationRates[1], rotationRates[2]);
|
||||||
break;
|
break;
|
||||||
|
case PACKET_HEADER_MIXED_AUDIO:
|
||||||
|
app->_audio.addReceivedAudioToBuffer(app->_incomingPacket, bytesReceived);
|
||||||
|
break;
|
||||||
case PACKET_HEADER_VOXEL_DATA:
|
case PACKET_HEADER_VOXEL_DATA:
|
||||||
case PACKET_HEADER_VOXEL_DATA_MONOCHROME:
|
case PACKET_HEADER_VOXEL_DATA_MONOCHROME:
|
||||||
case PACKET_HEADER_Z_COMMAND:
|
case PACKET_HEADER_Z_COMMAND:
|
||||||
|
|
|
@ -57,6 +57,8 @@ public:
|
||||||
void mouseReleaseEvent(QMouseEvent* event);
|
void mouseReleaseEvent(QMouseEvent* event);
|
||||||
|
|
||||||
void wheelEvent(QWheelEvent* event);
|
void wheelEvent(QWheelEvent* event);
|
||||||
|
|
||||||
|
const Avatar& getAvatar() const { return _myAvatar; }
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
|
|
||||||
|
@ -119,9 +121,6 @@ private:
|
||||||
void setMenuShortcutsEnabled(bool enabled);
|
void setMenuShortcutsEnabled(bool enabled);
|
||||||
|
|
||||||
static void attachNewHeadToAgent(Agent *newAgent);
|
static void attachNewHeadToAgent(Agent *newAgent);
|
||||||
#ifndef _WIN32
|
|
||||||
static void audioMixerUpdate(in_addr_t newMixerAddress, in_port_t newMixerPort);
|
|
||||||
#endif
|
|
||||||
static void* networkReceive(void* args);
|
static void* networkReceive(void* args);
|
||||||
|
|
||||||
QMainWindow* _window;
|
QMainWindow* _window;
|
||||||
|
|
|
@ -10,19 +10,21 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
|
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
#include <StdDev.h>
|
#include <StdDev.h>
|
||||||
#include <UDPSocket.h>
|
#include <UDPSocket.h>
|
||||||
#include <SharedUtil.h>
|
#include <SharedUtil.h>
|
||||||
#include <PacketHeaders.h>
|
#include <PacketHeaders.h>
|
||||||
|
#include <AgentList.h>
|
||||||
|
#include <AgentTypes.h>
|
||||||
|
|
||||||
|
#include "Application.h"
|
||||||
#include "Audio.h"
|
#include "Audio.h"
|
||||||
#include "Util.h"
|
#include "Util.h"
|
||||||
#include "Log.h"
|
#include "Log.h"
|
||||||
|
|
||||||
Oscilloscope * scope;
|
|
||||||
|
|
||||||
const int NUM_AUDIO_CHANNELS = 2;
|
const int NUM_AUDIO_CHANNELS = 2;
|
||||||
|
|
||||||
const int PACKET_LENGTH_BYTES = 1024;
|
const int PACKET_LENGTH_BYTES = 1024;
|
||||||
|
@ -55,15 +57,8 @@ const float AUDIO_CALLBACK_MSECS = (float)BUFFER_LENGTH_SAMPLES / (float)SAMPLE_
|
||||||
|
|
||||||
const int AGENT_LOOPBACK_MODIFIER = 307;
|
const int AGENT_LOOPBACK_MODIFIER = 307;
|
||||||
|
|
||||||
const char LOCALHOST_MIXER[] = "0.0.0.0";
|
int numStarves = 0;
|
||||||
const char WORKCLUB_MIXER[] = "192.168.1.19";
|
|
||||||
const char EC2_WEST_MIXER[] = "54.241.92.53";
|
|
||||||
|
|
||||||
const int AUDIO_UDP_LISTEN_PORT = 55444;
|
|
||||||
|
|
||||||
int starve_counter = 0;
|
|
||||||
StDev stdev;
|
StDev stdev;
|
||||||
bool stopAudioReceiveThread = false;
|
|
||||||
|
|
||||||
int samplesLeftForFlange = 0;
|
int samplesLeftForFlange = 0;
|
||||||
int lastYawMeasuredMaximum = 0;
|
int lastYawMeasuredMaximum = 0;
|
||||||
|
@ -71,43 +66,31 @@ float flangeIntensity = 0;
|
||||||
float flangeRate = 0;
|
float flangeRate = 0;
|
||||||
float flangeWeight = 0;
|
float flangeWeight = 0;
|
||||||
|
|
||||||
timeval firstPlaybackTimer;
|
|
||||||
int packetsReceivedThisPlayback = 0;
|
|
||||||
float usecsAtStartup = 0;
|
float usecsAtStartup = 0;
|
||||||
|
|
||||||
/**
|
// inputBuffer A pointer to an internal portaudio data buffer containing data read by portaudio.
|
||||||
* Audio callback used by portaudio.
|
// outputBuffer A pointer to an internal portaudio data buffer to be read by the configured output device.
|
||||||
* Communicates with Audio via a shared pointer to Audio::data.
|
// frames Number of frames that portaudio requests to be read/written.
|
||||||
* Writes input audio channels (if they exist) into Audio::data->buffer,
|
// timeInfo Portaudio time info. Currently unused.
|
||||||
multiplied by Audio::data->inputGain.
|
// statusFlags Portaudio status flags. Currently unused.
|
||||||
* Then writes Audio::data->buffer into output audio channels, and clears
|
// userData Pointer to supplied user data (in this case, a pointer to the parent Audio object
|
||||||
the portion of Audio::data->buffer that has been read from for reuse.
|
int audioCallback (const void* inputBuffer,
|
||||||
*
|
void* outputBuffer,
|
||||||
* @param[in] inputBuffer A pointer to an internal portaudio data buffer containing data read by portaudio.
|
|
||||||
* @param[out] outputBuffer A pointer to an internal portaudio data buffer to be read by the configured output device.
|
|
||||||
* @param[in] frames Number of frames that portaudio requests to be read/written.
|
|
||||||
(Valid size of input/output buffers = frames * number of channels (2) * sizeof data type (float)).
|
|
||||||
* @param[in] timeInfo Portaudio time info. Currently unused.
|
|
||||||
* @param[in] statusFlags Portaudio status flags. Currently unused.
|
|
||||||
* @param[in] userData Pointer to supplied user data (in this case, a pointer to Audio::data).
|
|
||||||
Used to communicate with external code (since portaudio calls this function from another thread).
|
|
||||||
* @return Should be of type PaStreamCallbackResult. Return paComplete to end the stream, or paContinue to continue (default).
|
|
||||||
Can be used to end the stream from within the callback.
|
|
||||||
*/
|
|
||||||
|
|
||||||
int audioCallback (const void *inputBuffer,
|
|
||||||
void *outputBuffer,
|
|
||||||
unsigned long frames,
|
unsigned long frames,
|
||||||
const PaStreamCallbackTimeInfo *timeInfo,
|
const PaStreamCallbackTimeInfo *timeInfo,
|
||||||
PaStreamCallbackFlags statusFlags,
|
PaStreamCallbackFlags statusFlags,
|
||||||
void *userData)
|
void* userData) {
|
||||||
{
|
|
||||||
AudioData *data = (AudioData *) userData;
|
Audio* parentAudio = (Audio*) userData;
|
||||||
|
AgentList* agentList = AgentList::getInstance();
|
||||||
|
|
||||||
|
Application* interface = (Application*) QCoreApplication::instance();
|
||||||
|
Avatar interfaceAvatar = interface->getAvatar();
|
||||||
|
|
||||||
int16_t *inputLeft = ((int16_t **) inputBuffer)[0];
|
int16_t *inputLeft = ((int16_t **) inputBuffer)[0];
|
||||||
|
|
||||||
// Add Procedural effects to input samples
|
// Add Procedural effects to input samples
|
||||||
data->addProceduralSounds(inputLeft, BUFFER_LENGTH_SAMPLES);
|
parentAudio->addProceduralSounds(inputLeft, BUFFER_LENGTH_SAMPLES);
|
||||||
|
|
||||||
if (inputLeft != NULL) {
|
if (inputLeft != NULL) {
|
||||||
|
|
||||||
|
@ -118,35 +101,32 @@ int audioCallback (const void *inputBuffer,
|
||||||
}
|
}
|
||||||
|
|
||||||
loudness /= BUFFER_LENGTH_SAMPLES;
|
loudness /= BUFFER_LENGTH_SAMPLES;
|
||||||
data->lastInputLoudness = loudness;
|
parentAudio->_lastInputLoudness = loudness;
|
||||||
|
|
||||||
// add data to the scope
|
// add data to the scope
|
||||||
scope->addSamples(0, inputLeft, BUFFER_LENGTH_SAMPLES);
|
parentAudio->_scope->addSamples(0, inputLeft, BUFFER_LENGTH_SAMPLES);
|
||||||
|
|
||||||
if (data->mixerAddress != 0) {
|
Agent* audioMixer = agentList->soloAgentOfType(AGENT_TYPE_AUDIO_MIXER);
|
||||||
sockaddr_in audioMixerSocket;
|
|
||||||
audioMixerSocket.sin_family = AF_INET;
|
if (audioMixer) {
|
||||||
audioMixerSocket.sin_addr.s_addr = data->mixerAddress;
|
|
||||||
audioMixerSocket.sin_port = data->mixerPort;
|
|
||||||
|
|
||||||
int leadingBytes = 2 + (sizeof(float) * 4);
|
int leadingBytes = 2 + (sizeof(float) * 4);
|
||||||
|
|
||||||
// we need the amount of bytes in the buffer + 1 for type
|
// we need the amount of bytes in the buffer + 1 for type
|
||||||
// + 12 for 3 floats for position + float for bearing + 1 attenuation byte
|
// + 12 for 3 floats for position + float for bearing + 1 attenuation byte
|
||||||
unsigned char dataPacket[BUFFER_LENGTH_BYTES + leadingBytes];
|
unsigned char dataPacket[BUFFER_LENGTH_BYTES + leadingBytes];
|
||||||
|
|
||||||
dataPacket[0] = PACKET_HEADER_INJECT_AUDIO;
|
dataPacket[0] = PACKET_HEADER_MICROPHONE_AUDIO;
|
||||||
unsigned char *currentPacketPtr = dataPacket + 1;
|
unsigned char *currentPacketPtr = dataPacket + 1;
|
||||||
|
|
||||||
// memcpy the three float positions
|
// memcpy the three float positions
|
||||||
memcpy(currentPacketPtr, &data->linkedAvatar->getHeadPosition(), sizeof(float) * 3);
|
memcpy(currentPacketPtr, &interfaceAvatar.getHeadPosition(), sizeof(float) * 3);
|
||||||
currentPacketPtr += (sizeof(float) * 3);
|
currentPacketPtr += (sizeof(float) * 3);
|
||||||
|
|
||||||
// tell the mixer not to add additional attenuation to our source
|
// tell the mixer not to add additional attenuation to our source
|
||||||
*(currentPacketPtr++) = 255;
|
*(currentPacketPtr++) = 255;
|
||||||
|
|
||||||
// memcpy the corrected render yaw
|
// memcpy the corrected render yaw
|
||||||
float correctedYaw = fmodf(-1 * data->linkedAvatar->getAbsoluteHeadYaw(), 360);
|
float correctedYaw = fmodf(-1 * interfaceAvatar.getAbsoluteHeadYaw(), 360);
|
||||||
|
|
||||||
if (correctedYaw > 180) {
|
if (correctedYaw > 180) {
|
||||||
correctedYaw -= 360;
|
correctedYaw -= 360;
|
||||||
|
@ -154,29 +134,30 @@ int audioCallback (const void *inputBuffer,
|
||||||
correctedYaw += 360;
|
correctedYaw += 360;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data->mixerLoopbackFlag) {
|
if (parentAudio->_mixerLoopbackFlag) {
|
||||||
correctedYaw = correctedYaw > 0
|
correctedYaw = correctedYaw > 0
|
||||||
? correctedYaw + AGENT_LOOPBACK_MODIFIER
|
? correctedYaw + AGENT_LOOPBACK_MODIFIER
|
||||||
: correctedYaw - AGENT_LOOPBACK_MODIFIER;
|
: correctedYaw - AGENT_LOOPBACK_MODIFIER;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(currentPacketPtr, &correctedYaw, sizeof(float));
|
memcpy(currentPacketPtr, &correctedYaw, sizeof(float));
|
||||||
currentPacketPtr += sizeof(float);
|
currentPacketPtr += sizeof(float);
|
||||||
|
|
||||||
// copy the audio data to the last BUFFER_LENGTH_BYTES bytes of the data packet
|
// copy the audio data to the last BUFFER_LENGTH_BYTES bytes of the data packet
|
||||||
memcpy(currentPacketPtr, inputLeft, BUFFER_LENGTH_BYTES);
|
memcpy(currentPacketPtr, inputLeft, BUFFER_LENGTH_BYTES);
|
||||||
|
|
||||||
data->audioSocket->send((sockaddr *)&audioMixerSocket, dataPacket, BUFFER_LENGTH_BYTES + leadingBytes);
|
agentList->getAgentSocket().send(audioMixer->getActiveSocket(), dataPacket, BUFFER_LENGTH_BYTES + leadingBytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int16_t *outputLeft = ((int16_t **) outputBuffer)[0];
|
int16_t* outputLeft = ((int16_t**) outputBuffer)[0];
|
||||||
int16_t *outputRight = ((int16_t **) outputBuffer)[1];
|
int16_t* outputRight = ((int16_t**) outputBuffer)[1];
|
||||||
|
|
||||||
memset(outputLeft, 0, PACKET_LENGTH_BYTES_PER_CHANNEL);
|
memset(outputLeft, 0, PACKET_LENGTH_BYTES_PER_CHANNEL);
|
||||||
memset(outputRight, 0, PACKET_LENGTH_BYTES_PER_CHANNEL);
|
memset(outputRight, 0, PACKET_LENGTH_BYTES_PER_CHANNEL);
|
||||||
|
|
||||||
AudioRingBuffer *ringBuffer = data->ringBuffer;
|
AudioRingBuffer* ringBuffer = &parentAudio->_ringBuffer;
|
||||||
|
|
||||||
// if we've been reset, and there isn't any new packets yet
|
// if we've been reset, and there isn't any new packets yet
|
||||||
// just play some silence
|
// just play some silence
|
||||||
|
@ -184,15 +165,16 @@ int audioCallback (const void *inputBuffer,
|
||||||
if (ringBuffer->getEndOfLastWrite() != NULL) {
|
if (ringBuffer->getEndOfLastWrite() != NULL) {
|
||||||
|
|
||||||
if (!ringBuffer->isStarted() && ringBuffer->diffLastWriteNextOutput() < PACKET_LENGTH_SAMPLES + JITTER_BUFFER_SAMPLES) {
|
if (!ringBuffer->isStarted() && ringBuffer->diffLastWriteNextOutput() < PACKET_LENGTH_SAMPLES + JITTER_BUFFER_SAMPLES) {
|
||||||
//printLog("Held back, buffer has %d of %d samples required.\n", ringBuffer->diffLastWriteNextOutput(), PACKET_LENGTH_SAMPLES + JITTER_BUFFER_SAMPLES);
|
//printLog("Held back, buffer has %d of %d samples required.\n",
|
||||||
|
// ringBuffer->diffLastWriteNextOutput(), PACKET_LENGTH_SAMPLES + JITTER_BUFFER_SAMPLES);
|
||||||
} else if (ringBuffer->diffLastWriteNextOutput() < PACKET_LENGTH_SAMPLES) {
|
} else if (ringBuffer->diffLastWriteNextOutput() < PACKET_LENGTH_SAMPLES) {
|
||||||
ringBuffer->setStarted(false);
|
ringBuffer->setStarted(false);
|
||||||
|
|
||||||
starve_counter++;
|
::numStarves++;
|
||||||
packetsReceivedThisPlayback = 0;
|
parentAudio->_packetsReceivedThisPlayback = 0;
|
||||||
|
|
||||||
// printLog("Starved #%d\n", starve_counter);
|
// printLog("Starved #%d\n", starve_counter);
|
||||||
data->wasStarved = 10; // Frames to render the indication that the system was starved.
|
parentAudio->_wasStarved = 10; // Frames to render the indication that the system was starved.
|
||||||
} else {
|
} else {
|
||||||
if (!ringBuffer->isStarted()) {
|
if (!ringBuffer->isStarted()) {
|
||||||
ringBuffer->setStarted(true);
|
ringBuffer->setStarted(true);
|
||||||
|
@ -206,21 +188,22 @@ int audioCallback (const void *inputBuffer,
|
||||||
// if we haven't fired off the flange effect, check if we should
|
// if we haven't fired off the flange effect, check if we should
|
||||||
// TODO: lastMeasuredHeadYaw is now relative to body - check if this still works.
|
// TODO: lastMeasuredHeadYaw is now relative to body - check if this still works.
|
||||||
|
|
||||||
int lastYawMeasured = fabsf(data->linkedAvatar->getLastMeasuredHeadYaw());
|
int lastYawMeasured = fabsf(interfaceAvatar.getLastMeasuredHeadYaw());
|
||||||
|
|
||||||
if (!samplesLeftForFlange && lastYawMeasured > MIN_FLANGE_EFFECT_THRESHOLD) {
|
if (!::samplesLeftForFlange && lastYawMeasured > MIN_FLANGE_EFFECT_THRESHOLD) {
|
||||||
// we should flange for one second
|
// we should flange for one second
|
||||||
if ((lastYawMeasuredMaximum = std::max(lastYawMeasuredMaximum, lastYawMeasured)) != lastYawMeasured) {
|
if ((::lastYawMeasuredMaximum = std::max(::lastYawMeasuredMaximum, lastYawMeasured)) != lastYawMeasured) {
|
||||||
lastYawMeasuredMaximum = std::min(lastYawMeasuredMaximum, MIN_FLANGE_EFFECT_THRESHOLD);
|
::lastYawMeasuredMaximum = std::min(::lastYawMeasuredMaximum, MIN_FLANGE_EFFECT_THRESHOLD);
|
||||||
|
|
||||||
samplesLeftForFlange = SAMPLE_RATE;
|
::samplesLeftForFlange = SAMPLE_RATE;
|
||||||
|
|
||||||
flangeIntensity = MIN_FLANGE_INTENSITY +
|
::flangeIntensity = MIN_FLANGE_INTENSITY +
|
||||||
((lastYawMeasuredMaximum - MIN_FLANGE_EFFECT_THRESHOLD) / (float)(MAX_FLANGE_EFFECT_THRESHOLD - MIN_FLANGE_EFFECT_THRESHOLD)) *
|
((::lastYawMeasuredMaximum - MIN_FLANGE_EFFECT_THRESHOLD) /
|
||||||
|
(float)(MAX_FLANGE_EFFECT_THRESHOLD - MIN_FLANGE_EFFECT_THRESHOLD)) *
|
||||||
(1 - MIN_FLANGE_INTENSITY);
|
(1 - MIN_FLANGE_INTENSITY);
|
||||||
|
|
||||||
flangeRate = FLANGE_BASE_RATE * flangeIntensity;
|
::flangeRate = FLANGE_BASE_RATE * ::flangeIntensity;
|
||||||
flangeWeight = MAX_FLANGE_SAMPLE_WEIGHT * flangeIntensity;
|
::flangeWeight = MAX_FLANGE_SAMPLE_WEIGHT * ::flangeIntensity;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -229,13 +212,14 @@ int audioCallback (const void *inputBuffer,
|
||||||
int leftSample = ringBuffer->getNextOutput()[s];
|
int leftSample = ringBuffer->getNextOutput()[s];
|
||||||
int rightSample = ringBuffer->getNextOutput()[s + PACKET_LENGTH_SAMPLES_PER_CHANNEL];
|
int rightSample = ringBuffer->getNextOutput()[s + PACKET_LENGTH_SAMPLES_PER_CHANNEL];
|
||||||
|
|
||||||
if (samplesLeftForFlange > 0) {
|
if (::samplesLeftForFlange > 0) {
|
||||||
float exponent = (SAMPLE_RATE - samplesLeftForFlange - (SAMPLE_RATE / flangeRate)) / (SAMPLE_RATE / flangeRate);
|
float exponent = (SAMPLE_RATE - ::samplesLeftForFlange - (SAMPLE_RATE / ::flangeRate)) /
|
||||||
int sampleFlangeDelay = (SAMPLE_RATE / (1000 * flangeIntensity)) * powf(2, exponent);
|
(SAMPLE_RATE / ::flangeRate);
|
||||||
|
int sampleFlangeDelay = (SAMPLE_RATE / (1000 * ::flangeIntensity)) * powf(2, exponent);
|
||||||
|
|
||||||
if (samplesLeftForFlange != SAMPLE_RATE || s >= (SAMPLE_RATE / 2000)) {
|
if (::samplesLeftForFlange != SAMPLE_RATE || s >= (SAMPLE_RATE / 2000)) {
|
||||||
// we have a delayed sample to add to this sample
|
// we have a delayed sample to add to this sample
|
||||||
|
|
||||||
int16_t *flangeFrame = ringBuffer->getNextOutput();
|
int16_t *flangeFrame = ringBuffer->getNextOutput();
|
||||||
int flangeIndex = s - sampleFlangeDelay;
|
int flangeIndex = s - sampleFlangeDelay;
|
||||||
|
|
||||||
|
@ -251,13 +235,13 @@ int audioCallback (const void *inputBuffer,
|
||||||
int16_t leftFlangeSample = flangeFrame[flangeIndex];
|
int16_t leftFlangeSample = flangeFrame[flangeIndex];
|
||||||
int16_t rightFlangeSample = flangeFrame[flangeIndex + PACKET_LENGTH_SAMPLES_PER_CHANNEL];
|
int16_t rightFlangeSample = flangeFrame[flangeIndex + PACKET_LENGTH_SAMPLES_PER_CHANNEL];
|
||||||
|
|
||||||
leftSample = (1 - flangeWeight) * leftSample + (flangeWeight * leftFlangeSample);
|
leftSample = (1 - ::flangeWeight) * leftSample + (::flangeWeight * leftFlangeSample);
|
||||||
rightSample = (1 - flangeWeight) * rightSample + (flangeWeight * rightFlangeSample);
|
rightSample = (1 - ::flangeWeight) * rightSample + (::flangeWeight * rightFlangeSample);
|
||||||
|
|
||||||
samplesLeftForFlange--;
|
::samplesLeftForFlange--;
|
||||||
|
|
||||||
if (samplesLeftForFlange == 0) {
|
if (::samplesLeftForFlange == 0) {
|
||||||
lastYawMeasuredMaximum = 0;
|
::lastYawMeasuredMaximum = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -267,8 +251,8 @@ int audioCallback (const void *inputBuffer,
|
||||||
}
|
}
|
||||||
|
|
||||||
// add data to the scope
|
// add data to the scope
|
||||||
scope->addSamples(1, outputLeft, PACKET_LENGTH_SAMPLES_PER_CHANNEL);
|
parentAudio->_scope->addSamples(1, outputLeft, PACKET_LENGTH_SAMPLES_PER_CHANNEL);
|
||||||
scope->addSamples(2, outputRight, PACKET_LENGTH_SAMPLES_PER_CHANNEL);
|
parentAudio->_scope->addSamples(2, outputRight, PACKET_LENGTH_SAMPLES_PER_CHANNEL);
|
||||||
|
|
||||||
ringBuffer->setNextOutput(ringBuffer->getNextOutput() + PACKET_LENGTH_SAMPLES);
|
ringBuffer->setNextOutput(ringBuffer->getNextOutput() + PACKET_LENGTH_SAMPLES);
|
||||||
|
|
||||||
|
@ -278,140 +262,108 @@ int audioCallback (const void *inputBuffer,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
gettimeofday(&data->lastCallback, NULL);
|
gettimeofday(&parentAudio->_lastCallbackTime, NULL);
|
||||||
return paContinue;
|
return paContinue;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Audio::updateMixerParams(in_addr_t newMixerAddress, in_port_t newMixerPort) {
|
void outputPortAudioError(PaError error) {
|
||||||
audioData->mixerAddress = newMixerAddress;
|
if (error != paNoError) {
|
||||||
audioData->mixerPort = newMixerPort;
|
printLog("-- portaudio termination error --\n");
|
||||||
}
|
printLog("PortAudio error (%d): %s\n", error, Pa_GetErrorText(error));
|
||||||
|
|
||||||
struct AudioRecThreadStruct {
|
|
||||||
AudioData *sharedAudioData;
|
|
||||||
};
|
|
||||||
|
|
||||||
void *receiveAudioViaUDP(void *args) {
|
|
||||||
AudioRecThreadStruct *threadArgs = (AudioRecThreadStruct *) args;
|
|
||||||
AudioData *sharedAudioData = threadArgs->sharedAudioData;
|
|
||||||
|
|
||||||
int16_t *receivedData = new int16_t[PACKET_LENGTH_SAMPLES];
|
|
||||||
ssize_t receivedBytes;
|
|
||||||
|
|
||||||
// Init Jitter timer values
|
|
||||||
timeval previousReceiveTime, currentReceiveTime = {};
|
|
||||||
gettimeofday(&previousReceiveTime, NULL);
|
|
||||||
gettimeofday(¤tReceiveTime, NULL);
|
|
||||||
|
|
||||||
int totalPacketsReceived = 0;
|
|
||||||
|
|
||||||
stdev.reset();
|
|
||||||
|
|
||||||
while (!stopAudioReceiveThread) {
|
|
||||||
|
|
||||||
if (sharedAudioData->audioSocket->receive((void *)receivedData, &receivedBytes)) {
|
|
||||||
|
|
||||||
gettimeofday(¤tReceiveTime, NULL);
|
|
||||||
totalPacketsReceived++;
|
|
||||||
|
|
||||||
double tDiff = diffclock(&previousReceiveTime, ¤tReceiveTime);
|
|
||||||
//printLog("tDiff %4.1f\n", tDiff);
|
|
||||||
// Discard first few received packets for computing jitter (often they pile up on start)
|
|
||||||
if (totalPacketsReceived > 3) stdev.addValue(tDiff);
|
|
||||||
if (stdev.getSamples() > 500) {
|
|
||||||
sharedAudioData->measuredJitter = stdev.getStDev();
|
|
||||||
//printLog("Avg: %4.2f, Stdev: %4.2f\n", stdev.getAverage(), sharedAudioData->measuredJitter);
|
|
||||||
stdev.reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
AudioRingBuffer *ringBuffer = sharedAudioData->ringBuffer;
|
|
||||||
|
|
||||||
|
|
||||||
if (!ringBuffer->isStarted()) {
|
|
||||||
packetsReceivedThisPlayback++;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
//printLog("Audio packet received at %6.0f\n", usecTimestampNow()/1000);
|
|
||||||
}
|
|
||||||
if (packetsReceivedThisPlayback == 1) gettimeofday(&firstPlaybackTimer, NULL);
|
|
||||||
|
|
||||||
ringBuffer->parseData((unsigned char *)receivedData, PACKET_LENGTH_BYTES);
|
|
||||||
|
|
||||||
previousReceiveTime = currentReceiveTime;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pthread_exit(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Audio::setMixerLoopbackFlag(bool newMixerLoopbackFlag) {
|
Audio::Audio(Oscilloscope* scope) :
|
||||||
audioData->mixerLoopbackFlag = newMixerLoopbackFlag;
|
_stream(NULL),
|
||||||
}
|
_ringBuffer(RING_BUFFER_SAMPLES, PACKET_LENGTH_SAMPLES),
|
||||||
|
_scope(scope),
|
||||||
bool Audio::getMixerLoopbackFlag() {
|
_averagedLatency(0.0),
|
||||||
return audioData->mixerLoopbackFlag;
|
_measuredJitter(0),
|
||||||
}
|
_wasStarved(0),
|
||||||
|
_lastInputLoudness(0),
|
||||||
/**
|
_mixerLoopbackFlag(false),
|
||||||
* Initialize portaudio and start an audio stream.
|
_lastVelocity(0),
|
||||||
* Should be called at the beginning of program exection.
|
_lastAcceleration(0),
|
||||||
* @seealso Audio::terminate
|
_totalPacketsReceived(0),
|
||||||
* @return Returns true if successful or false if an error occurred.
|
_firstPlaybackTime(),
|
||||||
Use Audio::getError() to retrieve the error code.
|
_packetsReceivedThisPlayback(0)
|
||||||
*/
|
{
|
||||||
Audio::Audio(Oscilloscope* s, Avatar* linkedAvatar) {
|
outputPortAudioError(Pa_Initialize());
|
||||||
paError = Pa_Initialize();
|
outputPortAudioError(Pa_OpenDefaultStream(&_stream,
|
||||||
if (paError != paNoError) goto error;
|
2,
|
||||||
|
2,
|
||||||
scope = s;
|
(paInt16 | paNonInterleaved),
|
||||||
|
SAMPLE_RATE,
|
||||||
audioData = new AudioData();
|
BUFFER_LENGTH_SAMPLES,
|
||||||
|
audioCallback,
|
||||||
audioData->linkedAvatar = linkedAvatar;
|
(void*) this));
|
||||||
|
|
||||||
// setup a UDPSocket
|
|
||||||
audioData->audioSocket = new UDPSocket(AUDIO_UDP_LISTEN_PORT);
|
|
||||||
audioData->ringBuffer = new AudioRingBuffer(RING_BUFFER_SAMPLES, PACKET_LENGTH_SAMPLES);
|
|
||||||
|
|
||||||
AudioRecThreadStruct threadArgs;
|
|
||||||
threadArgs.sharedAudioData = audioData;
|
|
||||||
|
|
||||||
pthread_create(&audioReceiveThread, NULL, receiveAudioViaUDP, (void *) &threadArgs);
|
|
||||||
|
|
||||||
paError = Pa_OpenDefaultStream(&stream,
|
|
||||||
2, // input channels
|
|
||||||
2, // output channels
|
|
||||||
(paInt16 | paNonInterleaved), // sample format
|
|
||||||
SAMPLE_RATE, // sample rate (hz)
|
|
||||||
BUFFER_LENGTH_SAMPLES, // frames per buffer
|
|
||||||
audioCallback, // callback function
|
|
||||||
(void *) audioData); // user data to be passed to callback
|
|
||||||
if (paError != paNoError) goto error;
|
|
||||||
|
|
||||||
initialized = true;
|
|
||||||
|
|
||||||
// start the stream now that sources are good to go
|
// start the stream now that sources are good to go
|
||||||
Pa_StartStream(stream);
|
outputPortAudioError(Pa_StartStream(_stream));
|
||||||
if (paError != paNoError) goto error;
|
|
||||||
|
gettimeofday(&_lastReceiveTime, NULL);
|
||||||
|
|
||||||
return;
|
|
||||||
|
|
||||||
error:
|
|
||||||
printLog("-- Failed to initialize portaudio --\n");
|
|
||||||
printLog("PortAudio error (%d): %s\n", paError, Pa_GetErrorText(paError));
|
|
||||||
initialized = false;
|
|
||||||
delete[] audioData;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Audio::~Audio() {
|
||||||
float Audio::getInputLoudness() const {
|
if (_stream) {
|
||||||
return audioData->lastInputLoudness;
|
outputPortAudioError(Pa_CloseStream(_stream));
|
||||||
|
outputPortAudioError(Pa_Terminate());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Audio::render(int screenWidth, int screenHeight)
|
// Take a pointer to the acquired microphone input samples and add procedural sounds
|
||||||
{
|
void Audio::addProceduralSounds(int16_t* inputBuffer, int numSamples) {
|
||||||
if (initialized) {
|
const float MAX_AUDIBLE_VELOCITY = 6.0;
|
||||||
|
const float MIN_AUDIBLE_VELOCITY = 0.1;
|
||||||
|
const int VOLUME_BASELINE = 400;
|
||||||
|
const float SOUND_PITCH = 8.f;
|
||||||
|
|
||||||
|
float speed = glm::length(_lastVelocity);
|
||||||
|
float volume = VOLUME_BASELINE * (1.f - speed / MAX_AUDIBLE_VELOCITY);
|
||||||
|
|
||||||
|
// Add a noise-modulated sinewave with volume that tapers off with speed increasing
|
||||||
|
if ((speed > MIN_AUDIBLE_VELOCITY) && (speed < MAX_AUDIBLE_VELOCITY)) {
|
||||||
|
for (int i = 0; i < numSamples; i++) {
|
||||||
|
inputBuffer[i] += (int16_t)((cosf((float) i / SOUND_PITCH * speed) * randFloat()) * volume * speed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Audio::addReceivedAudioToBuffer(unsigned char* receivedData, int receivedBytes) {
|
||||||
|
const int NUM_INITIAL_PACKETS_DISCARD = 3;
|
||||||
|
|
||||||
|
timeval currentReceiveTime;
|
||||||
|
gettimeofday(¤tReceiveTime, NULL);
|
||||||
|
_totalPacketsReceived++;
|
||||||
|
|
||||||
|
double timeDiff = diffclock(&_lastReceiveTime, ¤tReceiveTime);
|
||||||
|
|
||||||
|
// Discard first few received packets for computing jitter (often they pile up on start)
|
||||||
|
if (_totalPacketsReceived > NUM_INITIAL_PACKETS_DISCARD) {
|
||||||
|
::stdev.addValue(timeDiff);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (::stdev.getSamples() > 500) {
|
||||||
|
_measuredJitter = ::stdev.getStDev();
|
||||||
|
//printLog("Avg: %4.2f, Stdev: %4.2f\n", stdev.getAverage(), sharedAudioData->measuredJitter);
|
||||||
|
::stdev.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!_ringBuffer.isStarted()) {
|
||||||
|
_packetsReceivedThisPlayback++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_packetsReceivedThisPlayback == 1) {
|
||||||
|
gettimeofday(&_firstPlaybackTime, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
_ringBuffer.parseData((unsigned char *)receivedData, PACKET_LENGTH_BYTES);
|
||||||
|
|
||||||
|
_lastReceiveTime = currentReceiveTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Audio::render(int screenWidth, int screenHeight) {
|
||||||
|
if (_stream) {
|
||||||
glLineWidth(2.0);
|
glLineWidth(2.0);
|
||||||
glBegin(GL_LINES);
|
glBegin(GL_LINES);
|
||||||
glColor3f(1,1,1);
|
glColor3f(1,1,1);
|
||||||
|
@ -438,25 +390,23 @@ void Audio::render(int screenWidth, int screenHeight)
|
||||||
}
|
}
|
||||||
glEnd();
|
glEnd();
|
||||||
|
|
||||||
|
|
||||||
// Show a bar with the amount of audio remaining in ring buffer beyond current playback
|
// Show a bar with the amount of audio remaining in ring buffer beyond current playback
|
||||||
float remainingBuffer = 0;
|
float remainingBuffer = 0;
|
||||||
timeval currentTime;
|
timeval currentTime;
|
||||||
gettimeofday(¤tTime, NULL);
|
gettimeofday(¤tTime, NULL);
|
||||||
float timeLeftInCurrentBuffer = 0;
|
float timeLeftInCurrentBuffer = 0;
|
||||||
if (audioData->lastCallback.tv_usec > 0) {
|
if (_lastCallbackTime.tv_usec > 0) {
|
||||||
timeLeftInCurrentBuffer = AUDIO_CALLBACK_MSECS - diffclock(&audioData->lastCallback, ¤tTime);
|
timeLeftInCurrentBuffer = AUDIO_CALLBACK_MSECS - diffclock(&_lastCallbackTime, ¤tTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
// /(1000.0*(float)BUFFER_LENGTH_SAMPLES/(float)SAMPLE_RATE) * frameWidth
|
|
||||||
|
|
||||||
if (audioData->ringBuffer->getEndOfLastWrite() != NULL)
|
if (_ringBuffer.getEndOfLastWrite() != NULL)
|
||||||
remainingBuffer = audioData->ringBuffer->diffLastWriteNextOutput() / PACKET_LENGTH_SAMPLES * AUDIO_CALLBACK_MSECS;
|
remainingBuffer = _ringBuffer.diffLastWriteNextOutput() / PACKET_LENGTH_SAMPLES * AUDIO_CALLBACK_MSECS;
|
||||||
|
|
||||||
if (audioData->wasStarved == 0) glColor3f(0, 1, 0);
|
if (_wasStarved == 0) {
|
||||||
else {
|
glColor3f(0, 1, 0);
|
||||||
glColor3f(0.5 + (float)audioData->wasStarved/20.0, 0, 0);
|
} else {
|
||||||
audioData->wasStarved--;
|
glColor3f(0.5 + (_wasStarved / 20.0f), 0, 0);
|
||||||
|
_wasStarved--;
|
||||||
}
|
}
|
||||||
|
|
||||||
glBegin(GL_QUADS);
|
glBegin(GL_QUADS);
|
||||||
|
@ -466,26 +416,29 @@ void Audio::render(int screenWidth, int screenHeight)
|
||||||
glVertex2f(startX, bottomY - 2);
|
glVertex2f(startX, bottomY - 2);
|
||||||
glEnd();
|
glEnd();
|
||||||
|
|
||||||
if (audioData->averagedLatency == 0.0) audioData->averagedLatency = remainingBuffer + timeLeftInCurrentBuffer;
|
if (_averagedLatency == 0.0) {
|
||||||
else audioData->averagedLatency = 0.99*audioData->averagedLatency + 0.01*((float)remainingBuffer + (float)timeLeftInCurrentBuffer);
|
_averagedLatency = remainingBuffer + timeLeftInCurrentBuffer;
|
||||||
|
} else {
|
||||||
|
_averagedLatency = 0.99f * _averagedLatency + 0.01f * (remainingBuffer + timeLeftInCurrentBuffer);
|
||||||
|
}
|
||||||
|
|
||||||
// Show a yellow bar with the averaged msecs latency you are hearing (from time of packet receipt)
|
// Show a yellow bar with the averaged msecs latency you are hearing (from time of packet receipt)
|
||||||
glColor3f(1,1,0);
|
glColor3f(1,1,0);
|
||||||
glBegin(GL_QUADS);
|
glBegin(GL_QUADS);
|
||||||
glVertex2f(startX + audioData->averagedLatency/AUDIO_CALLBACK_MSECS*frameWidth - 2, topY - 2);
|
glVertex2f(startX + _averagedLatency / AUDIO_CALLBACK_MSECS * frameWidth - 2, topY - 2);
|
||||||
glVertex2f(startX + audioData->averagedLatency/AUDIO_CALLBACK_MSECS*frameWidth + 2, topY - 2);
|
glVertex2f(startX + _averagedLatency / AUDIO_CALLBACK_MSECS * frameWidth + 2, topY - 2);
|
||||||
glVertex2f(startX + audioData->averagedLatency/AUDIO_CALLBACK_MSECS*frameWidth + 2, bottomY + 2);
|
glVertex2f(startX + _averagedLatency / AUDIO_CALLBACK_MSECS * frameWidth + 2, bottomY + 2);
|
||||||
glVertex2f(startX + audioData->averagedLatency/AUDIO_CALLBACK_MSECS*frameWidth - 2, bottomY + 2);
|
glVertex2f(startX + _averagedLatency / AUDIO_CALLBACK_MSECS * frameWidth - 2, bottomY + 2);
|
||||||
glEnd();
|
glEnd();
|
||||||
|
|
||||||
char out[40];
|
char out[40];
|
||||||
sprintf(out, "%3.0f\n", audioData->averagedLatency);
|
sprintf(out, "%3.0f\n", _averagedLatency);
|
||||||
drawtext(startX + audioData->averagedLatency/AUDIO_CALLBACK_MSECS*frameWidth - 10, topY-10, 0.10, 0, 1, 0, out, 1,1,0);
|
drawtext(startX + _averagedLatency / AUDIO_CALLBACK_MSECS * frameWidth - 10, topY - 10, 0.10, 0, 1, 0, out, 1,1,0);
|
||||||
//drawtext(startX + 0, topY-10, 0.08, 0, 1, 0, out, 1,1,0);
|
//drawtext(startX + 0, topY-10, 0.08, 0, 1, 0, out, 1,1,0);
|
||||||
|
|
||||||
// Show a Cyan bar with the most recently measured jitter stdev
|
// Show a Cyan bar with the most recently measured jitter stdev
|
||||||
|
|
||||||
int jitterPels = (float) audioData->measuredJitter/ ((1000.0*(float)PACKET_LENGTH_SAMPLES/(float)SAMPLE_RATE)) * (float)frameWidth;
|
int jitterPels = _measuredJitter / ((1000.0f * PACKET_LENGTH_SAMPLES / SAMPLE_RATE)) * frameWidth;
|
||||||
|
|
||||||
glColor3f(0,1,1);
|
glColor3f(0,1,1);
|
||||||
glBegin(GL_QUADS);
|
glBegin(GL_QUADS);
|
||||||
|
@ -495,7 +448,7 @@ void Audio::render(int screenWidth, int screenHeight)
|
||||||
glVertex2f(startX + jitterPels - 2, bottomY + 2);
|
glVertex2f(startX + jitterPels - 2, bottomY + 2);
|
||||||
glEnd();
|
glEnd();
|
||||||
|
|
||||||
sprintf(out,"%3.1f\n", audioData->measuredJitter);
|
sprintf(out,"%3.1f\n", _measuredJitter);
|
||||||
drawtext(startX + jitterPels - 5, topY-10, 0.10, 0, 1, 0, out, 0,1,1);
|
drawtext(startX + jitterPels - 5, topY-10, 0.10, 0, 1, 0, out, 0,1,1);
|
||||||
|
|
||||||
sprintf(out, "%3.1fms\n", JITTER_BUFFER_LENGTH_MSECS);
|
sprintf(out, "%3.1fms\n", JITTER_BUFFER_LENGTH_MSECS);
|
||||||
|
@ -503,34 +456,4 @@ void Audio::render(int screenWidth, int screenHeight)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Close the running audio stream, and deinitialize portaudio.
|
|
||||||
* Should be called at the end of program execution.
|
|
||||||
* @return Returns true if the initialization was successful, or false if an error occured.
|
|
||||||
The error code may be retrieved by Audio::getError().
|
|
||||||
*/
|
|
||||||
bool Audio::terminate() {
|
|
||||||
stopAudioReceiveThread = true;
|
|
||||||
pthread_join(audioReceiveThread, NULL);
|
|
||||||
|
|
||||||
if (initialized) {
|
|
||||||
initialized = false;
|
|
||||||
|
|
||||||
paError = Pa_CloseStream(stream);
|
|
||||||
if (paError != paNoError) goto error;
|
|
||||||
|
|
||||||
paError = Pa_Terminate();
|
|
||||||
if (paError != paNoError) goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
delete audioData;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
|
|
||||||
error:
|
|
||||||
printLog("-- portaudio termination error --\n");
|
|
||||||
printLog("PortAudio error (%d): %s\n", paError, Pa_GetErrorText(paError));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -10,44 +10,46 @@
|
||||||
#define __interface__Audio__
|
#define __interface__Audio__
|
||||||
|
|
||||||
#include <portaudio.h>
|
#include <portaudio.h>
|
||||||
#include "AudioData.h"
|
|
||||||
|
#include <AudioRingBuffer.h>
|
||||||
|
|
||||||
#include "Oscilloscope.h"
|
#include "Oscilloscope.h"
|
||||||
#include "Avatar.h"
|
#include "Avatar.h"
|
||||||
|
|
||||||
class Audio {
|
class Audio {
|
||||||
public:
|
public:
|
||||||
// initializes audio I/O
|
// initializes audio I/O
|
||||||
Audio(Oscilloscope *s, Avatar *linkedAvatar);
|
Audio(Oscilloscope* scope);
|
||||||
|
~Audio();
|
||||||
void render();
|
|
||||||
void render(int screenWidth, int screenHeight);
|
void render(int screenWidth, int screenHeight);
|
||||||
|
|
||||||
bool getMixerLoopbackFlag();
|
void setMixerLoopbackFlag(bool mixerLoopbackFlag) { _mixerLoopbackFlag = mixerLoopbackFlag; }
|
||||||
void setMixerLoopbackFlag(bool newMixerLoopbackFlag);
|
|
||||||
|
|
||||||
float getInputLoudness() const;
|
float getLastInputLoudness() const { return _lastInputLoudness; };
|
||||||
void updateMixerParams(in_addr_t mixerAddress, in_port_t mixerPort);
|
|
||||||
|
|
||||||
void setLastAcceleration(glm::vec3 a) { audioData->setLastAcceleration(a); };
|
void setLastAcceleration(glm::vec3 lastAcceleration) { _lastAcceleration = lastAcceleration; };
|
||||||
void setLastVelocity(glm::vec3 v) { audioData->setLastVelocity(v); };
|
void setLastVelocity(glm::vec3 lastVelocity) { _lastVelocity = lastVelocity; };
|
||||||
|
|
||||||
// terminates audio I/O
|
void addProceduralSounds(int16_t* inputBuffer, int numSamples);
|
||||||
bool terminate();
|
|
||||||
|
void addReceivedAudioToBuffer(unsigned char* receivedData, int receivedBytes);
|
||||||
private:
|
private:
|
||||||
bool initialized;
|
PaStream* _stream;
|
||||||
AudioData *audioData;
|
AudioRingBuffer _ringBuffer;
|
||||||
|
Oscilloscope* _scope;
|
||||||
// protects constructor so that public init method is used
|
timeval _lastCallbackTime;
|
||||||
Audio();
|
timeval _lastReceiveTime;
|
||||||
|
float _averagedLatency;
|
||||||
// hold potential error returned from PortAudio functions
|
float _measuredJitter;
|
||||||
PaError paError;
|
int _wasStarved;
|
||||||
|
float _lastInputLoudness;
|
||||||
// audio stream handle
|
bool _mixerLoopbackFlag;
|
||||||
PaStream *stream;
|
glm::vec3 _lastVelocity;
|
||||||
|
glm::vec3 _lastAcceleration;
|
||||||
// audio receive thread
|
int _totalPacketsReceived;
|
||||||
pthread_t audioReceiveThread;
|
timeval _firstPlaybackTime;
|
||||||
|
int _packetsReceivedThisPlayback;
|
||||||
|
|
||||||
// give access to AudioData class from audioCallback
|
// give access to AudioData class from audioCallback
|
||||||
friend int audioCallback (const void*, void*, unsigned long, const PaStreamCallbackTimeInfo*, PaStreamCallbackFlags, void*);
|
friend int audioCallback (const void*, void*, unsigned long, const PaStreamCallbackTimeInfo*, PaStreamCallbackFlags, void*);
|
||||||
|
|
|
@ -1,48 +0,0 @@
|
||||||
//
|
|
||||||
// AudioData.cpp
|
|
||||||
// interface
|
|
||||||
//
|
|
||||||
// Created by Stephen Birarda on 1/29/13.
|
|
||||||
// Copyright (c) 2013 HighFidelity, Inc. All rights reserved.
|
|
||||||
//
|
|
||||||
#ifndef _WIN32
|
|
||||||
|
|
||||||
#include "AudioData.h"
|
|
||||||
|
|
||||||
AudioData::AudioData() {
|
|
||||||
mixerAddress = 0;
|
|
||||||
mixerPort = 0;
|
|
||||||
|
|
||||||
averagedLatency = 0.0;
|
|
||||||
lastCallback.tv_usec = 0;
|
|
||||||
wasStarved = 0;
|
|
||||||
measuredJitter = 0;
|
|
||||||
jitterBuffer = 0;
|
|
||||||
|
|
||||||
mixerLoopbackFlag = false;
|
|
||||||
audioSocket = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
AudioData::~AudioData() {
|
|
||||||
delete audioSocket;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Take a pointer to the acquired microphone input samples and add procedural sounds
|
|
||||||
void AudioData::addProceduralSounds(int16_t* inputBuffer, int numSamples) {
|
|
||||||
const float MAX_AUDIBLE_VELOCITY = 6.0;
|
|
||||||
const float MIN_AUDIBLE_VELOCITY = 0.1;
|
|
||||||
float speed = glm::length(_lastVelocity);
|
|
||||||
float volume = 400 * (1.f - speed/MAX_AUDIBLE_VELOCITY);
|
|
||||||
// Add a noise-modulated sinewave with volume that tapers off with speed increasing
|
|
||||||
if ((speed > MIN_AUDIBLE_VELOCITY) && (speed < MAX_AUDIBLE_VELOCITY)) {
|
|
||||||
for (int i = 0; i < numSamples; i++) {
|
|
||||||
inputBuffer[i] += (int16_t) ((cosf((float)i / 8.f * speed) * randFloat()) * volume * speed) ;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
|
|
@ -1,54 +0,0 @@
|
||||||
//
|
|
||||||
// AudioData.h
|
|
||||||
// interface
|
|
||||||
//
|
|
||||||
// Created by Stephen Birarda on 1/29/13.
|
|
||||||
// Copyright (c) 2013 HighFidelity, Inc. All rights reserved.
|
|
||||||
//
|
|
||||||
|
|
||||||
#ifndef __interface__AudioData__
|
|
||||||
#define __interface__AudioData__
|
|
||||||
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <glm/glm.hpp>
|
|
||||||
#include "AudioRingBuffer.h"
|
|
||||||
#include "UDPSocket.h"
|
|
||||||
#include "Avatar.h"
|
|
||||||
|
|
||||||
class AudioData {
|
|
||||||
public:
|
|
||||||
AudioData();
|
|
||||||
~AudioData();
|
|
||||||
AudioRingBuffer *ringBuffer;
|
|
||||||
|
|
||||||
UDPSocket *audioSocket;
|
|
||||||
|
|
||||||
Avatar *linkedAvatar;
|
|
||||||
|
|
||||||
// store current mixer address and port
|
|
||||||
in_addr_t mixerAddress;
|
|
||||||
in_port_t mixerPort;
|
|
||||||
|
|
||||||
timeval lastCallback;
|
|
||||||
float averagedLatency;
|
|
||||||
float measuredJitter;
|
|
||||||
float jitterBuffer;
|
|
||||||
int wasStarved;
|
|
||||||
|
|
||||||
float lastInputLoudness;
|
|
||||||
|
|
||||||
bool mixerLoopbackFlag;
|
|
||||||
|
|
||||||
// Added avatar acceleration and velocity for procedural effects sounds from client
|
|
||||||
void setLastVelocity(glm::vec3 v) { _lastVelocity = v; };
|
|
||||||
void setLastAcceleration(glm::vec3 a) { _lastAcceleration = a; };
|
|
||||||
void addProceduralSounds(int16_t* inputBuffer, int numSamples);
|
|
||||||
|
|
||||||
private:
|
|
||||||
glm::vec3 _lastVelocity;
|
|
||||||
glm::vec3 _lastAcceleration;
|
|
||||||
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif /* defined(__interface__AudioData__) */
|
|
|
@ -243,14 +243,10 @@ bool AgentList::addOrUpdateAgent(sockaddr *publicSocket, sockaddr *localSocket,
|
||||||
// set the agent active right away
|
// set the agent active right away
|
||||||
newAgent->activatePublicSocket();
|
newAgent->activatePublicSocket();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (newAgent->getType() == AGENT_TYPE_AUDIO_MIXER && audioMixerSocketUpdate != NULL) {
|
if (newAgent->getType() == AGENT_TYPE_VOXEL ||
|
||||||
// this is an audio mixer
|
newAgent->getType() == AGENT_TYPE_AVATAR_MIXER ||
|
||||||
// for now that means we need to tell the audio class
|
newAgent->getType() == AGENT_TYPE_AUDIO_MIXER) {
|
||||||
// to use the local socket information the domain server gave us
|
|
||||||
sockaddr_in *publicSocketIn = (sockaddr_in *)publicSocket;
|
|
||||||
audioMixerSocketUpdate(publicSocketIn->sin_addr.s_addr, publicSocketIn->sin_port);
|
|
||||||
} else if (newAgent->getType() == AGENT_TYPE_VOXEL || newAgent->getType() == AGENT_TYPE_AVATAR_MIXER) {
|
|
||||||
// this is currently the cheat we use to talk directly to our test servers on EC2
|
// this is currently the cheat we use to talk directly to our test servers on EC2
|
||||||
// to be removed when we have a proper identification strategy
|
// to be removed when we have a proper identification strategy
|
||||||
newAgent->activatePublicSocket();
|
newAgent->activatePublicSocket();
|
||||||
|
|
|
@ -46,7 +46,6 @@ public:
|
||||||
AgentListIterator end() const;
|
AgentListIterator end() const;
|
||||||
|
|
||||||
void(*linkedDataCreateCallback)(Agent *);
|
void(*linkedDataCreateCallback)(Agent *);
|
||||||
void(*audioMixerSocketUpdate)(in_addr_t, in_port_t);
|
|
||||||
|
|
||||||
int size() { return _numAgents; }
|
int size() { return _numAgents; }
|
||||||
|
|
||||||
|
|
|
@ -20,8 +20,9 @@
|
||||||
// Agent Type Codes
|
// Agent Type Codes
|
||||||
const char AGENT_TYPE_DOMAIN = 'D';
|
const char AGENT_TYPE_DOMAIN = 'D';
|
||||||
const char AGENT_TYPE_VOXEL = 'V';
|
const char AGENT_TYPE_VOXEL = 'V';
|
||||||
const char AGENT_TYPE_AVATAR = 'I'; // could also be injector???
|
const char AGENT_TYPE_AVATAR = 'I';
|
||||||
const char AGENT_TYPE_AUDIO_MIXER = 'M';
|
const char AGENT_TYPE_AUDIO_MIXER = 'M';
|
||||||
const char AGENT_TYPE_AVATAR_MIXER = 'W';
|
const char AGENT_TYPE_AVATAR_MIXER = 'W';
|
||||||
|
const char AGENT_TYPE_AUDIO_INJECTOR = 'A';
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -22,12 +22,9 @@ const float BUFFER_SEND_INTERVAL_USECS = (BUFFER_LENGTH_SAMPLES / SAMPLE_RATE) *
|
||||||
|
|
||||||
AudioInjector::AudioInjector(const char* filename) :
|
AudioInjector::AudioInjector(const char* filename) :
|
||||||
_numTotalBytesAudio(0),
|
_numTotalBytesAudio(0),
|
||||||
|
_position(),
|
||||||
_bearing(0),
|
_bearing(0),
|
||||||
_attenuationModifier(255)
|
_attenuationModifier(255) {
|
||||||
{
|
|
||||||
_position[0] = 0.0f;
|
|
||||||
_position[1] = 0.0f;
|
|
||||||
_position[2] = 0.0f;
|
|
||||||
|
|
||||||
std::fstream sourceFile;
|
std::fstream sourceFile;
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,9 @@
|
||||||
//
|
//
|
||||||
|
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
|
#include "PacketHeaders.h"
|
||||||
|
|
||||||
#include "AudioRingBuffer.h"
|
#include "AudioRingBuffer.h"
|
||||||
|
|
||||||
AudioRingBuffer::AudioRingBuffer(int ringSamples, int bufferSamples) :
|
AudioRingBuffer::AudioRingBuffer(int ringSamples, int bufferSamples) :
|
||||||
|
@ -46,18 +49,22 @@ AudioRingBuffer* AudioRingBuffer::clone() const {
|
||||||
const int AGENT_LOOPBACK_MODIFIER = 307;
|
const int AGENT_LOOPBACK_MODIFIER = 307;
|
||||||
|
|
||||||
int AudioRingBuffer::parseData(unsigned char* sourceBuffer, int numBytes) {
|
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));
|
unsigned int attenuationByte = *(dataBuffer++);
|
||||||
dataPtr += (sizeof(_position));
|
|
||||||
|
|
||||||
unsigned int attenuationByte = *(dataPtr++);
|
|
||||||
_attenuationRatio = attenuationByte / 255.0f;
|
_attenuationRatio = attenuationByte / 255.0f;
|
||||||
|
|
||||||
memcpy(&_bearing, dataPtr, sizeof(float));
|
memcpy(&_bearing, dataBuffer, sizeof(float));
|
||||||
dataPtr += sizeof(_bearing);
|
dataBuffer += sizeof(_bearing);
|
||||||
|
|
||||||
if (_bearing > 180 || _bearing < -180) {
|
if (_bearing > 180 || _bearing < -180) {
|
||||||
// we were passed an invalid bearing because this agent wants loopback (pressed the H key)
|
// 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 {
|
} else {
|
||||||
_shouldLoopbackForAgent = false;
|
_shouldLoopbackForAgent = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
sourceBuffer = dataPtr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!_endOfLastWrite) {
|
if (!_endOfLastWrite) {
|
||||||
|
@ -82,7 +87,7 @@ int AudioRingBuffer::parseData(unsigned char* sourceBuffer, int numBytes) {
|
||||||
_started = false;
|
_started = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(_endOfLastWrite, sourceBuffer, _bufferLengthSamples * sizeof(int16_t));
|
memcpy(_endOfLastWrite, dataBuffer, _bufferLengthSamples * sizeof(int16_t));
|
||||||
|
|
||||||
_endOfLastWrite += _bufferLengthSamples;
|
_endOfLastWrite += _bufferLengthSamples;
|
||||||
|
|
||||||
|
|
|
@ -20,6 +20,8 @@ const PACKET_HEADER PACKET_HEADER_PING_REPLY = 'R';
|
||||||
const PACKET_HEADER PACKET_HEADER_HEAD_DATA = 'H';
|
const PACKET_HEADER PACKET_HEADER_HEAD_DATA = 'H';
|
||||||
const PACKET_HEADER PACKET_HEADER_Z_COMMAND = 'Z';
|
const PACKET_HEADER PACKET_HEADER_Z_COMMAND = 'Z';
|
||||||
const PACKET_HEADER PACKET_HEADER_INJECT_AUDIO = 'I';
|
const PACKET_HEADER PACKET_HEADER_INJECT_AUDIO = 'I';
|
||||||
|
const PACKET_HEADER PACKET_HEADER_MIXED_AUDIO = 'A';
|
||||||
|
const PACKET_HEADER PACKET_HEADER_MICROPHONE_AUDIO = 'M';
|
||||||
const PACKET_HEADER PACKET_HEADER_SET_VOXEL = 'S';
|
const PACKET_HEADER PACKET_HEADER_SET_VOXEL = 'S';
|
||||||
const PACKET_HEADER PACKET_HEADER_SET_VOXEL_DESTRUCTIVE = 'O';
|
const PACKET_HEADER PACKET_HEADER_SET_VOXEL_DESTRUCTIVE = 'O';
|
||||||
const PACKET_HEADER PACKET_HEADER_ERASE_VOXEL = 'E';
|
const PACKET_HEADER PACKET_HEADER_ERASE_VOXEL = 'E';
|
||||||
|
|
Loading…
Reference in a new issue