diff --git a/src/openal.c b/src/openal.c index 9bda8f8..5008805 100644 --- a/src/openal.c +++ b/src/openal.c @@ -24,9 +24,9 @@ ACTIVESOUNDSAMPLE BlankActiveSound = {SID_NOSOUND,ASP_Minimum,0,0,NULL,0,0,0,0,0 SOUNDSAMPLEDATA BlankGameSound = {0,0,0,0,0,NULL,0,0,NULL,0}; SOUNDSAMPLEDATA GameSounds[SID_MAXIMUM]; -ALCdevice *AvpSoundDevice; -ALvoid *AvpSoundContext; -int AvpFrequency = 44100; +static ALCdevice *AvpSoundDevice; +static ALvoid *AvpSoundContext; +static int AvpFrequency = 44100; extern int WantSound; @@ -188,6 +188,7 @@ int PlatStartSoundSys() alcMakeContextCurrent(AvpSoundContext); + alListenerf(AL_GAIN, 1.0); alListenerfv(AL_POSITION, pos); alListenerfv(AL_VELOCITY, vel); alListenerfv(AL_ORIENTATION, or); @@ -488,28 +489,30 @@ int PlatPlaySound(int activeIndex) { int si; - if (!SoundActivated) + if (!SoundActivated) { return 0; - - if ((activeIndex < 0) || (activeIndex >= SOUND_MAXACTIVE)) + } + + if ((activeIndex < 0) || (activeIndex >= SOUND_MAXACTIVE)) { return 0; + } + si = ActiveSounds[activeIndex].soundIndex; - if ((si < 0) || (si >= SID_MAXIMUM)) + if ((si < 0) || (si >= SID_MAXIMUM)) { return 0; - if (!GameSounds[si].loaded) + } + if (!GameSounds[si].loaded) { return 0; + } - if (!PlatSoundHasStopped(activeIndex)) - PlatStopSound (activeIndex); - - alSourcei (ActiveSounds[activeIndex].ds3DBufferP, AL_BUFFER, + alSourceStop(ActiveSounds[activeIndex].ds3DBufferP); + + alSourcei(ActiveSounds[activeIndex].ds3DBufferP, AL_BUFFER, GameSounds[si].dsBufferP); - if (ActiveSounds[activeIndex].loop) - alSourcei (ActiveSounds[activeIndex].ds3DBufferP, AL_LOOPING, AL_TRUE); - else - alSourcei (ActiveSounds[activeIndex].ds3DBufferP, AL_LOOPING, AL_FALSE); - + alSourcei(ActiveSounds[activeIndex].ds3DBufferP, AL_LOOPING, + ActiveSounds[activeIndex].loop ? AL_TRUE : AL_FALSE); + if (1 || ActiveSounds[activeIndex].pitch != GameSounds[si].pitch) { PlatChangeSoundPitch(activeIndex, ActiveSounds[activeIndex].pitch); } @@ -555,15 +558,12 @@ void PlatStopSound(int activeIndex) #ifdef OPENAL_DEBUG fprintf(stderr, "OPENAL: PlatStopSound(%d)\n", activeIndex); #endif - if (!SoundActivated) + if (!SoundActivated) { return; - -// if (ActiveSounds[activeIndex].paused) -// alSourcePause (ActiveSounds[activeIndex].ds3DBufferP); -// else -// alSourceStop (ActiveSounds[activeIndex].ds3DBufferP); - if (!PlatSoundHasStopped (activeIndex)) - alSourceStop (ActiveSounds[activeIndex].ds3DBufferP); + } + + // TODO: should be able to release data here + alSourceStop(ActiveSounds[activeIndex].ds3DBufferP); } /* table generated by: @@ -590,9 +590,10 @@ static const float vol_to_gain_table[] = { int PlatChangeGlobalVolume(int volume) { - if (!SoundActivated) + if (!SoundActivated) { return 0; - + } + alListenerf(AL_GAIN, vol_to_gain_table[volume]); return 1; @@ -600,9 +601,10 @@ int PlatChangeGlobalVolume(int volume) int PlatChangeSoundVolume(int activeIndex, int volume) { - if (!SoundActivated) + if (!SoundActivated) { return 0; - + } + alSourcef(ActiveSounds[activeIndex].ds3DBufferP, AL_GAIN, vol_to_gain_table[volume]); @@ -615,11 +617,13 @@ int PlatChangeSoundPitch(int activeIndex, int pitch) SOUNDINDEX gsi = ActiveSounds[activeIndex].soundIndex; - if (!SoundActivated) + if (!SoundActivated) { return 0; - - if ((pitch < PITCH_MIN) || (pitch >= PITCH_MAX)) + } + + if ((pitch < PITCH_MIN) || (pitch >= PITCH_MAX)) { return 0; + } if (pitch == PITCH_DEFAULTPLAT) { frequency = GameSounds[gsi].dsFrequency; @@ -660,17 +664,20 @@ int PlatSoundHasStopped(int activeIndex) fprintf(stderr, "PlatSoundHasStopped(%d)\n", activeIndex); #endif - if (!SoundActivated) + if (!SoundActivated) { return 0; - + } + alGetSourceiv (ActiveSounds[activeIndex].ds3DBufferP, AL_SOURCE_STATE, &val); - if (alGetError () != AL_NO_ERROR) + if (alGetError () != AL_NO_ERROR) { return SOUND_PLATFORMERROR; + } - if ((val != AL_PLAYING) && (val != AL_PAUSED)) + if ((val != AL_PLAYING) && (val != AL_PAUSED)) { return 1; + } return 0; } @@ -681,11 +688,12 @@ int PlatDo3dSound(int activeIndex) VECTORCH relativePosn; int newVolume; - if (!SoundActivated) + if (!SoundActivated) { return 0; - - if (ActiveSounds[activeIndex].threedee == 0) - return 1; + } + +// if (ActiveSounds[activeIndex].threedee == 0) +// return 1; relativePosn.vx = ActiveSounds[activeIndex].threedeedata.position.vx - Global_VDB_Ptr->VDB_World.vx; @@ -695,16 +703,14 @@ int PlatDo3dSound(int activeIndex) Global_VDB_Ptr->VDB_World.vz; distance = Magnitude(&relativePosn); - + + /* Deal with paused looping sounds. */ if (ActiveSounds[activeIndex].paused) { if (distance < (ActiveSounds[activeIndex].threedeedata.outer_range + SOUND_DEACTIVATERANGE)) { - PlatStopSound (activeIndex); - - if (ActiveSounds[activeIndex].loop) - alSourcei (ActiveSounds[activeIndex].ds3DBufferP, AL_LOOPING, AL_TRUE); - else - alSourcei (ActiveSounds[activeIndex].ds3DBufferP, AL_LOOPING, AL_FALSE); - + + alSourcei(ActiveSounds[activeIndex].ds3DBufferP, AL_LOOPING, + ActiveSounds[activeIndex].loop ? AL_TRUE : AL_FALSE); + alSourcePlay (ActiveSounds[activeIndex].ds3DBufferP); newVolume = 0; ActiveSounds[activeIndex].paused = 0; @@ -716,31 +722,35 @@ int PlatDo3dSound(int activeIndex) if (distance < ActiveSounds[activeIndex].threedeedata.inner_range) { newVolume = ActiveSounds[activeIndex].volume; } else { + /* Use proper 3D, but our own attenuation. */ if (distance < ActiveSounds[activeIndex].threedeedata.outer_range) { float in_to_dis_to_out = ActiveSounds[activeIndex].threedeedata.outer_range - distance; float in_to_out = ActiveSounds[activeIndex].threedeedata.outer_range - ActiveSounds[activeIndex].threedeedata.inner_range; if (in_to_out > 0.0) { - newVolume = (int) - ((float)ActiveSounds[activeIndex].volume * (in_to_dis_to_out / in_to_out)); + newVolume = (int)((float)ActiveSounds[activeIndex].volume * (in_to_dis_to_out / in_to_out)); } else { newVolume = 0; } } else { newVolume = 0; + /* Deal with looping sounds. */ if ((distance < (ActiveSounds[activeIndex].threedeedata.outer_range + SOUND_DEACTIVATERANGE)) && ActiveSounds[activeIndex].loop) { - PlatStopSound (activeIndex); + + alSourcePause(ActiveSounds[activeIndex].ds3DBufferP); ActiveSounds[activeIndex].paused = 1; } } } - if (newVolume > VOLUME_MAX) + if (newVolume > VOLUME_MAX) { newVolume = VOLUME_MAX; - if (newVolume < VOLUME_MIN) + } + if (newVolume < VOLUME_MIN) { newVolume = VOLUME_MIN; + } #ifdef OPENAL_DEBUG fprintf(stderr, "OPENAL: PlatDo3dSound: idx = %d, volume = %d, distance = %d\n", activeIndex, newVolume, distance); @@ -751,25 +761,23 @@ int PlatDo3dSound(int activeIndex) } if (distance < ActiveSounds[activeIndex].threedeedata.outer_range) { -#if 0 - ActiveSounds[activeIndex].PropSetP_pos[0] = ActiveSounds[activeIndex].threedeedata.position.vx; // 10000.0; - ActiveSounds[activeIndex].PropSetP_pos[1] = ActiveSounds[activeIndex].threedeedata.position.vy; // 10000.0; - ActiveSounds[activeIndex].PropSetP_pos[2] = ActiveSounds[activeIndex].threedeedata.position.vz; // 10000.0; -#endif - ActiveSounds[activeIndex].PropSetP_pos[0] = (ALfloat)relativePosn.vx / (ALfloat)distance; - ActiveSounds[activeIndex].PropSetP_pos[1] = (ALfloat)relativePosn.vy / (ALfloat)distance; - ActiveSounds[activeIndex].PropSetP_pos[2] = (ALfloat)relativePosn.vz / (ALfloat)distance; + ActiveSounds[activeIndex].PropSetP_pos[0] = (ALfloat)relativePosn.vx; + ActiveSounds[activeIndex].PropSetP_pos[1] = (ALfloat)relativePosn.vy; + ActiveSounds[activeIndex].PropSetP_pos[2] = (ALfloat)relativePosn.vz; alSourcefv (ActiveSounds[activeIndex].ds3DBufferP, AL_POSITION, ActiveSounds[activeIndex].PropSetP_pos); + #ifdef OPENAL_DEBUG fprintf(stderr, "OPENAL: Sound : (%f, %f, %f) [%d] [%d,%d]\n", ActiveSounds[activeIndex].PropSetP_pos[0], ActiveSounds[activeIndex].PropSetP_pos[1], ActiveSounds[activeIndex].PropSetP_pos[2], activeIndex, ActiveSounds[activeIndex].threedeedata.inner_range, ActiveSounds[activeIndex].threedeedata.outer_range); #endif - ActiveSounds[activeIndex].PropSetP_vel[0] = - ActiveSounds[activeIndex].threedeedata.velocity.vx; - ActiveSounds[activeIndex].PropSetP_vel[1] = - ActiveSounds[activeIndex].threedeedata.velocity.vy; - ActiveSounds[activeIndex].PropSetP_vel[2] = - ActiveSounds[activeIndex].threedeedata.velocity.vz; + +// No doppler for now. +// ActiveSounds[activeIndex].PropSetP_vel[0] = +// ActiveSounds[activeIndex].threedeedata.velocity.vx; +// ActiveSounds[activeIndex].PropSetP_vel[1] = +// ActiveSounds[activeIndex].threedeedata.velocity.vy; +// ActiveSounds[activeIndex].PropSetP_vel[2] = +// ActiveSounds[activeIndex].threedeedata.velocity.vz; // alSourcefv (ActiveSounds[activeIndex].ds3DBufferP, // AL_VELOCITY, ActiveSounds[activeIndex].PropSetP_vel); } @@ -825,11 +833,17 @@ void PlatUpdatePlayer() #ifdef OPENAL_DEBUG fprintf(stderr, "OPENAL: Player: (%f, %f, %f) (%f, %f, %f %f, %f, %f)\n", pos[0], pos[1], pos[2], or[0], or[1], or[2], or[3], or[4], or[5]); #endif - // fixme: add reverb check + + pos[0] = 0.0f; + pos[1] = 0.0f; + pos[2] = 0.0f; + alListenerfv (AL_ORIENTATION, or); - // alListenerfv (AL_VELOCITY, vel); - /* alListenerfv (AL_POSITION, pos); */ + alListenerfv (AL_VELOCITY, vel); + alListenerfv (AL_POSITION, pos); } + + // TODO: reverb handling } void PlatEndGameSound(SOUNDINDEX index) @@ -839,14 +853,15 @@ void PlatEndGameSound(SOUNDINDEX index) GameSounds[index].loaded = 0; GameSounds[index].dsFrequency = 0; - if (GameSounds[index].wavName) { + if (GameSounds[index].wavName != NULL) { DeallocateMem(GameSounds[index].wavName); GameSounds[index].wavName = NULL; } - if (!SoundActivated) + if (!SoundActivated) { return; - + } + if((index<0)||(index>=SID_MAXIMUM)) return; /* no such sound */ for (i = 0; i < SOUND_MAXACTIVE; i++) { @@ -896,6 +911,7 @@ void InitialiseBaseFrequency(SOUNDINDEX soundNum) void PlatSetEnviroment(unsigned int env_index, float reverb_mix) { + // TODO: implement #ifdef OPENAL_DEBUG fprintf(stderr, "OPENAL: PlatSetEnvironment(%d, %f)\n", env_index, reverb_mix); #endif @@ -911,17 +927,20 @@ void UpdateSoundFrequencies() fprintf(stderr, "OPENAL: UpdateSoundFreqncies()\n"); #endif - if (!SoundActivated) + if (!SoundActivated) { return; + } - if (!SoundSwitchedOn) + if (!SoundSwitchedOn) { return; + } for (i = 0; i < SOUND_MAXACTIVE; i++) { int gameIndex = ActiveSounds[i].soundIndex; - if (gameIndex == SID_NOSOUND) + if (gameIndex == SID_NOSOUND) { continue; + } if (TimeScale != ONE_FIXED) { #ifdef OPENAL_DEBUG @@ -929,8 +948,9 @@ void UpdateSoundFrequencies() #endif } - if (ActiveSounds[i].pitch != GameSounds[gameIndex].pitch) + if (ActiveSounds[i].pitch != GameSounds[gameIndex].pitch) { PlatChangeSoundPitch(i,ActiveSounds[i].pitch); + } } } @@ -988,25 +1008,32 @@ int LoadWavFile(int soundNum, char * wavFileName) ALvoid *data, *bufferPtr; int len, seclen; FILE *fp; - char* wavname; + const char* wavname; #ifdef OPENAL_DEBUG fprintf(stderr, "LoadWavFile(%d, %s) - sound\n", soundNum, wavFileName); #endif - if (!SoundActivated) + if (!SoundActivated) { return 0; + } /* TODO: Perm for now, until custom rifs can be loaded in ~/.avp */ fp = OpenGameFile(wavFileName, FILEMODE_READONLY, FILETYPE_PERM); - if (fp == NULL) + if (fp == NULL) { return 0; + } fseek(fp, 0, SEEK_END); len = ftell(fp); rewind(fp); - data = malloc(len); + data = (ALvoid*) malloc(len); + if( data == NULL ) { + fclose(fp); + return 0; + } + fread(data, 1, len, fp); fclose(fp); @@ -1015,29 +1042,32 @@ int LoadWavFile(int soundNum, char * wavFileName) return 0; } - alGenBuffers (1, &(GameSounds[soundNum].dsBufferP)); - alBufferData (GameSounds[soundNum].dsBufferP, format, bufferPtr, len, freq); + alGenBuffers(1, &(GameSounds[soundNum].dsBufferP)); + alBufferData(GameSounds[soundNum].dsBufferP, format, bufferPtr, len, freq); - wavname = strrchr (wavFileName, '\\'); - if (wavname) + // get the basename of the filename + wavname = strrchr(wavFileName, '\\'); + if (wavname != NULL) { wavname++; - else + } else { wavname = wavFileName; - GameSounds[soundNum].wavName = (char *)AllocateMem (strlen (wavname) + 1); - strcpy (GameSounds[soundNum].wavName, wavname); + } + + GameSounds[soundNum].wavName = (char *)AllocateMem(strlen(wavname) + 1); + strcpy(GameSounds[soundNum].wavName, wavname); GameSounds[soundNum].flags = SAMPLE_IN_HW; GameSounds[soundNum].length = (seclen != 0) ? seclen : 1; GameSounds[soundNum].dsFrequency = freq; - free (data); + free(data); return 1; } unsigned char *ExtractWavFile(int soundIndex, unsigned char *bufferPtr) { - ALint len, seclen = 0; + ALint len, seclen; void *udata; ALushort rfmt, rfreq; size_t slen; @@ -1046,13 +1076,14 @@ unsigned char *ExtractWavFile(int soundIndex, unsigned char *bufferPtr) fprintf(stderr, "OPENAL: ExtractWavFile(%d, %p)\n", soundIndex, bufferPtr); #endif + if (!SoundActivated) { + return 0; + } + slen = strlen (bufferPtr) + 1; GameSounds[soundIndex].wavName = (char *)AllocateMem (slen); strcpy (GameSounds[soundIndex].wavName, bufferPtr); - if (!SoundActivated) - return 0; - bufferPtr += slen; #ifdef OPENAL_DEBUG @@ -1060,15 +1091,15 @@ fprintf(stderr, "OPENAL: Loaded %s\n", GameSounds[soundIndex].wavName); #endif if( LoadWAV( bufferPtr, &udata, &rfmt, &rfreq, &len, &seclen ) ) { - alGenBuffers (1, &(GameSounds[soundIndex].dsBufferP)); - alBufferData (GameSounds[soundIndex].dsBufferP, - rfmt, udata, len, rfreq); - /* GameSounds[soundIndex].loaded = 1; */ - GameSounds[soundIndex].flags = SAMPLE_IN_HW; - GameSounds[soundIndex].length = (seclen != 0) ? seclen : 1; - GameSounds[soundIndex].dsFrequency = rfreq; - /* GameSounds[soundIndex].pitch = PITCH_DEFAULTPLAT; */ + alGenBuffers(1, &(GameSounds[soundIndex].dsBufferP)); + alBufferData (GameSounds[soundIndex].dsBufferP, rfmt, udata, len, rfreq); + + //GameSounds[soundIndex].loaded = 1; + GameSounds[soundIndex].flags = SAMPLE_IN_HW; + GameSounds[soundIndex].length = (seclen != 0) ? seclen : 1; + GameSounds[soundIndex].dsFrequency = rfreq; + //GameSounds[soundIndex].pitch = PITCH_DEFAULTPLAT; } /* read RIFF chunk length and jump past it */