Skip to content

Commit

Permalink
- Duke sound is working again.
Browse files Browse the repository at this point in the history
  • Loading branch information
coelckers authored and mjr4077au committed Nov 26, 2022
1 parent dc9183b commit f68732f
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 79 deletions.
31 changes: 25 additions & 6 deletions source/common/audio/sound/s_sound.cpp
Expand Up @@ -1510,6 +1510,27 @@ FSoundID SoundEngine::FindSoundNoHash(const char* logicalname)
return NO_SOUND;
}

//==========================================================================
//
// S_FindSoundByResIDNoHash
//
// same with resource IDs.
//==========================================================================

FSoundID SoundEngine::FindSoundByResIDNoHash(int resid)
{
unsigned int i;

for (i = 1; i < S_sfx.Size(); i++)
{
if (S_sfx[i].ResourceId == resid)
{
return FSoundID::fromInt(i);
}
}
return NO_SOUND;
}

//==========================================================================
//
// S_FindSoundByLump
Expand Down Expand Up @@ -1550,8 +1571,6 @@ FSoundID SoundEngine::AddSoundLump(const char* logicalname, int lump, int Curren
newsfx.ResourceId = resid;
newsfx.bTentative = false;
auto id = FSoundID::fromInt(S_sfx.Size() - 1);

if (resid >= 0) ResIdMap[resid] = id;
return id;
}

Expand All @@ -1565,12 +1584,12 @@ FSoundID SoundEngine::AddSoundLump(const char* logicalname, int lump, int Curren
// an associated lump is created.
//==========================================================================

FSoundID SoundEngine::FindSoundTentative(const char* name)
FSoundID SoundEngine::FindSoundTentative(const char* name, int nearlimit)
{
auto id = FindSoundNoHash(name);
if (id == NO_SOUND)
{
id = AddSoundLump(name, -1, 0);
id = AddSoundLump(name, -1, 0, -1, nearlimit);
S_sfx[id.index()].bTentative = true;
}
return id;
Expand Down Expand Up @@ -1694,9 +1713,9 @@ void SoundEngine::HashSounds()
S_sfx[i].next = S_sfx[j].index;
S_sfx[j].index = i;

if (S_sfx[j].ResourceId != -1)
if (S_sfx[i].ResourceId != -1)
{
ResIdMap.Insert(S_sfx[j].ResourceId, FSoundID::fromInt(i));
ResIdMap.Insert(S_sfx[i].ResourceId, FSoundID::fromInt(i));
}
}
S_rnd.ShrinkToFit();
Expand Down
7 changes: 2 additions & 5 deletions source/common/audio/sound/s_soundinternal.h
Expand Up @@ -390,18 +390,15 @@ class SoundEngine
FSoundID FindSound(const char* logicalname);
FSoundID FindSoundByResID(int rid);
FSoundID FindSoundNoHash(const char* logicalname);
FSoundID FindSoundByResIDNoHash(int rid);
FSoundID FindSoundByLump(int lump);
virtual FSoundID AddSoundLump(const char* logicalname, int lump, int CurrentPitchMask, int resid = -1, int nearlimit = 2);
FSoundID FindSoundTentative(const char* name);
FSoundID FindSoundTentative(const char* name, int nearlimit = 2);
void CacheRandomSound(sfxinfo_t* sfx);
unsigned int GetMSLength(FSoundID sound);
FSoundID PickReplacement(FSoundID refid);
void HashSounds();
void AddRandomSound(FSoundID Owner, TArray<FSoundID> list);
void RemoveResourceID(int id)
{
ResIdMap.Remove(id);
}

TArray<sfxinfo_t>& GetSounds() //We still need this for a short time...
{
Expand Down
158 changes: 112 additions & 46 deletions source/core/music/s_advsound.cpp
Expand Up @@ -54,7 +54,9 @@ enum SICommands
SI_MidiDevice,
SI_MusicAlias,
SI_ConReserve,
SI_Alias
SI_Alias,
SI_Limit,
SI_Singular
};


Expand Down Expand Up @@ -83,77 +85,59 @@ static const char *SICommandStrings[] =
"$musicalias",
"$conreserve",
"$alias",
"$limit",
"$singular",
NULL
};


static const int DEFAULT_LIMIT = 6;
// CODE --------------------------------------------------------------------

//==========================================================================
//
// S_ReserveSoundSlot
// S_AddSound
//
// Reserves an empty sound slot and assigns it a resource ID and a name
//
// If logical name is already in S_sfx, updates it to use the new sound
// lump. Otherwise, adds the new mapping by using S_AddSoundLump().
//==========================================================================

