Skip to content

Commit

Permalink
- made more use of FSoundID.
Browse files Browse the repository at this point in the history
  • Loading branch information
coelckers committed Nov 24, 2022
1 parent 225399a commit 7e8de9d
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 104 deletions.
6 changes: 3 additions & 3 deletions src/common/audio/sound/s_sound.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -1695,13 +1695,13 @@ void SoundEngine::HashSounds()
S_rnd.ShrinkToFit();
}

void SoundEngine::AddRandomSound(int Owner, TArray<uint32_t> list)
void SoundEngine::AddRandomSound(FSoundID Owner, TArray<FSoundID> 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;
}
Expand Down
118 changes: 59 additions & 59 deletions src/common/audio/sound/s_soundinternal.h
Expand Up @@ -2,60 +2,13 @@

#include "i_sound.h"

struct FRandomSoundList
{
TArray<uint32_t> Choices;
uint32_t Owner = 0;
};

enum
{
sfx_empty = -1
};



//
// 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<int> 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
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -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<FSoundID> 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<int> 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
Expand Down Expand Up @@ -418,7 +418,7 @@ class SoundEngine
unsigned int GetMSLength(FSoundID sound);
int PickReplacement(int refid);
void HashSounds();
void AddRandomSound(int Owner, TArray<uint32_t> list);
void AddRandomSound(FSoundID Owner, TArray<FSoundID> list);
};


Expand Down
18 changes: 9 additions & 9 deletions src/playsim/actor.h
Expand Up @@ -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;
Expand Down
39 changes: 12 additions & 27 deletions src/sound/s_advsound.cpp
Expand Up @@ -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 ----------------------------------------------

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -635,7 +635,7 @@ void S_AddLocalSndInfo(int lump)
static void S_AddSNDINFO (int lump)
{
bool skipToEndIf;
TArray<uint32_t> list;
TArray<FSoundID> list;
int wantassigns = -1;

FScanner sc(lump);
Expand Down Expand Up @@ -804,7 +804,7 @@ static void S_AddSNDINFO (int lump)
// $playercompat <player class> <gender> <logical name> <compat sound name>
FString pclass;
int gender, refid;
int sfxfrom, aliasto;
FSoundID sfxfrom, aliasto;

S_ParsePlayerSoundCommon (sc, pclass, gender, refid);
sfxfrom = S_AddSound (sc.String, -1, &sc);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);

Expand All @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
10 changes: 4 additions & 6 deletions src/sound/s_sound.h
Expand Up @@ -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);
Expand Down

0 comments on commit 7e8de9d

Please sign in to comment.