diff --git a/src/common/audio/sound/s_sound.cpp b/src/common/audio/sound/s_sound.cpp index 867ccef2472..81b51eb746e 100644 --- a/src/common/audio/sound/s_sound.cpp +++ b/src/common/audio/sound/s_sound.cpp @@ -738,7 +738,7 @@ sfxinfo_t *SoundEngine::LoadSound(sfxinfo_t *sfx) (!sfx->bLoadRAW || (sfx->RawRate == S_sfx[i].RawRate))) // Raw sounds with different sample rates may not share buffers, even if they use the same source data. { DPrintf (DMSG_NOTIFY, "Linked %s to %s (%d)\n", sfx->name.GetChars(), S_sfx[i].name.GetChars(), i); - sfx->link = i; + sfx->link = FSoundID::fromInt(i); // This is necessary to avoid using the rolloff settings of the linked sound if its // settings are different. if (sfx->Rolloff.MinDistance == 0) sfx->Rolloff = S_Rolloff; @@ -1695,13 +1695,13 @@ void SoundEngine::HashSounds() S_rnd.ShrinkToFit(); } -void SoundEngine::AddRandomSound(int Owner, TArray list) +void SoundEngine::AddRandomSound(FSoundID Owner, TArray list) { auto index = S_rnd.Reserve(1); auto& random = S_rnd.Last(); random.Choices = std::move(list); random.Owner = Owner; - S_sfx[Owner].link = index; + S_sfx[Owner].link = FSoundID::fromInt(index); S_sfx[Owner].bRandomHeader = true; S_sfx[Owner].NearLimit = -1; } diff --git a/src/common/audio/sound/s_soundinternal.h b/src/common/audio/sound/s_soundinternal.h index 9fc17080788..dbcffe09085 100644 --- a/src/common/audio/sound/s_soundinternal.h +++ b/src/common/audio/sound/s_soundinternal.h @@ -2,12 +2,6 @@ #include "i_sound.h" -struct FRandomSoundList -{ - TArray Choices; - uint32_t Owner = 0; -}; - enum { sfx_empty = -1 @@ -15,47 +9,6 @@ enum -// -// SoundFX struct. -// -struct sfxinfo_t -{ - // Next field is for use by the system sound interface. - // A non-null data means the sound has been loaded. - SoundHandle data{}; - - FString name; // [RH] Sound name defined in SNDINFO - int lumpnum = sfx_empty; // lump number of sfx - - unsigned int next = -1, index = 0; // [RH] For hashing - float Volume = 1.f; - - int ResourceId = -1; // Resource ID as implemented by Blood. Not used by Doom but added for completeness. - float LimitRange = 256*256; // Range for sound limiting (squared for faster computations) - float DefPitch = 0.f; // A defined pitch instead of a random one the sound plays at, similar to A_StartSound. - float DefPitchMax = 0.f; // Randomized range with stronger control over pitch itself. - - int16_t NearLimit = 4; // 0 means unlimited. - uint8_t PitchMask = 0; - bool bRandomHeader = false; - bool bLoadRAW = false; - bool b16bit = false; - bool bUsed = false; - bool bSingular = false; - bool bTentative = true; - - TArray UserData; - - int RawRate = 0; // Sample rate to use when bLoadRAW is true - int LoopStart = -1; // -1 means no specific loop defined - - unsigned int link = NO_LINK;; - enum { NO_LINK = 0xffffffff }; - - FRolloffInfo Rolloff{}; - float Attenuation = 1.f; // Multiplies the attenuation passed to S_Sound. -}; - // Rolloff types enum { @@ -78,9 +31,12 @@ class FSoundID { return FSoundID(S_FindSoundByResID(ndx)); } - FSoundID(int id) + constexpr FSoundID(int id) : ID(id) + { + } + static constexpr FSoundID fromInt(int i) { - ID = id; + return FSoundID(i); } FSoundID(const char *name) { @@ -118,20 +74,64 @@ class FSoundID { return ID; } + constexpr bool isvalid() const + { + return ID > 0; + } private: int ID; -protected: - enum EDummy { NoInit }; - FSoundID(EDummy) {} }; - class FSoundIDNoInit : public FSoundID -{ -public: - FSoundIDNoInit() : FSoundID(NoInit) {} - using FSoundID::operator=; -}; + struct FRandomSoundList + { + TArray Choices; + FSoundID Owner = 0; + }; + + + // +// SoundFX struct. +// + struct sfxinfo_t + { + // Next field is for use by the system sound interface. + // A non-null data means the sound has been loaded. + SoundHandle data{}; + + FString name; // [RH] Sound name defined in SNDINFO + int lumpnum = sfx_empty; // lump number of sfx + + unsigned int next = -1, index = 0; // [RH] For hashing + float Volume = 1.f; + + int ResourceId = -1; // Resource ID as implemented by Blood. Not used by Doom but added for completeness. + float LimitRange = 256 * 256; // Range for sound limiting (squared for faster computations) + float DefPitch = 0.f; // A defined pitch instead of a random one the sound plays at, similar to A_StartSound. + float DefPitchMax = 0.f; // Randomized range with stronger control over pitch itself. + + int16_t NearLimit = 4; // 0 means unlimited. + uint8_t PitchMask = 0; + bool bRandomHeader = false; + bool bLoadRAW = false; + bool b16bit = false; + bool bUsed = false; + bool bSingular = false; + bool bTentative = true; + + TArray UserData; + + int RawRate = 0; // Sample rate to use when bLoadRAW is true + int LoopStart = -1; // -1 means no specific loop defined + + FSoundID link = NO_LINK; + constexpr static FSoundID NO_LINK = FSoundID::fromInt(-1); + + FRolloffInfo Rolloff{}; + float Attenuation = 1.f; // Multiplies the attenuation passed to S_Sound. + }; + constexpr FSoundID NO_SOUND = FSoundID::fromInt(0); + constexpr FSoundID INVALID_SOUND = FSoundID::fromInt(-1); struct FSoundChan : public FISoundChannel @@ -418,7 +418,7 @@ class SoundEngine unsigned int GetMSLength(FSoundID sound); int PickReplacement(int refid); void HashSounds(); - void AddRandomSound(int Owner, TArray list); + void AddRandomSound(FSoundID Owner, TArray list); }; diff --git a/src/playsim/actor.h b/src/playsim/actor.h index be0cc104441..33623ad3d85 100644 --- a/src/playsim/actor.h +++ b/src/playsim/actor.h @@ -1231,15 +1231,15 @@ class AActor final : public DThinker uint32_t BloodTranslation; // [RH] Stuff that used to be part of an Actor Info - FSoundIDNoInit SeeSound; - FSoundIDNoInit AttackSound; - FSoundIDNoInit PainSound; - FSoundIDNoInit DeathSound; - FSoundIDNoInit ActiveSound; - FSoundIDNoInit UseSound; // [RH] Sound to play when an actor is used. - FSoundIDNoInit BounceSound; - FSoundIDNoInit WallBounceSound; - FSoundIDNoInit CrushPainSound; + FSoundID SeeSound; + FSoundID AttackSound; + FSoundID PainSound; + FSoundID DeathSound; + FSoundID ActiveSound; + FSoundID UseSound; // [RH] Sound to play when an actor is used. + FSoundID BounceSound; + FSoundID WallBounceSound; + FSoundID CrushPainSound; double MaxDropOffHeight; double MaxStepHeight; diff --git a/src/sound/s_advsound.cpp b/src/sound/s_advsound.cpp index 0a9eb5dee27..9db16f2d082 100644 --- a/src/sound/s_advsound.cpp +++ b/src/sound/s_advsound.cpp @@ -186,12 +186,12 @@ static void S_RestorePlayerSounds(); static int S_AddPlayerClass (const char *name); static int S_AddPlayerGender (int classnum, int gender); static int S_FindPlayerClass (const char *name); -static int S_LookupPlayerSound (int classidx, int gender, FSoundID refid); +static FSoundID S_LookupPlayerSound (int classidx, int gender, FSoundID refid); static void S_ParsePlayerSoundCommon (FScanner &sc, FString &pclass, int &gender, int &refid); static void S_AddSNDINFO (int lumpnum); static void S_AddBloodSFX (int lumpnum); static void S_AddStrifeVoice (int lumpnum); -static int S_AddSound (const char *logicalname, int lumpnum, FScanner *sc=NULL); +static FSoundID S_AddSound (const char *logicalname, int lumpnum, FScanner *sc=NULL); // EXTERNAL DATA DECLARATIONS ---------------------------------------------- @@ -406,13 +406,13 @@ DEFINE_ACTION_FUNCTION(DObject,S_GetLength) // lump. Otherwise, adds the new mapping by using S_AddSoundLump(). //========================================================================== -int S_AddSound (const char *logicalname, const char *lumpname, FScanner *sc) +FSoundID S_AddSound (const char *logicalname, const char *lumpname, FScanner *sc) { int lump = fileSystem.CheckNumForFullName (lumpname, true, ns_sounds); return S_AddSound (logicalname, lump); } -static int S_AddSound (const char *logicalname, int lumpnum, FScanner *sc) +static FSoundID S_AddSound (const char *logicalname, int lumpnum, FScanner *sc) { FSoundID sfxid = soundEngine->FindSoundNoHash (logicalname); @@ -635,7 +635,7 @@ void S_AddLocalSndInfo(int lump) static void S_AddSNDINFO (int lump) { bool skipToEndIf; - TArray list; + TArray list; int wantassigns = -1; FScanner sc(lump); @@ -804,7 +804,7 @@ static void S_AddSNDINFO (int lump) // $playercompat FString pclass; int gender, refid; - int sfxfrom, aliasto; + FSoundID sfxfrom, aliasto; S_ParsePlayerSoundCommon (sc, pclass, gender, refid); sfxfrom = S_AddSound (sc.String, -1, &sc); @@ -839,7 +839,7 @@ static void S_AddSNDINFO (int lump) { sfxfrom = sfx->link; } - sfx->link = soundEngine->FindSoundTentative (sc.String); + sfx->link = FSoundID::fromInt(soundEngine->FindSoundTentative (sc.String)); sfx->NearLimit = -1; // Aliases must use the original sound's limit. } break; @@ -1155,7 +1155,7 @@ static void S_ParsePlayerSoundCommon (FScanner &sc, FString &pclass, int &gender } if (sfx->bTentative) { - sfx->link = NumPlayerReserves++; + sfx->link = FSoundID::fromInt(NumPlayerReserves++); sfx->bTentative = false; sfx->UserData[0] |= SND_PlayerReserve; } @@ -1293,17 +1293,7 @@ static int SortPlayerClasses (const void *a, const void *b) // Returns the sound for the given player class, gender, and sound name. //========================================================================== -int S_LookupPlayerSound (const char *pclass, int gender, const char *name) -{ - int refid = S_FindSound (name); - if (refid != 0) - { - refid = S_LookupPlayerSound (pclass, gender, refid); - } - return refid; -} - -int S_LookupPlayerSound (const char *pclass, int gender, FSoundID refid) +FSoundID S_LookupPlayerSound (const char *pclass, int gender, FSoundID refid) { auto sfxp = soundEngine->GetWritableSfx(refid); @@ -1315,7 +1305,7 @@ int S_LookupPlayerSound (const char *pclass, int gender, FSoundID refid) return S_LookupPlayerSound (S_FindPlayerClass (pclass), gender, refid); } -static int S_LookupPlayerSound (int classidx, int gender, FSoundID refid) +static FSoundID S_LookupPlayerSound (int classidx, int gender, FSoundID refid) { int ingender = gender; @@ -1414,12 +1404,7 @@ static void S_RestorePlayerSounds() // Returns true if two sounds are essentially the same thing //========================================================================== -bool S_AreSoundsEquivalent (AActor *actor, const char *name1, const char *name2) -{ - return S_AreSoundsEquivalent (actor, S_FindSound (name1), S_FindSound (name2)); -} - -bool S_AreSoundsEquivalent (AActor *actor, int id1, int id2) +bool S_AreSoundsEquivalent (AActor *actor, FSoundID id1, FSoundID id2) { sfxinfo_t *sfx; @@ -1494,7 +1479,7 @@ const char *S_GetSoundClass(AActor *pp) // Calls S_LookupPlayerSound, deducing the class and gender from actor. //========================================================================== -int S_FindSkinnedSound (AActor *actor, FSoundID refid) +FSoundID S_FindSkinnedSound (AActor *actor, FSoundID refid) { const char *pclass; int gender = 0; diff --git a/src/sound/s_sound.h b/src/sound/s_sound.h index c4c0655cfa7..42b2d24926f 100644 --- a/src/sound/s_sound.h +++ b/src/sound/s_sound.h @@ -66,14 +66,12 @@ extern MusicAliasMap MusicAliases; void S_ClearSoundData(); void S_ParseSndInfo (bool redefine); -bool S_AreSoundsEquivalent (AActor *actor, int id1, int id2); -bool S_AreSoundsEquivalent (AActor *actor, const char *name1, const char *name2); -int S_LookupPlayerSound (const char *playerclass, int gender, const char *logicalname); -int S_LookupPlayerSound (const char *playerclass, int gender, FSoundID refid); +bool S_AreSoundsEquivalent (AActor *actor, FSoundID id1, FSoundID id2); +FSoundID S_LookupPlayerSound (const char *playerclass, int gender, FSoundID refid); const char *S_GetSoundClass(AActor *pp); -int S_FindSkinnedSound (AActor *actor, FSoundID refid); +FSoundID S_FindSkinnedSound (AActor *actor, FSoundID refid); int S_FindSkinnedSoundEx (AActor *actor, const char *logicalname, const char *extendedname); -int S_AddSound (const char *logicalname, const char *lumpname, FScanner *sc=NULL); // Add sound by lumpname +FSoundID S_AddSound (const char *logicalname, const char *lumpname, FScanner *sc=NULL); // Add sound by lumpname int S_AddPlayerSound (const char *playerclass, const int gender, int refid, const char *lumpname); int S_AddPlayerSound (const char *playerclass, const int gender, int refid, int lumpnum, bool fromskin=false); int S_AddPlayerSoundExisting (const char *playerclass, const int gender, int refid, int aliasto, bool fromskin=false);