diff --git a/source/duke3d/src/sounds.cpp b/source/duke3d/src/sounds.cpp index 51cafcd07..72c118e7d 100644 --- a/source/duke3d/src/sounds.cpp +++ b/source/duke3d/src/sounds.cpp @@ -30,17 +30,18 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #define DQSIZE 256 -int32_t g_numEnvSoundsPlaying, g_highestSoundIdx = 0; +int32_t g_numEnvSoundsPlaying, g_highestSoundIdx; -static int32_t MusicIsWaveform = 0; -static char *MusicPtr = NULL; +static char *MusicPtr; + +static int32_t MusicIsWaveform; static int32_t MusicVoice = -1; -static bool MusicPaused = false; -static bool SoundPaused = false; -std::atomic dnum; -uint32_t dq[DQSIZE]; -static mutex_t m_callback; +static bool MusicPaused; +static bool SoundPaused; + +std::atomic dnum, dq[DQSIZE]; +// static mutex_t m_callback; static inline void S_SetProperties(assvoice_t *snd, int const owner, int const voice, int const dist, int const clock) { @@ -88,7 +89,7 @@ void S_SoundStartup(void) S_MusicVolume(ud.config.MusicVolume); FX_SetReverseStereo(ud.config.ReverseStereo); - mutex_init(&m_callback); + // mutex_init(&m_callback); FX_SetCallBack(S_Callback); FX_SetPrintf(OSD_Printf); } @@ -387,7 +388,7 @@ void S_StopMusic(void) void S_Cleanup(void) { - static uint32_t ldnum; + static uint32_t ldnum = 0; while (ldnum < dnum) { @@ -396,8 +397,10 @@ void S_Cleanup(void) // negative index is RTS playback if ((int32_t)num < 0) { - if (rts_lumplockbyte[-(int32_t)num] >= 200) - rts_lumplockbyte[-(int32_t)num]--; + int const rtsindex = klabs((int32_t)num); + + if (rts_lumplockbyte[rtsindex] >= 200) + --rts_lumplockbyte[rtsindex]; continue; } @@ -411,19 +414,18 @@ void S_Cleanup(void) continue; } - int const vidx = num & (MAXSOUNDINSTANCES - 1); + int const voiceindex = num & (MAXSOUNDINSTANCES - 1); - num = (num - vidx) / MAXSOUNDINSTANCES; + num = (num - voiceindex) / MAXSOUNDINSTANCES; auto &snd = g_sounds[num]; - auto &voice = g_sounds[num].voices[vidx]; + auto &voice = snd.voices[voiceindex]; int const spriteNum = voice.owner; if (EDUKE32_PREDICT_FALSE(snd.num > MAXSOUNDINSTANCES)) OSD_Printf(OSD_ERROR "S_Cleanup(): num exceeds MAXSOUNDINSTANCES! g_sounds[%d].num %d wtf?\n", num, snd.num); - - if (snd.num > 0) + else if (snd.num > 0) --snd.num; // MUSICANDSFX uses t_data[0] to control restarting the sound @@ -467,7 +469,8 @@ int32_t S_LoadSound(int num) void cacheAllSounds(void) { - for (int32_t i = 0, j = 0; i <= g_highestSoundIdx; ++i) + for (int i=0, j=0; i <= g_highestSoundIdx; ++i) + { if (g_sounds[i].ptr == 0) { j++; @@ -476,6 +479,7 @@ void cacheAllSounds(void) S_LoadSound(i); } + } } static inline int S_GetPitch(int num) @@ -540,12 +544,10 @@ static inline int S_GetAngle(int ang, const vec3_t *cam, const vec3_t *pos) return (2048 + ang - getangle(cam->x - pos->x, cam->y - pos->y)) & 2047; } -static bool S_CalcDistAndAng(int32_t spriteNum, int32_t soundNum, int32_t sectNum, int32_t angle, - const vec3_t *cam, const vec3_t *pos, - int32_t *distPtr, int32_t *angPtr) +static int S_CalcDistAndAng(int spriteNum, int soundNum, int sectNum, int angle, + const vec3_t *cam, const vec3_t *pos, int *distPtr, int *angPtr) { - int32_t sndang = 0, sndist = 0; - bool explosion = false; + int sndang = 0, sndist = 0, explosion = 0; if (PN(spriteNum) == APLAYER && P_Get(spriteNum) == screenpeek) goto sound_further_processing; @@ -590,13 +592,19 @@ sound_further_processing: if (sndist < 0) sndist = 0; - if (sectNum > -1 && sndist && PN(spriteNum) != MUSICANDSFX +#ifndef EDUKE32_STANDALONE + if (!FURY && sectNum > -1 && sndist && PN(spriteNum) != MUSICANDSFX && !cansee(cam->x, cam->y, cam->z - (24 << 8), sectNum, SX(spriteNum), SY(spriteNum), SZ(spriteNum) - (24 << 8), SECT(spriteNum))) sndist += sndist>>5; +#else + UNREFERENCED_PARAMETER(sectNum); +#endif if ((g_sounds[soundNum].m & (SF_GLOBAL|SF_DTAG)) == (SF_GLOBAL|SF_DTAG)) { +#ifndef EDUKE32_STANDALONE boost: +#endif explosion = true; if (sndist > 6144) sndist = 6144; @@ -815,17 +823,15 @@ int S_PlaySound(int num) } snd.num++; - snd.voices[sndnum].owner = -1; - snd.voices[sndnum].id = voice; - snd.voices[sndnum].dist = 255 - LOUDESTVOLUME; - snd.voices[sndnum].clock = 0; + S_SetProperties(&snd.voices[sndnum], -1, voice, 255-LOUDESTVOLUME, 0); return voice; } int A_PlaySound(int soundNum, int spriteNum) { - if (EDUKE32_PREDICT_FALSE((unsigned)soundNum > (unsigned)g_highestSoundIdx)) return -1; + if (EDUKE32_PREDICT_FALSE((unsigned)soundNum > (unsigned)g_highestSoundIdx)) + return -1; return (unsigned)spriteNum >= MAXSPRITES ? S_PlaySound(soundNum) : S_PlaySound3D(soundNum, spriteNum, (vec3_t *)&sprite[spriteNum]); @@ -833,34 +839,35 @@ int A_PlaySound(int soundNum, int spriteNum) void S_StopEnvSound(int32_t num, int32_t i) { - if (EDUKE32_PREDICT_FALSE((unsigned)num > (unsigned)g_highestSoundIdx) || g_sounds[num].num <= 0) + if (EDUKE32_PREDICT_FALSE((unsigned)num > (unsigned)g_highestSoundIdx) + || g_sounds[num].num <= 0) return; - int32_t j; + S_Cleanup(); - do + for (int j=0; j FX_Ok) || (i != -1 && g_sounds[num].voices[j].owner == i)) - { + if ((i == -1 && voice.id > FX_Ok) || (i != -1 && voice.owner == i)) + { #ifdef DEBUGGINGAIDS - if (EDUKE32_PREDICT_FALSE(i >= 0 && g_sounds[num].voices[j].id <= FX_Ok)) - initprintf(OSD_ERROR "S_StopEnvSound(): bad voice %d for sound ID %d index %d!\n", g_sounds[num].voices[j].id, num, j); - else + if (EDUKE32_PREDICT_FALSE(i >= 0 && voice.id <= FX_Ok)) + initprintf(OSD_ERROR "S_StopEnvSound(): bad voice %d for sound ID %d index %d!\n", voice.id, num, j); + else #endif - if (g_sounds[num].voices[j].id > FX_Ok) + if (voice.id > FX_Ok) + { + if (FX_SoundActive(voice.id)) { - if (FX_SoundActive(g_sounds[num].voices[j].id)) - FX_StopSound(g_sounds[num].voices[j].id); - break; + FX_StopSound(voice.id); + S_Cleanup(); } + j = 0; + continue; } } } - while (j < MAXSOUNDINSTANCES); } // Do not remove this or make it inline. @@ -937,7 +944,7 @@ void S_Update(void) if ((unsigned)spriteNum >= MAXSPRITES || voice.id <= FX_Ok || !FX_SoundActive(voice.id)) continue; - int32_t sndist, sndang; + int sndist, sndang; S_CalcDistAndAng(spriteNum, sndnum, cs, ca, c, (const vec3_t *)&sprite[spriteNum], &sndist, &sndang); @@ -959,11 +966,9 @@ void S_Callback(intptr_t num) if (num == MUSIC_ID) return; - mutex_lock(&m_callback); - int const ldnum = dnum; - dq[ldnum & (DQSIZE - 1)] = num; - dnum++; - mutex_unlock(&m_callback); +// mutex_lock(&m_callback); + dq[dnum++ & (DQSIZE - 1)] = num; +// mutex_unlock(&m_callback); } void S_ClearSoundLocks(void) @@ -984,7 +989,7 @@ void S_ClearSoundLocks(void) #endif } -bool A_CheckSoundPlaying(int spriteNum, int soundNum) +int A_CheckSoundPlaying(int spriteNum, int soundNum) { if (EDUKE32_PREDICT_FALSE((unsigned)soundNum > (unsigned)g_highestSoundIdx)) return 0; @@ -999,7 +1004,7 @@ bool A_CheckSoundPlaying(int spriteNum, int soundNum) } // Check if actor is playing any sound. -bool A_CheckAnySoundPlaying(int spriteNum) +int A_CheckAnySoundPlaying(int spriteNum) { int const msp = g_highestSoundIdx; @@ -1013,7 +1018,7 @@ bool A_CheckAnySoundPlaying(int spriteNum) return 0; } -bool S_CheckSoundPlaying(int soundNum) +int S_CheckSoundPlaying(int soundNum) { if (EDUKE32_PREDICT_FALSE((unsigned)soundNum > (unsigned)g_highestSoundIdx)) return false; return (g_sounds[soundNum].num != 0); diff --git a/source/duke3d/src/sounds.h b/source/duke3d/src/sounds.h index 675421f4e..e128523f3 100644 --- a/source/duke3d/src/sounds.h +++ b/source/duke3d/src/sounds.h @@ -63,11 +63,11 @@ extern char g_soundlocks[MAXSOUNDS]; extern sound_t g_sounds[MAXSOUNDS]; extern int32_t g_numEnvSoundsPlaying,g_highestSoundIdx; -bool A_CheckSoundPlaying(int spriteNum,int soundNum); +int A_CheckSoundPlaying(int spriteNum,int soundNum); int A_PlaySound(int soundNum, int spriteNum); void S_Callback(intptr_t num); -bool A_CheckAnySoundPlaying(int spriteNum); -bool S_CheckSoundPlaying(int soundNum); +int A_CheckAnySoundPlaying(int spriteNum); +int S_CheckSoundPlaying(int soundNum); void S_Cleanup(void); void S_ClearSoundLocks(void); int32_t S_LoadSound(uint32_t num);