FSoundID S_ReserveSoundSlot(const char* logicalname, int slotnum, int limit = 6)
static FSoundID S_AddSound(const char* logicalname, int lumpnum, FScanner* sc)
{
auto& S_sfx = soundEngine->GetSounds();

auto sfxid = soundEngine->FindSoundNoHash(logicalname);
FSoundID sfxid = soundEngine->FindSoundNoHash(logicalname);

if (soundEngine->isValidSoundId(sfxid))
if (sfxid.isvalid())
{ // If the sound has already been defined, change the old definition
sfxinfo_t* sfx = soundEngine->GetWritableSfx(sfxid);
auto sfx = soundEngine->GetWritableSfx(sfxid);

if (sfx->ResourceId != -1)
{
// If the name was reseved before, delete that mapping.
soundEngine->RemoveResourceID(sfx->ResourceId);
}
if (sfx->bRandomHeader)
{
FRandomSoundList* rnd = soundEngine->ResolveRandomSound(sfx);
rnd->Choices.Reset();
rnd->Owner = NO_SOUND;
}
sfx->ResourceId = slotnum;
sfx->lumpnum = sfx_empty;
sfx->lumpnum = lumpnum;
sfx->bRandomHeader = false;
sfx->link = sfxinfo_t::NO_LINK;
sfx->bTentative = true;
sfx->bTentative = false;
if (sfx->NearLimit == -1)
{
sfx->NearLimit = 2;
sfx->NearLimit = 6;
sfx->LimitRange = 256 * 256;
}
//sfx->PitchMask = CurrentPitchMask;
}
else
{ // Otherwise, create a new definition.
sfxid = soundEngine->AddSoundLump(logicalname, sfx_empty, 0, slotnum, limit);
sfxid = soundEngine->AddSoundLump(logicalname, lumpnum, 0);
}

return sfxid;
}

//==========================================================================
//
// S_ParseSndInfo
//
// Parses all loaded SNDINFO lumps.
// Also registers Blood SFX files and Strife's voices.
//==========================================================================

void S_ParseSndInfo ()
FSoundID S_AddSound(const char* logicalname, const char* lumpname, FScanner* sc)
{
int lump, lastlump = 0;

while ((lump = fileSystem.FindLumpFullName("engine/mussetting.txt", &lastlump)) >= 0)
{
S_AddSNDINFO (lump);
}
int lump = fileSystem.CheckNumForFullName(lumpname, true, ns_sounds);
return S_AddSound(logicalname, lump, sc);
}

//==========================================================================
Expand All @@ -168,6 +152,7 @@ static void S_AddSNDINFO (int lump)
{
bool skipToEndIf;
TArray<uint32_t> list;
int wantassigns = -1;

FScanner sc(lump);
skipToEndIf = false;
Expand Down Expand Up @@ -262,24 +247,105 @@ static void S_AddSNDINFO (int lump)
}
break;

case SI_Alias: {
// $alias <name of alias> <name of real sound>
FSoundID sfxfrom;

sc.MustGetString ();
sfxfrom = S_AddSound (sc.String, -1, &sc);
sc.MustGetString ();
auto sfx = soundEngine->GetWritableSfx(sfxfrom);
sfx->link = soundEngine->FindSoundTentative (sc.String);
sfx->NearLimit = -1; // Aliases must use the original sound's limit. (Can be changed later)
}
break;

case SI_Limit: {
// $limit <logical name> <max channels> [<distance>]
FSoundID sfxfrom;

sc.MustGetString ();
sfxfrom = soundEngine->FindSoundTentative (sc.String);
sc.MustGetNumber ();
auto sfx = soundEngine->GetWritableSfx(sfxfrom);
sfx->NearLimit = min(max(sc.Number, 0), 255);
if (sc.CheckFloat())
{
sfx->LimitRange = float(sc.Float * sc.Float);
}
}
break;

case SI_Singular: {
// $singular <logical name>
FSoundID sfx;

sc.MustGetString ();
sfx = soundEngine->FindSoundTentative (sc.String, DEFAULT_LIMIT);
auto sfxp = soundEngine->GetWritableSfx(sfx);
sfxp->bSingular = true;
}
break;


case SI_ConReserve: {
sc.MustGetNumber();
int num = sc.Number;
sc.MustGetStringName("=");
// $conreserve <logical name> <resource id>
// Assigns a resource ID to the given sound.
sc.MustGetString();
FString name = sc.String;
int limit = 6;
if (sc.CheckString(","))
FSoundID sfx = soundEngine->FindSoundTentative(sc.String, DEFAULT_LIMIT);
auto sfxp = soundEngine->GetWritableSfx(sfx);

sc.MustGetNumber();
sfxp->ResourceId = sc.Number;
break;
}

