2007-03-17 03:14:41 +00:00
|
|
|
/*
|
|
|
|
#FILENAME#
|
|
|
|
|
|
|
|
#DESCRIPTION#
|
|
|
|
|
|
|
|
Copyright (C) 2004 #AUTHOR#
|
|
|
|
|
|
|
|
Author: #AUTHOR#
|
|
|
|
Date: #DATE#
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "QF/cmd.h"
|
|
|
|
#include "QF/cvar.h"
|
|
|
|
#include "QF/hash.h"
|
|
|
|
#include "QF/quakefs.h"
|
|
|
|
#include "QF/sys.h"
|
|
|
|
#include "QF/va.h"
|
|
|
|
|
2011-07-23 06:56:22 +00:00
|
|
|
#include "snd_internal.h"
|
2007-03-17 03:14:41 +00:00
|
|
|
|
|
|
|
#define MAX_SFX 512
|
|
|
|
static sfx_t snd_sfx[MAX_SFX];
|
|
|
|
static int snd_num_sfx;
|
|
|
|
static hashtab_t *snd_sfx_hash;
|
|
|
|
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
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 },
|
|
|
|
};
|
2007-03-17 03:14:41 +00:00
|
|
|
|
|
|
|
static const char *
|
2012-07-18 13:34:37 +00:00
|
|
|
snd_sfx_getkey (const void *sfx, void *unused)
|
2007-03-17 03:14:41 +00:00
|
|
|
{
|
|
|
|
return ((sfx_t *) sfx)->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
snd_sfx_free (void *_sfx, void *unused)
|
|
|
|
{
|
|
|
|
sfx_t *sfx = (sfx_t *) _sfx;
|
|
|
|
free ((char *) sfx->name);
|
|
|
|
sfx->name = 0;
|
2022-06-05 07:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
snd_block_close (sfxbuffer_t *buffer)
|
|
|
|
{
|
|
|
|
SND_Memory_Release (buffer);
|
2007-03-17 03:14:41 +00:00
|
|
|
}
|
|
|
|
|
2007-03-18 01:44:46 +00:00
|
|
|
void
|
2022-06-04 14:55:31 +00:00
|
|
|
SND_SFX_Block (sfx_t *sfx, char *realname, wavinfo_t info,
|
|
|
|
sfxbuffer_t *(*load) (sfxblock_t *block))
|
2007-03-18 01:44:46 +00:00
|
|
|
{
|
|
|
|
sfxblock_t *block = calloc (1, sizeof (sfxblock_t));
|
|
|
|
|
2022-06-05 07:57:13 +00:00
|
|
|
sfx->block = block;
|
2007-03-18 01:44:46 +00:00
|
|
|
sfx->wavinfo = SND_CacheWavinfo;
|
|
|
|
sfx->touch = SND_CacheTouch;
|
|
|
|
sfx->retain = SND_CacheRetain;
|
|
|
|
sfx->release = SND_CacheRelease;
|
2007-03-18 10:32:01 +00:00
|
|
|
sfx->getbuffer = SND_CacheGetBuffer;
|
2022-06-05 07:57:13 +00:00
|
|
|
sfx->loopstart = SND_ResamplerFrames (sfx, info.loopstart);
|
|
|
|
sfx->length = SND_ResamplerFrames (sfx, info.frames);
|
2007-03-18 01:44:46 +00:00
|
|
|
|
|
|
|
block->sfx = sfx;
|
|
|
|
block->file = realname;
|
|
|
|
block->wavinfo = info;
|
2022-06-04 14:55:31 +00:00
|
|
|
block->buffer = load (block);
|
2022-06-05 07:57:13 +00:00
|
|
|
SND_Memory_Retain (block->buffer);
|
|
|
|
block->buffer->close = snd_block_close;
|
2007-03-18 01:44:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SND_SFX_Stream (sfx_t *sfx, char *realname, wavinfo_t info,
|
2022-06-05 07:57:13 +00:00
|
|
|
sfxbuffer_t *(*open) (sfx_t *sfx))
|
2007-03-18 01:44:46 +00:00
|
|
|
{
|
|
|
|
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;
|
2007-03-18 10:32:01 +00:00
|
|
|
sfx->getbuffer = SND_StreamGetBuffer;
|
2022-06-05 07:57:13 +00:00
|
|
|
sfx->stream = stream;
|
|
|
|
sfx->loopstart = SND_ResamplerFrames (sfx, info.loopstart);
|
|
|
|
sfx->length = SND_ResamplerFrames (sfx, info.frames);
|
2007-03-18 01:44:46 +00:00
|
|
|
|
|
|
|
stream->file = realname;
|
|
|
|
stream->wavinfo = info;
|
|
|
|
}
|
|
|
|
|
2022-06-05 07:57:13 +00:00
|
|
|
sfxbuffer_t *
|
2007-03-18 01:44:46 +00:00
|
|
|
SND_SFX_StreamOpen (sfx_t *sfx, void *file,
|
2010-08-11 23:47:03 +00:00
|
|
|
long (*read)(void *, float **),
|
|
|
|
int (*seek)(sfxstream_t *, int),
|
2022-06-05 07:57:13 +00:00
|
|
|
void (*close) (sfxbuffer_t *))
|
2007-03-18 01:44:46 +00:00
|
|
|
{
|
2021-06-23 15:04:02 +00:00
|
|
|
snd_t *snd = sfx->snd;
|
2022-06-05 07:57:13 +00:00
|
|
|
sfxstream_t *stream = sfx->stream;
|
2007-03-18 01:44:46 +00:00
|
|
|
wavinfo_t *info = &stream->wavinfo;
|
2010-08-11 23:44:34 +00:00
|
|
|
int frames;
|
2007-03-18 01:44:46 +00:00
|
|
|
|
2010-11-14 11:46:29 +00:00
|
|
|
// if the speed is 0, there is no sound driver (probably failed to connect
|
|
|
|
// to jackd)
|
2021-06-23 15:04:02 +00:00
|
|
|
if (!snd->speed)
|
2010-11-14 11:46:29 +00:00
|
|
|
return 0;
|
|
|
|
|
2021-06-23 15:04:02 +00:00
|
|
|
frames = snd->speed * 0.3;
|
2010-08-11 23:44:34 +00:00
|
|
|
frames = (frames + 255) & ~255;
|
2007-03-18 01:44:46 +00:00
|
|
|
|
2022-06-04 09:45:11 +00:00
|
|
|
stream = calloc (1, sizeof (sfxstream_t));
|
|
|
|
stream->buffer = SND_Memory_AllocBuffer (frames * info->channels);
|
2022-06-04 16:42:30 +00:00
|
|
|
if (!stream->buffer) {
|
|
|
|
free (stream);
|
|
|
|
return 0;
|
|
|
|
}
|
2022-06-05 07:57:13 +00:00
|
|
|
|
2007-03-18 01:44:46 +00:00
|
|
|
stream->file = file;
|
2022-06-05 07:57:13 +00:00
|
|
|
stream->sfx = sfx;
|
2010-08-11 23:47:03 +00:00
|
|
|
stream->ll_read = read;
|
2010-08-11 23:47:31 +00:00
|
|
|
stream->ll_seek = seek;
|
2007-03-18 01:44:46 +00:00
|
|
|
|
2007-03-18 10:32:01 +00:00
|
|
|
stream->wavinfo = *sfx->wavinfo (sfx);
|
|
|
|
|
2022-06-05 07:57:13 +00:00
|
|
|
stream->buffer->stream = stream;
|
2022-06-04 09:45:11 +00:00
|
|
|
stream->buffer->size = frames;
|
|
|
|
stream->buffer->advance = SND_StreamAdvance;
|
|
|
|
stream->buffer->setpos = SND_StreamSetPos;
|
2022-06-05 07:57:13 +00:00
|
|
|
stream->buffer->sfx_length = info->frames;
|
|
|
|
stream->buffer->channels = info->channels;
|
|
|
|
stream->buffer->close = close;
|
2022-06-04 09:45:11 +00:00
|
|
|
SND_SetPaint (stream->buffer);
|
2007-03-18 01:44:46 +00:00
|
|
|
|
2022-06-04 09:45:11 +00:00
|
|
|
SND_SetupResampler (stream->buffer, 1); // get sfx setup properly
|
|
|
|
stream->buffer->setpos (stream->buffer, 0); // pre-fill the buffer
|
2007-03-18 01:44:46 +00:00
|
|
|
|
2022-06-05 07:57:13 +00:00
|
|
|
return stream->buffer;
|
2007-03-18 01:44:46 +00:00
|
|
|
}
|
|
|
|
|
2010-08-11 23:47:03 +00:00
|
|
|
void
|
2022-06-05 07:57:13 +00:00
|
|
|
SND_SFX_StreamClose (sfxstream_t *stream)
|
2010-08-11 23:47:03 +00:00
|
|
|
{
|
|
|
|
SND_PulldownResampler (stream);
|
2022-06-05 07:57:13 +00:00
|
|
|
SND_Memory_Free (stream->buffer);
|
2010-08-11 23:47:03 +00:00
|
|
|
free (stream);
|
|
|
|
}
|
|
|
|
|
2007-03-17 03:14:41 +00:00
|
|
|
sfx_t *
|
2021-06-23 15:04:02 +00:00
|
|
|
SND_LoadSound (snd_t *snd, const char *name)
|
2007-03-17 03:14:41 +00:00
|
|
|
{
|
|
|
|
sfx_t *sfx;
|
|
|
|
|
|
|
|
if (!snd_sfx_hash)
|
|
|
|
return 0;
|
|
|
|
if ((sfx = (sfx_t *) Hash_Find (snd_sfx_hash, name)))
|
|
|
|
return sfx;
|
|
|
|
|
|
|
|
if (snd_num_sfx == MAX_SFX)
|
|
|
|
Sys_Error ("s_load_sound: out of sfx_t");
|
|
|
|
|
|
|
|
sfx = &snd_sfx[snd_num_sfx++];
|
2021-06-23 15:04:02 +00:00
|
|
|
sfx->snd = snd;
|
2007-03-17 03:14:41 +00:00
|
|
|
sfx->name = strdup (name);
|
2010-08-08 03:02:55 +00:00
|
|
|
if (SND_Load (sfx) == -1) {
|
|
|
|
snd_num_sfx--;
|
|
|
|
return 0;
|
|
|
|
}
|
2007-03-17 03:14:41 +00:00
|
|
|
Hash_Add (snd_sfx_hash, sfx);
|
|
|
|
return sfx;
|
|
|
|
}
|
|
|
|
|
|
|
|
sfx_t *
|
2021-06-23 15:04:02 +00:00
|
|
|
SND_PrecacheSound (snd_t *snd, const char *name)
|
2007-03-17 03:14:41 +00:00
|
|
|
{
|
|
|
|
sfx_t *sfx;
|
|
|
|
|
|
|
|
if (!name)
|
|
|
|
Sys_Error ("SND_PrecacheSound: NULL");
|
|
|
|
|
2021-06-23 15:04:02 +00:00
|
|
|
sfx = SND_LoadSound (snd, va (0, "sound/%s", name));
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
if (sfx && precache) {
|
2007-03-17 03:14:41 +00:00
|
|
|
if (sfx->retain (sfx))
|
|
|
|
sfx->release (sfx);
|
|
|
|
}
|
|
|
|
return sfx;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-06-04 07:06:04 +00:00
|
|
|
s_gamedir (int phase, void *data)
|
2007-03-17 03:14:41 +00:00
|
|
|
{
|
|
|
|
snd_num_sfx = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
s_soundlist_f (void)
|
|
|
|
{
|
|
|
|
int load, 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-06-23 15:04:02 +00:00
|
|
|
SND_SFX_Init (snd_t *snd)
|
2007-03-17 03:14:41 +00:00
|
|
|
{
|
2020-03-25 06:43:16 +00:00
|
|
|
snd_sfx_hash = Hash_NewTable (511, snd_sfx_getkey, snd_sfx_free, 0, 0);
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
Cvar_Register (&precache_cvar, 0, 0);
|
2007-03-17 03:14:41 +00:00
|
|
|
|
2022-06-04 07:06:04 +00:00
|
|
|
QFS_GamedirCallback (s_gamedir, 0);
|
2007-03-17 03:14:41 +00:00
|
|
|
|
|
|
|
Cmd_AddCommand ("soundlist", s_soundlist_f,
|
|
|
|
"Reports a list of sounds in the cache");
|
|
|
|
}
|