[sound] Rename "cache" to "block" and clean out old code

I never liked "cache" as a name because it said where the sound was
stored rather than how it was loaded/played, but "stream" is ok, since
that's pretty much spot on. I'm not sure "block" is the best, but it at
least makes sense because the sounds are loaded as a single block (as
opposed to being streamed). An now, neither use the cache system.
This commit is contained in:
Bill Currie 2022-06-06 12:05:44 +09:00
parent 79f3f651a4
commit 5fa73e7ff2
8 changed files with 28 additions and 167 deletions

View file

@ -66,11 +66,6 @@ struct sfx_s
sfxblock_t *block;
};
sfxbuffer_t *(*touch) (sfx_t *sfx);
sfxbuffer_t *(*retain) (sfx_t *sfx);
void (*release) (sfx_t *sfx);
sfxbuffer_t *(*getbuffer) (sfx_t *sfx);
struct wavinfo_s *(*wavinfo) (const sfx_t *sfx);
sfxbuffer_t *(*open) (sfx_t *sfx);
@ -139,8 +134,8 @@ struct wavinfo_s {
unsigned datalen; //!< chunk bytes
};
/** Buffer for storing sound samples in memory. For cached sounds, acts as
an ordinary buffer. For streamed sounds, acts as a ring buffer.
/** Buffer for storing sound samples in memory. For block-loaded sounds, acts
as an ordinary buffer. For streamed sounds, acts as a ring buffer.
*/
struct sfxbuffer_s {
_Atomic unsigned head; //!< ring buffer head position in sampels
@ -228,7 +223,7 @@ struct sfxblock_s {
const sfx_t *sfx; //!< owning sfx_t instance
void *file; //!< handle for "file" representing the block
wavinfo_t wavinfo; //!< description of sound data
sfxbuffer_t *buffer; //!< pointer to cached buffer
sfxbuffer_t *buffer; //!< pointer to block-loaded buffer
};
/** Representation of a sound being played.
@ -280,7 +275,7 @@ int SND_Memory_GetRetainCount (void *ptr) __attribute__((pure));
\ingroup sound_render_mix
*/
///@{
/** Cache sound data. Initializes caching fields of sfx.
/** Block sound data. Initializes block fields of sfx.
\param sfx
\param realname
\param info
@ -315,9 +310,9 @@ sfxbuffer_t *SND_SFX_StreamOpen (sfx_t *sfx, void *file,
*/
void SND_SFX_StreamClose (sfxstream_t *stream);
/** Pre-load a sound into the cache.
/** Load a sound into the system.
\param snd sound system state
\param sample name of sound to precache
\param sample name of sound to load
*/
sfx_t *SND_PrecacheSound (snd_t *snd, const char *sample);
@ -556,15 +551,15 @@ int SND_LoadWav (QFile *file, sfx_t *sfx, char *realname);
int SND_LoadMidi (QFile *file, sfx_t *sfx, char *realname);
///@}
/** \defgroup sound_render_cache_stream Cache/Stream Functions.
/** \defgroup sound_render_block_stream Block/Stream Functions.
\ingroup sound_render
*/
///@{
/** Retrieve wavinfo from a cached sound.
/** Retrieve wavinfo from a block-loaded sound.
\param sfx sound reference
\return pointer to sound's wavinfo
*/
wavinfo_t *SND_CacheWavinfo (const sfx_t *sfx) __attribute__((pure));
wavinfo_t *SND_BlockWavinfo (const sfx_t *sfx) __attribute__((pure));
/** Retrieve wavinfo from a streamed sound.
\param sfx sound reference
@ -572,51 +567,6 @@ wavinfo_t *SND_CacheWavinfo (const sfx_t *sfx) __attribute__((pure));
*/
wavinfo_t *SND_StreamWavinfo (const sfx_t *sfx) __attribute__((pure));
/** Ensure a cached sound is in memory.
\param sfx sound reference
\return poitner to sound buffer
*/
sfxbuffer_t *SND_CacheTouch (sfx_t *sfx);
/** Get the pointer to the sound buffer.
\param sfx sound reference
\return sound buffer or null
\note The sound must be retained with SND_CacheRetain() for the returned
buffer to be valid.
*/
sfxbuffer_t *SND_CacheGetBuffer (sfx_t *sfx) __attribute__((pure));
/** Lock a cached sound into memory. After calling this, SND_CacheGetBffer()
will return a valid buffer.
\param sfx sound reference
\return poitner to sound buffer
*/
sfxbuffer_t *SND_CacheRetain (sfx_t *sfx);
/** Unlock a cached sound from memory. After calling this, SND_CacheGetBffer()
will return a null buffer.
\param sfx sound reference
*/
void SND_CacheRelease (sfx_t *sfx);
/** Get the pointer to the sound buffer.
\param sfx sound reference
\return poitner to sound buffer
*/
sfxbuffer_t *SND_StreamGetBuffer (sfx_t *sfx) __attribute__((pure));
/** Lock a streamed sound into memory. Doesn't actually do anything other than
return a pointer to the buffer.
\param sfx sound reference
\return poitner to sound buffer
*/
sfxbuffer_t *SND_StreamRetain (sfx_t *sfx) __attribute__((pure));
/** Unlock a streamed sound from memory. Doesn't actually do anything.
\param sfx sound reference
*/
void SND_StreamRelease (sfx_t *sfx);
/** Advance the position with the stream, updating the ring buffer as
necessary. Null for chached sounds.
\param buffer "this"

View file

@ -329,7 +329,7 @@ flac_callback_load (sfxblock_t *block)
}
static void
flac_cache (sfx_t *sfx, char *realname, flacfile_t *ff, wavinfo_t info)
flac_block (sfx_t *sfx, char *realname, flacfile_t *ff, wavinfo_t info)
{
flac_close (ff);
SND_SFX_Block (sfx, realname, info, flac_callback_load);
@ -461,8 +461,8 @@ SND_LoadFLAC (QFile *file, sfx_t *sfx, char *realname)
return -1;
}
if (info.frames / info.rate < 3) {
Sys_MaskPrintf (SYS_snd, "cache %s\n", realname);
flac_cache (sfx, realname, ff, info);
Sys_MaskPrintf (SYS_snd, "block %s\n", realname);
flac_block (sfx, realname, ff, info);
} else {
Sys_MaskPrintf (SYS_snd, "stream %s\n", realname);
flac_stream (sfx, realname, ff, info);

View file

@ -760,7 +760,7 @@ SND_LocalSound (snd_t *snd, const char *sound)
sfx = SND_PrecacheSound (snd, sound);
if (!sfx) {
Sys_Printf ("S_LocalSound: can't cache %s\n", sound);
Sys_Printf ("S_LocalSound: can't load %s\n", sound);
return;
}
if (snd_render_data.viewentity)

View file

@ -159,17 +159,6 @@ SND_Memory_GetRetainCount (void *ptr)
return Z_GetRetainCount (snd_zone, ptr);
}
static sfxbuffer_t *
snd_fail (sfx_t *sfx)
{
return 0;
}
static void
snd_noop (sfx_t *sfx)
{
}
static sfxbuffer_t *
snd_open (sfx_t *sfx)
{
@ -184,48 +173,8 @@ snd_open_fail (sfx_t *sfx)
return 0;
}
sfxbuffer_t * __attribute__((pure))
SND_CacheTouch (sfx_t *sfx)
{
return sfx->block->buffer;
}
sfxbuffer_t *
SND_CacheGetBuffer (sfx_t *sfx)
{
return sfx->block->buffer;
}
sfxbuffer_t * __attribute__((pure))
SND_CacheRetain (sfx_t *sfx)
{
return sfx->block->buffer;
}
void
SND_CacheRelease (sfx_t *sfx)
{
}
sfxbuffer_t *
SND_StreamGetBuffer (sfx_t *sfx)
{
return sfx->stream->buffer;
}
sfxbuffer_t *
SND_StreamRetain (sfx_t *sfx)
{
return sfx->stream->buffer;
}
void
SND_StreamRelease (sfx_t *sfx)
{
}
wavinfo_t *
SND_CacheWavinfo (const sfx_t *sfx)
SND_BlockWavinfo (const sfx_t *sfx)
{
return &sfx->block->wavinfo;
}
@ -383,8 +332,6 @@ SND_Load (sfx_t *sfx)
char buf[4];
QFile *file;
sfx->touch = sfx->retain = snd_fail;
sfx->release = snd_noop;
sfx->open = snd_open_fail;
file = QFS_FOpenFile (sfx->name);

View file

@ -1,9 +1,10 @@
/*
snd_mem.c
snd_resample.c
sound caching
sound resampling
Copyright (C) 1996-1997 Id Software, Inc.
Copyright (C) 2010 Bill Currie <bill@taniwha.org>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License

View file

@ -53,16 +53,6 @@ static sfx_t snd_sfx[MAX_SFX];
static int snd_num_sfx;
static hashtab_t *snd_sfx_hash;
static int precache;
static cvar_t precache_cvar = {
.name = "precache",
.description =
"Toggle the use of a precache",
.default_value = "1",
.flags = CVAR_NONE,
.value = { .type = &cexpr_int, .value = &precache },
};
static const char *
snd_sfx_getkey (const void *sfx, void *unused)
{
@ -90,11 +80,7 @@ SND_SFX_Block (sfx_t *sfx, char *realname, wavinfo_t info,
sfxblock_t *block = calloc (1, sizeof (sfxblock_t));
sfx->block = block;
sfx->wavinfo = SND_CacheWavinfo;
sfx->touch = SND_CacheTouch;
sfx->retain = SND_CacheRetain;
sfx->release = SND_CacheRelease;
sfx->getbuffer = SND_CacheGetBuffer;
sfx->wavinfo = SND_BlockWavinfo;
sfx->loopstart = SND_ResamplerFrames (sfx, info.loopstart);
sfx->length = SND_ResamplerFrames (sfx, info.frames);
@ -112,10 +98,7 @@ SND_SFX_Stream (sfx_t *sfx, char *realname, wavinfo_t info,
{
sfxstream_t *stream = calloc (1, sizeof (sfxstream_t));
sfx->open = open;
sfx->wavinfo = SND_CacheWavinfo;
sfx->touch = sfx->retain = SND_StreamRetain;
sfx->release = SND_StreamRelease;
sfx->getbuffer = SND_StreamGetBuffer;
sfx->wavinfo = SND_StreamWavinfo;
sfx->stream = stream;
sfx->loopstart = SND_ResamplerFrames (sfx, info.loopstart);
sfx->length = SND_ResamplerFrames (sfx, info.frames);
@ -213,10 +196,6 @@ SND_PrecacheSound (snd_t *snd, const char *name)
Sys_Error ("SND_PrecacheSound: NULL");
sfx = SND_LoadSound (snd, va (0, "sound/%s", name));
if (sfx && precache) {
if (sfx->retain (sfx))
sfx->release (sfx);
}
return sfx;
}
@ -229,28 +208,13 @@ s_gamedir (int phase, void *data)
static void
s_soundlist_f (void)
{
int load, total, i;
int total, i;
sfx_t *sfx;
if (Cmd_Argc() >= 2 && Cmd_Argv (1)[0])
load = 1;
else
load = 0;
total = 0;
for (sfx = snd_sfx, i = 0; i < snd_num_sfx; i++, sfx++) {
if (load) {
if (!sfx->retain (sfx))
continue;
} else {
if (!sfx->touch (sfx))
continue;
}
total += sfx->length;
Sys_Printf ("%6d %6d %s\n", sfx->loopstart, sfx->length, sfx->name);
if (load)
sfx->release (sfx);
}
Sys_Printf ("Total resident: %i\n", total);
}
@ -259,10 +223,9 @@ void
SND_SFX_Init (snd_t *snd)
{
snd_sfx_hash = Hash_NewTable (511, snd_sfx_getkey, snd_sfx_free, 0, 0);
Cvar_Register (&precache_cvar, 0, 0);
QFS_GamedirCallback (s_gamedir, 0);
Cmd_AddCommand ("soundlist", s_soundlist_f,
"Reports a list of sounds in the cache");
"Reports a list of loaded sounds");
}

View file

@ -209,8 +209,8 @@ vorbis_callback_load (sfxblock_t *block)
return vorbis_load (&vf, block);
}
static void
vorbis_cache (sfx_t *sfx, char *realname, OggVorbis_File *vf, wavinfo_t info)
static void//extra _ in name because vorbis_block is a vorbis type
vorbis__block (sfx_t *sfx, char *realname, OggVorbis_File *vf, wavinfo_t info)
{
ov_clear (vf);
SND_SFX_Block (sfx, realname, info, vorbis_callback_load);
@ -302,8 +302,8 @@ SND_LoadOgg (QFile *file, sfx_t *sfx, char *realname)
return -1;
}
if (info.frames / info.rate < 3) {
Sys_MaskPrintf (SYS_snd, "cache %s\n", realname);
vorbis_cache (sfx, realname, &vf, info);
Sys_MaskPrintf (SYS_snd, "block %s\n", realname);
vorbis__block (sfx, realname, &vf, info);
} else {
Sys_MaskPrintf (SYS_snd, "stream %s\n", realname);
vorbis_stream (sfx, realname, &vf, info);

View file

@ -100,7 +100,7 @@ bail:
}
static void
wav_cache (sfx_t *sfx, char *realname, void *file, wavinfo_t info)
wav_block (sfx_t *sfx, char *realname, void *file, wavinfo_t info)
{
Qclose (file);
SND_SFX_Block (sfx, realname, info, wav_callback_load);
@ -289,8 +289,8 @@ SND_LoadWav (QFile *file, sfx_t *sfx, char *realname)
}
if (info.frames / info.rate < 3) {
Sys_MaskPrintf (SYS_snd, "cache %s\n", realname);
wav_cache (sfx, realname, file, info);
Sys_MaskPrintf (SYS_snd, "block %s\n", realname);
wav_block (sfx, realname, file, info);
} else {
Sys_MaskPrintf (SYS_snd, "stream %s\n", realname);
wav_stream (sfx, realname, file, info);