mirror of
https://github.com/JulianGro/overte.git
synced 2025-04-14 11:46:34 +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_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
|
||||
unsigned char dataPacket[BUFFER_LENGTH_BYTES + leadingBytes];
|
||||
|
||||
dataPacket[0] = 'I';
|
||||
unsigned char *currentPacketPtr = dataPacket + 1;
|
||||
|
||||
// memcpy the three float positions
|
||||
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
|
||||
memcpy(dataPacket + leadingBytes, inputLeft, BUFFER_LENGTH_BYTES);
|
||||
memcpy(currentPacketPtr, inputLeft, BUFFER_LENGTH_BYTES);
|
||||
|
||||
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++) {
|
||||
loudness += abs(inputLeft[i]);
|
||||
}
|
||||
|
||||
loudness /= BUFFER_LENGTH_SAMPLES;
|
||||
data->lastInputLoudness = loudness;
|
||||
data->averagedInputLoudness = 0.66*data->averagedInputLoudness + 0.33*loudness;
|
||||
|
@ -246,10 +261,6 @@ void *receiveAudioViaUDP(void *args) {
|
|||
pthread_exit(0);
|
||||
}
|
||||
|
||||
void Audio::setSourcePosition(glm::vec3 newPosition) {
|
||||
audioData->sourcePosition = newPosition;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize portaudio and start an audio stream.
|
||||
* 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.
|
||||
Use Audio::getError() to retrieve the error code.
|
||||
*/
|
||||
Audio::Audio(Oscilloscope * s)
|
||||
Audio::Audio(Oscilloscope *s, Head *linkedHead)
|
||||
{
|
||||
paError = Pa_Initialize();
|
||||
if (paError != paNoError) goto error;
|
||||
|
@ -266,6 +277,8 @@ Audio::Audio(Oscilloscope * s)
|
|||
|
||||
audioData = new AudioData();
|
||||
|
||||
audioData->linkedHead = linkedHead;
|
||||
|
||||
// setup a UDPSocket
|
||||
audioData->audioSocket = new UDPSocket(AUDIO_UDP_LISTEN_PORT);
|
||||
audioData->ringBuffer = new AudioRingBuffer(RING_BUFFER_SAMPLES, PACKET_LENGTH_SAMPLES);
|
||||
|
|
|
@ -13,11 +13,12 @@
|
|||
#include <portaudio.h>
|
||||
#include "AudioData.h"
|
||||
#include "Oscilloscope.h"
|
||||
#include "Head.h"
|
||||
|
||||
class Audio {
|
||||
public:
|
||||
// initializes audio I/O
|
||||
Audio(Oscilloscope * s);
|
||||
Audio(Oscilloscope *s, Head *linkedHead);
|
||||
|
||||
void render();
|
||||
void render(int screenWidth, int screenHeight);
|
||||
|
@ -25,8 +26,6 @@ public:
|
|||
void getInputLoudness(float * lastLoudness, float * averageLoudness);
|
||||
void updateMixerParams(in_addr_t mixerAddress, in_port_t mixerPort);
|
||||
|
||||
void setSourcePosition(glm::vec3 position);
|
||||
|
||||
// terminates audio I/O
|
||||
bool terminate();
|
||||
private:
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
#include <glm/glm.hpp>
|
||||
#include "AudioRingBuffer.h"
|
||||
#include "UDPSocket.h"
|
||||
#include "Head.h"
|
||||
|
||||
class AudioData {
|
||||
public:
|
||||
|
@ -23,9 +24,7 @@ class AudioData {
|
|||
|
||||
UDPSocket *audioSocket;
|
||||
|
||||
int16_t *samplesToQueue;
|
||||
|
||||
glm::vec3 sourcePosition;
|
||||
Head *linkedHead;
|
||||
|
||||
// store current mixer address and port
|
||||
in_addr_t mixerAddress;
|
||||
|
|
|
@ -106,7 +106,7 @@ Lattice lattice(160,100);
|
|||
Finger myFinger(WIDTH, HEIGHT);
|
||||
Field field;
|
||||
|
||||
Audio audio(&audioScope);
|
||||
Audio audio(&audioScope, &myHead);
|
||||
|
||||
#define RENDER_FRAME_MSECS 8
|
||||
int steps_per_frame = 0;
|
||||
|
@ -512,7 +512,6 @@ void simulateHead(float frametime)
|
|||
myHead.setRenderYaw(myHead.getRenderYaw() + render_yaw_rate);
|
||||
myHead.setRenderPitch(render_pitch);
|
||||
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
|
||||
float loudness, averageLoudness;
|
||||
|
|
|
@ -94,11 +94,17 @@ void AudioRingBuffer::parseData(void *data, int size) {
|
|||
|
||||
if (size > (bufferLengthSamples * sizeof(int16_t))) {
|
||||
|
||||
unsigned char *dataPtr = audioDataStart + 1;
|
||||
|
||||
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) {
|
||||
|
|
|
@ -33,12 +33,15 @@ class AudioRingBuffer : public AgentData {
|
|||
void setAddedToMix(bool added);
|
||||
float* getPosition();
|
||||
void setPosition(float newPosition[]);
|
||||
float getBearing();
|
||||
float setBearing(float newBearing);
|
||||
|
||||
short diffLastWriteNextOutput();
|
||||
private:
|
||||
int ringBufferLengthSamples;
|
||||
int bufferLengthSamples;
|
||||
float position[3];
|
||||
float bearing;
|
||||
int16_t *nextOutput;
|
||||
int16_t *endOfLastWrite;
|
||||
int16_t *buffer;
|
||||
|
|
Loading…
Reference in a new issue