Sound cleanups

git-svn-id: https://svn.eduke32.com/eduke32@7915 1a8010ca-5511-0410-912e-c29ae57300e0

# Conflicts:
#	source/duke3d/src/sounds.h
This commit is contained in:
terminx 2019-08-07 22:44:29 +00:00 committed by Christoph Oelckers
parent 48f9edf743
commit b7f349cd92
2 changed files with 62 additions and 57 deletions

View file

@ -30,17 +30,18 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#define DQSIZE 256 #define DQSIZE 256
int32_t g_numEnvSoundsPlaying, g_highestSoundIdx = 0; int32_t g_numEnvSoundsPlaying, g_highestSoundIdx;
static int32_t MusicIsWaveform = 0; static char *MusicPtr;
static char *MusicPtr = NULL;
static int32_t MusicIsWaveform;
static int32_t MusicVoice = -1; static int32_t MusicVoice = -1;
static bool MusicPaused = false;
static bool SoundPaused = false;
std::atomic<uint32_t> dnum; static bool MusicPaused;
uint32_t dq[DQSIZE]; static bool SoundPaused;
static mutex_t m_callback;
std::atomic<uint32_t> dnum, dq[DQSIZE];
// static mutex_t m_callback;
static inline void S_SetProperties(assvoice_t *snd, int const owner, int const voice, int const dist, int const clock) static inline void S_SetProperties(assvoice_t *snd, int const owner, int const voice, int const dist, int const clock)
{ {
@ -88,7 +89,7 @@ void S_SoundStartup(void)
S_MusicVolume(ud.config.MusicVolume); S_MusicVolume(ud.config.MusicVolume);
FX_SetReverseStereo(ud.config.ReverseStereo); FX_SetReverseStereo(ud.config.ReverseStereo);
mutex_init(&m_callback); // mutex_init(&m_callback);
FX_SetCallBack(S_Callback); FX_SetCallBack(S_Callback);
FX_SetPrintf(OSD_Printf); FX_SetPrintf(OSD_Printf);
} }
@ -387,7 +388,7 @@ void S_StopMusic(void)
void S_Cleanup(void) void S_Cleanup(void)
{ {
static uint32_t ldnum; static uint32_t ldnum = 0;
while (ldnum < dnum) while (ldnum < dnum)
{ {
@ -396,8 +397,10 @@ void S_Cleanup(void)
// negative index is RTS playback // negative index is RTS playback
if ((int32_t)num < 0) if ((int32_t)num < 0)
{ {
if (rts_lumplockbyte[-(int32_t)num] >= 200) int const rtsindex = klabs((int32_t)num);
rts_lumplockbyte[-(int32_t)num]--;
if (rts_lumplockbyte[rtsindex] >= 200)
--rts_lumplockbyte[rtsindex];
continue; continue;
} }
@ -411,19 +414,18 @@ void S_Cleanup(void)
continue; continue;
} }
int const vidx = num & (MAXSOUNDINSTANCES - 1); int const voiceindex = num & (MAXSOUNDINSTANCES - 1);
num = (num - vidx) / MAXSOUNDINSTANCES; num = (num - voiceindex) / MAXSOUNDINSTANCES;
auto &snd = g_sounds[num]; auto &snd = g_sounds[num];
auto &voice = g_sounds[num].voices[vidx]; auto &voice = snd.voices[voiceindex];
int const spriteNum = voice.owner; int const spriteNum = voice.owner;
if (EDUKE32_PREDICT_FALSE(snd.num > MAXSOUNDINSTANCES)) if (EDUKE32_PREDICT_FALSE(snd.num > MAXSOUNDINSTANCES))
OSD_Printf(OSD_ERROR "S_Cleanup(): num exceeds MAXSOUNDINSTANCES! g_sounds[%d].num %d wtf?\n", num, snd.num); OSD_Printf(OSD_ERROR "S_Cleanup(): num exceeds MAXSOUNDINSTANCES! g_sounds[%d].num %d wtf?\n", num, snd.num);
else if (snd.num > 0)
if (snd.num > 0)
--snd.num; --snd.num;
// MUSICANDSFX uses t_data[0] to control restarting the sound // MUSICANDSFX uses t_data[0] to control restarting the sound
@ -467,7 +469,8 @@ int32_t S_LoadSound(int num)
void cacheAllSounds(void) void cacheAllSounds(void)
{ {
for (int32_t i = 0, j = 0; i <= g_highestSoundIdx; ++i) for (int i=0, j=0; i <= g_highestSoundIdx; ++i)
{
if (g_sounds[i].ptr == 0) if (g_sounds[i].ptr == 0)
{ {
j++; j++;
@ -477,6 +480,7 @@ void cacheAllSounds(void)
S_LoadSound(i); S_LoadSound(i);
} }
} }
}
static inline int S_GetPitch(int num) static inline int S_GetPitch(int num)
{ {
@ -540,12 +544,10 @@ static inline int S_GetAngle(int ang, const vec3_t *cam, const vec3_t *pos)
return (2048 + ang - getangle(cam->x - pos->x, cam->y - pos->y)) & 2047; return (2048 + ang - getangle(cam->x - pos->x, cam->y - pos->y)) & 2047;
} }
static bool S_CalcDistAndAng(int32_t spriteNum, int32_t soundNum, int32_t sectNum, int32_t angle, static int S_CalcDistAndAng(int spriteNum, int soundNum, int sectNum, int angle,
const vec3_t *cam, const vec3_t *pos, const vec3_t *cam, const vec3_t *pos, int *distPtr, int *angPtr)
int32_t *distPtr, int32_t *angPtr)
{ {
int32_t sndang = 0, sndist = 0; int sndang = 0, sndist = 0, explosion = 0;
bool explosion = false;
if (PN(spriteNum) == APLAYER && P_Get(spriteNum) == screenpeek) if (PN(spriteNum) == APLAYER && P_Get(spriteNum) == screenpeek)
goto sound_further_processing; goto sound_further_processing;
@ -590,13 +592,19 @@ sound_further_processing:
if (sndist < 0) if (sndist < 0)
sndist = 0; sndist = 0;
if (sectNum > -1 && sndist && PN(spriteNum) != MUSICANDSFX #ifndef EDUKE32_STANDALONE
if (!FURY && sectNum > -1 && sndist && PN(spriteNum) != MUSICANDSFX
&& !cansee(cam->x, cam->y, cam->z - (24 << 8), sectNum, SX(spriteNum), SY(spriteNum), SZ(spriteNum) - (24 << 8), SECT(spriteNum))) && !cansee(cam->x, cam->y, cam->z - (24 << 8), sectNum, SX(spriteNum), SY(spriteNum), SZ(spriteNum) - (24 << 8), SECT(spriteNum)))
sndist += sndist>>5; sndist += sndist>>5;
#else
UNREFERENCED_PARAMETER(sectNum);
#endif
if ((g_sounds[soundNum].m & (SF_GLOBAL|SF_DTAG)) == (SF_GLOBAL|SF_DTAG)) if ((g_sounds[soundNum].m & (SF_GLOBAL|SF_DTAG)) == (SF_GLOBAL|SF_DTAG))
{ {
#ifndef EDUKE32_STANDALONE
boost: boost:
#endif
explosion = true; explosion = true;
if (sndist > 6144) if (sndist > 6144)
sndist = 6144; sndist = 6144;
@ -815,17 +823,15 @@ int S_PlaySound(int num)
} }
snd.num++; snd.num++;
snd.voices[sndnum].owner = -1; S_SetProperties(&snd.voices[sndnum], -1, voice, 255-LOUDESTVOLUME, 0);
snd.voices[sndnum].id = voice;
snd.voices[sndnum].dist = 255 - LOUDESTVOLUME;
snd.voices[sndnum].clock = 0;
return voice; return voice;
} }
int A_PlaySound(int soundNum, int spriteNum) int A_PlaySound(int soundNum, int spriteNum)
{ {
if (EDUKE32_PREDICT_FALSE((unsigned)soundNum > (unsigned)g_highestSoundIdx)) return -1; if (EDUKE32_PREDICT_FALSE((unsigned)soundNum > (unsigned)g_highestSoundIdx))
return -1;
return (unsigned)spriteNum >= MAXSPRITES ? S_PlaySound(soundNum) : return (unsigned)spriteNum >= MAXSPRITES ? S_PlaySound(soundNum) :
S_PlaySound3D(soundNum, spriteNum, (vec3_t *)&sprite[spriteNum]); S_PlaySound3D(soundNum, spriteNum, (vec3_t *)&sprite[spriteNum]);
@ -833,35 +839,36 @@ int A_PlaySound(int soundNum, int spriteNum)
void S_StopEnvSound(int32_t num, int32_t i) void S_StopEnvSound(int32_t num, int32_t i)
{ {
if (EDUKE32_PREDICT_FALSE((unsigned)num > (unsigned)g_highestSoundIdx) || g_sounds[num].num <= 0) if (EDUKE32_PREDICT_FALSE((unsigned)num > (unsigned)g_highestSoundIdx)
|| g_sounds[num].num <= 0)
return; return;
int32_t j;
do
{
for (j=0; j<MAXSOUNDINSTANCES; ++j)
{
S_Cleanup(); S_Cleanup();
if ((i == -1 && g_sounds[num].voices[j].id > FX_Ok) || (i != -1 && g_sounds[num].voices[j].owner == i)) for (int j=0; j<MAXSOUNDINSTANCES; ++j)
{
auto &voice = g_sounds[num].voices[j];
if ((i == -1 && voice.id > FX_Ok) || (i != -1 && voice.owner == i))
{ {
#ifdef DEBUGGINGAIDS #ifdef DEBUGGINGAIDS
if (EDUKE32_PREDICT_FALSE(i >= 0 && g_sounds[num].voices[j].id <= FX_Ok)) if (EDUKE32_PREDICT_FALSE(i >= 0 && voice.id <= FX_Ok))
initprintf(OSD_ERROR "S_StopEnvSound(): bad voice %d for sound ID %d index %d!\n", g_sounds[num].voices[j].id, num, j); initprintf(OSD_ERROR "S_StopEnvSound(): bad voice %d for sound ID %d index %d!\n", voice.id, num, j);
else else
#endif #endif
if (g_sounds[num].voices[j].id > FX_Ok) if (voice.id > FX_Ok)
{ {
if (FX_SoundActive(g_sounds[num].voices[j].id)) if (FX_SoundActive(voice.id))
FX_StopSound(g_sounds[num].voices[j].id); {
break; FX_StopSound(voice.id);
S_Cleanup();
}
j = 0;
continue;
} }
} }
} }
} }
while (j < MAXSOUNDINSTANCES);
}
// Do not remove this or make it inline. // Do not remove this or make it inline.
void S_StopAllSounds(void) void S_StopAllSounds(void)
@ -937,7 +944,7 @@ void S_Update(void)
if ((unsigned)spriteNum >= MAXSPRITES || voice.id <= FX_Ok || !FX_SoundActive(voice.id)) if ((unsigned)spriteNum >= MAXSPRITES || voice.id <= FX_Ok || !FX_SoundActive(voice.id))
continue; continue;
int32_t sndist, sndang; int sndist, sndang;
S_CalcDistAndAng(spriteNum, sndnum, cs, ca, c, (const vec3_t *)&sprite[spriteNum], &sndist, &sndang); S_CalcDistAndAng(spriteNum, sndnum, cs, ca, c, (const vec3_t *)&sprite[spriteNum], &sndist, &sndang);
@ -959,11 +966,9 @@ void S_Callback(intptr_t num)
if (num == MUSIC_ID) if (num == MUSIC_ID)
return; return;
mutex_lock(&m_callback); // mutex_lock(&m_callback);
int const ldnum = dnum; dq[dnum++ & (DQSIZE - 1)] = num;
dq[ldnum & (DQSIZE - 1)] = num; // mutex_unlock(&m_callback);
dnum++;
mutex_unlock(&m_callback);
} }
void S_ClearSoundLocks(void) void S_ClearSoundLocks(void)
@ -984,7 +989,7 @@ void S_ClearSoundLocks(void)
#endif #endif
} }
bool A_CheckSoundPlaying(int spriteNum, int soundNum) int A_CheckSoundPlaying(int spriteNum, int soundNum)
{ {
if (EDUKE32_PREDICT_FALSE((unsigned)soundNum > (unsigned)g_highestSoundIdx)) return 0; if (EDUKE32_PREDICT_FALSE((unsigned)soundNum > (unsigned)g_highestSoundIdx)) return 0;
@ -999,7 +1004,7 @@ bool A_CheckSoundPlaying(int spriteNum, int soundNum)
} }
// Check if actor <i> is playing any sound. // Check if actor <i> is playing any sound.
bool A_CheckAnySoundPlaying(int spriteNum) int A_CheckAnySoundPlaying(int spriteNum)
{ {
int const msp = g_highestSoundIdx; int const msp = g_highestSoundIdx;
@ -1013,7 +1018,7 @@ bool A_CheckAnySoundPlaying(int spriteNum)
return 0; return 0;
} }
bool S_CheckSoundPlaying(int soundNum) int S_CheckSoundPlaying(int soundNum)
{ {
if (EDUKE32_PREDICT_FALSE((unsigned)soundNum > (unsigned)g_highestSoundIdx)) return false; if (EDUKE32_PREDICT_FALSE((unsigned)soundNum > (unsigned)g_highestSoundIdx)) return false;
return (g_sounds[soundNum].num != 0); return (g_sounds[soundNum].num != 0);

View file

@ -63,11 +63,11 @@ extern char g_soundlocks[MAXSOUNDS];
extern sound_t g_sounds[MAXSOUNDS]; extern sound_t g_sounds[MAXSOUNDS];
extern int32_t g_numEnvSoundsPlaying,g_highestSoundIdx; extern int32_t g_numEnvSoundsPlaying,g_highestSoundIdx;
bool A_CheckSoundPlaying(int spriteNum,int soundNum); int A_CheckSoundPlaying(int spriteNum,int soundNum);
int A_PlaySound(int soundNum, int spriteNum); int A_PlaySound(int soundNum, int spriteNum);
void S_Callback(intptr_t num); void S_Callback(intptr_t num);
bool A_CheckAnySoundPlaying(int spriteNum); int A_CheckAnySoundPlaying(int spriteNum);
bool S_CheckSoundPlaying(int soundNum); int S_CheckSoundPlaying(int soundNum);
void S_Cleanup(void); void S_Cleanup(void);
void S_ClearSoundLocks(void); void S_ClearSoundLocks(void);
int32_t S_LoadSound(uint32_t num); int32_t S_LoadSound(uint32_t num);