Skip to content

Commit

Permalink
- give Blood's sounds well defined names so they can be defined via S…
Browse files Browse the repository at this point in the history
…NDINFO.
  • Loading branch information
coelckers committed Jan 15, 2023
1 parent 841402a commit bb7f009
Show file tree
Hide file tree
Showing 8 changed files with 974 additions and 26 deletions.
1 change: 1 addition & 0 deletions source/common/audio/sound/s_soundinternal.h
Expand Up @@ -97,6 +97,7 @@ constexpr FSoundID INVALID_SOUND = FSoundID::fromInt(-1);
bool bUsed = false;
bool bSingular = false;
bool bTentative = true;
bool bExternal = false;

TArray<int> UserData;

Expand Down
4 changes: 2 additions & 2 deletions source/core/savegamehelp.cpp
Expand Up @@ -106,8 +106,8 @@ static void SerializeGlobals(FSerializer& arc)

static void SerializeSession(FSerializer& arc)
{
// In Duke we now have reliable sound names.
if (isDukeEngine()) arc.SetUniqueSoundNames();
// In Duke and Blood we now have reliable sound names.
if (isDukeEngine() || isBlood()) arc.SetUniqueSoundNames();

arc.ReadObjects(false);
SerializeMap(arc);
Expand Down
2 changes: 1 addition & 1 deletion source/games/blood/src/seq.cpp
Expand Up @@ -386,7 +386,7 @@ void SEQINST::Update()
if (snd.isvalid())
{
auto udata = soundEngine->GetUserData(snd);
int relVol = udata ? udata[2] : 255;
int relVol = udata ? udata[0] : 255;
sfxPlay3DSoundCP(actor, sndId, -1, 0, 0, (surfSfxMove[surf][2] != relVol) ? relVol : surfSfxMove[surf][3]);
}
}
Expand Down
11 changes: 5 additions & 6 deletions source/games/blood/src/sfx.cpp
Expand Up @@ -147,13 +147,12 @@ void GameInterface::UpdateSounds()
//
//---------------------------------------------------------------------------

