Skip to content

Commit

Permalink
- fixed code up to the point where everything compiles again. Duke so…
Browse files Browse the repository at this point in the history
…unds are currently non-functional.

All Duke script code has been changed to use strings as sound names now, just like GZDoom these will be looked up at compile time.
Original Duke sound indices still exist but are now being managed as resource IDs, not sound engine indices anymore.
  • Loading branch information
coelckers authored and mjr4077au committed Nov 26, 2022
1 parent d0b9ff0 commit dc9183b
Show file tree
Hide file tree
Showing 36 changed files with 1,184 additions and 1,123 deletions.
8 changes: 8 additions & 0 deletions source/common/audio/sound/s_sound.cpp
Expand Up @@ -1681,6 +1681,7 @@ void SoundEngine::HashSounds()

S_sfx.ShrinkToFit();
size = S_sfx.Size();
ResIdMap.Clear();

// Mark all buckets as empty
for (i = 0; i < size; i++)
Expand All @@ -1692,8 +1693,15 @@ void SoundEngine::HashSounds()
j = MakeKey(S_sfx[i].name) % size;
S_sfx[i].next = S_sfx[j].index;
S_sfx[j].index = i;

if (S_sfx[j].ResourceId != -1)
{
ResIdMap.Insert(S_sfx[j].ResourceId, FSoundID::fromInt(i));
}
}
S_rnd.ShrinkToFit();


}

void SoundEngine::AddRandomSound(FSoundID Owner, TArray<FSoundID> list)
Expand Down
4 changes: 4 additions & 0 deletions source/common/audio/sound/s_soundinternal.h
Expand Up @@ -398,6 +398,10 @@ class SoundEngine
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
1 change: 1 addition & 0 deletions source/core/gamecontrol.cpp
Expand Up @@ -1093,6 +1093,7 @@ int RunGame()

StartWindow->Progress();
I_InitSound();
gi->StartSoundEngine();
StartWindow->Progress();
Mus_InitMusic();
S_ParseSndInfo();
Expand Down
1 change: 1 addition & 0 deletions source/core/gamestruct.h
Expand Up @@ -127,6 +127,7 @@ struct GameInterface
virtual void AddQAVInterpProps(const int res_id, const FString& interptype, const bool loopable, const TMap<int, TArray<int>>&& ignoredata) { }
virtual void RemoveQAVInterpProps(const int res_id) { }
virtual bool WantEscape() { return false; }
virtual void StartSoundEngine() = 0;