default:
{ // Got a logical sound mapping
FString name (sc.String);
if (wantassigns == -1)
{
sc.MustGetNumber();
limit = sc.Number;
wantassigns = sc.CheckString("=");
}
S_ReserveSoundSlot(name, num, limit);
}
else if (wantassigns)
{
sc.MustGetStringName("=");
}

sc.MustGetString ();
S_AddSound (name, sc.String, &sc);
}

}

}
}
}

//==========================================================================
//
// S_ParseSndInfo
//
// Parses all loaded SNDINFO lumps.
//
//==========================================================================

void S_ParseSndInfo()
{
int lump;

soundEngine->Clear();
MusicAliases.Clear();
MidiDevices.Clear();
MusicVolumes.Clear();

S_AddSound("{ no sound }", "DSEMPTY", nullptr); // Sound 0 is no sound at all

int lastlump = 0;
while ((lump = fileSystem.FindLump("SNDINFO", &lastlump, false)) != -1)
{
S_AddSNDINFO(lump);
}
soundEngine->HashSounds();
}

47 changes: 38 additions & 9 deletions source/games/duke/src/sounds.cpp
Expand Up @@ -201,20 +201,50 @@ int S_GetUserFlags(FSoundID soundid)

int S_DefineSound(unsigned index, const char *filename, int minpitch, int maxpitch, int priority, int type, int distance, float volume)
{
FSoundID s_index = soundEngine->FindSoundByResID(index);
sfxinfo_t* sfx;
FSoundID s_index = soundEngine->FindSoundByResIDNoHash(index); // cannot use the hash while editing.
if (!s_index.isvalid())
{
// If the slot isn't defined, give it a meaningful name containing the index.
sfx = soundEngine->AllocateSound();
sfx->name.Format("ConSound@%04d", index);
sfx->ResourceId = index;
s_index = soundEngine->FindSoundTentative(FStringf("ConSound@%04d", index));
}
else sfx = soundEngine->GetWritableSfx(s_index);
auto sfx = soundEngine->GetWritableSfx(s_index);

// Check if we are allowed to override this sound.
// If it is still tentative, it's ok. Otherwise check if SF_CONDEFINED is set, This means it was defined by CON and may be overwritten.
// If the sound was defined by other means we leave it alone, but set the flags to defaults, if they are not present.

bool settable = sfx->bTentative;

if (!settable)
{
if (sfx->UserData.Size() >= kMaxUserData)
{
auto& sndinf = sfx->UserData;
settable = !!(sndinf[kFlags] & SF_CONDEFINED);
}
}
if (!settable)
{
if (sfx->UserData.Size() < kMaxUserData)
{
// sound is defined but has no userdata.
// This means it was defined through SNDINFO without setting extended properties and should not be overwritten.
// Set everything to 0 to have default handling.
sfx->UserData.Resize(kMaxUserData);
auto& sndinf = sfx->UserData;
sndinf[kPitchStart] = 0;
sndinf[kPitchEnd] = 0;
sndinf[kPriority] = 0; // Raze's sound engine does not use this.
sndinf[kVolAdjust] = 0;
sndinf[kWorldTourMapping] = 0;
sndinf[kFlags] = 0;
}
}

sfx->ResourceId = index;
sfx->UserData.Resize(kMaxUserData);
auto sndinf = sfx->UserData.Data();
sndinf[kFlags] = type & ~SF_ONEINST_INTERNAL;
auto& sndinf = sfx->UserData;
sndinf[kFlags] = (type & ~SF_ONEINST_INTERNAL) | SF_CONDEFINED;
if (sndinf[kFlags] & SF_LOOP)
sndinf[kFlags] |= SF_ONEINST_INTERNAL;

Expand All @@ -239,7 +269,6 @@ int S_DefineSound(unsigned index, const char *filename, int minpitch, int maxpit
sfx->Volume = volume;
//sfx->NearLimit = index == TELEPORTER + 1? 6 : 0; // the teleporter sound cannot be unlimited due to how it gets used.
sfx->bTentative = false;
sfx->name = std::move(fn);
return 0;
}

Expand Down
1 change: 1 addition & 0 deletions source/games/duke/src/sounds.h
Expand Up @@ -19,6 +19,7 @@ enum {
SF_ADULT = 8,
SF_GLOBAL = 16,
SF_ONEINST_INTERNAL = 32,
SF_CONDEFINED = 64,

SF_DTAG = 128,
};
Expand Down

0 comments on commit f68732f

Please sign in to comment.