FSoundID getSfx(FSoundID soundId, float& attenuation, int& pitch, int& relvol)
FSoundID getSfx(FSoundID soundId, float& attenuation, int& relvol)
{
auto udata = soundEngine->GetUserData(soundId);
if (pitch < 0) pitch = udata ? udata[0] : 0x10000;

if (relvol < 0) relvol = 0;
else if (relvol == 0) relvol = udata && udata[2] ? udata[2] : 80;
else if (relvol == 0) relvol = udata && udata[0] ? udata[0] : 80;
if (relvol > 255) relvol = 255;
// Limit the attenuation. More than 2.0 is simply too much.
attenuation = relvol > 0 ? clamp(80.f / relvol, 0.f, 2.f) : 1.f;
Expand All @@ -177,7 +176,7 @@ void sfxPlay3DSound(const DVector3& pos, int soundId, sectortype* pSector)
float attenuation;
int pitch = -1;
int relvol = 0;
sid = getSfx(sid, attenuation, pitch, relvol);
sid = getSfx(sid, attenuation, relvol);
auto sfx = soundEngine->GetSfx(sid);
EChanFlags flags = CHANF_OVERLAP;
if (sfx && sfx->LoopStart >= 0) flags |= CHANF_LOOP;
Expand All @@ -194,14 +193,14 @@ void sfxPlay3DSound(const DVector3& pos, int soundId, sectortype* pSector)

void sfxPlay3DSoundCP(DBloodActor* pActor, int soundId, int playchannel, int playflags, int pitch, int volume)
{
if (!SoundEnabled() || soundId < 0 || !pActor) return;
if (!SoundEnabled() || soundId <= 0 || !pActor) return;
auto sid = soundEngine->FindSoundByResID(soundId);
if (!sid.isvalid()) return;

auto svec = GetSoundPos(pActor->spr.pos);

float attenuation;
sid = getSfx(sid, attenuation, pitch, volume);
sid = getSfx(sid, attenuation, volume);
if (volume == -1) volume = 80;

if (playchannel >= 0)
Expand Down
47 changes: 34 additions & 13 deletions source/games/blood/src/sound.cpp
Expand Up @@ -62,24 +62,42 @@ void ByteSwapSFX(SFX* pSFX)
//
// S_AddBloodSFX
//
// Registers a new sound with the name "<lumpname>.sfx"
// Actual sound data is searched for in the ns_bloodraw namespace.
//
//==========================================================================

static void S_AddBloodSFX(int lumpnum)
{
FSoundID sfxnum;

int resid = fileSystem.GetResourceId(lumpnum);
sfxinfo_t* soundfx = nullptr;

sfxnum = soundEngine->FindSoundByResIDNoHash(resid);
if (sfxnum.isvalid())
{
soundfx = soundEngine->GetWritableSfx(sfxnum);
if (soundfx->UserData.Size() == 0)
{
soundfx->UserData.Resize(1);
soundfx->UserData[1] = 80; // default for RelVol
}
if (!soundfx->bTentative) return; // sound was already defined.
}

auto sfxlump = fileSystem.ReadFile(lumpnum);
SFX* sfx = (SFX*)sfxlump.GetMem();
ByteSwapSFX(sfx);

FStringf rawname("%s.raw", sfx->rawName);
auto rawlump = fileSystem.FindFile(rawname);
FSoundID sfxnum;

if (rawlump != -1)
{
sfxnum = soundEngine->AddSoundLump(sfx->rawName, rawlump, 0, fileSystem.GetResourceId(lumpnum), 6);
auto soundfx = soundEngine->GetWritableSfx(sfxnum);
if (!sfxnum.isvalid())
{
sfxnum = soundEngine->AddSoundLump(FStringf("SfxSound@%04d", resid), rawlump, 0, resid, 6); // use a generic name here in case sound replacements are being used.
soundfx = soundEngine->GetWritableSfx(sfxnum);
soundfx->UserData.Resize(1);
}
if (sfx->format < 5 || sfx->format > 12)
{ // [0..4] + invalid formats
soundfx->RawRate = 11025;
Expand All @@ -92,14 +110,18 @@ static void S_AddBloodSFX(int lumpnum)
{ // [9..12]
soundfx->RawRate = 44100;
}
soundfx->NearLimit = 6;
soundfx->lumpnum = rawlump;
soundfx->bLoadRAW = true;
soundfx->bExternal = true;
soundfx->bTentative = false;
soundfx->LoopStart = LittleLong(sfx->loopStart);
//S_sfx[sfxnum].Volume = sfx->relVol / 255.f; This cannot be done because this volume setting is optional.
soundfx->UserData.Resize(3);
// pitchrange is unused.
if (sfx->pitch != 0x10000) soundfx->DefPitch = sfx->pitch / 65536.f;
else soundfx->DefPitch = 0;
int* udata = (int*)soundfx->UserData.Data();
udata[0] = sfx->pitch;
udata[1] = sfx->pitchRange;
udata[2] = sfx->relVol;
udata[0] = sfx->relVol;
}
}

Expand All @@ -122,8 +144,7 @@ void sndInit(void)
auto type = fileSystem.GetResourceType(i);
if (!stricmp(type, "SFX"))
{
if (soundEngine->FindSoundByResID(fileSystem.GetResourceId(i)) == NO_SOUND)
S_AddBloodSFX(i);
S_AddBloodSFX(i);
}
else if (!stricmp(type, "WAV") || !stricmp(type, "OGG") || !stricmp(type, "FLAC") || !stricmp(type, "VOC"))
{
Expand Down Expand Up @@ -193,7 +214,7 @@ void sndStartSample(unsigned int nSound, int nVolume, int nChannel, bool bLoop,
if (nVolume < 0)
{
auto udata = soundEngine->GetUserData(snd);
if (udata) nVolume = min(Scale(udata[2], 255, 100), 255);
if (udata) nVolume = min(Scale(udata[0], 255, 100), 255);
else nVolume = 255;
}
if (bLoop) chanflags |= CHANF_LOOP;
Expand Down
5 changes: 3 additions & 2 deletions source/games/duke/src/sounds.cpp
Expand Up @@ -209,7 +209,7 @@ int S_DefineSound(unsigned index, const char *filename, int minpitch, int maxpit
if (sfx->UserData.Size() >= kMaxUserData)
{
auto& sndinf = sfx->UserData;
settable = !!(sndinf[kFlags] & SF_CONDEFINED);
settable = sfx->bExternal;
}
}
if (!settable)
Expand All @@ -229,8 +229,9 @@ int S_DefineSound(unsigned index, const char *filename, int minpitch, int maxpit

sfx->ResourceId = index;
sfx->UserData.Resize(kMaxUserData);
sfx->bExternal = true;
auto& sndinf = sfx->UserData;
sndinf[kFlags] = (type & SF_CON_MASK) | SF_CONDEFINED;
sndinf[kFlags] = (type & SF_CON_MASK);

// Take care of backslashes in sound names. Also double backslashes which occur in World Tour.
FString fn = filename;
Expand Down
3 changes: 1 addition & 2 deletions source/games/duke/src/sounds.h
Expand Up @@ -18,10 +18,9 @@ enum {
SF_TALK = 4,
SF_ADULT = 8,
SF_GLOBAL = 16,
SF_CON_MASK = 31,
SF_CONDEFINED = 64,

SF_DTAG = 128,
SF_CON_MASK = 159,
};

enum esound_t
Expand Down

0 comments on commit bb7f009

Please sign in to comment.