From d24bd0fc1410e6b841e4791554946fd765e4b6bf Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Mon, 16 Dec 2019 16:18:47 +0100 Subject: [PATCH] - added refactored sound code to RR frontend. - removed most code using the low level sound interface directly from Duke and RR frontends. --- source/audiolib/include/fx_man.h | 2 +- source/blood/src/blood.cpp | 2 +- source/blood/src/credits.cpp | 2 +- source/blood/src/endgame.cpp | 2 +- source/blood/src/menu.cpp | 4 +- source/common/menu/menu.cpp | 2 +- source/common/sound/s_soundinternal.h | 4 + source/duke3d/src/gamedef.cpp | 3 - source/duke3d/src/player.cpp | 19 +- source/duke3d/src/sector.cpp | 6 +- source/duke3d/src/sounds.cpp | 13 +- source/duke3d/src/sounds.h | 4 +- source/duke3d/src/sounds_old.cpp | 720 --------------- source/rr/src/actors.cpp | 16 +- source/rr/src/anim.cpp | 6 +- source/rr/src/d_menu.cpp | 17 +- source/rr/src/game.cpp | 53 +- source/rr/src/game.h | 3 + source/rr/src/gamedef.cpp | 64 +- source/rr/src/global.h | 1 - source/rr/src/player.cpp | 20 +- source/rr/src/premap.cpp | 16 +- source/rr/src/screens.cpp | 2 +- source/rr/src/sector.cpp | 17 +- source/rr/src/sounds.cpp | 1157 +++++++++++-------------- source/rr/src/sounds.h | 51 +- source/sw/src/sounds.cpp | 2 +- 27 files changed, 636 insertions(+), 1572 deletions(-) delete mode 100644 source/duke3d/src/sounds_old.cpp diff --git a/source/audiolib/include/fx_man.h b/source/audiolib/include/fx_man.h index ea0f6e428..32b0bbef2 100644 --- a/source/audiolib/include/fx_man.h +++ b/source/audiolib/include/fx_man.h @@ -112,6 +112,6 @@ static FORCE_INLINE int FX_SoundActive(int handle) { return MV_VoicePlaying(hand static FORCE_INLINE int FX_SoundValidAndActive(int handle) { return handle > 0 && MV_VoicePlaying(handle); } static FORCE_INLINE int FX_SoundsPlaying(void) { return MV_VoicesPlaying(); } static FORCE_INLINE int FX_StopSound(int handle) { return FX_CheckMVErr(MV_Kill(handle)); } -static FORCE_INLINE int FX_StopAllSounds(void) { return FX_CheckMVErr(MV_KillAllVoices()); } +static FORCE_INLINE int FX_StopAllSounds_(void) { return FX_CheckMVErr(MV_KillAllVoices()); } #endif diff --git a/source/blood/src/blood.cpp b/source/blood/src/blood.cpp index 86a115d73..e09857edf 100644 --- a/source/blood/src/blood.cpp +++ b/source/blood/src/blood.cpp @@ -1367,7 +1367,7 @@ RESTART: { UpdateDacs(0, true); Mus_Stop(); - FX_StopAllSounds(); + FX_StopAllSounds_(); gQuitGame = 0; gQuitRequest = 0; gRestartGame = 0; diff --git a/source/blood/src/credits.cpp b/source/blood/src/credits.cpp index 1ca8799e6..cfc3db162 100644 --- a/source/blood/src/credits.cpp +++ b/source/blood/src/credits.cpp @@ -282,7 +282,7 @@ void credPlaySmk(const char *_pzSMK, const char *_pzWAV, int nWav) Smacker_Close(hSMK); ctrlClearAllInput(); - FX_StopAllSounds(); + FX_StopAllSounds_(); videoSetPalette(0, 0, 8+2); tileDelete(kSMKTile); Bfree(pzSMK_); diff --git a/source/blood/src/endgame.cpp b/source/blood/src/endgame.cpp index 4a55bf6d5..caadd4c48 100644 --- a/source/blood/src/endgame.cpp +++ b/source/blood/src/endgame.cpp @@ -119,7 +119,7 @@ void CEndGameMgr::Finish(void) { levelSetupOptions(gGameOptions.nEpisode, gNextLevel); gInitialNetPlayers = numplayers; - FX_StopAllSounds(); + FX_StopAllSounds_(); sndKillAllSounds(); gStartNewGame = 1; gInputMode = (INPUT_MODE)at1; diff --git a/source/blood/src/menu.cpp b/source/blood/src/menu.cpp index ef66e2a5d..277b287e4 100644 --- a/source/blood/src/menu.cpp +++ b/source/blood/src/menu.cpp @@ -153,7 +153,7 @@ void NetworkHostGame(CGameMenuItemChain *pItem) { UNREFERENCED_PARAMETER(pItem); Mus_Stop(); - FX_StopAllSounds(); + FX_StopAllSounds_(); UpdateDacs(0, true); gNetPlayers = itemNetworkHostPlayerNum.nValue; gNetPort = strtoul(zNetPortBuffer, NULL, 10); @@ -169,7 +169,7 @@ void NetworkJoinGame(CGameMenuItemChain *pItem) { UNREFERENCED_PARAMETER(pItem); Mus_Stop(); - FX_StopAllSounds(); + FX_StopAllSounds_(); UpdateDacs(0, true); strcpy(gNetAddress, zNetAddressBuffer); gNetPort = strtoul(zNetPortBuffer, NULL, 10); diff --git a/source/common/menu/menu.cpp b/source/common/menu/menu.cpp index 1d893c972..321f30bdf 100644 --- a/source/common/menu/menu.cpp +++ b/source/common/menu/menu.cpp @@ -366,7 +366,7 @@ void M_StartControlPanel (bool makeSound) created = true; M_CreateMenus(); } - FX_StopAllSounds(); + FX_StopAllSounds_(); gi->MenuOpened(); if (makeSound) gi->MenuSound(ActivateSound); diff --git a/source/common/sound/s_soundinternal.h b/source/common/sound/s_soundinternal.h index f2f58102c..6ee9ea24f 100644 --- a/source/common/sound/s_soundinternal.h +++ b/source/common/sound/s_soundinternal.h @@ -448,4 +448,8 @@ struct FReverbField unsigned int Flag; }; +inline void FX_StopAllSounds(void) +{ + soundEngine->StopAllChannels(); +} diff --git a/source/duke3d/src/gamedef.cpp b/source/duke3d/src/gamedef.cpp index 71362aaac..803f7af22 100644 --- a/source/duke3d/src/gamedef.cpp +++ b/source/duke3d/src/gamedef.cpp @@ -5364,7 +5364,6 @@ repeatcase: case CON_DEFINESOUND: { int ps, pe, vo, pr, m; - float volume; g_scriptPtr--; C_GetNextValue(LABEL_DEFINE); @@ -5423,8 +5422,6 @@ repeatcase: int res = S_DefineSound(k, buffer.Data(), ps, pe, pr, m, vo, 1.f); - volume = 1.f; - if (g_dynamicSoundMapping && j >= 0 && (labeltype[j] & LABEL_DEFINE)) G_ProcessDynamicSoundMapping(label + (j << 6), k); continue; diff --git a/source/duke3d/src/player.cpp b/source/duke3d/src/player.cpp index 374255547..147c4f3a6 100644 --- a/source/duke3d/src/player.cpp +++ b/source/duke3d/src/player.cpp @@ -3730,12 +3730,7 @@ void P_FragPlayer(int playerNum) pPlayer->holoduke_on = -1; S_StopEnvSound(DUKE_JETPACK_IDLE, pPlayer->i); - - if (pPlayer->scream_voice > FX_Ok) - { - FX_StopSound(pPlayer->scream_voice); - pPlayer->scream_voice = -1; - } + S_StopEnvSound(-1, pPlayer->i, CHAN_VOICE); } #endif @@ -4996,11 +4991,9 @@ void P_ProcessInput(int playerNum) if (pPlayer->vel.z > 2400 && pPlayer->falling_counter < 255) { pPlayer->falling_counter++; - if (pPlayer->falling_counter >= 38 && pPlayer->scream_voice <= FX_Ok) + if (pPlayer->falling_counter >= 38 && !A_CheckSoundPlaying(pPlayer->i, -1, CHAN_VOICE)) { - int32_t voice = A_PlaySound(DUKE_SCREAM,pPlayer->i); - if (voice <= 127) // XXX: p->scream_voice is an int8_t - pPlayer->scream_voice = voice; + A_PlaySound(DUKE_SCREAM, pPlayer->i, CHAN_VOICE); } } @@ -5044,11 +5037,7 @@ void P_ProcessInput(int playerNum) { pPlayer->falling_counter = 0; - if (pPlayer->scream_voice > FX_Ok) - { - FX_StopSound(pPlayer->scream_voice); - pPlayer->scream_voice = -1; - } + S_StopEnvSound(-1, pPlayer->i, CHAN_VOICE); if ((sectorLotag != ST_1_ABOVE_WATER && sectorLotag != ST_2_UNDERWATER) && (pPlayer->on_ground == 0 && pPlayer->vel.z > (6144 >> 1))) diff --git a/source/duke3d/src/sector.cpp b/source/duke3d/src/sector.cpp index 388701303..72bd8f687 100644 --- a/source/duke3d/src/sector.cpp +++ b/source/duke3d/src/sector.cpp @@ -3025,11 +3025,7 @@ CHECKINV1: if (pPlayer->jetpack_on) { pPlayer->inven_icon = ICON_JETPACK; - if (pPlayer->scream_voice > FX_Ok) - { - FX_StopSound(pPlayer->scream_voice); - pPlayer->scream_voice = -1; - } + S_StopEnvSound(-1, pPlayer->i, CHAN_VOICE); #ifndef EDUKE32_STANDALONE if (!FURY) A_PlaySound(DUKE_JETPACK_ON,pPlayer->i); diff --git a/source/duke3d/src/sounds.cpp b/source/duke3d/src/sounds.cpp index 6cd8fcba4..4cfaf39b0 100644 --- a/source/duke3d/src/sounds.cpp +++ b/source/duke3d/src/sounds.cpp @@ -350,11 +350,9 @@ void S_Update(void) // // //========================================================================== -CVAR(Int, soundtest, -1, 0) // Debug aid to block all sounds except the one selected int S_PlaySound3D(int num, int spriteNum, const vec3_t* pos, int flags) { - if (soundtest > -1 && num != soundtest) return -1; int sndnum = VM_OnEventWithReturn(EVENT_SOUND, spriteNum, screenpeek, num); auto const pPlayer = g_player[myconnectindex].ps; @@ -455,7 +453,6 @@ int S_PlaySound3D(int num, int spriteNum, const vec3_t* pos, int flags) int S_PlaySound(int num, int flags) { - if (soundtest > -1 && num != soundtest) return -1; int sndnum = VM_OnEventWithReturn(EVENT_SOUND, g_player[screenpeek].ps->i, screenpeek, num); if (!soundEngine->isValidSoundId(sndnum+1) || !SoundEnabled()) return -1; @@ -483,12 +480,13 @@ int A_PlaySound(int soundNum, int spriteNum, int flags) S_PlaySound3D(soundNum, spriteNum, &sprite[spriteNum].pos, flags); } -void S_StopEnvSound(int sndNum, int sprNum) +void S_StopEnvSound(int sndNum, int sprNum, int flags) { if (sprNum < -1 || sprNum >= MAXSPRITES) return; if (sprNum == -1) soundEngine->StopSoundID(sndNum+1); - else soundEngine->StopSound(SOURCE_Actor, &sprite[sprNum], -1, sndNum+1); + else if (flags == -1) soundEngine->StopSound(SOURCE_Actor, &sprite[sprNum], -1, sndNum+1); + else soundEngine->StopSound(SOURCE_Actor, &sprite[sprNum], flags, -1); } void S_ChangeSoundPitch(int soundNum, int spriteNum, int pitchoffset) @@ -511,11 +509,11 @@ void S_ChangeSoundPitch(int soundNum, int spriteNum, int pitchoffset) // //========================================================================== -int A_CheckSoundPlaying(int spriteNum, int soundNum) +int A_CheckSoundPlaying(int spriteNum, int soundNum, int flags) { if (spriteNum == -1) return soundEngine->GetSoundPlayingInfo(SOURCE_Any, nullptr, soundNum+1); if ((unsigned)spriteNum >= MAXSPRITES) return false; - return soundEngine->IsSourcePlayingSomething(SOURCE_Actor, &sprite[spriteNum], CHAN_AUTO, soundNum+1); + return soundEngine->IsSourcePlayingSomething(SOURCE_Actor, &sprite[spriteNum], flags, soundNum+1); } // Check if actor is playing any sound. @@ -570,7 +568,6 @@ void S_PlayLevelMusicOrNothing(unsigned int m) if (retval >= 0) { - // Thanks to scripting that stupid slot hijack cannot be refactored - but we'll store the real data elsewhere anyway! auto& mr = m == USERMAPMUSICFAKESLOT ? userMapRecord : mapList[m]; Mus_Play(mr.labelName, mr.music, true); S_SetMusicIndex(m); diff --git a/source/duke3d/src/sounds.h b/source/duke3d/src/sounds.h index a625ae1cc..ba7402f62 100644 --- a/source/duke3d/src/sounds.h +++ b/source/duke3d/src/sounds.h @@ -45,7 +45,7 @@ typedef struct int priority, flags; } sound_t; -int A_CheckSoundPlaying(int spriteNum,int soundNum); +int A_CheckSoundPlaying(int spriteNum, int soundNum, int flags = 0); int A_PlaySound(int soundNum, int spriteNum, int flags = 0); void S_Callback(intptr_t num); int A_CheckAnySoundPlaying(int spriteNum); @@ -61,7 +61,7 @@ void S_PlaySpecialMusicOrNothing(unsigned int); void S_ContinueLevelMusic(void); int S_PlaySound(int num, int flags = 0); int S_PlaySound3D(int num, int spriteNum, const vec3_t *pos, int flags = 0); -void S_StopEnvSound(int sndNum,int sprNum); +void S_StopEnvSound(int sndNum,int sprNum, int flags = -1); void S_StopAllSounds(void); void S_Update(void); void S_ChangeSoundPitch(int soundNum, int spriteNum, int pitchoffset); diff --git a/source/duke3d/src/sounds_old.cpp b/source/duke3d/src/sounds_old.cpp deleted file mode 100644 index 48949bdee..000000000 --- a/source/duke3d/src/sounds_old.cpp +++ /dev/null @@ -1,720 +0,0 @@ -//------------------------------------------------------------------------- -/* -Copyright (C) 2016 EDuke32 developers and contributors - -This file is part of EDuke32. - -EDuke32 is free software; you can redistribute it and/or -modify it under the terms of the GNU General Public License version 2 -as published by the Free Software Foundation. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - -See the GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -*/ -//------------------------------------------------------------------------- - -#include "ns.h" // Must come before everything else! - -#include "compat.h" - -#include "duke3d.h" -#include "renderlayer.h" // for win_gethwnd() -#include "al_midi.h" -#include "openaudio.h" -#include "z_music.h" -#include "mapinfo.h" -#include - -BEGIN_DUKE_NS - - -#define DQSIZE 256 - -int32_t g_numEnvSoundsPlaying, g_highestSoundIdx; - -static char *MusicPtr; - -static int32_t MusicIsWaveform; -static int32_t MusicVoice = -1; - -static bool MusicPaused; -static bool SoundPaused; - -static 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) -{ - snd->owner = owner; - snd->id = voice; - snd->dist = dist; - snd->clock = clock; -} - -void S_SoundStartup(void) -{ -#ifdef _WIN32 - void *initdata = (void *) win_gethwnd(); // used for DirectSound -#else - void *initdata = NULL; -#endif - - initprintf("Initializing sound... "); - - int status = FX_Init(snd_numvoices, snd_numchannels, snd_mixrate, initdata); - if (status != FX_Ok) - { - initprintf("failed! %s\n", FX_ErrorString(status)); - return; - } - - initprintf("%d voices, %d channels, 16-bit %d Hz\n", *snd_numvoices, *snd_numchannels, *snd_mixrate); - - for (int i = 0; i <= g_highestSoundIdx; ++i) - { - for (auto & voice : g_sounds[i].voices) - { - g_sounds[i].num = 0; - S_SetProperties(&voice, -1, 0, UINT16_MAX, 0); - } - } - - snd_fxvolume.Callback(); - - snd_reversestereo.Callback(); - FX_SetCallBack(S_Callback); - FX_SetPrintf(OSD_Printf); -} - -void S_SoundShutdown(void) -{ - int status = FX_Shutdown(); - if (status != FX_Ok) - { - Bsprintf(tempbuf, "S_SoundShutdown(): error: %s", FX_ErrorString(status)); - G_GameExit(tempbuf); - } -} - -void S_PauseSounds(bool paused) -{ - if (SoundPaused == paused) - return; - - SoundPaused = paused; - - for (int i = 0; i <= g_highestSoundIdx; ++i) - { - for (auto & voice : g_sounds[i].voices) - if (voice.id > 0) - FX_PauseVoice(voice.id, paused); - } -} - -void S_MenuSound(void) -{ - static int SoundNum; - int const menusnds[] = { - LASERTRIP_EXPLODE, DUKE_GRUNT, DUKE_LAND_HURT, CHAINGUN_FIRE, SQUISHED, KICK_HIT, - PISTOL_RICOCHET, PISTOL_BODYHIT, PISTOL_FIRE, SHOTGUN_FIRE, BOS1_WALK, RPG_EXPLODE, - PIPEBOMB_BOUNCE, PIPEBOMB_EXPLODE, NITEVISION_ONOFF, RPG_SHOOT, SELECT_WEAPON, - }; - int s = VM_OnEventWithReturn(EVENT_OPENMENUSOUND, g_player[screenpeek].ps->i, screenpeek, FURY ? -1 : menusnds[SoundNum++ % ARRAY_SIZE(menusnds)]); - if (s != -1) - S_PlaySound(s); -} - - -static void S_SetMusicIndex(unsigned int m) -{ - ud.music_episode = m / MAXLEVELS; - ud.music_level = m % MAXLEVELS; -} - -void S_PlayLevelMusicOrNothing(unsigned int m) -{ - ud.returnvar[0] = m / MAXLEVELS; - ud.returnvar[1] = m % MAXLEVELS; - - int retval = VM_OnEvent(EVENT_PLAYLEVELMUSICSLOT, g_player[myconnectindex].ps->i, myconnectindex); - - if (retval >= 0) - { - // Thanks to scripting that stupid slot hijack cannot be refactored - but we'll store the real data elsewhere anyway! - auto &mr = m == USERMAPMUSICFAKESLOT ? userMapRecord : mapList[m]; - Mus_Play(mr.labelName, mr.music, true); - S_SetMusicIndex(m); - } -} - -int S_TryPlaySpecialMusic(unsigned int m) -{ - auto &musicfn = mapList[m].music; - if (musicfn.IsNotEmpty()) - { - if (!Mus_Play(nullptr, musicfn, true)) - { - S_SetMusicIndex(m); - return 0; - } - } - - return 1; -} - -void S_PlaySpecialMusicOrNothing(unsigned int m) -{ - if (S_TryPlaySpecialMusic(m)) - { - S_SetMusicIndex(m); - } -} - -void S_ContinueLevelMusic(void) -{ - VM_OnEvent(EVENT_CONTINUELEVELMUSICSLOT, g_player[myconnectindex].ps->i, myconnectindex); -} - -void S_Cleanup(void) -{ - static uint32_t ldnum = 0; - - while (ldnum < dnum) - { - uint32_t num = dq[ldnum++ & (DQSIZE - 1)]; - - // negative index is RTS playback - if ((int32_t)num < 0) - { - continue; - } - - // num + (MAXSOUNDS*MAXSOUNDINSTANCES) is a sound played globally - // for which there was no open slot to keep track of the voice - if (num >= (MAXSOUNDS*MAXSOUNDINSTANCES)) - { - continue; - } - - int const voiceindex = num & (MAXSOUNDINSTANCES - 1); - - num = (num - voiceindex) / MAXSOUNDINSTANCES; - - auto &snd = g_sounds[num]; - 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); - else if (snd.num > 0) - --snd.num; - - // MUSICANDSFX uses t_data[0] to control restarting the sound - // CLEAR_SOUND_T0 - if (spriteNum != -1 && S_IsAmbientSFX(spriteNum) && sector[SECT(spriteNum)].lotag < 3) // ST_2_UNDERWATER - actor[spriteNum].t_data[0] = 0; - - S_SetProperties(&voice, -1, 0, UINT16_MAX, 0); - - } -} - -// returns number of bytes read -int32_t S_LoadSound(int num) -{ - if ((unsigned)num > (unsigned)g_highestSoundIdx || EDUKE32_PREDICT_FALSE(g_sounds[num].filename == NULL)) - return 0; - - auto &snd = g_sounds[num]; - - auto fp = S_OpenAudio(snd.filename, 0, 0); - - if (!fp.isOpen()) - { - OSD_Printf(OSDTEXT_RED "Sound %s(#%d) not found!\n", snd.filename, num); - return 0; - } - - int32_t l = fp.GetLength(); - snd.siz = l; - cacheAllocateBlock((intptr_t *)&snd.ptr, l, nullptr); - l = fp.Read(snd.ptr, l); - - return l; -} - -void cacheAllSounds(void) -{ - for (int i=0, j=0; i <= g_highestSoundIdx; ++i) - { - if (g_sounds[i].ptr == 0) - { - j++; - if ((j&7) == 0) - gameHandleEvents(); - - S_LoadSound(i); - } - } -} - -static inline int S_GetPitch(int num) -{ - auto const &snd = g_sounds[num]; - int const range = klabs(snd.pe - snd.ps); - - return (range == 0) ? snd.ps : min(snd.ps, snd.pe) + rand() % range; -} - -static int S_TakeSlot(int soundNum) -{ - S_Cleanup(); - - uint16_t dist = 0; - uint16_t clock = 0; - - int bestslot = 0; - int slot = 0; - - auto &snd = g_sounds[soundNum]; - - while (slot < MAXSOUNDINSTANCES && snd.voices[slot].id > 0) - { - auto &voice = snd.voices[slot]; - - if (voice.dist > dist || (voice.dist == dist && voice.clock > clock)) - { - clock = voice.clock; - dist = voice.dist; - - bestslot = slot; - } - - slot++; - } - - if (slot != MAXSOUNDINSTANCES) - return slot; - - if (FX_SoundActive(snd.voices[bestslot].id)) - FX_StopSound(snd.voices[bestslot].id); - - mutex_lock(&m_callback); - unative_t const ldnum = dnum; - dq[ldnum & (DQSIZE-1)] = (soundNum * MAXSOUNDINSTANCES) + bestslot; - dnum++; - mutex_unlock(&m_callback); - - S_Cleanup(); - - return bestslot; -} - -static int S_GetSlot(int soundNum) -{ - int slot = 0; - - while (slot < MAXSOUNDINSTANCES && g_sounds[soundNum].voices[slot].id > 0) - slot++; - - return slot == MAXSOUNDINSTANCES ? S_TakeSlot(soundNum) : slot; -} - -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 int S_CalcDistAndAng(int spriteNum, int soundNum, int sectNum, int angle, - const vec3_t *cam, const vec3_t *pos, int *distPtr, int *angPtr) -{ - int sndang = 0, sndist = 0, explosion = 0; - - if (PN(spriteNum) == APLAYER && P_Get(spriteNum) == screenpeek) - goto sound_further_processing; - - sndang = S_GetAngle(angle, cam, pos); - sndist = FindDistance3D(cam->x-pos->x, cam->y-pos->y, (cam->z-pos->z)); - - if ((g_sounds[soundNum].m & (SF_GLOBAL|SF_DTAG)) != SF_GLOBAL && S_IsAmbientSFX(spriteNum) && (sector[SECT(spriteNum)].lotag&0xff) < 9) // ST_9_SLIDING_ST_DOOR - sndist = divscale14(sndist, SHT(spriteNum)+1); - -sound_further_processing: - sndist += g_sounds[soundNum].vo; - if (sndist < 0) - sndist = 0; - - 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; - - if ((g_sounds[soundNum].m & (SF_GLOBAL|SF_DTAG)) == (SF_GLOBAL|SF_DTAG)) - { -boost: - int const sdist = g_sounds[soundNum].vo > 0 ? g_sounds[soundNum].vo : 6144; - - explosion = true; - - if (sndist > sdist) - sndist = sdist; - } - else if (!FURY) - { - switch (DYNAMICSOUNDMAP(soundNum)) - { - case PIPEBOMB_EXPLODE__STATIC: - case LASERTRIP_EXPLODE__STATIC: - case RPG_EXPLODE__STATIC: - goto boost; - } - } - - if ((g_sounds[soundNum].m & (SF_GLOBAL|SF_DTAG)) == SF_GLOBAL || sndist < ((255-LOUDESTVOLUME) << 6)) - sndist = ((255-LOUDESTVOLUME) << 6); - - *distPtr = sndist; - *angPtr = sndang; - - return explosion; -} - -int S_PlaySound3D(int num, int spriteNum, const vec3_t *pos) -{ - int32_t j = VM_OnEventWithReturn(EVENT_SOUND, spriteNum, screenpeek, num); - - if ((j == -1 && num != -1) || !SoundEnabled()) // check that the user returned -1, but only if -1 wasn't playing already (in which case, warn) - return -1; - - int const sndNum = j; - sound_t & snd = g_sounds[sndNum]; - - if (EDUKE32_PREDICT_FALSE((unsigned) sndNum > (unsigned) g_highestSoundIdx || snd.filename == NULL || snd.ptr == NULL)) - { - OSD_Printf("WARNING: invalid sound #%d\n", num); - return -1; - } - - auto const pPlayer = g_player[myconnectindex].ps; - - if (((snd.m & SF_ADULT) && adult_lockout) || (unsigned)spriteNum >= MAXSPRITES || (pPlayer->gm & MODE_MENU) || !FX_VoiceAvailable(snd.pr) - || (pPlayer->timebeforeexit > 0 && pPlayer->timebeforeexit <= GAMETICSPERSEC * 3)) - return -1; - - // Duke talk - if (snd.m & SF_TALK) - { - if ((g_netServer || ud.multimode > 1) && PN(spriteNum) == APLAYER && P_Get(spriteNum) != screenpeek) // other player sound - { - if ((snd_speech & 4) != 4) - return -1; - } - else if ((snd_speech & 1) != 1) - return -1; - - // don't play if any Duke talk sounds are already playing - for (j = 0; j <= g_highestSoundIdx; ++j) - if ((g_sounds[j].m & SF_TALK) && g_sounds[j].num > 0) - return -1; - } - else if ((snd.m & (SF_DTAG|SF_GLOBAL)) == SF_DTAG) // Duke-Tag sound - { - int const voice = S_PlaySound(sndNum); - - if (voice <= FX_Ok) - return -1; - - j = 0; - while (j < MAXSOUNDINSTANCES && snd.voices[j].id != voice) - j++; - - - snd.voices[j].owner = spriteNum; - - return voice; - } - - int32_t sndist, sndang; - int const explosionp = S_CalcDistAndAng(spriteNum, sndNum, CAMERA(sect), fix16_to_int(CAMERA(q16ang)), &CAMERA(pos), pos, &sndist, &sndang); - int pitch = S_GetPitch(sndNum); - auto const pOther = g_player[screenpeek].ps; - - - if (pOther->sound_pitch) - pitch += pOther->sound_pitch; - - if (explosionp) - { - if (pOther->cursectnum > -1 && sector[pOther->cursectnum].lotag == ST_2_UNDERWATER) - pitch -= 1024; - } - else - { - if (sndist > 32767 && PN(spriteNum) != MUSICANDSFX && (snd.m & (SF_LOOP|SF_MSFX)) == 0) - return -1; - - if (pOther->cursectnum > -1 && sector[pOther->cursectnum].lotag == ST_2_UNDERWATER - && (snd.m & SF_TALK) == 0) - pitch = -768; - } - - if (snd.num > 0 && PN(spriteNum) != MUSICANDSFX) - S_StopEnvSound(sndNum, spriteNum); - - int const sndSlot = S_GetSlot(sndNum); - - if (sndSlot >= MAXSOUNDINSTANCES) - { - return -1; - } - - int const repeatp = (snd.m & SF_LOOP); - - if (repeatp && (snd.m & SF_ONEINST_INTERNAL) && snd.num > 0) - { - return -1; - } - - int const voice = FX_Play3D(snd.ptr, snd.siz, repeatp ? FX_LOOP : FX_ONESHOT, pitch, sndang >> 4, sndist >> 6, - snd.pr, snd.volume, (sndNum * MAXSOUNDINSTANCES) + sndSlot); - - if (voice <= FX_Ok) - { - return -1; - } - - snd.num++; - - S_SetProperties(&snd.voices[sndSlot], spriteNum, voice, sndist >> 6, 0); - - return voice; -} - -int S_PlaySound(int num) -{ - int sndnum = VM_OnEventWithReturn(EVENT_SOUND, g_player[screenpeek].ps->i, screenpeek, num); - - if ((sndnum == -1 && num != -1) || !SoundEnabled()) // check that the user returned -1, but only if -1 wasn't playing already (in which case, warn) - return -1; - - num = sndnum; - - sound_t & snd = g_sounds[num]; - - if (EDUKE32_PREDICT_FALSE((unsigned)num > (unsigned)g_highestSoundIdx || snd.filename == NULL || snd.ptr == NULL)) - { - OSD_Printf("WARNING: invalid sound #%d\n",num); - return -1; - } - - if ((!(snd_speech & 1) && (snd.m & SF_TALK)) || ((snd.m & SF_ADULT) && adult_lockout) || !FX_VoiceAvailable(snd.pr)) - return -1; - - int const pitch = S_GetPitch(num); - - sndnum = S_GetSlot(num); - - if (sndnum >= MAXSOUNDINSTANCES) - { - return -1; - } - - int const voice = (snd.m & SF_LOOP) ? FX_Play(snd.ptr, snd.siz, 0, -1, pitch, LOUDESTVOLUME, LOUDESTVOLUME, - LOUDESTVOLUME, snd.siz, snd.volume, (num * MAXSOUNDINSTANCES) + sndnum) - : FX_Play3D(snd.ptr, snd.siz, FX_ONESHOT, pitch, 0, 255 - LOUDESTVOLUME, snd.pr, snd.volume, - (num * MAXSOUNDINSTANCES) + sndnum); - - if (voice <= FX_Ok) - { - return -1; - } - - snd.num++; - 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; - - return (unsigned)spriteNum >= MAXSPRITES ? S_PlaySound(soundNum) : - S_PlaySound3D(soundNum, spriteNum, &sprite[spriteNum].pos); -} - -void S_StopEnvSound(int sndNum, int sprNum) -{ - if (EDUKE32_PREDICT_FALSE((unsigned)sndNum > (unsigned)g_highestSoundIdx) || g_sounds[sndNum].num <= 0) - return; - - int j; - - do - { - for (j=0; j FX_Ok) || (sprNum != -1 && voice.owner == sprNum)) - { - if (voice.id > FX_Ok) - { - if (FX_SoundActive(voice.id)) - FX_StopSound(voice.id); - break; - } - } - } - } - while (j < MAXSOUNDINSTANCES); -} - -// Do not remove this or make it inline. -void S_StopAllSounds(void) -{ - FX_StopAllSounds(); -} - -void S_ChangeSoundPitch(int soundNum, int spriteNum, int pitchoffset) -{ - if ((unsigned)soundNum > (unsigned)g_highestSoundIdx || g_sounds[soundNum].num <= 0) - return; - - for (auto &voice : g_sounds[soundNum].voices) - { - if ((spriteNum == -1 && voice.id > FX_Ok) || (spriteNum != -1 && voice.owner == spriteNum)) - { - if (EDUKE32_PREDICT_FALSE(spriteNum >= 0 && voice.id <= FX_Ok)) - initprintf(OSD_ERROR "S_ChangeSoundPitch(): bad voice %d for sound ID %d!\n", voice.id, soundNum); - else if (voice.id > FX_Ok && FX_SoundActive(voice.id)) - FX_SetPitch(voice.id, pitchoffset); - break; - } - } -} - -void S_Update(void) -{ - if ((g_player[myconnectindex].ps->gm & (MODE_GAME|MODE_DEMO)) == 0) - return; - - g_numEnvSoundsPlaying = 0; - - const vec3_t *c; - int32_t ca,cs; - - if (ud.camerasprite == -1) - { - if (ud.overhead_on != 2) - { - c = &CAMERA(pos); - cs = CAMERA(sect); - ca = fix16_to_int(CAMERA(q16ang)); - } - else - { - auto pPlayer = g_player[screenpeek].ps; - c = &pPlayer->pos; - cs = pPlayer->cursectnum; - ca = fix16_to_int(pPlayer->q16ang); - } - } - else - { - c = &sprite[ud.camerasprite].pos; - cs = sprite[ud.camerasprite].sectnum; - ca = sprite[ud.camerasprite].ang; - } - - int sndnum = 0; - int const highest = g_highestSoundIdx; - - do - { - if (g_sounds[sndnum].num == 0) - continue; - - S_Cleanup(); - - for (auto &voice : g_sounds[sndnum].voices) - { - int const spriteNum = voice.owner; - - if ((unsigned)spriteNum >= MAXSPRITES || voice.id <= FX_Ok || !FX_SoundActive(voice.id)) - continue; - - int sndist, sndang; - - S_CalcDistAndAng(spriteNum, sndnum, cs, ca, c, (const vec3_t *)&sprite[spriteNum], &sndist, &sndang); - - if (S_IsAmbientSFX(spriteNum)) - g_numEnvSoundsPlaying++; - - // AMBIENT_SOUND - FX_Pan3D(voice.id, sndang >> 4, sndist >> 6); - voice.dist = sndist >> 6; - voice.clock++; - } - } while (++sndnum <= highest); -} - -// S_Callback() can be called from either the audio thread when a sound ends, or the main thread -// when playing back a new sound needs an existing sound to be stopped first -void S_Callback(intptr_t num) -{ - if ((int32_t)num == MUSIC_ID) - return; - - mutex_lock(&m_callback); - unative_t const ldnum = dnum; - dq[ldnum & (DQSIZE - 1)] = (uint32_t)num; - dnum++; - mutex_unlock(&m_callback); -} - -int A_CheckSoundPlaying(int spriteNum, int soundNum) -{ - if (EDUKE32_PREDICT_FALSE((unsigned)soundNum > (unsigned)g_highestSoundIdx)) return 0; - - if (g_sounds[soundNum].num > 0 && spriteNum >= 0) - { - for (auto &voice : g_sounds[soundNum].voices) - if (voice.owner == spriteNum) - return 1; - } - - return (spriteNum == -1) ? (g_sounds[soundNum].num != 0) : 0; -} - -// Check if actor is playing any sound. -int A_CheckAnySoundPlaying(int spriteNum) -{ - int const msp = g_highestSoundIdx; - - for (int j = 0; j <= msp; ++j) - { - for (auto &voice : g_sounds[j].voices) - if (voice.owner == spriteNum) - return 1; - } - - return 0; -} - -int S_CheckSoundPlaying(int soundNum) -{ - if (EDUKE32_PREDICT_FALSE((unsigned)soundNum > (unsigned)g_highestSoundIdx)) return false; - return (g_sounds[soundNum].num != 0); -} -END_DUKE_NS diff --git a/source/rr/src/actors.cpp b/source/rr/src/actors.cpp index 00bcadf60..525c49c68 100644 --- a/source/rr/src/actors.cpp +++ b/source/rr/src/actors.cpp @@ -1359,7 +1359,8 @@ ACTOR_STATIC void G_MoveFX(void) else if (pSprite->lotag < 999 && (unsigned)sector[pSprite->sectnum].lotag < 9 && // ST_9_SLIDING_ST_DOOR snd_ambience && sector[SECT(spriteNum)].floorz != sector[SECT(spriteNum)].ceilingz) { - if (g_sounds[pSprite->lotag].m & SF_MSFX) + auto flags = S_GetUserFlags(pSprite->lotag); + if (flags & SF_MSFX) { int playerDist = dist(&sprite[pPlayer->i], pSprite); @@ -1372,11 +1373,10 @@ ACTOR_STATIC void G_MoveFX(void) } #endif - if (playerDist < spriteHitag && T1(spriteNum) == 0 && FX_VoiceAvailable(g_sounds[pSprite->lotag].pr-1)) + if (playerDist < spriteHitag && T1(spriteNum) == 0)// && FX_VoiceAvailable(g_sounds[pSprite->lotag].pr-1)) { // Start playing an ambience sound. - - char om = g_sounds[pSprite->lotag].m; +#if 0 // let the sound system handle this internally. if (g_numEnvSoundsPlaying == snd_numvoices) { int32_t j; @@ -1392,10 +1392,8 @@ ACTOR_STATIC void G_MoveFX(void) if (j == -1) goto next_sprite; } - - g_sounds[pSprite->lotag].m |= SF_LOOP; - A_PlaySound(pSprite->lotag,spriteNum); - g_sounds[pSprite->lotag].m = om; +#endif + A_PlaySound(pSprite->lotag, spriteNum, CHAN_LOOP); T1(spriteNum) = 1; // AMBIENT_SFX_PLAYING } else if (playerDist >= spriteHitag && T1(spriteNum) == 1) @@ -1408,7 +1406,7 @@ ACTOR_STATIC void G_MoveFX(void) } } - if (g_sounds[pSprite->lotag].m & SF_GLOBAL) + if (flags & SF_GLOBAL) { // Randomly playing global sounds (flyby of planes, screams, ...) diff --git a/source/rr/src/anim.cpp b/source/rr/src/anim.cpp index 8ea40f3ee..f11a381f1 100644 --- a/source/rr/src/anim.cpp +++ b/source/rr/src/anim.cpp @@ -396,7 +396,7 @@ int32_t Anim_Play(const char *fn) if (sound == -1) FX_StopAllSounds(); else - S_PlaySound(sound); + S_PlaySound(sound, CHAN_UI); soundidx++; } @@ -410,7 +410,7 @@ int32_t Anim_Play(const char *fn) if (sound == -1) FX_StopAllSounds(); else - S_PlaySound(sound); + S_PlaySound(sound, CHAN_UI); soundidx++; } @@ -566,7 +566,7 @@ int32_t Anim_Play(const char *fn) if (sound == -1) FX_StopAllSounds(); else - S_PlaySound(sound); + S_PlaySound(sound, CHAN_UI); soundidx++; } diff --git a/source/rr/src/d_menu.cpp b/source/rr/src/d_menu.cpp index d275f62fb..bf0097b91 100644 --- a/source/rr/src/d_menu.cpp +++ b/source/rr/src/d_menu.cpp @@ -361,15 +361,15 @@ void GameInterface::MenuSound(EMenuSounds snd) switch (snd) { case CursorSound: - S_PlaySound(RR ? 335 : KICK_HIT); + S_PlaySound(RR ? 335 : KICK_HIT, CHAN_UI); break; case AdvanceSound: - S_PlaySound(RR? 341 : PISTOL_BODYHIT); + S_PlaySound(RR? 341 : PISTOL_BODYHIT, CHAN_UI); break; case CloseSound: - S_PlaySound(EXITMENUSOUND); + S_PlaySound(EXITMENUSOUND, CHAN_UI); break; default: @@ -443,7 +443,16 @@ void GameInterface::StartGame(FGameStartup& gs) } ud.m_player_skill = gs.Skill + 1; - if (menu_sounds) g_skillSoundVoice = S_PlaySound(skillsound); + if (menu_sounds && skillsound >= 0 && SoundEnabled()) + { + S_PlaySound(skillsound, CHAN_UI); + + while (S_CheckSoundPlaying(skillsound)) + { + S_Update(); + G_HandleAsync(); + } + } ud.m_respawn_monsters = (gs.Skill == 3); ud.m_monsters_off = ud.monsters_off = 0; ud.m_respawn_items = 0; diff --git a/source/rr/src/game.cpp b/source/rr/src/game.cpp index 8033a6990..6389448fa 100644 --- a/source/rr/src/game.cpp +++ b/source/rr/src/game.cpp @@ -162,9 +162,7 @@ enum gametokens static void gameTimerHandler(void) { - S_Cleanup(); - MUSIC_Update(); - + S_Update(); G_HandleSpecialKeys(); } @@ -6046,7 +6044,7 @@ void G_HandleLocalKeys(void) { if (G_ChangeHudLayout(1)) { - S_PlaySound(RR ? 341 : THUD); + S_PlaySound(RR ? 341 : THUD, CHAN_UI); } } else @@ -6065,7 +6063,7 @@ void G_HandleLocalKeys(void) { if (G_ChangeHudLayout(-1)) { - S_PlaySound(RR ? 341 : THUD); + S_PlaySound(RR ? 341 : THUD, CHAN_UI); } } else @@ -6311,43 +6309,6 @@ void G_HandleLocalKeys(void) } } -static int32_t S_DefineAudioIfSupported(char **fn, const char *name) -{ -#if !defined HAVE_FLAC || !defined HAVE_VORBIS - const char *extension = Bstrrchr(name, '.'); -# if !defined HAVE_FLAC - if (extension && !Bstrcasecmp(extension, ".flac")) - return -2; -# endif -# if !defined HAVE_VORBIS - if (extension && !Bstrcasecmp(extension, ".ogg")) - return -2; -# endif -#endif - realloc_copy(fn, name); - return 0; -} - -static int32_t S_DefineSound(int sndidx, const char *name, int minpitch, int maxpitch, int priority, int type, int distance, float volume) -{ - if ((unsigned)sndidx >= MAXSOUNDS || S_DefineAudioIfSupported(&g_sounds[sndidx].filename, name)) - return -1; - - auto &snd = g_sounds[sndidx]; - - snd.ps = clamp(minpitch, INT16_MIN, INT16_MAX); - snd.pe = clamp(maxpitch, INT16_MIN, INT16_MAX); - snd.pr = priority & 255; - snd.m = type & ~SF_ONEINST_INTERNAL; - snd.vo = clamp(distance, INT16_MIN, INT16_MAX); - snd.volume = volume; - - if (snd.m & SF_LOOP) - snd.m |= SF_ONEINST_INTERNAL; - - return 0; -} - // Returns: // 0: all OK // -1: ID declaration was invalid: @@ -6816,10 +6777,6 @@ static void G_Cleanup(void) Bfree(g_player[i].inputBits); } - for (i=MAXSOUNDS-1; i>=0; i--) - { - Bfree(g_sounds[i].filename); - } if (label != (char *)&sprite[0]) Bfree(label); if (labelcode != (int32_t *)§or[0]) Bfree(labelcode); Bfree(apScript); @@ -6843,7 +6800,6 @@ static void G_Cleanup(void) void G_Shutdown(void) { - S_SoundShutdown(); G_SetFog(0); engineUnInit(); G_Cleanup(); @@ -7306,6 +7262,7 @@ int GameInterface::app_main() { I_Error("app_main: There was a problem initializing the Build engine: %s\n", engineerrstr); } + S_InitSound(); g_logFlushWindow = 0; @@ -7487,7 +7444,6 @@ int GameInterface::app_main() } videoSetPalette(0, g_player[myconnectindex].ps->palette, 0); - S_SoundStartup(); } // check if the minifont will support lowercase letters (3136-3161) @@ -7656,7 +7612,6 @@ MAIN_LOOP_RESTART: (g_player[myconnectindex].ps->gm&MODE_GAME)) { G_MoveLoop(); - S_Update(); } if (totalclock - moveClock >= TICSPERFRAME) diff --git a/source/rr/src/game.h b/source/rr/src/game.h index cc4d938f0..254f94079 100644 --- a/source/rr/src/game.h +++ b/source/rr/src/game.h @@ -199,6 +199,9 @@ extern user_defs ud; extern const char *s_buildDate; extern char boardfilename[BMAX_PATH]; +#define USERMAPMUSICFAKEVOLUME MAXVOLUMES +#define USERMAPMUSICFAKELEVEL (MAXLEVELS-1) +#define USERMAPMUSICFAKESLOT ((USERMAPMUSICFAKEVOLUME * MAXLEVELS) + USERMAPMUSICFAKELEVEL) static inline int G_HaveUserMap(void) { diff --git a/source/rr/src/gamedef.cpp b/source/rr/src/gamedef.cpp index 219c23bac..ab98708d0 100644 --- a/source/rr/src/gamedef.cpp +++ b/source/rr/src/gamedef.cpp @@ -945,6 +945,7 @@ static inline void C_FinishBitOr(int32_t value) static int32_t C_ParseCommand(int32_t loop) { int32_t i, j=0, k=0, tw; + TArray buffer; do { @@ -1940,8 +1941,8 @@ static int32_t C_ParseCommand(int32_t loop) C_SkipSpace(); - TArray buffer; - while (*textptr != 0x0a && *textptr != 0x0d && *textptr != 0) + buffer.Clear(); + while (*textptr != 0x0a && *textptr != 0x0d && *textptr != 0) { buffer.Push(*textptr); textptr++; @@ -1951,77 +1952,68 @@ static int32_t C_ParseCommand(int32_t loop) continue; } case CON_DEFINESOUND: + { + int ps, pe, vo, pr, m; + g_scriptPtr--; C_GetNextValue(LABEL_DEFINE); // Ideally we could keep the value of i from C_GetNextValue() instead of having to hash_find() again. // This depends on tempbuf remaining in place after C_GetNextValue(): - j = hash_find(&h_labels,tempbuf); + j = hash_find(&h_labels, tempbuf); - k = *(g_scriptPtr-1); - if (EDUKE32_PREDICT_FALSE((unsigned)k >= MAXSOUNDS-1)) + k = g_scriptPtr[-1]; + if ((unsigned)k >= MAXSOUNDS - 1) { - initprintf("%s:%d: error: index exceeds sound limit of %d.\n",g_scriptFileName,g_lineNumber, MAXSOUNDS-1); + initprintf("%s:%d: error: sound index exceeds limit of %d.\n", g_scriptFileName, g_lineNumber, MAXSOUNDS - 1); g_errorCnt++; - k = MAXSOUNDS-1; + k = MAXSOUNDS - 1; } + /*else if (g_sounds[k].filename != NULL) + { + initprintf("%s:%d: warning: sound %d already defined (%s)\n", g_scriptFileName, g_lineNumber, k, g_sounds[k].filename); + g_warningCnt++; + }*/ + g_scriptPtr--; i = 0; C_SkipComments(); - if (g_sounds[k].filename == NULL) - g_sounds[k].filename = (char *)Xcalloc(BMAX_PATH,sizeof(uint8_t)); + buffer.Clear(); if (*textptr == '\"') { textptr++; while (*textptr && *textptr != '\"') { - g_sounds[k].filename[i++] = *textptr++; - if (EDUKE32_PREDICT_FALSE(i >= BMAX_PATH-1)) - { - initprintf("%s:%d: error: sound filename exceeds limit of %d characters.\n",g_scriptFileName,g_lineNumber,BMAX_PATH-1); - g_errorCnt++; - C_SkipComments(); - break; - } + buffer.Push(*textptr++); } textptr++; } else while (*textptr != ' ' && *textptr != '\t' && *textptr != '\r' && *textptr != '\n') { - g_sounds[k].filename[i++] = *textptr++; - if (EDUKE32_PREDICT_FALSE(i >= BMAX_PATH-1)) - { - initprintf("%s:%d: error: sound filename exceeds limit of %d characters.\n",g_scriptFileName,g_lineNumber,BMAX_PATH-1); - g_errorCnt++; - C_SkipComments(); - break; - } + buffer.Push(*textptr++); } - g_sounds[k].filename[i] = '\0'; + buffer.Push(0); C_GetNextValue(LABEL_DEFINE); - g_sounds[k].ps = *(g_scriptPtr-1); + ps = g_scriptPtr[-1]; C_GetNextValue(LABEL_DEFINE); - g_sounds[k].pe = *(g_scriptPtr-1); + pe = g_scriptPtr[-1]; C_GetNextValue(LABEL_DEFINE); - g_sounds[k].pr = *(g_scriptPtr-1); + pr = g_scriptPtr[-1]; C_GetNextValue(LABEL_DEFINE); - g_sounds[k].m = *(g_scriptPtr-1) & ~SF_ONEINST_INTERNAL; - if (*(g_scriptPtr-1) & SF_LOOP) - g_sounds[k].m |= SF_ONEINST_INTERNAL; + m = g_scriptPtr[-1]; C_GetNextValue(LABEL_DEFINE); - g_sounds[k].vo = *(g_scriptPtr-1); + vo = g_scriptPtr[-1]; g_scriptPtr -= 5; - g_sounds[k].volume = 1.f; + int res = S_DefineSound(k, buffer.Data(), ps, pe, pr, m, vo, 1.f); - if (k > g_highestSoundIdx) - g_highestSoundIdx = k; continue; + } case CON_ENDA: if (EDUKE32_PREDICT_FALSE(!g_parsingActorPtr)) diff --git a/source/rr/src/global.h b/source/rr/src/global.h index 28c900642..be920e86d 100644 --- a/source/rr/src/global.h +++ b/source/rr/src/global.h @@ -246,7 +246,6 @@ G_EXTERN input_t inputfifo[MOVEFIFOSIZ][MAXPLAYERS]; G_EXTERN int32_t g_noEnemies; G_EXTERN int32_t g_restorePalette; G_EXTERN int32_t g_screenCapture; -G_EXTERN sound_t g_sounds[MAXSOUNDS]; G_EXTERN uint32_t everyothertime; G_EXTERN uint32_t g_moveThingsCount; G_EXTERN double g_gameUpdateTime; diff --git a/source/rr/src/player.cpp b/source/rr/src/player.cpp index 434182de6..d8fb37069 100644 --- a/source/rr/src/player.cpp +++ b/source/rr/src/player.cpp @@ -4417,12 +4417,7 @@ void P_FragPlayer(int playerNum) if (!RR) S_StopEnvSound(DUKE_JETPACK_IDLE, pPlayer->i); - if (pPlayer->scream_voice > FX_Ok) - { - FX_StopSound(pPlayer->scream_voice); - S_Cleanup(); - pPlayer->scream_voice = -1; - } + S_StopEnvSound(-1, pPlayer->i, CHAN_VOICE); if (pSprite->pal != 1 && (pSprite->cstat & 32768) == 0) pSprite->cstat = 0; @@ -7123,11 +7118,9 @@ check_enemy_sprite: if (pPlayer->vel.z > 2400 && pPlayer->falling_counter < 255) { pPlayer->falling_counter++; - if (pPlayer->falling_counter >= 38 && pPlayer->scream_voice <= FX_Ok) + if (pPlayer->falling_counter >= 38 && !A_CheckSoundPlaying(pPlayer->i, -1, CHAN_VOICE)) { - int32_t voice = A_PlaySound(DUKE_SCREAM,pPlayer->i); - if (voice <= 127) // XXX: p->scream_voice is an int8_t - pPlayer->scream_voice = voice; + A_PlaySound(DUKE_SCREAM, pPlayer->i, CHAN_VOICE); } } @@ -7179,12 +7172,7 @@ check_enemy_sprite: { pPlayer->falling_counter = 0; - if (pPlayer->scream_voice > FX_Ok) - { - FX_StopSound(pPlayer->scream_voice); - S_Cleanup(); - pPlayer->scream_voice = -1; - } + S_StopEnvSound(-1, pPlayer->i, CHAN_VOICE); if ((sectorLotag != ST_1_ABOVE_WATER && sectorLotag != ST_2_UNDERWATER) && (pPlayer->on_ground == 0 && pPlayer->vel.z > (6144 >> 1))) diff --git a/source/rr/src/premap.cpp b/source/rr/src/premap.cpp index 3a98f6808..ad24d7475 100644 --- a/source/rr/src/premap.cpp +++ b/source/rr/src/premap.cpp @@ -575,7 +575,7 @@ void G_CacheMapData(void) starttime = timerGetTicks(); - S_PrecacheSounds(); + cacheAllSounds(); G_PrecacheSprites(); for (i=0; i= 0 && SoundEnabled()) - { - while (FX_SoundActive(g_skillSoundVoice)) - G_HandleAsync(); - } - g_skillSoundVoice = -1; ready2send = 0; @@ -2282,7 +2276,7 @@ void G_SetupFilenameBasedMusic(char *nameBuf, const char *fileName) } } - userMapRecord.music = "dethtoll.mid"; + if (!RR) userMapRecord.music = "dethtoll.mid"; } int G_EnterLevel(int gameMode) @@ -2425,7 +2419,11 @@ int G_EnterLevel(int gameMode) if (ud.recstat != 2) { - S_PlayLevelMusicOrNothing(mii); + if (Menu_HaveUserMap()) + { + S_PlayLevelMusicOrNothing(USERMAPMUSICFAKESLOT); + } + else S_PlayLevelMusicOrNothing(mii); } if (RR && !(gameMode & MODE_DEMO)) diff --git a/source/rr/src/screens.cpp b/source/rr/src/screens.cpp index 462ac5da2..946f581c8 100644 --- a/source/rr/src/screens.cpp +++ b/source/rr/src/screens.cpp @@ -2979,7 +2979,7 @@ void G_BonusScreenRRRA(int32_t bonusonly) Net_GetPackets(); break; } - S_Cleanup(); + if (!S_CheckSoundPlaying(-1,35)) break; if (I_CheckAllInput()) { diff --git a/source/rr/src/sector.cpp b/source/rr/src/sector.cpp index 84d2046f5..6f5fe66e2 100644 --- a/source/rr/src/sector.cpp +++ b/source/rr/src/sector.cpp @@ -75,9 +75,10 @@ int A_CallSound(int sectNum, int spriteNum) if (spriteNum == -1) spriteNum = SFXsprite; + auto flags = S_GetUserFlags(soundNum); if (T1(SFXsprite) == 0) { - if ((g_sounds[soundNum].m & SF_GLOBAL) == 0) + if ((flags & (SF_GLOBAL | SF_DTAG)) != SF_GLOBAL) { if (soundNum) { @@ -98,7 +99,7 @@ int A_CallSound(int sectNum, int spriteNum) if (SHT(SFXsprite)) A_PlaySound(SHT(SFXsprite), spriteNum); - if ((g_sounds[soundNum].m & SF_LOOP) || (SHT(SFXsprite) && SHT(SFXsprite) != soundNum)) + if ((flags & SF_LOOP) || (SHT(SFXsprite) && SHT(SFXsprite) != soundNum)) S_StopEnvSound(soundNum, T6(SFXsprite)); T6(SFXsprite) = spriteNum; @@ -1783,7 +1784,9 @@ default_case: S_PlaySound3D(SWITCH_ON, (switchType == SWITCH_SPRITE) ? wallOrSprite : g_player[playerNum].ps->i, &davector); else if (hitag) { - if (switchType == SWITCH_SPRITE && (g_sounds[hitag].m & SF_TALK) == 0) + auto flags = S_GetUserFlags(hitag); + + if (switchType == SWITCH_SPRITE && (flags & SF_TALK) == 0) S_PlaySound3D(hitag, wallOrSprite, &davector); else A_PlaySound(hitag, g_player[playerNum].ps->i); @@ -3037,7 +3040,7 @@ void A_DamageObject(int spriteNum, int const dmgSrc) sprite[dmgSrc].xvel = (sprite[spriteNum].xvel>>1)+(sprite[spriteNum].xvel>>2); sprite[dmgSrc].ang -= (SA(spriteNum)<<1)+1024; SA(spriteNum) = getangle(SX(spriteNum)-sprite[dmgSrc].x,SY(spriteNum)-sprite[dmgSrc].y)-512; - if (g_sounds[POOLBALLHIT].num < 2) + if (S_CheckSoundPlaying(POOLBALLHIT) < 2) A_PlaySound(POOLBALLHIT, spriteNum); } else if (RR && (sprite[dmgSrc].picnum == RRTILE3440 || sprite[dmgSrc].picnum == RRTILE3440+1)) @@ -4192,11 +4195,7 @@ rrtripbomb_case: if (pPlayer->jetpack_on) { pPlayer->inven_icon = ICON_JETPACK; - if (pPlayer->scream_voice > FX_Ok) - { - FX_StopSound(pPlayer->scream_voice); - pPlayer->scream_voice = -1; - } + S_StopEnvSound(-1, pPlayer->i, CHAN_VOICE); A_PlaySound(DUKE_JETPACK_ON,pPlayer->i); diff --git a/source/rr/src/sounds.cpp b/source/rr/src/sounds.cpp index b86632a8f..475bc6837 100644 --- a/source/rr/src/sounds.cpp +++ b/source/rr/src/sounds.cpp @@ -1,10 +1,9 @@ //------------------------------------------------------------------------- /* Copyright (C) 2016 EDuke32 developers and contributors +Copyright (C) 2019 Christoph Oelckers -This file is part of EDuke32. - -EDuke32 is free software; you can redistribute it and/or +This is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation. @@ -19,96 +18,523 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ //------------------------------------------------------------------------- + #include "ns.h" // Must come before everything else! #include "compat.h" #include "duke3d.h" -#include "renderlayer.h" // for win_gethwnd() -#include "openaudio.h" #include "z_music.h" -#include #include "mapinfo.h" +#include "sound/s_soundinternal.h" BEGIN_RR_NS -#define DQSIZE 128 +// Coordinate factors to map Build coordinate space to sound system coordinate space. +const float xmul = 1 / 16.f; +const float ymul = -1 / 16.f; +const float zmul = -1 / 256.f; -int32_t g_numEnvSoundsPlaying, g_highestSoundIdx = 0; - -static int32_t MusicIsWaveform = 0; -static char *MusicPtr = NULL; -static int32_t MusicVoice = -1; -static bool MusicPaused = false; -static bool SoundPaused = false; - -std::atomic dnum; -uint32_t dq[DQSIZE]; - -void S_SoundStartup(void) +class DukeSoundEngine : public SoundEngine { -#ifdef MIXERTYPEWIN - void *initdata = (void *) win_gethwnd(); // used for DirectSound -#else - void *initdata = NULL; -#endif + // client specific parts of the sound engine go in this class. + void CalcPosVel(int type, const void* source, const float pt[3], int channum, int chanflags, FSoundID chanSound, FVector3* pos, FVector3* vel) override; + TArray ReadSound(int lumpnum); - initprintf("Initializing sound... "); - - if (FX_Init(snd_numvoices, snd_numchannels, snd_mixrate, initdata) != FX_Ok) +public: + DukeSoundEngine() { - initprintf("failed! %s\n", FX_ErrorString(FX_Error)); - return; + S_Rolloff.RolloffType = ROLLOFF_Doom; // Seems like Duke uses the same rolloff type as Doom. + S_Rolloff.MinDistance = 144; // was originally 576 which looks like a bug and sounds like crap. + S_Rolloff.MaxDistance = 1088; } +}; - initprintf("%d voices, %d channels, 16-bit %d Hz\n", *snd_numvoices, *snd_numchannels, *snd_mixrate); - - for (int i = 0; i <= g_highestSoundIdx; ++i) - { - for (auto & voice : g_sounds[i].voices) - { - g_sounds[i].num = 0; - voice.id = 0; - voice.owner = -1; - voice.dist = UINT16_MAX; - voice.clock = 0; - } - } - - S_PrecacheSounds(); - - snd_fxvolume.Callback(); - - snd_reversestereo.Callback(); - FX_SetCallBack(S_Callback); - FX_SetPrintf(OSD_Printf); +void S_InitSound() +{ + soundEngine = new DukeSoundEngine; } -void S_SoundShutdown(void) +//========================================================================== +// +// +// +//========================================================================== + +TArray DukeSoundEngine::ReadSound(int lumpnum) { - if (FX_Shutdown() != FX_Ok) - { - Bsprintf(tempbuf, "S_SoundShutdown(): error: %s", FX_ErrorString(FX_Error)); - G_GameExit(tempbuf); - } + auto wlump = fileSystem.OpenFileReader(lumpnum); + return wlump.Read(); } +//========================================================================== +// +// +// +//========================================================================== void S_PauseSounds(bool paused) { - if (SoundPaused == paused) - return; + soundEngine->SetPaused(paused); +} - SoundPaused = paused; +//========================================================================== +// +// +// +//========================================================================== - for (int i = 0; i <= g_highestSoundIdx; ++i) +void cacheAllSounds(void) +{ + auto& sfx = soundEngine->GetSounds(); + int i = 0; + for(auto &snd : sfx) { - for (auto & voice : g_sounds[i].voices) - if (voice.id > 0) - FX_PauseVoice(voice.id, paused); + soundEngine->CacheSound(&snd); + if (((++i)&31) == 0) + G_HandleAsync(); } } +//========================================================================== +// +// +// +//========================================================================== + +static inline int S_GetPitch(int num) +{ + auto const* snd = (sound_t*)soundEngine->GetUserData(num+1); + int const range = abs(snd->pitchEnd - snd->pitchStart); + + return (range == 0) ? snd->pitchStart : min(snd->pitchStart, snd->pitchEnd) + rand() % range; +} + +float S_ConvertPitch(int lpitch) +{ + return pow(2, lpitch / 1200.); // I hope I got this right that ASS uses a linear scale where 1200 is a full octave. +} + +int S_GetUserFlags(int sndnum) +{ + return ((sound_t*)soundEngine->GetUserData(sndnum + 1))->flags; +} + +//========================================================================== +// +// +// +//========================================================================== + +int S_DefineSound(unsigned index, const char *filename, int minpitch, int maxpitch, int priority, int type, int distance, float volume) +{ + if ((unsigned)index >= MAXSOUNDS) + return -1; + + auto& S_sfx = soundEngine->GetSounds(); + index++; + unsigned oldindex = S_sfx.Size(); + if (index >= S_sfx.Size()) + { + S_sfx.Resize(index + 1); + for (; oldindex <= index; oldindex++) + { + S_sfx[oldindex].Clear(); + } + } + auto sfx = &S_sfx[index]; + bool alreadydefined = !sfx->bTentative; + sfx->UserData.Resize(sizeof(sound_t)); + auto sndinf = (sound_t*)sfx->UserData.Data(); + sndinf->flags = type & ~SF_ONEINST_INTERNAL; + if (sndinf->flags & SF_LOOP) + sndinf->flags |= SF_ONEINST_INTERNAL; + + sfx->lumpnum = fileSystem.FindFile(filename); + sndinf->pitchStart = clamp(minpitch, INT16_MIN, INT16_MAX); + sndinf->pitchEnd = clamp(maxpitch, INT16_MIN, INT16_MAX); + sndinf->priority = priority & 255; + sndinf->volAdjust = clamp(distance, INT16_MIN, INT16_MAX); + sfx->Volume = volume; + sfx->NearLimit = 6; + sfx->bTentative = false; + sfx->name = filename; + return 0; +} + + +//========================================================================== +// +// +// +//========================================================================== + +static int S_CalcDistAndAng(int spriteNum, int soundNum, int sectNum, + const vec3_t *cam, const vec3_t *pos, int *distPtr, FVector3 *sndPos) +{ + // There's a lot of hackery going on here that could be mapped to rolloff and attenuation parameters. + // However, ultimately rolloff would also just reposition the sound source so this can remain as it is. + + int orgsndist = 0, sndang = 0, sndist = 0, explosion = 0; + auto const* snd = (sound_t*)soundEngine->GetUserData(soundNum+1); + int userflags = snd->flags; + int dist_adjust = snd->volAdjust; + + if (PN(spriteNum) != APLAYER || P_Get(spriteNum) != screenpeek) + { + orgsndist = sndist = FindDistance3D(cam->x - pos->x, cam->y - pos->y, (cam->z - pos->z)); + + if ((userflags & (SF_GLOBAL | SF_DTAG)) != SF_GLOBAL && S_IsAmbientSFX(spriteNum) && (sector[SECT(spriteNum)].lotag & 0xff) < 9) // ST_9_SLIDING_ST_DOOR + sndist = divscale14(sndist, SHT(spriteNum) + 1); + } + + sndist += dist_adjust; + if (sndist < 0) + sndist = 0; + + if (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>>(RR?2:5); + + // Here the sound distance was clamped to a minimum of 144*4. + // It's better to handle rolloff in the backend instead of whacking the sound origin here. + // That way the lower end can be made customizable instead of losing all precision right here at the source. + if (sndist < 0) sndist = 0; + + if (distPtr) + { + *distPtr = sndist; + } + + if (sndPos) + { + FVector3 sndorg = { pos->x * xmul, pos->z * zmul, pos->y * ymul }; + FVector3 campos = { cam->x * xmul, cam->z * zmul, cam->y * ymul }; + // Now calculate the virtual position in sound system coordinates. + FVector3 sndvec = sndorg - campos; + if (orgsndist > 0) + { + float scale = float(sndist) / orgsndist; // adjust by what was calculated above; + *sndPos = campos + sndvec * scale; + } + else *sndPos = campos; + } + + return false; +} + +//========================================================================== +// +// +// +//========================================================================== + +void S_GetCamera(vec3_t** c, int32_t* ca, int32_t* cs) +{ + if (ud.camerasprite == -1) + { + if (ud.overhead_on != 2) + { + if (c) *c = &CAMERA(pos); + if (cs) *cs = CAMERA(sect); + if (ca) *ca = fix16_to_int(CAMERA(q16ang)); + } + else + { + auto pPlayer = g_player[screenpeek].ps; + if (c) *c = &pPlayer->pos; + if (cs) *cs = pPlayer->cursectnum; + if (ca) *ca = fix16_to_int(pPlayer->q16ang); + } + } + else + { + if (c) *c = &sprite[ud.camerasprite].pos; + if (cs) *cs = sprite[ud.camerasprite].sectnum; + if (ca) *ca = sprite[ud.camerasprite].ang; + } +} + +//========================================================================= +// +// CalcPosVel +// +// The game specific part of the sound updater. +// +//========================================================================= + +void DukeSoundEngine::CalcPosVel(int type, const void* source, const float pt[3], int channum, int chanflags, FSoundID chanSound, FVector3* pos, FVector3* vel) +{ + if (pos != nullptr) + { + vec3_t* campos; + int32_t camsect; + + S_GetCamera(&campos, nullptr, &camsect); + if (vel) vel->Zero(); + + if (type == SOURCE_Unattached) + { + pos->X = pt[0]; + pos->Y = pt[1]; + pos->Z = pt[2]; + } + else if (type == SOURCE_Actor) + { + auto actor = (spritetype*)source; + assert(actor != nullptr); + if (actor != nullptr) + { + S_CalcDistAndAng(int(actor - sprite), chanSound - 1, camsect, campos, &actor->pos, nullptr, pos); + /* + if (vel) // DN3D does not properly maintain this. + { + vel->X = float(actor->Vel.X * TICRATE); + vel->Y = float(actor->Vel.Z * TICRATE); + vel->Z = float(actor->Vel.Y * TICRATE); + } + */ + } + } + if ((chanflags & CHAN_LISTENERZ) && campos != nullptr && type != SOURCE_None) + { + pos->Y = campos->z / 256.f; + } + } +} + + +//========================================================================== +// +// +// +//========================================================================== + +void S_Update(void) +{ + SoundListener listener; + vec3_t* c; + int32_t ca, cs; + +#if 0 + if (RR /*&& todo: fix the conditions here */ ) + S_PlayRRMusic(); +#endif + + S_GetCamera(&c, &ca, &cs); + + if (c != nullptr) + { + listener.angle = -(float)ca * pi::pi() / 1024; // Build uses a period of 2048. + listener.velocity.Zero(); + listener.position = { c->x * xmul, c->z * zmul, c->y * ymul }; + listener.underwater = false; + // This should probably use a real environment instead of the pitch hacking in S_PlaySound3D. + // listenactor->waterlevel == 3; + //assert(primaryLevel->Zones.Size() > listenactor->Sector->ZoneNumber); + listener.Environment = 0;// primaryLevel->Zones[listenactor->Sector->ZoneNumber].Environment; + listener.valid = true; + } + else + { + listener.angle = 0; + listener.position.Zero(); + listener.velocity.Zero(); + listener.underwater = false; + listener.Environment = nullptr; + listener.valid = false; + } + listener.ListenerObject = ud.camerasprite == -1 ? nullptr : &sprite[ud.camerasprite]; + soundEngine->SetListener(listener); + soundEngine->UpdateSounds((int)totalclock); +} + + +//========================================================================== +// +// +// +//========================================================================== + +int S_PlaySound3D(int sndnum, int spriteNum, const vec3_t* pos, int flags) +{ + auto const pPlayer = g_player[myconnectindex].ps; + if (!soundEngine->isValidSoundId(sndnum) || !SoundEnabled() || (unsigned)spriteNum >= MAXSPRITES || (pPlayer->gm & MODE_MENU) || + (pPlayer->timebeforeexit > 0 && pPlayer->timebeforeexit <= GAMETICSPERSEC * 3)) return -1; + + int userflags = S_GetUserFlags(sndnum); + if ((!(snd_speech & 1) && (userflags & SF_TALK)) || ((userflags & SF_ADULT) && adult_lockout)) + return -1; + + // Duke talk + if (userflags & SF_TALK) + { + if ((g_netServer || ud.multimode > 1) && PN(spriteNum) == APLAYER && P_Get(spriteNum) != screenpeek) // other player sound + { + if ((snd_speech & 4) != 4) + return -1; + } + else if ((snd_speech & 1) != 1) + return -1; + + + bool foundone = soundEngine->EnumerateChannels([&](FSoundChan* chan) + { + auto sid = chan->SoundID; + auto flags = ((sound_t*)soundEngine->GetUserData(sid))->flags; + return !!(flags & SF_TALK); + }); + // don't play if any Duke talk sounds are already playing + if (foundone) return -1; + } + else if ((userflags & (SF_DTAG | SF_GLOBAL)) == SF_DTAG) // Duke-Tag sound + { + return S_PlaySound(sndnum); + + } + + int32_t sndist; + FVector3 sndpos; // this is in sound engine space. + + vec3_t* campos; + int32_t camsect; + + S_GetCamera(&campos, nullptr, &camsect); + S_CalcDistAndAng(spriteNum, sndnum, camsect, campos, pos, &sndist, &sndpos); + int pitch = S_GetPitch(sndnum); + auto const pOther = g_player[screenpeek].ps; + + + if (pOther->sound_pitch) + pitch += pOther->sound_pitch; + + bool explosionp = ((userflags & (SF_GLOBAL | SF_DTAG)) == (SF_GLOBAL | SF_DTAG)) || ((sndnum == PIPEBOMB_EXPLODE || sndnum == LASERTRIP_EXPLODE || sndnum == RPG_EXPLODE)); + + if (explosionp) + { + if (pOther->cursectnum > -1 && sector[pOther->cursectnum].lotag == ST_2_UNDERWATER) + pitch -= 1024; + } + else + { + if (sndist > 32767 && PN(spriteNum) != MUSICANDSFX && (userflags & (SF_LOOP | SF_MSFX)) == 0) + return -1; + + if (pOther->cursectnum > -1 && sector[pOther->cursectnum].lotag == ST_2_UNDERWATER + && (userflags & SF_TALK) == 0) + pitch = -768; + } + + bool is_playing = soundEngine->GetSoundPlayingInfo(SOURCE_Any, nullptr, sndnum+1); + if (is_playing && PN(spriteNum) != MUSICANDSFX) + S_StopEnvSound(sndnum, spriteNum); + + int const repeatp = (userflags & SF_LOOP); + + if (repeatp && (userflags & SF_ONEINST_INTERNAL) && is_playing) + { + return -1; + } + + // These explosion sounds originally used some distance hackery to make them louder but due to how the rolloff was set up they always played at full volume as a result. + // I think it is better to lower their attenuation so that they are louder than the rest but still fade in the distance. + // For the original effect, attenuation needs to be set to ATTN_NONE here. + float attenuation; + if (explosionp) attenuation = 0.5f; + else attenuation = (userflags & (SF_GLOBAL | SF_DTAG)) == SF_GLOBAL ? ATTN_NONE : ATTN_NORM; + + if (userflags & SF_LOOP) flags |= CHAN_LOOP; + auto chan = soundEngine->StartSound(SOURCE_Actor, &sprite[spriteNum], &sndpos, flags, sndnum+1, attenuation == ATTN_NONE? 0.8f : 1.f, attenuation, nullptr, S_ConvertPitch(pitch)); + return chan ? 0 : -1; +} + +//========================================================================== +// +// +// +//========================================================================== + +int S_PlaySound(int sndnum, int flags) +{ + if (!soundEngine->isValidSoundId(sndnum+1) || !SoundEnabled()) return -1; + + int userflags = S_GetUserFlags(sndnum); + if ((!(snd_speech & 1) && (userflags & SF_TALK)) || ((userflags & SF_ADULT) && adult_lockout)) + return -1; + + int const pitch = S_GetPitch(sndnum); + + if (userflags & SF_LOOP) flags |= CHAN_LOOP; + auto chan = soundEngine->StartSound(SOURCE_None, nullptr, nullptr, flags, sndnum + 1, 0.8f, ATTN_NONE, nullptr, S_ConvertPitch(pitch)); + return chan ? 0 : -1; +} + +//========================================================================== +// +// +// +//========================================================================== + +int A_PlaySound(int soundNum, int spriteNum, int flags) +{ + return (unsigned)spriteNum >= MAXSPRITES ? S_PlaySound(soundNum, flags) : + S_PlaySound3D(soundNum, spriteNum, &sprite[spriteNum].pos, flags); +} + +void S_StopEnvSound(int sndNum, int sprNum, int flags) +{ + if (sprNum < -1 || sprNum >= MAXSPRITES) return; + + if (sprNum == -1) soundEngine->StopSoundID(sndNum+1); + else if (flags == -1) soundEngine->StopSound(SOURCE_Actor, &sprite[sprNum], -1, sndNum+1); + else soundEngine->StopSound(SOURCE_Actor, &sprite[sprNum], flags, -1); +} + +void S_ChangeSoundPitch(int soundNum, int spriteNum, int pitchoffset) +{ + if (spriteNum < -1 || spriteNum >= MAXSPRITES) return; + double expitch = pow(2, pitchoffset / 1200.); // I hope I got this right that ASS uses a linear scale where 1200 is a full octave. + if (spriteNum == -1) + { + soundEngine->ChangeSoundPitch(SOURCE_Unattached, nullptr, CHAN_AUTO, expitch, soundNum+1); + } + else + { + soundEngine->ChangeSoundPitch(SOURCE_Actor, &sprite[spriteNum], CHAN_AUTO, expitch, soundNum+1); + } +} + +//========================================================================== +// +// +// +//========================================================================== + +int A_CheckSoundPlaying(int spriteNum, int soundNum, int flags) +{ + if (spriteNum == -1) return soundEngine->GetSoundPlayingInfo(SOURCE_Any, nullptr, soundNum+1); + if ((unsigned)spriteNum >= MAXSPRITES) return false; + return soundEngine->IsSourcePlayingSomething(SOURCE_Actor, &sprite[spriteNum], flags, soundNum+1); +} + +// Check if actor is playing any sound. +int A_CheckAnySoundPlaying(int spriteNum) +{ + if ((unsigned)spriteNum >= MAXSPRITES) return false; + return soundEngine->IsSourcePlayingSomething(SOURCE_Actor, &sprite[spriteNum], CHAN_AUTO, 0); +} + +int S_CheckSoundPlaying(int soundNum) +{ + return soundEngine->GetSoundPlayingInfo(SOURCE_Any, nullptr, soundNum+1); +} + +//========================================================================== +// +// +// +//========================================================================== + void S_MenuSound(void) { static int SoundNum; @@ -122,21 +548,33 @@ void S_MenuSound(void) S_PlaySound(s); } +//========================================================================== +// +// Music +// +//========================================================================== + +static void S_SetMusicIndex(unsigned int m) +{ + ud.music_episode = m / MAXLEVELS; + ud.music_level = m % MAXLEVELS; +} void S_PlayLevelMusicOrNothing(unsigned int m) { - Mus_Play(mapList[m].labelName, RR ? nullptr : mapList[m].music, true); + auto& mr = m == USERMAPMUSICFAKESLOT ? userMapRecord : mapList[m]; + Mus_Play(mr.labelName, mr.music, true); + S_SetMusicIndex(m); } int S_TryPlaySpecialMusic(unsigned int m) { - if (RR) - return 1; - auto &musicfn = mapList[m].music; + auto& musicfn = mapList[m].music; if (musicfn.IsNotEmpty()) { - if (!Mus_Play(nullptr, musicfn, 1)) + if (!Mus_Play(nullptr, musicfn, true)) { + S_SetMusicIndex(m); return 0; } } @@ -144,13 +582,21 @@ int S_TryPlaySpecialMusic(unsigned int m) return 1; } +void S_PlaySpecialMusicOrNothing(unsigned int m) +{ + if (S_TryPlaySpecialMusic(m)) + { + S_SetMusicIndex(m); + } +} + void S_PlayRRMusic(int newTrack) { char fileName[16]; if (!RR || !mus_redbook) return; Mus_Stop(); - g_cdTrack = newTrack != -1 ? newTrack : g_cdTrack+1; + g_cdTrack = newTrack != -1 ? newTrack : g_cdTrack + 1; if (newTrack != 10 && (g_cdTrack > 9 || g_cdTrack < 2)) g_cdTrack = 2; @@ -158,576 +604,5 @@ void S_PlayRRMusic(int newTrack) Mus_Play(fileName, 0, true); } -void S_PlaySpecialMusicOrNothing(unsigned int m) -{ - S_TryPlaySpecialMusic(m); -} -void S_Cleanup(void) -{ - static uint32_t ldnum; - uint32_t const odnum = dnum; - - while (ldnum < odnum) - { - uint32_t num = dq[ldnum++ & (DQSIZE - 1)]; - - // negative index is RTS playback - if ((int32_t)num < 0) - { - continue; - } - - // num + (MAXSOUNDS*MAXSOUNDINSTANCES) is a sound played globally - // for which there was no open slot to keep track of the voice - if (num >= (MAXSOUNDS*MAXSOUNDINSTANCES)) - { - continue; - } - - int const vidx = num & (MAXSOUNDINSTANCES - 1); - - num = (num - vidx) / MAXSOUNDINSTANCES; - - auto &snd = g_sounds[num]; - auto &voice = g_sounds[num].voices[vidx]; - - 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) - --snd.num; - - // MUSICANDSFX uses t_data[0] to control restarting the sound - // CLEAR_SOUND_T0 - if (spriteNum != -1 && S_IsAmbientSFX(spriteNum) && sector[SECT(spriteNum)].lotag < 3) // ST_2_UNDERWATER - actor[spriteNum].t_data[0] = 0; - - voice.owner = -1; - voice.id = 0; - voice.dist = UINT16_MAX; - voice.clock = 0; - } -} - -// returns number of bytes read -int32_t S_LoadSound(int num) -{ - if ((unsigned)num > (unsigned)g_highestSoundIdx || EDUKE32_PREDICT_FALSE(g_sounds[num].filename == NULL)) - return 0; - - auto &snd = g_sounds[num]; - - auto fp = S_OpenAudio(snd.filename, 0, 0); - - if (!fp.isOpen()) - { - OSD_Printf(OSDTEXT_RED "Sound %s(#%d) not found!\n", snd.filename, num); - return 0; - } - - int32_t l = fp.GetLength(); - snd.siz = l; - cacheAllocateBlock((intptr_t *)&snd.ptr, l, nullptr); - l = fp.Read(snd.ptr, l); - - return l; -} - -void S_PrecacheSounds(void) -{ - for (int32_t i = 0, j = 0; i <= g_highestSoundIdx; ++i) - if (g_sounds[i].ptr == 0) - { - j++; - if ((j&7) == 0) - G_HandleAsync(); - - S_LoadSound(i); - } -} - -static inline int S_GetPitch(int num) -{ - auto const &snd = g_sounds[num]; - int const range = klabs(snd.pe - snd.ps); - - return (range == 0) ? snd.ps : min(snd.ps, snd.pe) + rand() % range; -} - -static int S_TakeSlot(int soundNum) -{ - S_Cleanup(); - - uint16_t dist = 0; - uint16_t clock = 0; - - int bestslot = 0; - int slot = 0; - - auto &snd = g_sounds[soundNum]; - - while (slot < MAXSOUNDINSTANCES && snd.voices[slot].id > 0) - { - auto &voice = snd.voices[slot]; - - if (voice.dist > dist || (voice.dist == dist && voice.clock > clock)) - { - clock = voice.clock; - dist = voice.dist; - - bestslot = slot; - } - - slot++; - } - - if (slot != MAXSOUNDINSTANCES) - return slot; - - if (FX_SoundActive(snd.voices[bestslot].id)) - FX_StopSound(snd.voices[bestslot].id); - - dq[dnum++ & (DQSIZE-1)] = (soundNum * MAXSOUNDINSTANCES) + bestslot; - S_Cleanup(); - - return bestslot; -} - -static int S_GetSlot(int soundNum) -{ - int slot = 0; - - while (slot < MAXSOUNDINSTANCES && g_sounds[soundNum].voices[slot].id > 0) - slot++; - - return slot == MAXSOUNDINSTANCES ? S_TakeSlot(soundNum) : slot; -} - -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) -{ - int32_t sndang = 0, sndist = 0; - bool explosion = false; - - if (PN(spriteNum) == APLAYER && P_Get(spriteNum) == screenpeek) - goto sound_further_processing; - - sndang = S_GetAngle(angle, cam, pos); - sndist = FindDistance3D(cam->x-pos->x, cam->y-pos->y, (cam->z-pos->z)); - -#ifdef SPLITSCREEN_MOD_HACKS - if (g_fakeMultiMode==2) - { - // HACK for splitscreen mod: take the min of sound distances - // to 1st and 2nd player. - - if (PN(spriteNum) == APLAYER && P_Get(spriteNum) == 1) - { - sndist = sndang = 0; - goto sound_further_processing; - } - - { - const vec3_t *cam2 = &g_player[1].ps->pos; - int32_t sndist2 = FindDistance3D(cam2->x-pos->x, cam2->y-pos->y, (cam2->z-pos->z)); - - if (sndist2 < sndist) - { - cam = cam2; - sectNum = g_player[1].ps->cursectnum; - angle = g_player[1].ps->ang; - - sndist = sndist2; - sndang = S_GetAngle(angle, cam, pos); - } - } - } -#endif - - if ((g_sounds[soundNum].m & SF_GLOBAL) == 0 && S_IsAmbientSFX(spriteNum) && (sector[SECT(spriteNum)].lotag&0xff) < 9) // ST_9_SLIDING_ST_DOOR - sndist = divscale14(sndist, SHT(spriteNum)+1); - -sound_further_processing: - sndist += g_sounds[soundNum].vo; - if (sndist < 0) - sndist = 0; - - if (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>>(RR?2:5); - - switch (DYNAMICSOUNDMAP(soundNum)) - { - case PIPEBOMB_EXPLODE__STATIC: - case LASERTRIP_EXPLODE__STATIC: - case RPG_EXPLODE__STATIC: - explosion = true; - if (sndist > 6144) - sndist = 6144; - break; - } - - if ((g_sounds[soundNum].m & SF_GLOBAL) || sndist < ((255-LOUDESTVOLUME) << 6)) - sndist = ((255-LOUDESTVOLUME) << 6); - - *distPtr = sndist; - *angPtr = sndang; - - return explosion; -} - -int S_PlaySound3D(int num, int spriteNum, const vec3_t *pos) -{ - int32_t j; - - if (!SoundEnabled()) // check that the user returned -1, but only if -1 wasn't playing already (in which case, warn) - return -1; - - int const sndNum = num; - sound_t & snd = g_sounds[sndNum]; - - if (EDUKE32_PREDICT_FALSE((unsigned) sndNum > (unsigned) g_highestSoundIdx || snd.filename == NULL || snd.ptr == NULL)) - { - OSD_Printf("WARNING: invalid sound #%d\n", num); - return -1; - } - - const DukePlayer_t *const pPlayer = g_player[myconnectindex].ps; - - if (((snd.m & SF_ADULT) && adult_lockout) || (unsigned)spriteNum >= MAXSPRITES || (pPlayer->gm & MODE_MENU) || !FX_VoiceAvailable(snd.pr) - || (pPlayer->timebeforeexit > 0 && pPlayer->timebeforeexit <= GAMETICSPERSEC * 3)) - return -1; - - // Duke talk - if (snd.m & SF_TALK) - { - if ((g_netServer || ud.multimode > 1) && PN(spriteNum) == APLAYER && P_Get(spriteNum) != screenpeek) // other player sound - { - if ((snd_speech & 4) != 4) - return -1; - } - else if ((snd_speech & 1) != 1) - return -1; - - // don't play if any Duke talk sounds are already playing - for (j = 0; j <= g_highestSoundIdx; ++j) - if ((g_sounds[j].m & SF_TALK) && g_sounds[j].num > 0) - return -1; - } - else if (snd.m & SF_DTAG) // Duke-Tag sound - { - int const voice = S_PlaySound(sndNum); - - if (voice <= FX_Ok) - return -1; - - j = 0; - while (j < MAXSOUNDINSTANCES && snd.voices[j].id != voice) - j++; - -#ifdef DEBUGGINGAIDS - if (EDUKE32_PREDICT_FALSE(j >= MAXSOUNDINSTANCES)) - { - OSD_Printf(OSD_ERROR "%s %d: WTF?\n", __FILE__, __LINE__); - return -1; - } -#endif - - snd.voices[j].owner = spriteNum; - - return voice; - } - - int32_t sndist, sndang; - int const explosionp = S_CalcDistAndAng(spriteNum, sndNum, CAMERA(sect), fix16_to_int(CAMERA(q16ang)), &CAMERA(pos), pos, &sndist, &sndang); - int pitch = S_GetPitch(sndNum); - auto const pOther = g_player[screenpeek].ps; - -#ifdef SPLITSCREEN_MOD_HACKS - if (g_fakeMultiMode==2) - { - // splitscreen HACK - if (g_player[1].ps->i == spriteNum) - pOther = g_player[1].ps; -} -#endif - - if (pOther->sound_pitch) - pitch += pOther->sound_pitch; - - if (explosionp) - { - if (pOther->cursectnum > -1 && sector[pOther->cursectnum].lotag == ST_2_UNDERWATER) - pitch -= 1024; - } - else - { - if (sndist > 32767 && PN(spriteNum) != MUSICANDSFX && (snd.m & (SF_LOOP|SF_MSFX)) == 0) - return -1; - - if (pOther->cursectnum > -1 && sector[pOther->cursectnum].lotag == ST_2_UNDERWATER - && (snd.m & SF_TALK) == 0) - pitch = -768; - } - - if (snd.num > 0 && PN(spriteNum) != MUSICANDSFX) - S_StopEnvSound(sndNum, spriteNum); - - - int const sndSlot = S_GetSlot(sndNum); - - if (sndSlot >= MAXSOUNDINSTANCES) - { - return -1; - } - - int const repeatp = (snd.m & SF_LOOP); - - if (repeatp && (snd.m & SF_ONEINST_INTERNAL) && snd.num > 0) - { - return -1; - } - - // XXX: why is 'right' 0? - // Ambient MUSICANDSFX always start playing using the 3D routines! - int const ambsfxp = S_IsAmbientSFX(spriteNum); - int const voice = (repeatp && !ambsfxp) ? FX_Play(snd.ptr, snd.siz, 0, -1, pitch, sndist >> 6, sndist >> 6, 0, snd.pr, - snd.volume, (sndNum * MAXSOUNDINSTANCES) + sndSlot) - : FX_Play3D(snd.ptr, snd.siz, repeatp ? FX_LOOP : FX_ONESHOT, pitch, sndang >> 4, sndist >> 6, - snd.pr, snd.volume, (sndNum * MAXSOUNDINSTANCES) + sndSlot); - - if (voice <= FX_Ok) - { - return -1; - } - - snd.num++; - snd.voices[sndSlot].owner = spriteNum; - snd.voices[sndSlot].id = voice; - snd.voices[sndSlot].dist = sndist >> 6; - snd.voices[sndSlot].clock = 0; - - return voice; -} - -int S_PlaySound(int num) -{ - int sndnum; - - if (!SoundEnabled()) // check that the user returned -1, but only if -1 wasn't playing already (in which case, warn) - return -1; - - sound_t & snd = g_sounds[num]; - - if (EDUKE32_PREDICT_FALSE((unsigned)num > (unsigned)g_highestSoundIdx || snd.filename == NULL || snd.ptr == NULL)) - { - OSD_Printf("WARNING: invalid sound #%d\n",num); - return -1; - } - - if ((!(snd_speech & 1) && (snd.m & SF_TALK)) || ((snd.m & SF_ADULT) && adult_lockout) || !FX_VoiceAvailable(snd.pr)) - return -1; - - int const pitch = S_GetPitch(num); - - sndnum = S_GetSlot(num); - - if (sndnum >= MAXSOUNDINSTANCES) - { - return -1; - } - - int const voice = (snd.m & SF_LOOP) ? FX_Play(snd.ptr, snd.siz, 0, -1, pitch, LOUDESTVOLUME, LOUDESTVOLUME, - LOUDESTVOLUME, snd.siz, snd.volume, (num * MAXSOUNDINSTANCES) + sndnum) - : FX_Play3D(snd.ptr, snd.siz, FX_ONESHOT, pitch, 0, 255 - LOUDESTVOLUME, snd.pr, snd.volume, - (num * MAXSOUNDINSTANCES) + sndnum); - - if (voice <= FX_Ok) - { - return -1; - } - - snd.num++; - snd.voices[sndnum].owner = -1; - snd.voices[sndnum].id = voice; - snd.voices[sndnum].dist = 255 - LOUDESTVOLUME; - snd.voices[sndnum].clock = 0; - - return voice; -} - -int A_PlaySound(int soundNum, int spriteNum) -{ - 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]); -} - -void S_StopEnvSound(int32_t num, int32_t i) -{ - if (EDUKE32_PREDICT_FALSE((unsigned)num > (unsigned)g_highestSoundIdx) || g_sounds[num].num <= 0) - return; - - int32_t j; - - do - { - for (j=0; j FX_Ok) || (i != -1 && g_sounds[num].voices[j].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 -#endif - if (g_sounds[num].voices[j].id > FX_Ok) - { - FX_StopSound(g_sounds[num].voices[j].id); - S_Cleanup(); - break; - } - } - } - } - while (j < MAXSOUNDINSTANCES); -} - -// Do not remove this or make it inline. -void S_StopAllSounds(void) -{ - FX_StopAllSounds(); -} - -void S_ChangeSoundPitch(int soundNum, int spriteNum, int pitchoffset) -{ - if ((unsigned)soundNum > (unsigned)g_highestSoundIdx || g_sounds[soundNum].num <= 0) - return; - - for (auto &voice : g_sounds[soundNum].voices) - { - if ((spriteNum == -1 && voice.id > FX_Ok) || (spriteNum != -1 && voice.owner == spriteNum)) - { - if (EDUKE32_PREDICT_FALSE(spriteNum >= 0 && voice.id <= FX_Ok)) - initprintf(OSD_ERROR "S_ChangeSoundPitch(): bad voice %d for sound ID %d!\n", voice.id, soundNum); - else if (voice.id > FX_Ok && FX_SoundActive(voice.id)) - FX_SetPitch(voice.id, pitchoffset); - break; - } - } -} - -void S_Update(void) -{ - S_Cleanup(); - - if ((g_player[myconnectindex].ps->gm & (MODE_GAME|MODE_DEMO)) == 0) - return; - - if (RR && MusicIsWaveform && MusicVoice >= 0 && !FX_SoundActive(MusicVoice)) - S_PlayRRMusic(); - - g_numEnvSoundsPlaying = 0; - - const vec3_t *c; - int32_t ca,cs; - - if (ud.camerasprite == -1) - { - c = &CAMERA(pos); - cs = CAMERA(sect); - ca = fix16_to_int(CAMERA(q16ang)); - } - else - { - c = (vec3_t *)&sprite[ud.camerasprite]; - cs = sprite[ud.camerasprite].sectnum; - ca = sprite[ud.camerasprite].ang; - } - - int sndnum = 0; - int const highest = g_highestSoundIdx; - - do - { - if (g_sounds[sndnum].num == 0) - continue; - - for (auto &voice : g_sounds[sndnum].voices) - { - int const spriteNum = voice.owner; - - if ((unsigned)spriteNum >= MAXSPRITES || voice.id <= FX_Ok || !FX_SoundActive(voice.id)) - continue; - - int32_t sndist, sndang; - - S_CalcDistAndAng(spriteNum, sndnum, cs, ca, c, (const vec3_t *)&sprite[spriteNum], &sndist, &sndang); - - if (S_IsAmbientSFX(spriteNum)) - g_numEnvSoundsPlaying++; - - // AMBIENT_SOUND - FX_Pan3D(voice.id, sndang >> 4, sndist >> 6); - voice.dist = sndist >> 6; - voice.clock++; - } - } while (++sndnum <= highest); -} - -void S_Callback(intptr_t num) -{ - if (num == MUSIC_ID) - return; - - dq[dnum & (DQSIZE - 1)] = (uint32_t)num; - dnum++; -} - - -bool A_CheckSoundPlaying(int spriteNum, int soundNum) -{ - if (EDUKE32_PREDICT_FALSE((unsigned)soundNum > (unsigned)g_highestSoundIdx)) return 0; - - if (g_sounds[soundNum].num > 0 && spriteNum >= 0) - { - for (auto &voice : g_sounds[soundNum].voices) - if (voice.owner == spriteNum) - return 1; - } - - return (spriteNum == -1) ? (g_sounds[soundNum].num != 0) : 0; -} - -// Check if actor is playing any sound. -bool A_CheckAnySoundPlaying(int spriteNum) -{ - int const msp = g_highestSoundIdx; - - for (int j = 0; j <= msp; ++j) - { - for (auto &voice : g_sounds[j].voices) - if (voice.owner == spriteNum) - return 1; - } - - return 0; -} - -bool S_CheckSoundPlaying(int spriteNum, int soundNum) -{ - if (EDUKE32_PREDICT_FALSE((unsigned)soundNum > (unsigned)g_highestSoundIdx)) return false; - return (g_sounds[soundNum].num != 0); -} - -END_RR_NS +END_DUKE_NS diff --git a/source/rr/src/sounds.h b/source/rr/src/sounds.h index 39cb58900..20b58cdbe 100644 --- a/source/rr/src/sounds.h +++ b/source/rr/src/sounds.h @@ -30,61 +30,46 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #define sounds_public_h_ #include "sounds_common.h" +#include "sound/s_soundinternal.h" #include "z_music.h" BEGIN_RR_NS // KEEPINSYNC lunatic/con_lang.lua #define MAXSOUNDS 4096 -#define MAXSOUNDINSTANCES 8 #define LOUDESTVOLUME 111 -#define MUSIC_ID -65536 typedef struct { - int16_t owner; - int16_t id; - uint16_t dist; - uint16_t clock; -} assvoice_t; - -typedef struct -{ - char * ptr, *filename; // 8b/16b - int32_t length, num, siz; // 12b - float volume; // 4b - assvoice_t voices[MAXSOUNDINSTANCES]; // 64b - int16_t ps, pe, vo; // 6b - char pr, m; // 2b + int pitchStart, pitchEnd, volAdjust; + int priority, flags; } sound_t; -extern sound_t g_sounds[MAXSOUNDS]; -extern int32_t g_skillSoundVoice; -extern int32_t g_numEnvSoundsPlaying,g_highestSoundIdx; - -bool A_CheckSoundPlaying(int spriteNum,int soundNum); -int A_PlaySound(int soundNum, int spriteNum); +int A_CheckSoundPlaying(int spriteNum, int soundNum, int flags = 0); +int A_PlaySound(int soundNum, int spriteNum, int flags = 0); void S_Callback(intptr_t num); -bool A_CheckAnySoundPlaying(int spriteNum); -bool S_CheckSoundPlaying(int spriteNum,int soundNum); -void S_Cleanup(void); +int A_CheckAnySoundPlaying(int spriteNum); +int S_CheckSoundPlaying(int soundNum); +inline int S_CheckSoundPlaying(int sprnum, int soundNum) { return S_CheckSoundPlaying(soundNum); } inline void S_ClearSoundLocks(void) {} -int32_t S_LoadSound(uint32_t num); -void S_PrecacheSounds(void); +void cacheAllSounds(void); void S_MenuSound(void); +void S_PauseMusic(bool paused); void S_PauseSounds(bool paused); -void S_PlayRRMusic(int newTrack = -1); void S_PlayLevelMusicOrNothing(unsigned int); int S_TryPlaySpecialMusic(unsigned int); void S_PlaySpecialMusicOrNothing(unsigned int); -int S_PlaySound(int num); -int S_PlaySound3D(int num, int spriteNum, const vec3_t *pos); -void S_SoundShutdown(void); -void S_SoundStartup(void); -void S_StopEnvSound(int32_t num,int32_t i); +void S_ContinueLevelMusic(void); +int S_PlaySound(int num, int flags = 0); +int S_PlaySound3D(int num, int spriteNum, const vec3_t *pos, int flags = 0); +void S_StopEnvSound(int sndNum,int sprNum, int flags = -1); void S_StopAllSounds(void); void S_Update(void); void S_ChangeSoundPitch(int soundNum, int spriteNum, int pitchoffset); +int S_GetUserFlags(int sndnum); +int S_DefineSound(unsigned index, const char* filename, int ps, int pe, int pr, int m, int vo, float vol); +void S_InitSound(); +void S_PlayRRMusic(int newTrack = -1); static inline bool S_IsAmbientSFX(int spriteNum) { diff --git a/source/sw/src/sounds.cpp b/source/sw/src/sounds.cpp index 95d534544..25d3253a9 100644 --- a/source/sw/src/sounds.cpp +++ b/source/sw/src/sounds.cpp @@ -326,7 +326,7 @@ SWBOOL PlaySong(const char* mapname, const char* song_file_name, int cdaudio_tra void StopFX(void) { - FX_StopAllSounds(); + FX_StopAllSounds_(); } void