virtual FString statFPS()
{
Expand Down
11 changes: 5 additions & 6 deletions source/core/music/s_advsound.cpp
Expand Up @@ -97,16 +97,15 @@ static const char *SICommandStrings[] =
//
//==========================================================================

int S_ReserveSoundSlot(const char* logicalname, int slotnum, int limit = 6)
FSoundID S_ReserveSoundSlot(const char* logicalname, int slotnum, int limit = 6)
{
auto& S_sfx = soundEngine->GetSounds();
int sfxid;

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

if (sfxid > 0 && (unsigned int)sfxid < S_sfx.Size())
if (soundEngine->isValidSoundId(sfxid))
{ // If the sound has already been defined, change the old definition
sfxinfo_t* sfx = &S_sfx[sfxid];
sfxinfo_t* sfx = soundEngine->GetWritableSfx(sfxid);

if (sfx->ResourceId != -1)
{
Expand All @@ -117,7 +116,7 @@ int S_ReserveSoundSlot(const char* logicalname, int slotnum, int limit = 6)
{
FRandomSoundList* rnd = soundEngine->ResolveRandomSound(sfx);
rnd->Choices.Reset();
rnd->Owner = 0;
rnd->Owner = NO_SOUND;
}
sfx->ResourceId = slotnum;
sfx->lumpnum = sfx_empty;
Expand Down
5 changes: 5 additions & 0 deletions source/core/raze_sound.h
Expand Up @@ -38,6 +38,11 @@ int S_ReserveSoundSlot(const char* logicalname, int slotnum, int limit = 6);
class RazeSoundEngine : public SoundEngine
{
public:
RazeSoundEngine()
{
// add the empty sound right now.
AddSoundLump("no_sound", fileSystem.CheckNumForFullName("engine/dsempty.lmp"), 0);
}
virtual bool SourceIsActor(FSoundChan* chan) { return chan->SourceType == SOURCE_Actor; }
virtual int SoundSourceIndex(FSoundChan* chan) { return 0; }
virtual void SetSource(FSoundChan* chan, int index) {}
Expand Down
1 change: 1 addition & 0 deletions source/games/blood/src/blood.h
Expand Up @@ -147,6 +147,7 @@ struct GameInterface : public ::GameInterface
bool IsQAVInterpTypeValid(const FString& type) override;
void AddQAVInterpProps(const int res_id, const FString& interptype, const bool loopable, const TMap<int, TArray<int>>&& ignoredata) override;
void RemoveQAVInterpProps(const int res_id) override;
void StartSoundEngine() override;

GameStats getStats() override;
};
Expand Down
6 changes: 5 additions & 1 deletion source/games/blood/src/sound.cpp
Expand Up @@ -109,9 +109,13 @@ static void S_AddBloodSFX(int lumpnum)
//
//---------------------------------------------------------------------------

void sndInit(void)
void GameInterface::StartSoundEngine()
{
soundEngine = new BloodSoundEngine;
}

void sndInit(void)
{
soundEngine->AddSoundLump("", 0, 0, -1, 6); // add a dummy entry at index #0
for (int i = fileSystem.GetNumEntries() - 1; i >= 0; i--)
{
Expand Down
1 change: 1 addition & 0 deletions source/games/duke/src/duke3d.h
Expand Up @@ -63,6 +63,7 @@ struct GameInterface : public ::GameInterface
void AddExcludedEpisode(const FString& episode) override;
int GetCurrentSkill() override;
bool WantEscape() override;
void StartSoundEngine() override;

};

Expand Down
4 changes: 1 addition & 3 deletions source/games/duke/src/game.cpp
Expand Up @@ -364,9 +364,7 @@ void GameInterface::app_init()
connectpoint2[0] = -1;

SetDispatcher();
S_InitSound();



loadcons();
fi.initactorflags();
duke_menufont->Callback(); // depends on the .CON files so it must be after loadcons
Expand Down
1 change: 1 addition & 0 deletions source/games/duke/src/gamedef.cpp
Expand Up @@ -3239,6 +3239,7 @@ void loadcons()
// These can only be retrieved AFTER loading the scripts.
FinalizeGameVars();
S_WorldTourMappingsForOldSounds(); // create a sound mapping for World Tour.
soundEngine->HashSounds();
S_CacheAllSounds();
comp.setmusic();

Expand Down
87 changes: 39 additions & 48 deletions source/games/duke/src/sounds.cpp
Expand Up @@ -92,8 +92,8 @@ class DukeSoundEngine : public RazeSoundEngine
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;
}

}
void StopChannel(FSoundChan* chan) override
{
if (chan && chan->SysChannel != NULL && !(chan->ChanFlags & CHANF_EVICTED) && chan->SourceType == SOURCE_Actor)
Expand Down Expand Up @@ -122,18 +122,18 @@ class DukeSoundEngine : public RazeSoundEngine

};

void S_InitSound()
void GameInterface::StartSoundEngine()
{
soundEngine = new DukeSoundEngine;
}

static int GetReplacementSound(int soundNum)
static FSoundID GetReplacementSound(FSoundID soundNum)
{
if (wt_forcevoc && isWorldTour() && soundEngine->isValidSoundId(FSoundID::fromInt(soundNum+1)))
if (wt_forcevoc && isWorldTour() && soundEngine->isValidSoundId(soundNum))
{
auto const* snd = soundEngine->GetUserData(FSoundID::fromInt(soundNum + 1));
auto const* snd = soundEngine->GetUserData(soundNum);
int sndx = snd[kWorldTourMapping];
if (sndx > 0) soundNum = sndx-1;
if (sndx > 0) soundNum = FSoundID::fromInt(sndx);
}
return soundNum;
}
Expand Down Expand Up @@ -172,9 +172,8 @@ void S_CacheAllSounds(void)
//
//==========================================================================

static inline int S_GetPitch(FSoundID num)
static inline int S_GetPitch(FSoundID soundid)
{
auto soundid = FSoundID::fromInt(num + 1);
auto const* snd = soundEngine->GetUserData(soundid);
if (!snd) return 0;
int const range = abs(snd[kPitchEnd] - snd[kPitchStart]);
Expand All @@ -186,9 +185,8 @@ float S_ConvertPitch(int lpitch)
return powf(2, lpitch / 1200.f); // I hope I got this right that ASS uses a linear scale where 1200 is a full octave.
}

int S_GetUserFlags(int num)
int S_GetUserFlags(FSoundID soundid)
{
auto soundid = FSoundID::fromInt(num + 1);
if (!soundEngine->isValidSoundId(soundid)) return 0;
auto const* snd = soundEngine->GetUserData(soundid);
if (!snd) return 0;
Expand All @@ -203,14 +201,17 @@ int S_GetUserFlags(int num)

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

auto sfx = &soundEngine->GetSounds()[s_index];
sfx->UserData.Resize(kMaxUserData);
auto sndinf = sfx->UserData.Data();
sndinf[kFlags] = type & ~SF_ONEINST_INTERNAL;
Expand Down Expand Up @@ -254,13 +255,12 @@ inline bool S_IsAmbientSFX(DDukeActor* actor)
//
//==========================================================================

static int GetPositionInfo(DDukeActor* actor, FSoundID soundID, sectortype* sect,
static int GetPositionInfo(DDukeActor* actor, FSoundID soundid, sectortype* sect,
const DVector3 &cam, const DVector3 &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.

auto soundid = FSoundID::fromInt(soundNum + 1);
int orgsndist = 0, sndist = 0;
auto const* snd = soundEngine->GetUserData(soundid);
int userflags = snd ? snd[kFlags] : 0;
Expand Down Expand Up @@ -364,7 +364,7 @@ void DukeSoundEngine::CalcPosVel(int type, const void* source, const float pt[3]
auto aactor = (DDukeActor*)source;
if (aactor != nullptr)
{
GetPositionInfo(aactor, chanSound.index() - 1, camsect, campos, aactor->spr.pos, nullptr, pos);
GetPositionInfo(aactor, chanSound, camsect, campos, aactor->spr.pos, nullptr, pos);
/*
if (vel) // DN3D does not properly maintain this.
{
Expand Down Expand Up @@ -422,21 +422,19 @@ void GameInterface::UpdateSounds(void)
//
//==========================================================================

int S_PlaySound3D(int sndnum, DDukeActor* actor, const DVector3& pos, int channel, EChanFlags flags)
int S_PlaySound3D(FSoundID soundid, DDukeActor* actor, const DVector3& pos, int channel, EChanFlags flags)
{
auto soundid = FSoundID::fromInt(sndnum + 1);
auto const pl = &ps[myconnectindex];
if (!soundEngine->isValidSoundId(soundid) || !SoundEnabled() || actor == nullptr || !playrunning() ||
(pl->timebeforeexit > 0 && pl->timebeforeexit <= REALGAMETICSPERSEC * 3)) return -1;

sndnum = GetReplacementSound(sndnum);
soundid = FSoundID::fromInt(sndnum + 1);
int userflags = S_GetUserFlags(sndnum);
soundid = GetReplacementSound(soundid);
int userflags = S_GetUserFlags(soundid);

if ((userflags & (SF_DTAG | SF_GLOBAL)) == SF_DTAG)
{
// Duke-Tag sound does not play in 3D.
return S_PlaySound(sndnum);
return S_PlaySound(soundid);
}

if (userflags & SF_TALK)
Expand All @@ -445,7 +443,7 @@ int S_PlaySound3D(int sndnum, DDukeActor* actor, const DVector3& pos, int channe
bool foundone = soundEngine->EnumerateChannels([&](FSoundChan* chan)
{
auto sid = chan->OrgID;
auto flags = S_GetUserFlags(sid.index() - 1);
auto flags = S_GetUserFlags(sid);
return !!(flags & SF_TALK);
});
// don't play if any Duke talk sounds are already playing
Expand All @@ -466,10 +464,12 @@ int S_PlaySound3D(int sndnum, DDukeActor* actor, const DVector3& pos, int channe
sectortype* camsect;

S_GetCamera(&campos, nullptr, &camsect);
GetPositionInfo(actor, sndnum, camsect, campos, pos, &sndist, &sndpos);
int pitch = S_GetPitch(sndnum);
GetPositionInfo(actor, soundid, camsect, campos, pos, &sndist, &sndpos);
int pitch = S_GetPitch(soundid);

bool explosion = ((userflags & (SF_GLOBAL | SF_DTAG)) == (SF_GLOBAL | SF_DTAG)) || ((sndnum == PIPEBOMB_EXPLODE || sndnum == LASERTRIP_EXPLODE || sndnum == RPG_EXPLODE));
auto sfx = soundEngine->GetSfx(soundid);
bool explosion = ((userflags & (SF_GLOBAL | SF_DTAG)) == (SF_GLOBAL | SF_DTAG)) ||
((sfx->ResourceId == PIPEBOMB_EXPLODE || sfx->ResourceId == LASERTRIP_EXPLODE || sfx->ResourceId == RPG_EXPLODE));

bool underwater = ps[screenpeek].insector() && ps[screenpeek].cursector->lotag == ST_2_UNDERWATER;
if (explosion)
Expand All @@ -488,7 +488,7 @@ int S_PlaySound3D(int sndnum, DDukeActor* actor, const DVector3& pos, int channe

bool is_playing = soundEngine->GetSoundPlayingInfo(SOURCE_Any, nullptr, soundid);
if (is_playing && !issoundcontroller(actor))
S_StopSound(sndnum, actor);
S_StopSound(soundid, actor);

int const repeatp = (userflags & SF_LOOP);

Expand Down Expand Up @@ -518,19 +518,17 @@ int S_PlaySound3D(int sndnum, DDukeActor* actor, const DVector3& pos, int channe
//
//==========================================================================

int S_PlaySound(int sndnum, int channel, EChanFlags flags, float vol)
int S_PlaySound(FSoundID soundid, int channel, EChanFlags flags, float vol)
{
auto soundid = FSoundID::fromInt(sndnum + 1);
if (!soundEngine->isValidSoundId(soundid) || !SoundEnabled()) return -1;

sndnum = GetReplacementSound(sndnum);
soundid = FSoundID::fromInt(sndnum + 1);
soundid = GetReplacementSound(soundid);

int userflags = S_GetUserFlags(sndnum);
int userflags = S_GetUserFlags(soundid);
if ((!(snd_speech & 1) && (userflags & SF_TALK)))
return -1;

int const pitch = S_GetPitch(sndnum);
int const pitch = S_GetPitch(soundid);

if (userflags & SF_LOOP) flags |= CHANF_LOOP;
if (currentCommentarySound != NO_SOUND) vol *= 0.25f;
Expand All @@ -545,17 +543,16 @@ int S_PlaySound(int sndnum, int channel, EChanFlags flags, float vol)
//
//==========================================================================

int S_PlayActorSound(int soundNum, DDukeActor* actor, int channel, EChanFlags flags)
int S_PlayActorSound(FSoundID soundNum, DDukeActor* actor, int channel, EChanFlags flags)
{
return (actor == nullptr ? S_PlaySound(soundNum, channel, flags) :
S_PlaySound3D(soundNum, actor, actor->spr.pos, channel, flags));
}

void S_StopSound(int sndNum, DDukeActor* actor, int channel)
void S_StopSound(FSoundID soundid, DDukeActor* actor, int channel)
{
sndNum = GetReplacementSound(sndNum);
soundid = GetReplacementSound(soundid);

auto soundid = FSoundID::fromInt(sndNum + 1);
if (!actor) soundEngine->StopSoundID(soundid);
else
{
Expand All @@ -568,11 +565,8 @@ void S_StopSound(int sndNum, DDukeActor* actor, int channel)
}
}

void S_ChangeSoundPitch(int soundNum, DDukeActor* actor, int pitchoffset)
void S_ChangeSoundPitch(FSoundID soundid, DDukeActor* actor, int pitchoffset)
{
soundNum = GetReplacementSound(soundNum);
auto soundid = FSoundID::fromInt(soundNum + 1);

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 (!actor)
{
Expand All @@ -590,10 +584,9 @@ void S_ChangeSoundPitch(int soundNum, DDukeActor* actor, int pitchoffset)
//
//==========================================================================

int S_CheckActorSoundPlaying(DDukeActor* actor, int soundNum, int channel)
int S_CheckActorSoundPlaying(DDukeActor* actor, FSoundID soundid, int channel)
{
soundNum = GetReplacementSound(soundNum);
auto soundid = FSoundID::fromInt(soundNum + 1);
soundid = GetReplacementSound(soundid);

if (actor == nullptr) return soundEngine->GetSoundPlayingInfo(SOURCE_Any, nullptr, soundid);
return soundEngine->IsSourcePlayingSomething(SOURCE_Actor, actor, channel, soundid);
Expand All @@ -606,10 +599,8 @@ int S_CheckAnyActorSoundPlaying(DDukeActor* actor)
return soundEngine->IsSourcePlayingSomething(SOURCE_Actor, actor, CHAN_AUTO);
}

int S_CheckSoundPlaying(int soundNum)
int S_CheckSoundPlaying(FSoundID soundid)
{
soundNum = GetReplacementSound(soundNum);
auto soundid = FSoundID::fromInt(soundNum + 1);
return soundEngine->GetSoundPlayingInfo(SOURCE_Any, nullptr, soundid);
}

Expand Down

0 comments on commit dc9183b

Please sign in to comment.