From d9caf566dee3a477da19f305fa5a2ebf79f6cf56 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Thu, 24 Jan 2013 13:08:26 -0800 Subject: [PATCH] render audio sources, vary amplitude depending on distance --- Source/audio.cpp | 33 ++++++++++++++++++++++++++++----- Source/audio.h | 9 ++++++++- Source/main.cpp | 9 ++++++++- 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/Source/audio.cpp b/Source/audio.cpp index 308a59e160..a00b149284 100644 --- a/Source/audio.cpp +++ b/Source/audio.cpp @@ -48,9 +48,7 @@ int audioCallback (const void *inputBuffer, int16_t *outputLeft = ((int16_t **) outputBuffer)[0]; int16_t *outputRight = ((int16_t **) outputBuffer)[1]; - float yawRatio = data->linkedHead != NULL - ? data->linkedHead->getYaw() / 90.0 - : 0; + float yawRatio = 0; int numSamplesDelay = abs(floor(yawRatio * PHASE_DELAY_AT_90)); @@ -81,6 +79,17 @@ int audioCallback (const void *inputBuffer, memcpy(trailingBuffer, data->delayBuffer + (PHASE_DELAY_AT_90 - numSamplesDelay), offsetBytes); memcpy(trailingBuffer + numSamplesDelay, samplesToQueue, BUFFER_LENGTH_BYTES - offsetBytes); + glm::vec3 headPos = data->linkedHead->getPos(); + glm::vec3 sourcePos = data->sources[0].position; + + float distance = sqrtf(powf(-headPos[0] - sourcePos[0], 2) + powf(-headPos[2] - sourcePos[2], 2)); + float amplitudeRatio = powf(0.5, cbrtf(distance)); + + for (int i = 0; i < BUFFER_LENGTH_BYTES / sizeof(int16_t); i++) { + leadingBuffer[i] *= amplitudeRatio; + trailingBuffer[i] *= amplitudeRatio; + } + if (wrapAroundSamples > 0) { delete[] samplesToQueue; } @@ -100,7 +109,8 @@ Use Audio::getError() to retrieve the error code. */ bool Audio::init() { - return Audio::init(NULL); + Head deadHead = Head(); + return Audio::init(&deadHead); } bool Audio::init(Head* mainHead) @@ -138,6 +148,20 @@ error: return false; } +void Audio::sourceSetup() +{ + // render gl objects on screen for our three sources + // assume that glMatrixPush() has been called and we are at 0,0,0 + glColor3f(1, 0, 0); + + AudioSource source1 = AudioSource(); + source1.position = glm::vec3(3, 0, -1); + data->sources[0] = source1; + + glTranslatef(source1.position[0], source1.position[1], source1.position[2]); + glutSolidCube(0.5); +} + void Audio::readFile() { FILE *soundFile = fopen("love.raw", "r"); @@ -150,7 +174,6 @@ void Audio::readFile() data->fileBuffer = new int16_t[data->fileSamples]; std::fread(data->fileBuffer, sizeof(int16_t), data->fileSamples, soundFile); - std::fclose(soundFile); } diff --git a/Source/audio.h b/Source/audio.h index ed4052c0b5..a97e7ff970 100644 --- a/Source/audio.h +++ b/Source/audio.h @@ -16,12 +16,19 @@ #define BUFFER_LENGTH_BYTES 1024 #define PHASE_DELAY_AT_90 20 +struct AudioSource { + glm::vec3 position; + int16_t *audioData; +}; + class Audio { public: // initializes audio I/O static bool init(); static bool init(Head* mainHead); + static void sourceSetup(); + // terminates audio I/O static bool terminate(); private: @@ -34,11 +41,11 @@ private: int fileSamples; // length in bytes of audio buffer - int16_t *delayBuffer; int16_t *fileBuffer; Head* linkedHead; + AudioSource sources[3]; AudioData() { samplePointer = 0; diff --git a/Source/main.cpp b/Source/main.cpp index d43ff20fbf..31c954e3e3 100644 --- a/Source/main.cpp +++ b/Source/main.cpp @@ -103,7 +103,7 @@ ParticleSystem balls(0, ); -Cloud cloud(50000, // Particles +Cloud cloud(0, // Particles box, // Bounding Box false // Wrap ); @@ -600,6 +600,13 @@ void display(void) // Render the world box if (!display_head && stats_on) render_world_box(); + // render audio sources and start them + if (audio_on) { + glPushMatrix(); + Audio::sourceSetup(); + glPopMatrix(); + } + //glm::vec3 test(0.5, 0.5, 0.5); //render_vector(&test);