- 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) else if (pChannel->distance > 0)
{ {
FVector3 pt{}; 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; pChannel->distance = 0;
} }

View file

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

View file

@ -332,7 +332,7 @@ void SEQINST::Update(ACTIVE *pActive)
auto snd = soundEngine->FindSoundByResID(sndId); auto snd = soundEngine->FindSoundByResID(sndId);
if (snd > 0) if (snd > 0)
{ {
auto udata = (int*)soundEngine->GetUserData(snd); auto udata = soundEngine->GetUserData(snd);
int relVol = udata ? udata[2] : 255; int relVol = udata ? udata[2] : 255;
sfxPlay3DSoundCP(pSprite, sndId, -1, 0, 0, (surfSfxMove[surf][2] != relVol) ? relVol : surfSfxMove[surf][3]); 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) 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 (pitch < 0) pitch = udata ? udata[0] : 0x10000;
if (relvol < 0) relvol = udata && udata[2] ? udata[2] : 80; 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].bLoadRAW = true;
S_sfx[sfxnum].LoopStart = LittleLong(sfx->loopStart); 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].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(); int* udata = (int*)S_sfx[sfxnum].UserData.Data();
udata[0] = sfx->pitch; udata[0] = sfx->pitch;
udata[1] = sfx->pitchRange; udata[1] = sfx->pitchRange;
@ -159,7 +159,7 @@ void sndStartSample(unsigned int nSound, int nVolume, int nChannel, bool bLoop)
{ {
if (nVolume < 0) if (nVolume < 0)
{ {
auto udata = (int*)soundEngine->GetUserData(snd); auto udata = soundEngine->GetUserData(snd);
if (udata) nVolume = udata[2]; if (udata) nVolume = udata[2];
else nVolume = 255; else nVolume = 255;
} }

View file

@ -1479,7 +1479,7 @@ FSerializer &Serialize(FSerializer &arc, const char *key, FSoundID &sid, FSoundI
{ {
#if 1 #if 1
int id = sid; int id = sid;
Serialize(arc, key, sid, def); Serialize(arc, key, id, nullptr);
if (arc.isReading()) sid = FSoundID(id); if (arc.isReading()) sid = FSoundID(id);
#else #else
if (arc.isWriting()) 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_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_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_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; typedef TFlags<EChanFlag> EChanFlags;

View file

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

View file

@ -100,11 +100,10 @@ void cacheAllSounds(void)
static inline int S_GetPitch(int num) 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; if (!snd) return 0;
int const range = abs(snd->pitchEnd - snd->pitchStart); int const range = abs(snd[kPitchEnd] - snd[kPitchStart]);
return (range == 0) ? snd[kPitchStart] : min(snd[kPitchStart], snd[kPitchEnd]) + rand() % range;
return (range == 0) ? snd->pitchStart : min(snd->pitchStart, snd->pitchEnd) + rand() % range;
} }
float S_ConvertPitch(int lpitch) float S_ConvertPitch(int lpitch)
@ -115,9 +114,9 @@ float S_ConvertPitch(int lpitch)
int S_GetUserFlags(int num) int S_GetUserFlags(int num)
{ {
if (!soundEngine->isValidSoundId(num+1)) return 0; 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; if (!snd) return 0;
return snd->flags; return snd[kFlags];
} }
//========================================================================== //==========================================================================
@ -126,7 +125,7 @@ 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_DefineSound(unsigned index, const char* filename, int minpitch, int maxpitch, int priority, int type, int distance, float volume)
{ {
if ((unsigned)index >= MAXSOUNDS) if ((unsigned)index >= MAXSOUNDS)
return -1; return -1;
@ -144,17 +143,17 @@ int S_DefineSound(unsigned index, const char *filename, int minpitch, int maxpit
} }
auto sfx = &S_sfx[index]; auto sfx = &S_sfx[index];
bool alreadydefined = !sfx->bTentative; bool alreadydefined = !sfx->bTentative;
sfx->UserData.Resize(sizeof(sound_t)); sfx->UserData.Resize(kMaxUserData);
auto sndinf = (sound_t*)sfx->UserData.Data(); auto sndinf = sfx->UserData.Data();
sndinf->flags = type & ~SF_ONEINST_INTERNAL; sndinf[kFlags] = type & ~SF_ONEINST_INTERNAL;
if (sndinf->flags & SF_LOOP) if (sndinf[kFlags] & SF_LOOP)
sndinf->flags |= SF_ONEINST_INTERNAL; sndinf[kFlags] |= SF_ONEINST_INTERNAL;
sfx->lumpnum = S_LookupSound(filename); sfx->lumpnum = S_LookupSound(filename);
sndinf->pitchStart = clamp(minpitch, INT16_MIN, INT16_MAX); sndinf[kPitchStart] = clamp(minpitch, INT16_MIN, INT16_MAX);
sndinf->pitchEnd = clamp(maxpitch, INT16_MIN, INT16_MAX); sndinf[kPitchEnd] = clamp(maxpitch, INT16_MIN, INT16_MAX);
sndinf->priority = priority & 255; sndinf[kPriority] = priority & 255;
sndinf->volAdjust = clamp(distance, INT16_MIN, INT16_MAX); sndinf[kVolAdjust] = clamp(distance, INT16_MIN, INT16_MAX);
sfx->Volume = volume; sfx->Volume = volume;
sfx->NearLimit = 6; sfx->NearLimit = 6;
sfx->bTentative = false; 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. // 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; int orgsndist = 0, sndang = 0, sndist = 0, explosion = 0;
auto const* snd = (sound_t*)soundEngine->GetUserData(soundNum+1); auto const* snd = soundEngine->GetUserData(soundNum+1);
int userflags = snd? snd->flags : 0; int userflags = snd? snd[kFlags] : 0;
int dist_adjust = snd? snd->volAdjust : 0; int dist_adjust = snd? snd[kVolAdjust] : 0;
if (PN(spriteNum) != APLAYER || P_Get(spriteNum) != screenpeek) if (PN(spriteNum) != APLAYER || P_Get(spriteNum) != screenpeek)
{ {

View file

@ -39,11 +39,15 @@ BEGIN_DUKE_NS
#define MAXSOUNDS 4096 #define MAXSOUNDS 4096
#define LOUDESTVOLUME 111 #define LOUDESTVOLUME 111
typedef struct enum esound_t
{ {
int pitchStart, pitchEnd, volAdjust; kPitchStart,
int priority, flags; kPitchEnd,
} sound_t; kVolAdjust,
kPriority,
kFlags,
kMaxUserData
};
int A_CheckSoundPlaying(int spriteNum, int soundNum, int channel = 0); int A_CheckSoundPlaying(int spriteNum, int soundNum, int channel = 0);
int A_PlaySound(int soundNum, int spriteNum, int channel = CHAN_AUTO, EChanFlags flags = 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) 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; if (!snd) return 0;
int const range = abs(snd->pitchEnd - snd->pitchStart); int const range = abs(snd[kPitchEnd] - snd[kPitchStart]);
return (range == 0) ? snd[kPitchStart] : min(snd[kPitchStart], snd[kPitchEnd]) + rand() % range;
return (range == 0) ? snd->pitchStart : min(snd->pitchStart, snd->pitchEnd) + rand() % range;
} }
float S_ConvertPitch(int lpitch) float S_ConvertPitch(int lpitch)
@ -114,9 +113,9 @@ float S_ConvertPitch(int lpitch)
int S_GetUserFlags(int num) int S_GetUserFlags(int num)
{ {
if (!soundEngine->isValidSoundId(num+1)) return 0; 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; 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]; auto sfx = &S_sfx[index];
bool alreadydefined = !sfx->bTentative; bool alreadydefined = !sfx->bTentative;
sfx->UserData.Resize(sizeof(sound_t)); sfx->UserData.Resize(kMaxUserData);
auto sndinf = (sound_t*)sfx->UserData.Data(); auto sndinf = sfx->UserData.Data();
sndinf->flags = type & ~SF_ONEINST_INTERNAL; sndinf[kFlags] = type & ~SF_ONEINST_INTERNAL;
if (sndinf->flags & SF_LOOP) if (sndinf[kFlags] & SF_LOOP)
sndinf->flags |= SF_ONEINST_INTERNAL; sndinf[kFlags] |= SF_ONEINST_INTERNAL;
sfx->lumpnum = S_LookupSound(filename); sfx->lumpnum = S_LookupSound(filename);
sndinf->pitchStart = clamp(minpitch, INT16_MIN, INT16_MAX); sndinf[kPitchStart] = clamp(minpitch, INT16_MIN, INT16_MAX);
sndinf->pitchEnd = clamp(maxpitch, INT16_MIN, INT16_MAX); sndinf[kPitchEnd] = clamp(maxpitch, INT16_MIN, INT16_MAX);
sndinf->priority = priority & 255; sndinf[kPriority] = priority & 255;
sndinf->volAdjust = clamp(distance, INT16_MIN, INT16_MAX); sndinf[kVolAdjust] = clamp(distance, INT16_MIN, INT16_MAX);
sfx->Volume = volume; sfx->Volume = volume;
sfx->NearLimit = 6; sfx->NearLimit = 6;
sfx->bTentative = false; 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. // 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; int orgsndist = 0, sndang = 0, sndist = 0, explosion = 0;
auto const* snd = (sound_t*)soundEngine->GetUserData(soundNum + 1); auto const* snd = soundEngine->GetUserData(soundNum + 1);
int userflags = snd ? snd->flags : 0; int userflags = snd ? snd[kFlags] : 0;
int dist_adjust = snd ? snd->volAdjust : 0; int dist_adjust = snd ? snd[kVolAdjust] : 0;
if (PN(spriteNum) != APLAYER || P_Get(spriteNum) != screenpeek) if (PN(spriteNum) != APLAYER || P_Get(spriteNum) != screenpeek)
{ {

View file

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