mirror of
https://github.com/overte-org/overte.git
synced 2025-08-09 16:41:02 +02:00
relink head to AudioRingBuffer, send source bearing
This commit is contained in:
parent
29df6e6a3e
commit
a42fc47231
6 changed files with 37 additions and 18 deletions
|
@ -93,20 +93,34 @@ int audioCallback (const void *inputBuffer,
|
||||||
audioMixerSocket.sin_addr.s_addr = data->mixerAddress;
|
audioMixerSocket.sin_addr.s_addr = data->mixerAddress;
|
||||||
audioMixerSocket.sin_port = data->mixerPort;
|
audioMixerSocket.sin_port = data->mixerPort;
|
||||||
|
|
||||||
int leadingBytes = 1 + (sizeof(float) * 3);
|
int leadingBytes = 1 + (sizeof(float) * 4);
|
||||||
|
|
||||||
// we need the amount of bytes in the buffer + 1 for type + 12 for 3 floats for position
|
// we need the amount of bytes in the buffer + 1 for type + 12 for 3 floats for position
|
||||||
unsigned char dataPacket[BUFFER_LENGTH_BYTES + leadingBytes];
|
unsigned char dataPacket[BUFFER_LENGTH_BYTES + leadingBytes];
|
||||||
|
|
||||||
dataPacket[0] = 'I';
|
dataPacket[0] = 'I';
|
||||||
|
unsigned char *currentPacketPtr = dataPacket + 1;
|
||||||
|
|
||||||
// memcpy the three float positions
|
// memcpy the three float positions
|
||||||
for (int p = 0; p < 3; p++) {
|
for (int p = 0; p < 3; p++) {
|
||||||
memcpy(dataPacket + 1 + (p * sizeof(float)), &data->sourcePosition[p], sizeof(float));
|
memcpy(currentPacketPtr, &data->linkedHead->getPos()[p], sizeof(float));
|
||||||
|
currentPacketPtr += sizeof(float);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// memcpy the corrected render yaw
|
||||||
|
float correctedYaw = fmodf(data->linkedHead->getRenderYaw(), 360);
|
||||||
|
|
||||||
|
if (correctedYaw > 180) {
|
||||||
|
correctedYaw -= 360;
|
||||||
|
} else if (correctedYaw < -180) {
|
||||||
|
correctedYaw += 360;
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(currentPacketPtr, &correctedYaw, 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(dataPacket + leadingBytes, inputLeft, BUFFER_LENGTH_BYTES);
|
memcpy(currentPacketPtr, inputLeft, BUFFER_LENGTH_BYTES);
|
||||||
|
|
||||||
data->audioSocket->send((sockaddr *)&audioMixerSocket, dataPacket, BUFFER_LENGTH_BYTES + leadingBytes);
|
data->audioSocket->send((sockaddr *)&audioMixerSocket, dataPacket, BUFFER_LENGTH_BYTES + leadingBytes);
|
||||||
}
|
}
|
||||||
|
@ -118,6 +132,7 @@ int audioCallback (const void *inputBuffer,
|
||||||
for (int i = 0; i < BUFFER_LENGTH_SAMPLES; i++) {
|
for (int i = 0; i < BUFFER_LENGTH_SAMPLES; i++) {
|
||||||
loudness += abs(inputLeft[i]);
|
loudness += abs(inputLeft[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
loudness /= BUFFER_LENGTH_SAMPLES;
|
loudness /= BUFFER_LENGTH_SAMPLES;
|
||||||
data->lastInputLoudness = loudness;
|
data->lastInputLoudness = loudness;
|
||||||
data->averagedInputLoudness = 0.66*data->averagedInputLoudness + 0.33*loudness;
|
data->averagedInputLoudness = 0.66*data->averagedInputLoudness + 0.33*loudness;
|
||||||
|
@ -246,10 +261,6 @@ void *receiveAudioViaUDP(void *args) {
|
||||||
pthread_exit(0);
|
pthread_exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Audio::setSourcePosition(glm::vec3 newPosition) {
|
|
||||||
audioData->sourcePosition = newPosition;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize portaudio and start an audio stream.
|
* Initialize portaudio and start an audio stream.
|
||||||
* Should be called at the beginning of program exection.
|
* Should be called at the beginning of program exection.
|
||||||
|
@ -257,7 +268,7 @@ void Audio::setSourcePosition(glm::vec3 newPosition) {
|
||||||
* @return Returns true if successful or false if an error occurred.
|
* @return Returns true if successful or false if an error occurred.
|
||||||
Use Audio::getError() to retrieve the error code.
|
Use Audio::getError() to retrieve the error code.
|
||||||
*/
|
*/
|
||||||
Audio::Audio(Oscilloscope * s)
|
Audio::Audio(Oscilloscope *s, Head *linkedHead)
|
||||||
{
|
{
|
||||||
paError = Pa_Initialize();
|
paError = Pa_Initialize();
|
||||||
if (paError != paNoError) goto error;
|
if (paError != paNoError) goto error;
|
||||||
|
@ -266,6 +277,8 @@ Audio::Audio(Oscilloscope * s)
|
||||||
|
|
||||||
audioData = new AudioData();
|
audioData = new AudioData();
|
||||||
|
|
||||||
|
audioData->linkedHead = linkedHead;
|
||||||
|
|
||||||
// setup a UDPSocket
|
// setup a UDPSocket
|
||||||
audioData->audioSocket = new UDPSocket(AUDIO_UDP_LISTEN_PORT);
|
audioData->audioSocket = new UDPSocket(AUDIO_UDP_LISTEN_PORT);
|
||||||
audioData->ringBuffer = new AudioRingBuffer(RING_BUFFER_SAMPLES, PACKET_LENGTH_SAMPLES);
|
audioData->ringBuffer = new AudioRingBuffer(RING_BUFFER_SAMPLES, PACKET_LENGTH_SAMPLES);
|
||||||
|
|
|
@ -13,11 +13,12 @@
|
||||||
#include <portaudio.h>
|
#include <portaudio.h>
|
||||||
#include "AudioData.h"
|
#include "AudioData.h"
|
||||||
#include "Oscilloscope.h"
|
#include "Oscilloscope.h"
|
||||||
|
#include "Head.h"
|
||||||
|
|
||||||
class Audio {
|
class Audio {
|
||||||
public:
|
public:
|
||||||
// initializes audio I/O
|
// initializes audio I/O
|
||||||
Audio(Oscilloscope * s);
|
Audio(Oscilloscope *s, Head *linkedHead);
|
||||||
|
|
||||||
void render();
|
void render();
|
||||||
void render(int screenWidth, int screenHeight);
|
void render(int screenWidth, int screenHeight);
|
||||||
|
@ -25,8 +26,6 @@ public:
|
||||||
void getInputLoudness(float * lastLoudness, float * averageLoudness);
|
void getInputLoudness(float * lastLoudness, float * averageLoudness);
|
||||||
void updateMixerParams(in_addr_t mixerAddress, in_port_t mixerPort);
|
void updateMixerParams(in_addr_t mixerAddress, in_port_t mixerPort);
|
||||||
|
|
||||||
void setSourcePosition(glm::vec3 position);
|
|
||||||
|
|
||||||
// terminates audio I/O
|
// terminates audio I/O
|
||||||
bool terminate();
|
bool terminate();
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
#include "AudioRingBuffer.h"
|
#include "AudioRingBuffer.h"
|
||||||
#include "UDPSocket.h"
|
#include "UDPSocket.h"
|
||||||
|
#include "Head.h"
|
||||||
|
|
||||||
class AudioData {
|
class AudioData {
|
||||||
public:
|
public:
|
||||||
|
@ -23,9 +24,7 @@ class AudioData {
|
||||||
|
|
||||||
UDPSocket *audioSocket;
|
UDPSocket *audioSocket;
|
||||||
|
|
||||||
int16_t *samplesToQueue;
|
Head *linkedHead;
|
||||||
|
|
||||||
glm::vec3 sourcePosition;
|
|
||||||
|
|
||||||
// store current mixer address and port
|
// store current mixer address and port
|
||||||
in_addr_t mixerAddress;
|
in_addr_t mixerAddress;
|
||||||
|
|
|
@ -106,7 +106,7 @@ Lattice lattice(160,100);
|
||||||
Finger myFinger(WIDTH, HEIGHT);
|
Finger myFinger(WIDTH, HEIGHT);
|
||||||
Field field;
|
Field field;
|
||||||
|
|
||||||
Audio audio(&audioScope);
|
Audio audio(&audioScope, &myHead);
|
||||||
|
|
||||||
#define RENDER_FRAME_MSECS 8
|
#define RENDER_FRAME_MSECS 8
|
||||||
int steps_per_frame = 0;
|
int steps_per_frame = 0;
|
||||||
|
@ -512,7 +512,6 @@ void simulateHead(float frametime)
|
||||||
myHead.setRenderYaw(myHead.getRenderYaw() + render_yaw_rate);
|
myHead.setRenderYaw(myHead.getRenderYaw() + render_yaw_rate);
|
||||||
myHead.setRenderPitch(render_pitch);
|
myHead.setRenderPitch(render_pitch);
|
||||||
myHead.setPos(glm::vec3(location[0], location[1], location[2]));
|
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
|
// Get audio loudness data from audio input device
|
||||||
float loudness, averageLoudness;
|
float loudness, averageLoudness;
|
||||||
|
|
|
@ -94,11 +94,17 @@ void AudioRingBuffer::parseData(void *data, int size) {
|
||||||
|
|
||||||
if (size > (bufferLengthSamples * sizeof(int16_t))) {
|
if (size > (bufferLengthSamples * sizeof(int16_t))) {
|
||||||
|
|
||||||
|
unsigned char *dataPtr = audioDataStart + 1;
|
||||||
|
|
||||||
for (int p = 0; p < 3; p ++) {
|
for (int p = 0; p < 3; p ++) {
|
||||||
memcpy(&position[p], audioDataStart + 1 + (sizeof(float) * p), sizeof(float));
|
memcpy(&position[p], dataPtr, sizeof(float));
|
||||||
|
dataPtr += sizeof(float);
|
||||||
}
|
}
|
||||||
|
|
||||||
audioDataStart += (1 + (sizeof(float) * 3));
|
memcpy(&bearing, dataPtr, sizeof(float));
|
||||||
|
dataPtr += sizeof(float);
|
||||||
|
|
||||||
|
audioDataStart = dataPtr;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (endOfLastWrite == NULL) {
|
if (endOfLastWrite == NULL) {
|
||||||
|
|
|
@ -33,12 +33,15 @@ class AudioRingBuffer : public AgentData {
|
||||||
void setAddedToMix(bool added);
|
void setAddedToMix(bool added);
|
||||||
float* getPosition();
|
float* getPosition();
|
||||||
void setPosition(float newPosition[]);
|
void setPosition(float newPosition[]);
|
||||||
|
float getBearing();
|
||||||
|
float setBearing(float newBearing);
|
||||||
|
|
||||||
short diffLastWriteNextOutput();
|
short diffLastWriteNextOutput();
|
||||||
private:
|
private:
|
||||||
int ringBufferLengthSamples;
|
int ringBufferLengthSamples;
|
||||||
int bufferLengthSamples;
|
int bufferLengthSamples;
|
||||||
float position[3];
|
float position[3];
|
||||||
|
float bearing;
|
||||||
int16_t *nextOutput;
|
int16_t *nextOutput;
|
||||||
int16_t *endOfLastWrite;
|
int16_t *endOfLastWrite;
|
||||||
int16_t *buffer;
|
int16_t *buffer;
|
||||||
|
|
Loading…
Reference in a new issue