mirror of
https://github.com/overte-org/overte.git
synced 2025-08-09 09:08:47 +02:00
render audio sources, vary amplitude depending on distance
This commit is contained in:
parent
6ed9ca056d
commit
d9caf566de
3 changed files with 44 additions and 7 deletions
|
@ -48,9 +48,7 @@ int audioCallback (const void *inputBuffer,
|
||||||
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];
|
||||||
|
|
||||||
float yawRatio = data->linkedHead != NULL
|
float yawRatio = 0;
|
||||||
? data->linkedHead->getYaw() / 90.0
|
|
||||||
: 0;
|
|
||||||
|
|
||||||
int numSamplesDelay = abs(floor(yawRatio * PHASE_DELAY_AT_90));
|
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, data->delayBuffer + (PHASE_DELAY_AT_90 - numSamplesDelay), offsetBytes);
|
||||||
memcpy(trailingBuffer + numSamplesDelay, samplesToQueue, BUFFER_LENGTH_BYTES - 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) {
|
if (wrapAroundSamples > 0) {
|
||||||
delete[] samplesToQueue;
|
delete[] samplesToQueue;
|
||||||
}
|
}
|
||||||
|
@ -100,7 +109,8 @@ Use Audio::getError() to retrieve the error code.
|
||||||
*/
|
*/
|
||||||
bool Audio::init()
|
bool Audio::init()
|
||||||
{
|
{
|
||||||
return Audio::init(NULL);
|
Head deadHead = Head();
|
||||||
|
return Audio::init(&deadHead);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Audio::init(Head* mainHead)
|
bool Audio::init(Head* mainHead)
|
||||||
|
@ -138,6 +148,20 @@ error:
|
||||||
return false;
|
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()
|
void Audio::readFile()
|
||||||
{
|
{
|
||||||
FILE *soundFile = fopen("love.raw", "r");
|
FILE *soundFile = fopen("love.raw", "r");
|
||||||
|
@ -150,7 +174,6 @@ void Audio::readFile()
|
||||||
data->fileBuffer = new int16_t[data->fileSamples];
|
data->fileBuffer = new int16_t[data->fileSamples];
|
||||||
std::fread(data->fileBuffer, sizeof(int16_t), data->fileSamples, soundFile);
|
std::fread(data->fileBuffer, sizeof(int16_t), data->fileSamples, soundFile);
|
||||||
|
|
||||||
|
|
||||||
std::fclose(soundFile);
|
std::fclose(soundFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,12 +16,19 @@
|
||||||
#define BUFFER_LENGTH_BYTES 1024
|
#define BUFFER_LENGTH_BYTES 1024
|
||||||
#define PHASE_DELAY_AT_90 20
|
#define PHASE_DELAY_AT_90 20
|
||||||
|
|
||||||
|
struct AudioSource {
|
||||||
|
glm::vec3 position;
|
||||||
|
int16_t *audioData;
|
||||||
|
};
|
||||||
|
|
||||||
class Audio {
|
class Audio {
|
||||||
public:
|
public:
|
||||||
// initializes audio I/O
|
// initializes audio I/O
|
||||||
static bool init();
|
static bool init();
|
||||||
static bool init(Head* mainHead);
|
static bool init(Head* mainHead);
|
||||||
|
|
||||||
|
static void sourceSetup();
|
||||||
|
|
||||||
// terminates audio I/O
|
// terminates audio I/O
|
||||||
static bool terminate();
|
static bool terminate();
|
||||||
private:
|
private:
|
||||||
|
@ -34,11 +41,11 @@ private:
|
||||||
int fileSamples;
|
int fileSamples;
|
||||||
|
|
||||||
// length in bytes of audio buffer
|
// length in bytes of audio buffer
|
||||||
|
|
||||||
int16_t *delayBuffer;
|
int16_t *delayBuffer;
|
||||||
int16_t *fileBuffer;
|
int16_t *fileBuffer;
|
||||||
|
|
||||||
Head* linkedHead;
|
Head* linkedHead;
|
||||||
|
AudioSource sources[3];
|
||||||
|
|
||||||
AudioData() {
|
AudioData() {
|
||||||
samplePointer = 0;
|
samplePointer = 0;
|
||||||
|
|
|
@ -103,7 +103,7 @@ ParticleSystem balls(0,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
Cloud cloud(50000, // Particles
|
Cloud cloud(0, // Particles
|
||||||
box, // Bounding Box
|
box, // Bounding Box
|
||||||
false // Wrap
|
false // Wrap
|
||||||
);
|
);
|
||||||
|
@ -600,6 +600,13 @@ void display(void)
|
||||||
// Render the world box
|
// Render the world box
|
||||||
if (!display_head && stats_on) render_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);
|
//glm::vec3 test(0.5, 0.5, 0.5);
|
||||||
//render_vector(&test);
|
//render_vector(&test);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue