[Change] Just some small tweaks to sound. I am hunting a bug...

This commit is contained in:
Allanis 2013-02-22 23:22:59 +00:00
parent fccd9dcc22
commit 999bdc7c8a

View File

@ -9,7 +9,16 @@
#include "music.h" #include "music.h"
#include "sound.h" #include "sound.h"
#define SOUND_PREFIX "snd/sounds/" // ==============================================
// sound.c controls the routines for using a
// virtual voice wrapper system around the openal
// library to get 3D sound.
//
// We only use position sound and no doppler effect
// right now.
// ==============================================
#define SOUND_PREFIX "../snd/sounds/"
#define SOUND_SUFFIX ".wav" #define SOUND_SUFFIX ".wav"
// Give the buffers a name. // Give the buffers a name.
@ -45,6 +54,7 @@ static int mvoice_stack = 0;
static int sound_makeList(void); static int sound_makeList(void);
static int sound_load(ALuint* buffer, char* filename); static int sound_load(ALuint* buffer, char* filename);
static void sound_free(alSound* snd); static void sound_free(alSound* snd);
static int voice_getSource(alVoice* voc);
int sound_init(void) { int sound_init(void) {
sound_lock = SDL_CreateMutex(); sound_lock = SDL_CreateMutex();
@ -99,7 +109,7 @@ int sound_init(void) {
void sound_exit(void) { void sound_exit(void) {
int i; int i;
// Free the sounds. // Free the sounds.
for(i = 0; i < &nsound_list; i++) for(i = 0; i < nsound_list; i++)
sound_free(&sound_list[i]); sound_free(&sound_list[i]);
free(sound_list); free(sound_list);
sound_list = NULL; sound_list = NULL;
@ -231,19 +241,11 @@ void sound_update(void) {
SDL_mutexV(sound_lock); SDL_mutexV(sound_lock);
} }
// Create a dynamic moving voice. static int voice_getSource(alVoice* voc) {
alVoice* sound_addVoice(int priority, double px, double py, double vx, double vy, int ret;
const ALuint buffer, const int looping) {
alVoice* voc;
ALenum err; ALenum err;
nvoice_stack++; ret = 0; // Default return.
if(nvoice_stack > mvoice_stack)
voice_stack = realloc(voice_stack, ++mvoice_stack*sizeof(alVoice*));
voc = malloc(sizeof(alVoice));
voice_stack[nvoice_stack-1] = voc;
SDL_mutexP(sound_lock); SDL_mutexP(sound_lock);
@ -251,24 +253,17 @@ alVoice* sound_addVoice(int priority, double px, double py, double vx, double vy
voc->source = 0; voc->source = 0;
alGenSources(1, &voc->source); alGenSources(1, &voc->source);
err = alGetError(); err = alGetError();
if(err != AL_NO_ERROR) voc->source = 0;
// Set the data. if(err != AL_NO_ERROR) {
voc->priority = priority; voc->source = 0;
voc->start = SDL_GetTicks(); ret = 1;
voc->buffer = buffer; } else {
if(looping) voice_set(voc, VOICE_LOOPING); // Set the properties.
voc->px = px; alSourcei(voc->source, AL_BUFFER, voc->buffer);
voc->py = py;
//voc->vx = vx;
//voc->vy = vy;
// Set the source. // Distance model.
if(voc->source) { alSourcef(voc->source, AL_MAX_DISTANCE, 200.);
alSourcei(voc->source, AL_BUFFER, buffer); alSourcef(voc->source, AL_REFERENCE_DISTANCE, 50.);
alSourcef(voc->source, AL_MAX_DISTANCE, 1000.);
alSourcef(voc->source, AL_REFERENCE_DISTANCE, 250.);
alSourcei(voc->source, AL_SOURCE_RELATIVE, AL_FALSE); alSourcei(voc->source, AL_SOURCE_RELATIVE, AL_FALSE);
alSourcef(voc->source, AL_GAIN, 0.5); alSourcef(voc->source, AL_GAIN, 0.5);
@ -283,10 +278,40 @@ alVoice* sound_addVoice(int priority, double px, double py, double vx, double vy
alSourcePlay(voc->source); alSourcePlay(voc->source);
err = alGetError(); err = alGetError();
if(err == AL_NO_ERROR) voice_set(voc, VOICE_PLAYING); if(err == AL_NO_ERROR) voice_set(voc, VOICE_PLAYING);
else DEBUG("Source player failure"); else ret = 2;
} }
SDL_mutexV(sound_lock); SDL_mutexV(sound_lock);
return 0;
}
alVoice* sound_addVoice(int priority, double px, double py, double vx, double vy,
const ALuint buffer, const int looping) {
(void)vx;
(void)vy;
alVoice* voc;
nvoice_stack++;
if(nvoice_stack > mvoice_stack)
voice_stack = realloc(voice_stack, ++mvoice_stack*sizeof(alVoice*));
voc = malloc(sizeof(alVoice));
voice_stack[nvoice_stack-1] = voc;
// Set the data.
voc->priority = priority;
voc->start = SDL_GetTicks();
voc->buffer = buffer;
if(looping) voice_set(voc, VOICE_LOOPING);
voc->px = px;
voc->py = py;
//voc->vx = vx;
//voc->vy = vy;
voice_getSource(voc);
return voc; return voc;
} }
@ -322,6 +347,8 @@ void sound_delVoice(alVoice* voice) {
} }
void voice_update(alVoice* voice, double px, double py, double vx, double vy) { void voice_update(alVoice* voice, double px, double py, double vx, double vy) {
(void) vx;
(void) vy;
voice->px = px; voice->px = px;
voice->py = py; voice->py = py;
//voice->vx = vx; //voice->vx = vx;
@ -329,6 +356,8 @@ void voice_update(alVoice* voice, double px, double py, double vx, double vy) {
} }
void sound_listener(double dir, double px, double py, double vx, double vy) { void sound_listener(double dir, double px, double py, double vx, double vy) {
(void)vx;
(void)vy;
SDL_mutexP(sound_lock); SDL_mutexP(sound_lock);
ALfloat ori[] = { 0., 0., 0., 0., 0., 1. }; ALfloat ori[] = { 0., 0., 0., 0., 0., 1. };