From aff53cc9ba16638575e0c7f49b705bda2d73c2df Mon Sep 17 00:00:00 2001 From: Steven Fuller Date: Sat, 8 Dec 2001 00:27:43 +0000 Subject: [PATCH] Disable sound, not crash, when OpenAL init fails. --- src/openal.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 57 insertions(+), 6 deletions(-) diff --git a/src/openal.c b/src/openal.c index dea3f83..5dfd931 100644 --- a/src/openal.c +++ b/src/openal.c @@ -31,6 +31,8 @@ ALCdevice *AvpSoundDevice; ALvoid *AvpSoundContext; int AvpFrequency = 44100; +static int SoundActivated = 0; + /* openal.c TODO: 1. AL_PITCH code does not work. @@ -40,6 +42,7 @@ openal.c TODO: 4. Restarting sound system may or may not work. 5. Doppler (if it was originally used) isn't currently used. Probably also broken in OpenAL (see 1). +6. Better Error Handling (device not avail, etc). */ int PlatStartSoundSys() { @@ -61,12 +64,18 @@ int PlatStartSoundSys() attrlist[3] = AL_FALSE; attrlist[4] = 0; + SoundActivated = 0; + snprintf(buf, sizeof(buf), "'( (sampling-rate %d ))\n", AvpFrequency); AvpSoundDevice = alcOpenDevice(buf); + if (AvpSoundDevice == NULL) + return 0; + AvpSoundContext = alcCreateContext(AvpSoundDevice, attrlist); -// AvpSoundDevice = alcOpenDevice(NULL); -// AvpSoundContext = alcCreateContext(AvpSoundDevice, NULL); + if (AvpSoundContext == NULL) /* TODO: destroy sound device */ + return 0; + alcMakeContextCurrent(AvpSoundContext); alListenerfv(AL_POSITION, pos); @@ -111,6 +120,8 @@ int PlatStartSoundSys() alSourcef(p, AL_REFERENCE_DISTANCE, 1.0f); } + SoundActivated = 1; + return 1; } @@ -367,6 +378,9 @@ int PlatPlaySound(int activeIndex) { int si; + if (!SoundActivated) + return 0; + if ((activeIndex < 0) || (activeIndex >= SOUND_MAXACTIVE)) return 0; si = ActiveSounds[activeIndex].soundIndex; @@ -434,6 +448,9 @@ void PlatStopSound(int activeIndex) { /* printf("PlatStopSound(%d)\n", activeIndex); */ + if (!SoundActivated) + return 0; + // if (ActiveSounds[activeIndex].paused) // alSourcePause (ActiveSounds[activeIndex].ds3DBufferP); // else @@ -467,6 +484,9 @@ static float vol_to_gain_table[] = { int PlatChangeGlobalVolume(int volume) { + if (!SoundActivated) + return 0; + alListenerf(AL_GAIN, vol_to_gain_table[volume]); return 1; @@ -474,6 +494,9 @@ int PlatChangeGlobalVolume(int volume) int PlatChangeSoundVolume(int activeIndex, int volume) { + if (!SoundActivated) + return 0; + alSourcef(ActiveSounds[activeIndex].ds3DBufferP, AL_GAIN, vol_to_gain_table[volume]); @@ -483,7 +506,10 @@ int PlatChangeSoundVolume(int activeIndex, int volume) int PlatChangeSoundPitch(int activeIndex, int pitch) { float frequency; - + + if (!SoundActivated) + return 0; + if ((pitch < PITCH_MIN) || (pitch >= PITCH_MAX)) return 0; @@ -508,6 +534,10 @@ int PlatSoundHasStopped(int activeIndex) /* printf("PlatSoundHasStopped(%d)\n", activeIndex); */ + + if (!SoundActivated) + return 0; + alGetSourceiv (ActiveSounds[activeIndex].ds3DBufferP, AL_SOURCE_STATE, &val); @@ -526,6 +556,9 @@ int PlatDo3dSound(int activeIndex) VECTORCH relativePosn; int newVolume; + if (!SoundActivated) + return 0; + if (ActiveSounds[activeIndex].threedee == 0) return 1; @@ -622,6 +655,9 @@ void PlatUpdatePlayer() { ALfloat vel[3], or[6], pos[3]; + if (!SoundActivated) + return 0; + if (Global_VDB_Ptr) { extern int NormalFrameTime; extern int DopplerShiftIsOn; @@ -672,7 +708,10 @@ void PlatUpdatePlayer() void PlatEndGameSound(SOUNDINDEX index) { int i; - + + if (!SoundActivated) + return 0; + if((index<0)||(index>=SID_MAXIMUM)) return; /* no such sound */ for (i = 0; i < SOUND_MAXACTIVE; i++) { @@ -712,7 +751,7 @@ void InitialiseBaseFrequency(SOUNDINDEX soundNum) int frequency; printf("FREQ InitialiseBaseFrequency(%d) [%d]\n", soundNum, GameSounds[soundNum].pitch==PITCH_DEFAULTPLAT); - + if(GameSounds[soundNum].pitch>PITCH_MAXPLAT) GameSounds[soundNum].pitch=PITCH_MAXPLAT; if(GameSounds[soundNum].pitch