[Fix] Mkay. Sound seems to be working without any problems now.

This commit is contained in:
Allanis 2013-02-22 22:07:42 +00:00
parent b074bce927
commit fccd9dcc22
7 changed files with 110 additions and 29 deletions

View File

@ -19,6 +19,9 @@
#define BUFFER_SIZE (4096 * 8)
// Gobal sound mutex.
extern SDL_mutex* sound_lock;
// Saves the music to ram in this structure.
typedef struct {
char name[32]; // Name.
@ -67,6 +70,7 @@ int music_thread(void* unused) {
while(!music_is(MUSIC_KILL)) {
if(music_is(MUSIC_PLAYING)) {
SDL_mutexP(music_vorbis_lock); // Lock the mutex.
SDL_mutexP(sound_lock);
// Start playing current song.
active = 0; // Load first buffer.
@ -80,8 +84,12 @@ int music_thread(void* unused) {
if(stream_loadBuffer(music_buffer[active])) music_rm(MUSIC_PLAYING);
alSourceQueueBuffers(music_source, 1, &music_buffer[active]);
SDL_mutexV(sound_lock);
active = 0; // dive into the loop.
while(music_is(MUSIC_PLAYING)) {
SDL_mutexP(sound_lock);
alGetSourcei(music_source, AL_BUFFERS_PROCESSED, &stat);
if(stat > 0) {
// Refill active buffer.
@ -91,11 +99,16 @@ int music_thread(void* unused) {
active = 1 - active;
}
SDL_mutexV(sound_lock);
SDL_Delay(0);
}
SDL_mutexP(sound_lock);
alSourceStop(music_source);
alSourceUnqueueBuffers(music_source, 2, music_buffer);
SDL_mutexV(sound_lock);
SDL_mutexV(music_vorbis_lock);
}
SDL_Delay(0); // We must not kill resources.
@ -134,10 +147,16 @@ int music_init(void) {
music_vorbis_lock = SDL_CreateMutex();
music_find();
music_vorbis.file.fd = 0; // Indication that it's not loaded..
SDL_mutexP(sound_lock);
alGenBuffers(2, music_buffer);
alGenSources(1, &music_source);
alSourcef(music_source, AL_ROLLOFF_FACTOR, 0.);
alSourcei(music_source, AL_SOURCE_RELATIVE, AL_TRUE);
SDL_mutexV(sound_lock);
return 0;
}
@ -223,7 +242,11 @@ static void music_free(void) {
}
void music_volume(const double vol) {
SDL_mutexP(sound_lock);
alSourcef(music_source, AL_GAIN, (ALfloat)vol);
SDL_mutexV(sound_lock);
}
// Music control functions.

View File

@ -254,10 +254,6 @@ static void pilot_update(Pilot* pilot, const double dt) {
// Should not go faster.
vect_pset(&pilot->solid->vel, pilot->ship->speed,
VANGLE(pilot->solid->vel));
// Update the source.
//alSource3f(pilot->source, AL_POSITION, pilot->solid->pos.x, pilot->solid->pos.y, 0.);
//alSource3f(pilot->source, AL_VELOCITY, pilot->solid->vel.x, pilot->solid->vel.y, 0.);
}
// Pilot is getting ready or is in, hyperspace.
@ -365,7 +361,6 @@ void pilot_init(Pilot* pilot, Ship* ship, char* name, Faction* faction, AI_Profi
// Set flags and functions.
if(flags & PILOT_PLAYER) {
//alSourcef(pilot->source, AL_GAIN, 0.);
pilot->think = player_think; // Players don't need to thing! :P
pilot->render = NULL; // Render will be called from player_think
pilot_setFlag(pilot, PILOT_PLAYER); // It's a player!

View File

@ -67,9 +67,6 @@ typedef struct Pilot {
PilotOutfit* secondary; // Secondary weapon.
PilotOutfit* ammo; // Secondary ammo (if needed).
// Sound source.
ALuint source;
// Misc.
unsigned int flags; // Used for AI etc.
unsigned int ptimer; // Generic timer for internal pilot use.

View File

@ -798,12 +798,9 @@ void player_think(Pilot* player) {
vect_pset(&player->solid->force, player->ship->thrust * player_acc, player->solid->dir);
// Set the listener stuff.
ALfloat ori[] = { 0., 0., 0., 0., 0., 1. };
ori[0] = cos(player->solid->dir);
ori[1] = sin(player->solid->dir);
alListenerfv(AL_ORIENTATION, ori);
alListener3f(AL_POSITION, player->solid->pos.x, player->solid->pos.y, 0.);
alListener3f(AL_VELOCITY, player->solid->vel.x, player->solid->vel.y, 0.);
sound_listener(player->solid->dir,
player->solid->pos.x, player->solid->pos.y,
player->solid->vel.x, player->solid->vel.y);
}
// Modify the radar resolution.

View File

@ -23,6 +23,9 @@ typedef struct {
#define voice_set(v,f) ((v)->flags |= f)
#define voice_is(v,f) ((v)->flags & f)
// Global sound lock.
SDL_mutex* sound_lock;
// Gobal device and context.
static ALCcontext* al_context = NULL;
static ALCdevice* al_device = NULL;
@ -44,6 +47,9 @@ static int sound_load(ALuint* buffer, char* filename);
static void sound_free(alSound* snd);
int sound_init(void) {
sound_lock = SDL_CreateMutex();
SDL_mutexP(sound_lock);
const ALchar* device = alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER);
DEBUG("OpenAL using device '%s'", device);
// Open the default device.
@ -69,6 +75,15 @@ int sound_init(void) {
return -4;
}
// Set the master gain.
alListenerf(AL_GAIN, .5);
alDistanceModel(AL_INVERSE_DISTANCE_CLAMPED);
ALenum err = alGetError();
DEBUG("%d", err);
SDL_mutexV(sound_lock);
// Load up all the sounds.
sound_makeList();
@ -97,11 +112,16 @@ void sound_exit(void) {
SDL_WaitThread(music_player, NULL);
music_exit();
SDL_mutexP(sound_lock);
if(al_context) {
alcMakeContextCurrent(NULL);
alcDestroyContext(al_context);
}
if(al_device) alcCloseDevice(al_device);
SDL_mutexV(sound_lock);
SDL_DestroyMutex(sound_lock);
}
// Get the buffer to sound of [name].
@ -162,6 +182,8 @@ static int sound_load(ALuint* buffer, char* filename) {
// Get the file data buffer from the packfile.
wavdata = pack_readfile(DATA, filename, &size);
SDL_mutexP(sound_lock);
// Bind to OpenAL buffer.
alGenBuffers(1, buffer);
alBufferData(*buffer, AL_FORMAT_MONO16, wavdata, size, 22050);
@ -172,14 +194,20 @@ static int sound_load(ALuint* buffer, char* filename) {
return 0;
}
SDL_mutexV(sound_lock);
// Finish up.
free(wavdata);
return 0;
}
static void sound_free(alSound* snd) {
SDL_mutexP(sound_lock);
if(snd->name) free(snd->name);
alDeleteBuffers(1, &snd->buffer);
SDL_mutexV(sound_lock);
}
// Update the sounds and prioritize them.
@ -188,16 +216,19 @@ void sound_update(void) {
// TODO: Prioritize the things.
SDL_mutexP(sound_lock);
for(i = 0; i < nvoice_stack; i++) {
if(voice_is(voice_stack[i], VOICE_PLAYING)) {
// Update position.
alSource3f(voice_stack[i]->source, AL_POSITION,
voice_stack[i]->px, voice_stack[i]->py, 0.);
alSource3f(voice_stack[i]->source, AL_VELOCITY,
voice_stack[i]->vx, voice_stack[i]->vy, 0.);
//alSource3f(voice_stack[i]->source, AL_VELOCITY,
//voice_stack[i]->vx, voice_stack[i]->vy, 0.);
}
}
SDL_mutexV(sound_lock);
}
// Create a dynamic moving voice.
@ -214,6 +245,8 @@ alVoice* sound_addVoice(int priority, double px, double py, double vx, double vy
voc = malloc(sizeof(alVoice));
voice_stack[nvoice_stack-1] = voc;
SDL_mutexP(sound_lock);
// Try and grab a source.
voc->source = 0;
alGenSources(1, &voc->source);
@ -227,18 +260,24 @@ alVoice* sound_addVoice(int priority, double px, double py, double vx, double vy
if(looping) voice_set(voc, VOICE_LOOPING);
voc->px = px;
voc->py = py;
voc->vx = vx;
voc->vy = vy;
//voc->vx = vx;
//voc->vy = vy;
// Set the source.
if(voc->source) {
alSourcei(voc->source, AL_SOURCE_RELATIVE, AL_TRUE);
alSourcef(voc->source, AL_GAIN, 1.);
alSourcei(voc->source, AL_BUFFER, buffer);
alSourcef(voc->source, AL_MAX_DISTANCE, 1000.);
alSourcef(voc->source, AL_REFERENCE_DISTANCE, 250.);
alSourcei(voc->source, AL_SOURCE_RELATIVE, AL_FALSE);
alSourcef(voc->source, AL_GAIN, 0.5);
alSource3f(voc->source, AL_POSITION, voc->px, voc->py, 0.);
alSource3f(voc->source, AL_VELOCITY, voc->vx, voc->vy, 0.);
//alSource3f(voc->source, AL_VELOCITY, voc->vx, voc->vy, 0.);
if(voice_is(voc, VOICE_LOOPING))
alSourcei(voc->source, AL_LOOPING, AL_TRUE);
else
alSourcei(voc->source, AL_LOOPING, AL_FALSE);
// Try to play the source.
alSourcePlay(voc->source);
@ -246,6 +285,8 @@ alVoice* sound_addVoice(int priority, double px, double py, double vx, double vy
if(err == AL_NO_ERROR) voice_set(voc, VOICE_PLAYING);
else DEBUG("Source player failure");
}
SDL_mutexV(sound_lock);
return voc;
}
@ -264,12 +305,17 @@ void sound_delVoice(alVoice* voice) {
}
if(voice->source) {
SDL_mutexP(sound_lock);
alGetSourcei(voice->source, AL_SOURCE_STATE, &stat);
if(stat == AL_PLAYING) alSourceStop(voice->source);
alDeleteSources(1, &voice->source);
voice->source = 0;
SDL_mutexV(sound_lock);
}
free(voice_stack[i]);
nvoice_stack--;
for(; i < nvoice_stack; i++)
voice_stack[i] = voice_stack[i+1];
@ -278,7 +324,20 @@ void sound_delVoice(alVoice* voice) {
void voice_update(alVoice* voice, double px, double py, double vx, double vy) {
voice->px = px;
voice->py = py;
voice->vx = vx;
voice->vy = vy;
//voice->vx = vx;
//voice->vy = vy;
}
void sound_listener(double dir, double px, double py, double vx, double vy) {
SDL_mutexP(sound_lock);
ALfloat ori[] = { 0., 0., 0., 0., 0., 1. };
ori[0] = cos(dir);
ori[1] = sin(dir);
alListenerfv(AL_ORIENTATION, ori);
alListener3f(AL_POSITION, px, py, 0.);
//alListener3f(AL_VELOCITY, vx, vy, 0.);
SDL_mutexV(sound_lock);
}

View File

@ -33,4 +33,5 @@ alVoice* sound_addVoice(int priority, double px, double py, double vx, double vy
void sound_delVoice(alVoice* voice);
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);

View File

@ -14,8 +14,8 @@
#define weapon_isSmart(w) (w->think)
#define VOICE_PRIORITY_BOLD 10 // Default.
#define VOICE_PRIORITY_AMMP 8 // Higher.
#define VOICE_PRIORITY_BOLT 10 // Default.
#define VOICE_PRIORITY_AMMO 8 // Higher.
// Some stuff from pilot.
extern Pilot** pilot_stack;
@ -32,7 +32,7 @@ typedef struct Weapon {
const Outfit* outfit; // Related outfit that fired.
unsigned int timer; // Mainly used to see when the weapon was fired.
//alVoice voice; // Virtual voise.
alVoice* voice; // Virtual voise.
// Update position and render.
void(*update)(struct Weapon*, const double, WeaponLayer); // Position update and render.
@ -228,9 +228,9 @@ static void weapon_update(Weapon* w, const double dt, WeaponLayer layer) {
(*w->solid->update)(w->solid, dt);
// Update the sound.
/*if(w->voise)
if(w->voice)
voice_update(w->voice, w->solid->pos.x, w->solid->pos.y,
w->solid->vel.x, w->solid->vel.y);*/
w->solid->vel.x, w->solid->vel.y);
}
// Good shot.
@ -269,14 +269,22 @@ static Weapon* weapon_create(const Outfit* outfit, const double dir, const Vec2*
vectcpy(&v, vel);
vect_cadd(&v, outfit->speed*cos(rdir), outfit->speed*sin(rdir));
w->solid = solid_create(mass, rdir, pos, &v);
w->voice = sound_addVoice(VOICE_PRIORITY_BOLT,
w->solid->pos.x, w->solid->pos.y,
w->solid->vel.x, w->solid->vel.y, w->outfit->sound, 0);
break;
case OUTFIT_TYPE_MISSILE_SEEK_AMMO:
mass = w->outfit->mass;
w->solid = solid_create(mass, dir, pos, vel);
w->think = think_seeker; // Eeek!!!
w->voice = sound_addVoice(VOICE_PRIORITY_AMMO,
w->solid->pos.x, w->solid->pos.y,
w->solid->vel.x, w->solid->vel.y, w->outfit->sound, 0);
break;
default:
// Just dump it where the player is.
w->voice = NULL;
w->solid = solid_create(mass, dir, pos, vel);
break;
}
@ -355,6 +363,7 @@ static void weapon_destroy(Weapon* w, WeaponLayer layer) {
// Clear the weapon.
static void weapon_free(Weapon* w) {
sound_delVoice(w->voice);
solid_free(w->solid);
free(w);
}