diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index d1b734c3f..bdf69362f 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -621,6 +621,8 @@ file( GLOB HEADER_FILES common/console/*.h common/filesystem/*.h common/music/*.h + common/sound/*.h + common/sound/backend/*.h common/menu/*.h common/input/*.h @@ -902,6 +904,7 @@ include_directories( common/filesystem common/music common/sound + common/sound/backend common/dobject common/menu common/input diff --git a/source/audiolib/include/fx_man.h b/source/audiolib/include/fx_man.h index 32b0bbef2..66c0d73c7 100644 --- a/source/audiolib/include/fx_man.h +++ b/source/audiolib/include/fx_man.h @@ -91,7 +91,7 @@ static FORCE_INLINE void FX_SetVolume(int volume) { MV_SetVolume(volume); } static FORCE_INLINE int FX_GetVolume(void) { return MV_GetVolume(); } static FORCE_INLINE void FX_SetReverseStereo(int setting) { MV_SetReverseStereo(setting); } static FORCE_INLINE int FX_GetReverseStereo(void) { return MV_GetReverseStereo(); } -static FORCE_INLINE void FX_SetReverb(int reverb) { MV_SetReverb(reverb); } +static FORCE_INLINE void FX_SetReverb_(int reverb) { MV_SetReverb(reverb); } static FORCE_INLINE int FX_GetMaxReverbDelay(void) { return MV_GetMaxReverbDelay(); } static FORCE_INLINE int FX_GetReverbDelay(void) { return MV_GetReverbDelay(); } static FORCE_INLINE void FX_SetReverbDelay(int delay) { MV_SetReverbDelay(delay); } diff --git a/source/blood/src/sfx.cpp b/source/blood/src/sfx.cpp index 538fc16eb..e5329dbb2 100644 --- a/source/blood/src/sfx.cpp +++ b/source/blood/src/sfx.cpp @@ -488,22 +488,22 @@ void sfxSetReverb(bool toggle) { if (toggle) { - FX_SetReverb(128); + FX_SetReverb_(128); FX_SetReverbDelay(10); } else - FX_SetReverb(0); + FX_SetReverb_(0); } void sfxSetReverb2(bool toggle) { if (toggle) { - FX_SetReverb(128); + FX_SetReverb_(128); FX_SetReverbDelay(20); } else - FX_SetReverb(0); + FX_SetReverb_(0); } END_BLD_NS diff --git a/source/common/filesystem/filesystem.cpp b/source/common/filesystem/filesystem.cpp index b7bbee4e1..97466fb50 100644 --- a/source/common/filesystem/filesystem.cpp +++ b/source/common/filesystem/filesystem.cpp @@ -893,13 +893,14 @@ const char *FileSystem::GetResourceFileFullName (int rfnum) const noexcept // //========================================================================== -void FileSystem::AddFromBuffer(const char* name, const char* type, char* data, int size, int id, int flags) +int FileSystem::AddFromBuffer(const char* name, const char* type, char* data, int size, int id, int flags) { FStringf fullname("%s.%s", name, type); auto newlump = new FMemoryLump(data, size); newlump->LumpNameSetup(fullname); newlump->ResourceId = id; AddLump(newlump); + return Files.Size()-1; } //========================================================================== diff --git a/source/common/filesystem/filesystem.h b/source/common/filesystem/filesystem.h index 028716c44..dd464ba50 100644 --- a/source/common/filesystem/filesystem.h +++ b/source/common/filesystem/filesystem.h @@ -118,7 +118,7 @@ public: int FindResource (int resid, const char *type, int filenum = -1) const noexcept; int GetResource (int resid, const char *type, int filenum = -1) const; // Like FindFile, but throws an exception when it cannot find what it looks for. - void AddFromBuffer(const char* name, const char* type, char* data, int size, int id, int flags); + int AddFromBuffer(const char* name, const char* type, char* data, int size, int id, int flags); TArray GetAllFilesOfType(FName type, bool withsubdirs = false); diff --git a/source/common/rts.cpp b/source/common/rts.cpp index 372d12805..5ce19f36d 100644 --- a/source/common/rts.cpp +++ b/source/common/rts.cpp @@ -38,6 +38,7 @@ #include "filesystem/filesystem.h" #include "rts.h" #include "m_swap.h" +#include "s_soundinternal.h" struct WadInfo @@ -56,7 +57,7 @@ struct FileLump struct LumpInfoInternal { - int32_t position, size; + int32_t position, size, sid; }; //============= @@ -96,7 +97,7 @@ bool RTS_IsInitialized() LumpInfo.Resize(numlumps); for(unsigned i = 0; i < numlumps; i++, li++) { - LumpInfo[i] = { LittleLong(li->position), LittleLong(li->size) }; + LumpInfo[i] = { LittleLong(li->position), LittleLong(li->size), -1 }; if (unsigned(LumpInfo[i].position + LumpInfo[i].size) >= RTSFile.Size()) { LumpInfo[i].size = 0; // points to invalid data @@ -108,6 +109,19 @@ bool RTS_IsInitialized() } RTSFile.Reset(); LumpInfo.Reset(); + + // For the benefit of the sound system we have to link the RTS content into the file system. + // Unfortunately the file cannot be added directly because the internal names are meaningless. + int i = 0; + for (auto& li : LumpInfo) + { + if (li.size > 0) + { + FStringf rts("rts%02d", i); + int lump = fileSystem.AddFromBuffer(rts, "rts", (char*)RTSFile.Data() + li.position, li.size, -1, 0); + li.sid = soundEngine->AddSoundLump(rts, lump, 0, -1); + } + } return false; } @@ -127,3 +141,12 @@ void *RTS_GetSound(int lump) if(LumpInfo[lump].size <= 0) return nullptr; return RTSFile.Data() + LumpInfo[lump].position; } + +int RTS_GetSoundID(int lump) +{ + if (!RTS_IsInitialized()) return -1; + lump++; + if ((unsigned)lump >= LumpInfo.Size()) return -1; + if (LumpInfo[lump].size <= 0) return -1; + return LumpInfo[lump].sid; +} diff --git a/source/common/rts.h b/source/common/rts.h index f2a739c00..8ef937595 100644 --- a/source/common/rts.h +++ b/source/common/rts.h @@ -4,3 +4,4 @@ void RTS_Init(const char *filename); bool RTS_IsInitialized(); int RTS_SoundLength(int lump); void *RTS_GetSound(int lump); +int RTS_GetSoundID(int lump); diff --git a/source/common/sound/s_soundinternal.h b/source/common/sound/s_soundinternal.h index 6ee9ea24f..aa3ac98f1 100644 --- a/source/common/sound/s_soundinternal.h +++ b/source/common/sound/s_soundinternal.h @@ -453,3 +453,7 @@ inline void FX_StopAllSounds(void) soundEngine->StopAllChannels(); } +inline void FX_SetReverb(int strength) +{ + // todo: investigate how this works and set a proper environment. +} \ No newline at end of file diff --git a/source/duke3d/src/actors.cpp b/source/duke3d/src/actors.cpp index 12674006a..0c804b3f3 100644 --- a/source/duke3d/src/actors.cpp +++ b/source/duke3d/src/actors.cpp @@ -1486,7 +1486,6 @@ ACTOR_STATIC void G_MoveFX(void) else if (playerDist >= spriteHitag && T1(spriteNum) == 1) { FX_SetReverb(0); - FX_SetReverbDelay(0); T1(spriteNum) = 0; } } diff --git a/source/duke3d/src/duke3d.h b/source/duke3d/src/duke3d.h index 5153b823e..e96b6e432 100644 --- a/source/duke3d/src/duke3d.h +++ b/source/duke3d/src/duke3d.h @@ -29,7 +29,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "build.h" #include "cache1d.h" #include "compat.h" -#include "fx_man.h" #include "pragmas.h" #include "polymost.h" #include "gamecvars.h" diff --git a/source/duke3d/src/game.cpp b/source/duke3d/src/game.cpp index d1de8d1ab..5c3385db2 100644 --- a/source/duke3d/src/game.cpp +++ b/source/duke3d/src/game.cpp @@ -4392,16 +4392,15 @@ void G_InitTimer(int32_t ticspersec) static int32_t g_RTSPlaying; // Returns: started playing? -extern int G_StartRTS(int lumpNum, int localPlayer) +int G_StartRTS(int lumpNum, int localPlayer) { if (!adult_lockout && SoundEnabled() && RTS_IsInitialized() && g_RTSPlaying == 0 && (snd_speech & (localPlayer ? 1 : 4))) { - char *const pData = (char *)RTS_GetSound(lumpNum - 1); - - if (pData != NULL) + auto sid = RTS_GetSoundID(lumpNum - 1); + if (sid != -1) { - FX_Play3D(pData, RTS_SoundLength(lumpNum - 1), FX_ONESHOT, 0, 0, 1, 255, 1.f, -lumpNum); + S_PlaySound(sid, CHAN_UI); g_RTSPlaying = 7; return 1; } diff --git a/source/duke3d/src/sounds.cpp b/source/duke3d/src/sounds.cpp index e9e6c7117..c692cf335 100644 --- a/source/duke3d/src/sounds.cpp +++ b/source/duke3d/src/sounds.cpp @@ -104,7 +104,8 @@ void cacheAllSounds(void) static inline int S_GetPitch(int num) { - auto const* snd = (sound_t*)soundEngine->GetUserData(num+1); + auto const* snd = (sound_t*)soundEngine->GetUserData(num + 1); + if (!snd) return 0; int const range = abs(snd->pitchEnd - snd->pitchStart); return (range == 0) ? snd->pitchStart : min(snd->pitchStart, snd->pitchEnd) + rand() % range; @@ -115,9 +116,11 @@ 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) +int S_GetUserFlags(int num) { - return ((sound_t*)soundEngine->GetUserData(sndnum + 1))->flags; + auto const* snd = (sound_t*)soundEngine->GetUserData(num + 1); + if (!snd) return 0; + return snd->flags; } //========================================================================== @@ -177,8 +180,8 @@ static int S_CalcDistAndAng(int spriteNum, int soundNum, int sectNum, 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; + int userflags = snd? snd->flags : 0; + int dist_adjust = snd? snd->volAdjust : 0; if (PN(spriteNum) != APLAYER || P_Get(spriteNum) != screenpeek) { @@ -378,7 +381,7 @@ int S_PlaySound3D(int num, int spriteNum, const vec3_t* pos, int flags) bool foundone = soundEngine->EnumerateChannels([&](FSoundChan* chan) { auto sid = chan->OrgID; - auto flags = ((sound_t*)soundEngine->GetUserData(sid))->flags; + auto flags = S_GetUserFlags(sid - 1); return !!(flags & SF_TALK); }); // don't play if any Duke talk sounds are already playing diff --git a/source/rr/src/actors.cpp b/source/rr/src/actors.cpp index e411c61bc..ddb8d4697 100644 --- a/source/rr/src/actors.cpp +++ b/source/rr/src/actors.cpp @@ -1352,7 +1352,6 @@ ACTOR_STATIC void G_MoveFX(void) else if (playerDist >= spriteHitag && T1(spriteNum) == 1) { FX_SetReverb(0); - FX_SetReverbDelay(0); T1(spriteNum) = 0; } } diff --git a/source/rr/src/duke3d.h b/source/rr/src/duke3d.h index 964a8091b..1c3991fd7 100644 --- a/source/rr/src/duke3d.h +++ b/source/rr/src/duke3d.h @@ -30,7 +30,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "cache1d.h" #include "compat.h" -#include "fx_man.h" #include "pragmas.h" #include "polymost.h" diff --git a/source/rr/src/game.cpp b/source/rr/src/game.cpp index 6389448fa..3dbc59528 100644 --- a/source/rr/src/game.cpp +++ b/source/rr/src/game.cpp @@ -5946,16 +5946,15 @@ void G_InitTimer(int32_t ticspersec) static int32_t g_RTSPlaying; // Returns: started playing? -extern int G_StartRTS(int lumpNum, int localPlayer) +int G_StartRTS(int lumpNum, int localPlayer) { if (!adult_lockout && SoundEnabled() && RTS_IsInitialized() && g_RTSPlaying == 0 && (snd_speech & (localPlayer ? 1 : 4))) { - char *const pData = (char *)RTS_GetSound(lumpNum - 1); - - if (pData != NULL) + auto sid = RTS_GetSoundID(lumpNum - 1); + if (sid != -1) { - FX_Play3D(pData, RTS_SoundLength(lumpNum - 1), FX_ONESHOT, 0, 0, 1, 255, 1.f, -lumpNum); + S_PlaySound(sid, CHAN_UI); g_RTSPlaying = 7; return 1; } diff --git a/source/rr/src/sounds.cpp b/source/rr/src/sounds.cpp index bb9ad1be8..bfac522ba 100644 --- a/source/rr/src/sounds.cpp +++ b/source/rr/src/sounds.cpp @@ -105,6 +105,7 @@ void cacheAllSounds(void) static inline int S_GetPitch(int num) { auto const* snd = (sound_t*)soundEngine->GetUserData(num+1); + if (!snd) return 0; int const range = abs(snd->pitchEnd - snd->pitchStart); return (range == 0) ? snd->pitchStart : min(snd->pitchStart, snd->pitchEnd) + rand() % range; @@ -115,9 +116,11 @@ 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) +int S_GetUserFlags(int num) { - return ((sound_t*)soundEngine->GetUserData(sndnum + 1))->flags; + auto const* snd = (sound_t*)soundEngine->GetUserData(num + 1); + if (!snd) return 0; + return snd->flags; } //========================================================================== @@ -176,9 +179,9 @@ static int S_CalcDistAndAng(int spriteNum, int soundNum, int sectNum, // 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; + auto const* snd = (sound_t*)soundEngine->GetUserData(soundNum + 1); + int userflags = snd ? snd->flags : 0; + int dist_adjust = snd ? snd->volAdjust : 0; if (PN(spriteNum) != APLAYER || P_Get(spriteNum) != screenpeek) { @@ -381,7 +384,7 @@ int S_PlaySound3D(int sndnum, int spriteNum, const vec3_t* pos, int flags) bool foundone = soundEngine->EnumerateChannels([&](FSoundChan* chan) { auto sid = chan->OrgID; - auto flags = ((sound_t*)soundEngine->GetUserData(sid))->flags; + auto flags = S_GetUserFlags(sid - 1); return !!(flags & SF_TALK); }); // don't play if any Duke talk sounds are already playing diff --git a/source/sw/src/sounds.cpp b/source/sw/src/sounds.cpp index 25d3253a9..b52b0f173 100644 --- a/source/sw/src/sounds.cpp +++ b/source/sw/src/sounds.cpp @@ -894,7 +894,7 @@ SoundShutdown(void) void COVER_SetReverb(int amt) { - FX_SetReverb(amt); + FX_SetReverb_(amt); } ///////////////////////////////////////////////////////////////////////////////