- refactored sound user data to be easier to serialize.

This commit is contained in:
Christoph Oelckers 2020-02-23 18:30:48 +01:00
parent 83adb81756
commit 3aea6d1fad
12 changed files with 60 additions and 55 deletions

View File

@ -94,7 +94,7 @@ void ambProcess(void)
else if (pChannel->distance > 0)
{
FVector3 pt{};
soundEngine->StartSound(SOURCE_Ambient, pChannel, &pt, CHAN_BODY, CHANF_LOOP, pChannel->soundID, pChannel->distance / 255.f, ATTN_NONE);
soundEngine->StartSound(SOURCE_Ambient, pChannel, &pt, CHAN_BODY, CHANF_LOOP|CHANF_TRANSIENT, pChannel->soundID, pChannel->distance / 255.f, ATTN_NONE);
}
pChannel->distance = 0;
}

View File

@ -836,7 +836,6 @@ void WeaponLoadSaveConstruct(void);
#ifdef NOONE_EXTENSIONS
void NNLoadSaveConstruct(void);
#endif
void ASoundLoadSaveConstruct(void);
void LoadSaveSetup(void)
{
@ -858,7 +857,6 @@ void LoadSaveSetup(void)
#ifdef NOONE_EXTENSIONS
NNLoadSaveConstruct();
#endif
ASoundLoadSaveConstruct();
}
END_BLD_NS

View File

@ -332,7 +332,7 @@ void SEQINST::Update(ACTIVE *pActive)
auto snd = soundEngine->FindSoundByResID(sndId);
if (snd > 0)
{
auto udata = (int*)soundEngine->GetUserData(snd);
auto udata = soundEngine->GetUserData(snd);
int relVol = udata ? udata[2] : 255;
sfxPlay3DSoundCP(pSprite, sndId, -1, 0, 0, (surfSfxMove[surf][2] != relVol) ? relVol : surfSfxMove[surf][3]);
}

View File

