sound system clean up (#971)

* Explicitly use OpenAL buffer IDs instead of void pointers.

* Free the `openal_sources` array on shutdown.

* Remove more unused code
This commit is contained in:
Roman Fomin 2023-04-05 23:18:03 +07:00 committed by GitHub
parent 639069d399
commit b09678d980
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 57 deletions

View File

@ -2220,7 +2220,7 @@ void deh_procSounds(DEHFILE *fpin, FILE* fpout, char *line)
S_sfx[indexnum].priority = value;
else
if (!strcasecmp(key,deh_sfxinfo[3])) // Zero 1
S_sfx[indexnum].link = (sfxinfo_t *)(intptr_t)value;
; // ignored
else
if (!strcasecmp(key,deh_sfxinfo[4])) // Zero 2
S_sfx[indexnum].pitch = value;
@ -2229,13 +2229,13 @@ void deh_procSounds(DEHFILE *fpin, FILE* fpout, char *line)
S_sfx[indexnum].volume = value;
else
if (!strcasecmp(key,deh_sfxinfo[6])) // Zero 4
S_sfx[indexnum].data = (void *)(intptr_t) value; // killough 5/3/98: changed cast
; // ignored
else
if (!strcasecmp(key,deh_sfxinfo[7])) // Neg. One 1
S_sfx[indexnum].usefulness = value;
; // ignored
else
if (!strcasecmp(key,deh_sfxinfo[8])) // Neg. One 2
S_sfx[indexnum].lumpnum = value;
; // ignored
else
if (fpout) fprintf(fpout,
"Invalid sound string index for '%s'\n",key);

View File

@ -64,8 +64,8 @@ typedef struct {
// SFX id of the playing sound effect.
// Used to catch duplicates (like chainsaw).
sfxinfo_t *sfx;
// The channel data pointer.
ALuint *data;
boolean enabled;
// haleyjd 06/16/08: unique id number
int idnum;
} channel_info_t;
@ -83,43 +83,18 @@ float steptable[256];
//
static void StopChannel(int channel)
{
int cnum;
#ifdef RANGECHECK
// haleyjd 02/18/05: bounds checking
if (channel < 0 || channel >= MAX_CHANNELS)
return;
#endif
if (channelinfo[channel].data)
if (channelinfo[channel].enabled)
{
alSourceStop(openal_sources[channel]);
// [FG] immediately free samples not connected to a sound SFX
if (channelinfo[channel].sfx == NULL)
{
free(channelinfo[channel].data);
}
channelinfo[channel].data = NULL;
if (channelinfo[channel].sfx)
{
// haleyjd 06/03/06: see if we can free the sound
for (cnum = 0; cnum < MAX_CHANNELS; cnum++)
{
if (cnum == channel)
continue;
if (channelinfo[cnum].sfx &&
channelinfo[cnum].sfx->data == channelinfo[channel].sfx->data)
{
return; // still being used by some channel
}
}
}
channelinfo[channel].enabled = false;
}
channelinfo[channel].sfx = NULL;
}
#define SOUNDHDRSIZE 8
@ -166,12 +141,12 @@ static boolean CacheSound(sfxinfo_t *sfx, int channel)
}
// haleyjd 06/03/06: rewrote again to make sound data properly freeable
while (sfx->data == NULL)
while (sfx->cached == false)
{
byte *sampledata;
ALsizei size, freq;
ALenum format;
ALuint *buffer;
ALuint buffer;
// haleyjd: this should always be called (if lump is already loaded,
// W_CacheLumpNum handles that for us).
@ -207,15 +182,15 @@ static boolean CacheSound(sfxinfo_t *sfx, int channel)
sampledata = wavdata;
}
buffer = malloc(sizeof(*buffer));
alGenBuffers(1, buffer);
alBufferData(*buffer, format, sampledata, size, freq);
alGenBuffers(1, &buffer);
alBufferData(buffer, format, sampledata, size, freq);
if (alGetError() != AL_NO_ERROR)
{
fprintf(stderr, "CacheSound: Error buffering data.\n");
break;
}
sfx->data = buffer;
sfx->buffer = buffer;
sfx->cached = true;
}
// don't need original lump data any more
@ -228,7 +203,7 @@ static boolean CacheSound(sfxinfo_t *sfx, int channel)
free(wavdata);
}
if (sfx->data == NULL)
if (sfx->cached == false)
{
sfx->lumpnum = -2; // [FG] don't try again
return false;
@ -237,7 +212,7 @@ static boolean CacheSound(sfxinfo_t *sfx, int channel)
// Preserve sound SFX id
channelinfo[channel].sfx = sfx;
channelinfo[channel].data = sfx->data;
channelinfo[channel].enabled = true;
return true;
}
@ -368,7 +343,7 @@ int I_StartSound(sfxinfo_t *sound, int vol, int sep, int pitch, boolean loop)
// haleyjd 06/03/06: look for an unused hardware channel
for (channel = 0; channel < MAX_CHANNELS; channel++)
{
if (channelinfo[channel].data == NULL)
if (channelinfo[channel].enabled == false)
break;
}
@ -380,7 +355,7 @@ int I_StartSound(sfxinfo_t *sound, int vol, int sep, int pitch, boolean loop)
if (CacheSound(sound, channel))
{
ALuint source = openal_sources[channel];
ALuint buffer = *channelinfo[channel].data;
ALuint buffer = channelinfo[channel].sfx->buffer;
channelinfo[channel].idnum = id++; // give the sound a unique id
I_UpdateSoundParams(channel, vol, sep);
@ -485,7 +460,7 @@ void I_UpdateSound(void)
for (i = 0; i < MAX_CHANNELS; i++)
{
if (channelinfo[i].data && !I_SoundIsPlaying(i))
if (channelinfo[i].enabled && !I_SoundIsPlaying(i))
{
// Sound has finished playing on this channel,
// but sound data has not been released to cache
@ -523,10 +498,10 @@ void I_ShutdownSound(void)
alDeleteSources(MAX_CHANNELS, openal_sources);
for (i = 0; i < num_sfx; ++i)
{
if (S_sfx[i].data)
if (S_sfx[i].cached)
{
alDeleteBuffers(1, S_sfx[i].data);
free(S_sfx[i].data);
alDeleteBuffers(1, &S_sfx[i].buffer);
S_sfx[i].cached = false;
}
}
if (alGetError() != AL_NO_ERROR)
@ -538,6 +513,12 @@ void I_ShutdownSound(void)
alcDestroyContext(context);
alcCloseDevice(device);
if (openal_sources)
{
free(openal_sources);
openal_sources = NULL;
}
snd_init = false;
}

View File

@ -123,9 +123,9 @@ musicinfo_t S_music[] = {
.link = l ? &original_S_sfx[l] : NULL, \
.pitch = i, \
.volume = -1, \
.data = NULL, \
.usefulness = -1, \
.lumpnum = -1}
.buffer = 0, \
.lumpnum = -1, \
.cached = false}
#define SOUND(n, s, p) \
SOUND_LINK(n, s, p, 0, -1)

View File

@ -56,17 +56,14 @@ typedef struct sfxinfo_struct {
// volume if a link
int volume;
// sound data
void *data;
// this is checked every second to see if sound
// can be thrown out (if 0, then decrement, if -1,
// then throw out, if > 0, then it is in use)
int usefulness;
// OpenAL buffer id
uint32_t buffer;
// lump number of sfx
int lumpnum;
boolean cached;
} sfxinfo_t;
extern int parallel_sfx_limit;