@ -133,7 +133,7 @@ void sfxUpdate3DSounds(void)
FSoundID getSfx(FSoundID soundId, float &attenuation, int &pitch, int &relvol)
{
auto udata = (int*)soundEngine->GetUserData(soundId);
auto udata = soundEngine->GetUserData(soundId);
if (pitch < 0) pitch = udata ? udata[0] : 0x10000;
if (relvol < 0) relvol = udata && udata[2] ? udata[2] : 80;

View File

@ -90,7 +90,7 @@ static void S_AddBloodSFX(int lumpnum)
S_sfx[sfxnum].bLoadRAW = true;
S_sfx[sfxnum].LoopStart = LittleLong(sfx->loopStart);
//S_sfx[sfxnum].Volume = sfx->relVol / 255.f; This cannot be done because this volume setting is optional.
S_sfx[sfxnum].UserData.Resize(8);
S_sfx[sfxnum].UserData.Resize(2);
int* udata = (int*)S_sfx[sfxnum].UserData.Data();
udata[0] = sfx->pitch;
udata[1] = sfx->pitchRange;
@ -159,7 +159,7 @@ void sndStartSample(unsigned int nSound, int nVolume, int nChannel, bool bLoop)
{
if (nVolume < 0)
{
auto udata = (int*)soundEngine->GetUserData(snd);
auto udata = soundEngine->GetUserData(snd);
if (udata) nVolume = udata[2];
else nVolume = 255;
}

View File

@ -1479,7 +1479,7 @@ FSerializer &Serialize(FSerializer &arc, const char *key, FSoundID &sid, FSoundI
{
#if 1
int id = sid;
Serialize(arc, key, sid, def);
Serialize(arc, key, id, nullptr);
if (arc.isReading()) sid = FSoundID(id);
#else
if (arc.isWriting())

View File

@ -31,6 +31,7 @@ enum EChanFlag
CHANF_NOSTOP = 4096, // only for A_PlaySound. Does not start if channel is playing something.
CHANF_OVERLAP = 8192, // [MK] Does not stop any sounds in the channel and instead plays over them.
CHANF_ENDED = 16384, // Helper to detect broken ChannelEnded implementations.
CHANF_TRANSIENT = 32768, // Do not record in savegames - used for sounds that get restarted outside the sound system (e.g. ambients in SW and Blood)
};
typedef TFlags<EChanFlag> EChanFlags;

View File

@ -37,7 +37,7 @@ struct sfxinfo_t
unsigned bSingular:1;
unsigned bTentative:1;
TArray<uint8_t> UserData;
TArray<int> UserData;
int RawRate; // Sample rate to use when bLoadRAW is true
@ -369,7 +369,7 @@ public:
{
S_rnd.Clear();
}
void *GetUserData(int snd)
int *GetUserData(int snd)
{
return S_sfx[snd].UserData.Data();
}

View File

@ -100,11 +100,10 @@ void cacheAllSounds(void)
static inline int S_GetPitch(int num)
{
auto const* snd = (sound_t*)soundEngine->GetUserData(num + 1);
auto const* snd = soundEngine->GetUserData(num + 1);
if (!snd) return 0;
int const range = abs(snd->pitchEnd - snd->pitchStart);
return (range == 0) ? snd->pitchStart : min(snd->pitchStart, snd->pitchEnd) + rand() % range;
int const range = abs(snd[kPitchEnd] - snd[kPitchStart]);
return (range == 0) ? snd[kPitchStart] : min(snd[kPitchStart], snd[kPitchEnd]) + rand() % range;
}
float S_ConvertPitch(int lpitch)
@ -115,9 +114,9 @@ float S_ConvertPitch(int lpitch)
int S_GetUserFlags(int num)
{
if (!soundEngine->isValidSoundId(num+1)) return 0;
auto const* snd = (sound_t*)soundEngine->GetUserData(num + 1);
auto const* snd = soundEngine->GetUserData(num + 1);
if (!snd) return 0;
return snd->flags;
return snd[kFlags];
}
//==========================================================================
@ -144,17 +143,17 @@ int S_DefineSound(unsigned index, const char *filename, int minpitch, int maxpit
}
auto sfx = &S_sfx[index];
bool alreadydefined = !sfx->bTentative;
sfx->UserData.Resize(sizeof(sound_t));
auto sndinf = (sound_t*)sfx->UserData.Data();
sndinf->flags = type & ~SF_ONEINST_INTERNAL;
if (sndinf->flags & SF_LOOP)
sndinf->flags |= SF_ONEINST_INTERNAL;
sfx->UserData.Resize(kMaxUserData);
auto sndinf = sfx->UserData.Data();
sndinf[kFlags] = type & ~SF_ONEINST_INTERNAL;
if (sndinf[kFlags] & SF_LOOP)
sndinf[kFlags] |= SF_ONEINST_INTERNAL;
sfx->lumpnum = S_LookupSound(filename);
sndinf->pitchStart = clamp(minpitch, INT16_MIN, INT16_MAX);
sndinf->pitchEnd = clamp(maxpitch, INT16_MIN, INT16_MAX);
sndinf->priority = priority & 255;
sndinf->volAdjust = clamp(distance, INT16_MIN, INT16_MAX);
sndinf[kPitchStart] = clamp(minpitch, INT16_MIN, INT16_MAX);
sndinf[kPitchEnd] = clamp(maxpitch, INT16_MIN, INT16_MAX);
sndinf[kPriority] = priority & 255;
sndinf[kVolAdjust] = clamp(distance, INT16_MIN, INT16_MAX);
sfx->Volume = volume;
sfx->NearLimit = 6;
sfx->bTentative = false;
@ -176,9 +175,9 @@ static int S_CalcDistAndAng(int spriteNum, int soundNum, int sectNum,
// However, ultimately rolloff would also just reposition the sound source so this can remain as it is.
int orgsndist = 0, sndang = 0, sndist = 0, explosion = 0;
auto const* snd = (sound_t*)soundEngine->GetUserData(soundNum+1);
int userflags = snd? snd->flags : 0;
int dist_adjust = snd? snd->volAdjust : 0;
auto const* snd = soundEngine->GetUserData(soundNum+1);
int userflags = snd? snd[kFlags] : 0;
int dist_adjust = snd? snd[kVolAdjust] : 0;
if (PN(spriteNum) != APLAYER || P_Get(spriteNum) != screenpeek)
{

View File

@ -39,11 +39,15 @@ BEGIN_DUKE_NS
#define MAXSOUNDS 4096
#define LOUDESTVOLUME 111
typedef struct
enum esound_t
{
int pitchStart, pitchEnd, volAdjust;
int priority, flags;
} sound_t;
kPitchStart,
kPitchEnd,
kVolAdjust,
kPriority,
kFlags,
kMaxUserData
};
int A_CheckSoundPlaying(int spriteNum, int soundNum, int channel = 0);
int A_PlaySound(int soundNum, int spriteNum, int channel = CHAN_AUTO, EChanFlags flags = 0);

View File

@ -99,11 +99,10 @@ void cacheAllSounds(void)
static inline int S_GetPitch(int num)
{
auto const* snd = (sound_t*)soundEngine->GetUserData(num+1);
auto const* snd = soundEngine->GetUserData(num+1);
if (!snd) return 0;
int const range = abs(snd->pitchEnd - snd->pitchStart);
return (range == 0) ? snd->pitchStart : min(snd->pitchStart, snd->pitchEnd) + rand() % range;
int const range = abs(snd[kPitchEnd] - snd[kPitchStart]);
return (range == 0) ? snd[kPitchStart] : min(snd[kPitchStart], snd[kPitchEnd]) + rand() % range;
}
float S_ConvertPitch(int lpitch)
@ -114,9 +113,9 @@ float S_ConvertPitch(int lpitch)
int S_GetUserFlags(int num)
{
if (!soundEngine->isValidSoundId(num+1)) return 0;
auto const* snd = (sound_t*)soundEngine->GetUserData(num + 1);
auto const* snd = soundEngine->GetUserData(num + 1);
if (!snd) return 0;
return snd->flags;
return snd[kFlags];
}
//==========================================================================
@ -143,17 +142,17 @@ int S_DefineSound(unsigned index, const char *filename, int minpitch, int maxpit
}
auto sfx = &S_sfx[index];
bool alreadydefined = !sfx->bTentative;
sfx->UserData.Resize(sizeof(sound_t));
auto sndinf = (sound_t*)sfx->UserData.Data();
sndinf->flags = type & ~SF_ONEINST_INTERNAL;
if (sndinf->flags & SF_LOOP)
sndinf->flags |= SF_ONEINST_INTERNAL;
sfx->UserData.Resize(kMaxUserData);
auto sndinf = sfx->UserData.Data();
sndinf[kFlags] = type & ~SF_ONEINST_INTERNAL;
if (sndinf[kFlags] & SF_LOOP)
sndinf[kFlags] |= SF_ONEINST_INTERNAL;
sfx->lumpnum = S_LookupSound(filename);
sndinf->pitchStart = clamp(minpitch, INT16_MIN, INT16_MAX);
sndinf->pitchEnd = clamp(maxpitch, INT16_MIN, INT16_MAX);
sndinf->priority = priority & 255;
sndinf->volAdjust = clamp(distance, INT16_MIN, INT16_MAX);
sndinf[kPitchStart] = clamp(minpitch, INT16_MIN, INT16_MAX);
sndinf[kPitchEnd] = clamp(maxpitch, INT16_MIN, INT16_MAX);
sndinf[kPriority] = priority & 255;
sndinf[kVolAdjust] = clamp(distance, INT16_MIN, INT16_MAX);
sfx->Volume = volume;
sfx->NearLimit = 6;
sfx->bTentative = false;
@ -175,9 +174,9 @@ static int S_CalcDistAndAng(int spriteNum, int soundNum, int sectNum,
// However, ultimately rolloff would also just reposition the sound source so this can remain as it is.
int orgsndist = 0, sndang = 0, sndist = 0, explosion = 0;
auto const* snd = (sound_t*)soundEngine->GetUserData(soundNum + 1);
int userflags = snd ? snd->flags : 0;
int dist_adjust = snd ? snd->volAdjust : 0;
auto const* snd = soundEngine->GetUserData(soundNum + 1);
int userflags = snd ? snd[kFlags] : 0;
int dist_adjust = snd ? snd[kVolAdjust] : 0;
if (PN(spriteNum) != APLAYER || P_Get(spriteNum) != screenpeek)
{

View File

@ -39,11 +39,15 @@ BEGIN_RR_NS
#define MAXSOUNDS 4096
#define LOUDESTVOLUME 111
typedef struct
enum esound_t
{
int pitchStart, pitchEnd, volAdjust;
int priority, flags;
} sound_t;
kPitchStart,
kPitchEnd,
kVolAdjust,
kPriority,
kFlags,
kMaxUserData
};
int A_CheckSoundPlaying(int spriteNum, int soundNum, int channel = 0);
int A_PlaySound(int soundNum, int spriteNum, int channel = CHAN_AUTO, EChanFlags flags = 0);