spingle/source/snd_dma.c

1073 lines
23 KiB
C
Raw Normal View History

2019-11-24 20:45:15 -08:00
/*
Copyright (C) 1996-2001 Id Software, Inc.
Copyright (C) 2002-2009 John Fitzgibbons and others
Copyright (C) 2007-2008 Kristian Duske
Copyright (C) 2010-2011 O. Sezer <sezero@users.sourceforge.net>
Copyright (C) 2010-2014 QuakeSpasm developers
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 the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
// snd_dma.c -- main control for any streaming sound output device
2019-12-02 07:07:37 -08:00
#include "q_defs.h"
2019-11-24 20:45:15 -08:00
#include "snd_codec.h"
#include "bgmusic.h"
2019-11-25 17:40:18 -08:00
static void S_Play(void);
static void S_PlayVol(void);
static void S_SoundList(void);
static void S_Update_(void);
void S_StopAllSounds(bool clear);
static void S_StopAllSoundsC(void);
2019-11-24 20:45:15 -08:00
// =======================================================================
// Internal sound data & structures
// =======================================================================
2019-11-25 17:40:18 -08:00
channel_t snd_channels[MAX_CHANNELS];
int32_t total_channels;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
static int32_t snd_blocked = 0;
static bool snd_initialized = false;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
static dma_t sn;
volatile dma_t *shm = NULL;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
vec3_t listener_origin;
vec3_t listener_forward;
vec3_t listener_right;
vec3_t listener_up;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
#define sound_nominal_clip_dist 1000.0
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
int32_t soundtime; // sample PAIRS
int32_t paintedtime; // sample PAIRS
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
int32_t s_rawend;
portable_samplepair_t s_rawsamples[MAX_RAW_SAMPLES];
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
#define MAX_SFX 1024
static sfx_t *known_sfx = NULL; // hunk allocated [MAX_SFX]
static int32_t num_sfx;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
static sfx_t *ambient_sfx[NUM_AMBIENTS];
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
static bool sound_started = false;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
cvar_t bgmvolume = {"bgmvolume", "1", CVAR_ARCHIVE};
cvar_t sfxvolume = {"volume", "0.7", CVAR_ARCHIVE};
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
cvar_t precache = {"precache", "1", CVAR_NONE};
cvar_t loadas8bit = {"loadas8bit", "0", CVAR_NONE};
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
cvar_t sndspeed = {"sndspeed", "11025", CVAR_NONE};
cvar_t snd_mixspeed = {"snd_mixspeed", "44100", CVAR_NONE};
2019-11-24 20:45:15 -08:00
2019-12-02 04:24:20 -08:00
#if PLATFORM_IS(WINDOWS)
2019-11-24 20:45:15 -08:00
#define SND_FILTERQUALITY_DEFAULT "5"
#else
#define SND_FILTERQUALITY_DEFAULT "1"
#endif
2019-11-25 17:40:18 -08:00
cvar_t snd_filterquality = {"snd_filterquality", SND_FILTERQUALITY_DEFAULT,
CVAR_NONE
};
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
static cvar_t nosound = {"nosound", "0", CVAR_NONE};
static cvar_t ambient_level = {"ambient_level", "0.3", CVAR_NONE};
static cvar_t ambient_fade = {"ambient_fade", "100", CVAR_NONE};
static cvar_t snd_noextraupdate = {"snd_noextraupdate", "0", CVAR_NONE};
static cvar_t snd_show = {"snd_show", "0", CVAR_NONE};
2019-12-07 09:27:26 -08:00
static cvar_t snd_mixahead = {"_snd_mixahead", "0.1", CVAR_ARCHIVE};
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
static void S_SoundInfo_f(void)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
if(!sound_started || !shm)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
Con_Printf("sound system not started\n");
2019-11-24 20:45:15 -08:00
return;
}
2019-11-25 16:56:15 -08:00
Con_Printf("%" PRIi32 " bit, %s, %" PRIi32 " Hz\n", shm->samplebits,
2019-11-25 17:40:18 -08:00
(shm->channels == 2) ? "stereo" : "mono", shm->speed);
2019-11-25 17:29:41 -08:00
Con_Printf("%5" PRIi32 " samples\n", shm->samples);
Con_Printf("%5" PRIi32 " samplepos\n", shm->samplepos);
Con_Printf("%5" PRIi32 " submission_chunk\n", shm->submission_chunk);
Con_Printf("%5" PRIi32 " total_channels\n", total_channels);
2019-11-24 20:45:15 -08:00
Con_Printf("%p dma buffer\n", shm->buffer);
}
2019-11-25 17:40:18 -08:00
static void SND_Callback_sfxvolume(cvar_t *var)
2019-11-24 20:45:15 -08:00
{
2019-11-25 13:33:54 -08:00
(void)var;
2019-11-25 17:40:18 -08:00
SND_InitScaletable();
2019-11-24 20:45:15 -08:00
}
2019-11-25 17:40:18 -08:00
static void SND_Callback_snd_filterquality(cvar_t *var)
2019-11-24 20:45:15 -08:00
{
2019-11-25 13:33:54 -08:00
(void)var;
2019-11-25 17:40:18 -08:00
if(snd_filterquality.value < 1 || snd_filterquality.value > 5)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
Con_Printf("snd_filterquality must be between 1 and 5\n");
Cvar_SetQuick(&snd_filterquality, SND_FILTERQUALITY_DEFAULT);
2019-11-24 20:45:15 -08:00
}
}
/*
================
S_Startup
================
*/
2019-11-25 17:40:18 -08:00
void S_Startup(void)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
if(!snd_initialized)
2019-11-24 20:45:15 -08:00
return;
sound_started = SNDDMA_Init(&sn);
2019-11-25 17:40:18 -08:00
if(!sound_started)
2019-11-24 20:45:15 -08:00
{
Con_Printf("Failed initializing sound\n");
}
else
{
2019-11-25 16:56:15 -08:00
Con_Printf("Audio: %" PRIi32 " bit, %s, %" PRIi32 " Hz\n",
2019-11-25 17:40:18 -08:00
shm->samplebits,
(shm->channels == 2) ? "stereo" : "mono",
shm->speed);
2019-11-24 20:45:15 -08:00
}
}
/*
================
S_Init
================
*/
2019-11-25 17:40:18 -08:00
void S_Init(void)
2019-11-24 20:45:15 -08:00
{
2019-11-25 16:49:58 -08:00
int32_t i;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(snd_initialized)
2019-11-24 20:45:15 -08:00
{
Con_Printf("Sound is already initialized\n");
return;
}
Cvar_RegisterVariable(&nosound);
Cvar_RegisterVariable(&sfxvolume);
Cvar_RegisterVariable(&precache);
Cvar_RegisterVariable(&loadas8bit);
Cvar_RegisterVariable(&bgmvolume);
Cvar_RegisterVariable(&ambient_level);
Cvar_RegisterVariable(&ambient_fade);
Cvar_RegisterVariable(&snd_noextraupdate);
Cvar_RegisterVariable(&snd_show);
2019-12-07 09:27:26 -08:00
Cvar_RegisterVariable(&snd_mixahead);
2019-11-24 20:45:15 -08:00
Cvar_RegisterVariable(&sndspeed);
Cvar_RegisterVariable(&snd_mixspeed);
Cvar_RegisterVariable(&snd_filterquality);
2019-11-25 13:33:54 -08:00
2019-11-25 17:40:18 -08:00
if(safemode || COM_CheckParm("-nosound"))
2019-11-24 20:45:15 -08:00
return;
Cmd_AddCommand("play", S_Play);
Cmd_AddCommand("playvol", S_PlayVol);
Cmd_AddCommand("stopsound", S_StopAllSoundsC);
Cmd_AddCommand("soundlist", S_SoundList);
Cmd_AddCommand("soundinfo", S_SoundInfo_f);
i = COM_CheckParm("-sndspeed");
2019-11-25 17:40:18 -08:00
if(i && i < com_argc - 1)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
Cvar_SetQuick(&sndspeed, com_argv[i + 1]);
2019-11-24 20:45:15 -08:00
}
2019-11-25 13:33:54 -08:00
2019-11-24 20:45:15 -08:00
i = COM_CheckParm("-mixspeed");
2019-11-25 17:40:18 -08:00
if(i && i < com_argc - 1)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
Cvar_SetQuick(&snd_mixspeed, com_argv[i + 1]);
2019-11-24 20:45:15 -08:00
}
2019-11-25 17:40:18 -08:00
if(host_parms->memsize < 0x800000)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
Cvar_SetQuick(&loadas8bit, "1");
Con_Printf("loading all sounds as 8bit\n");
2019-11-24 20:45:15 -08:00
}
Cvar_SetCallback(&sfxvolume, SND_Callback_sfxvolume);
Cvar_SetCallback(&snd_filterquality, &SND_Callback_snd_filterquality);
2019-11-25 17:40:18 -08:00
SND_InitScaletable();
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
known_sfx = (sfx_t *) Hunk_AllocName(MAX_SFX * sizeof(sfx_t), "sfx_t");
2019-11-24 20:45:15 -08:00
num_sfx = 0;
snd_initialized = true;
2019-11-25 17:40:18 -08:00
S_Startup();
if(sound_started == 0)
2019-11-24 20:45:15 -08:00
return;
// provides a tick sound until washed clean
// if (shm->buffer)
2019-11-25 17:40:18 -08:00
// shm->buffer[4] = shm->buffer[5] = 0x7f; // force a pop for debugging
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
ambient_sfx[AMBIENT_WATER] = S_PrecacheSound("ambience/water1.wav");
ambient_sfx[AMBIENT_SKY] = S_PrecacheSound("ambience/wind2.wav");
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
S_CodecInit();
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
S_StopAllSounds(true);
2019-11-24 20:45:15 -08:00
}
// =======================================================================
// Shutdown sound engine
// =======================================================================
2019-11-25 17:40:18 -08:00
void S_Shutdown(void)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
if(!sound_started)
2019-11-24 20:45:15 -08:00
return;
sound_started = 0;
snd_blocked = 0;
S_CodecShutdown();
SNDDMA_Shutdown();
shm = NULL;
}
// =======================================================================
// Load a sound
// =======================================================================
/*
==================
S_FindName
==================
*/
2019-11-25 17:40:18 -08:00
static sfx_t *S_FindName(const char *name)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
int32_t i;
sfx_t *sfx;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(!name)
Sys_Error("S_FindName: NULL");
2019-11-24 20:45:15 -08:00
2019-12-07 09:27:26 -08:00
if(strlen(name) >= MAX_QPATH)
2019-11-25 17:40:18 -08:00
Sys_Error("Sound name too long: %s", name);
2019-11-24 20:45:15 -08:00
// see if already loaded
2019-11-25 17:40:18 -08:00
for(i = 0; i < num_sfx; i++)
2019-11-24 20:45:15 -08:00
{
2019-12-07 09:27:26 -08:00
if(!strcmp(known_sfx[i].name, name))
2019-11-24 20:45:15 -08:00
{
return &known_sfx[i];
}
}
2019-11-25 17:40:18 -08:00
if(num_sfx == MAX_SFX)
Sys_Error("S_FindName: out of sfx_t");
2019-11-24 20:45:15 -08:00
sfx = &known_sfx[i];
2019-11-25 17:40:18 -08:00
q_strlcpy(sfx->name, name, sizeof(sfx->name));
2019-11-24 20:45:15 -08:00
num_sfx++;
return sfx;
}
/*
==================
S_TouchSound
==================
*/
2019-11-25 17:40:18 -08:00
void S_TouchSound(const char *name)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
sfx_t *sfx;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(!sound_started)
2019-11-24 20:45:15 -08:00
return;
2019-11-25 17:40:18 -08:00
sfx = S_FindName(name);
Cache_Check(&sfx->cache);
2019-11-24 20:45:15 -08:00
}
/*
==================
S_PrecacheSound
==================
*/
2019-11-25 17:40:18 -08:00
sfx_t *S_PrecacheSound(const char *name)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
sfx_t *sfx;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(!sound_started || nosound.value)
2019-11-24 20:45:15 -08:00
return NULL;
2019-11-25 17:40:18 -08:00
sfx = S_FindName(name);
2019-11-24 20:45:15 -08:00
// cache it in
2019-11-25 17:40:18 -08:00
if(precache.value)
S_LoadSound(sfx);
2019-11-24 20:45:15 -08:00
return sfx;
}
//=============================================================================
/*
=================
SND_PickChannel
picks a channel based on priorities, empty slots, number of channels
=================
*/
2019-11-25 17:40:18 -08:00
channel_t *SND_PickChannel(int32_t entnum, int32_t entchannel)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
int32_t ch_idx;
int32_t first_to_die;
int32_t life_left;
2019-11-24 20:45:15 -08:00
// Check for replacement sound, or find the best one to replace
first_to_die = -1;
life_left = 0x7fffffff;
2019-11-25 17:40:18 -08:00
for(ch_idx = NUM_AMBIENTS; ch_idx < NUM_AMBIENTS + MAX_DYNAMIC_CHANNELS; ch_idx++)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
if(entchannel != 0 // channel 0 never overrides
&& snd_channels[ch_idx].entnum == entnum
&& (snd_channels[ch_idx].entchannel == entchannel || entchannel == -1))
{
// always override sound from same entity
2019-11-24 20:45:15 -08:00
first_to_die = ch_idx;
break;
}
// don't let monster sounds override player sounds
2019-11-25 17:40:18 -08:00
if(snd_channels[ch_idx].entnum == cl.viewentity && entnum != cl.viewentity && snd_channels[ch_idx].sfx)
2019-11-24 20:45:15 -08:00
continue;
2019-11-25 17:40:18 -08:00
if(snd_channels[ch_idx].end - paintedtime < life_left)
2019-11-24 20:45:15 -08:00
{
life_left = snd_channels[ch_idx].end - paintedtime;
first_to_die = ch_idx;
}
}
2019-11-25 17:40:18 -08:00
if(first_to_die == -1)
2019-11-24 20:45:15 -08:00
return NULL;
2019-11-25 17:40:18 -08:00
if(snd_channels[first_to_die].sfx)
2019-11-24 20:45:15 -08:00
snd_channels[first_to_die].sfx = NULL;
return &snd_channels[first_to_die];
}
/*
=================
SND_Spatialize
spatializes a channel
=================
*/
2019-11-25 17:40:18 -08:00
void SND_Spatialize(channel_t *ch)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
vec_t dot;
vec_t dist;
vec_t lscale, rscale, scale;
vec3_t source_vec;
2019-11-24 20:45:15 -08:00
// anything coming from the view entity will always be full volume
2019-11-25 17:40:18 -08:00
if(ch->entnum == cl.viewentity)
2019-11-24 20:45:15 -08:00
{
ch->leftvol = ch->master_vol;
ch->rightvol = ch->master_vol;
return;
}
// calculate stereo seperation and distance attenuation
VectorSubtract(ch->origin, listener_origin, source_vec);
dist = VectorNormalize(source_vec) * ch->dist_mult;
dot = DotProduct(listener_right, source_vec);
2019-11-25 17:40:18 -08:00
if(shm->channels == 1)
2019-11-24 20:45:15 -08:00
{
rscale = 1.0;
lscale = 1.0;
}
else
{
rscale = 1.0 + dot;
lscale = 1.0 - dot;
}
// add in distance effect
scale = (1.0 - dist) * rscale;
2019-11-25 17:40:18 -08:00
ch->rightvol = (int32_t)(ch->master_vol * scale);
if(ch->rightvol < 0)
2019-11-24 20:45:15 -08:00
ch->rightvol = 0;
scale = (1.0 - dist) * lscale;
2019-11-25 17:40:18 -08:00
ch->leftvol = (int32_t)(ch->master_vol * scale);
if(ch->leftvol < 0)
2019-11-24 20:45:15 -08:00
ch->leftvol = 0;
}
// =======================================================================
// Start a sound effect
// =======================================================================
2019-11-25 17:40:18 -08:00
void S_StartSound(int32_t entnum, int32_t entchannel, sfx_t *sfx, vec3_t origin, float fvol, float attenuation)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
channel_t *target_chan, *check;
sfxcache_t *sc;
int32_t ch_idx;
int32_t skip;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(!sound_started)
2019-11-24 20:45:15 -08:00
return;
2019-11-25 17:40:18 -08:00
if(!sfx)
2019-11-24 20:45:15 -08:00
return;
2019-11-25 17:40:18 -08:00
if(nosound.value)
2019-11-24 20:45:15 -08:00
return;
// pick a channel to play on
target_chan = SND_PickChannel(entnum, entchannel);
2019-11-25 17:40:18 -08:00
if(!target_chan)
2019-11-24 20:45:15 -08:00
return;
// spatialize
2019-11-25 17:40:18 -08:00
memset(target_chan, 0, sizeof(*target_chan));
2019-11-24 20:45:15 -08:00
VectorCopy(origin, target_chan->origin);
target_chan->dist_mult = attenuation / sound_nominal_clip_dist;
2019-11-25 17:40:18 -08:00
target_chan->master_vol = (int32_t)(fvol * 255);
2019-11-24 20:45:15 -08:00
target_chan->entnum = entnum;
target_chan->entchannel = entchannel;
SND_Spatialize(target_chan);
2019-11-25 17:40:18 -08:00
if(!target_chan->leftvol && !target_chan->rightvol)
return; // not audible at all
2019-11-24 20:45:15 -08:00
// new channel
2019-11-25 17:40:18 -08:00
sc = S_LoadSound(sfx);
if(!sc)
2019-11-24 20:45:15 -08:00
{
target_chan->sfx = NULL;
2019-11-25 17:40:18 -08:00
return; // couldn't load the sound's data
2019-11-24 20:45:15 -08:00
}
target_chan->sfx = sfx;
target_chan->pos = 0.0;
target_chan->end = paintedtime + sc->length;
// if an identical sound has also been started this frame, offset the pos
// a bit to keep it from just making the first one louder
check = &snd_channels[NUM_AMBIENTS];
2019-11-25 17:40:18 -08:00
for(ch_idx = NUM_AMBIENTS; ch_idx < NUM_AMBIENTS + MAX_DYNAMIC_CHANNELS; ch_idx++, check++)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
if(check == target_chan)
2019-11-24 20:45:15 -08:00
continue;
2019-11-25 17:40:18 -08:00
if(check->sfx == sfx && !check->pos)
2019-11-24 20:45:15 -08:00
{
/*
2019-11-25 16:49:58 -08:00
skip = rand () % (int32_t)(0.1 * shm->speed);
2019-11-24 20:45:15 -08:00
if (skip >= target_chan->end)
2019-11-25 17:40:18 -08:00
skip = target_chan->end - 1;
2019-11-24 20:45:15 -08:00
*/
/* LordHavoc: fixed skip calculations */
skip = 0.1 * shm->speed; /* 0.1 * sc->speed */
2019-11-25 17:40:18 -08:00
if(skip > sc->length)
2019-11-24 20:45:15 -08:00
skip = sc->length;
2019-11-25 17:40:18 -08:00
if(skip > 0)
2019-11-24 20:45:15 -08:00
skip = rand() % skip;
target_chan->pos += skip;
target_chan->end -= skip;
break;
}
}
}
2019-11-25 17:40:18 -08:00
void S_StopSound(int32_t entnum, int32_t entchannel)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
int32_t i;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
for(i = 0; i < MAX_DYNAMIC_CHANNELS; i++)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
if(snd_channels[i].entnum == entnum
&& snd_channels[i].entchannel == entchannel)
2019-11-24 20:45:15 -08:00
{
snd_channels[i].end = 0;
snd_channels[i].sfx = NULL;
return;
}
}
}
2019-11-25 17:40:18 -08:00
void S_StopAllSounds(bool clear)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
int32_t i;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(!sound_started)
2019-11-24 20:45:15 -08:00
return;
2019-11-25 17:40:18 -08:00
total_channels = MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS; // no statics
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
for(i = 0; i < MAX_CHANNELS; i++)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
if(snd_channels[i].sfx)
2019-11-24 20:45:15 -08:00
snd_channels[i].sfx = NULL;
}
memset(snd_channels, 0, MAX_CHANNELS * sizeof(channel_t));
2019-11-25 17:40:18 -08:00
if(clear)
S_ClearBuffer();
2019-11-24 20:45:15 -08:00
}
2019-11-25 17:40:18 -08:00
static void S_StopAllSoundsC(void)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
S_StopAllSounds(true);
2019-11-24 20:45:15 -08:00
}
2019-11-25 17:40:18 -08:00
void S_ClearBuffer(void)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
int32_t clear;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(!sound_started || !shm)
2019-11-24 20:45:15 -08:00
return;
2019-11-25 17:40:18 -08:00
SNDDMA_LockBuffer();
if(! shm->buffer)
2019-11-24 20:45:15 -08:00
return;
s_rawend = 0;
2019-11-25 17:40:18 -08:00
if(shm->samplebits == 8 && !shm->signed8)
2019-11-24 20:45:15 -08:00
clear = 0x80;
else
clear = 0;
memset(shm->buffer, clear, shm->samples * shm->samplebits / 8);
2019-11-25 17:40:18 -08:00
SNDDMA_Submit();
2019-11-24 20:45:15 -08:00
}
/*
=================
S_StaticSound
=================
*/
2019-11-25 17:40:18 -08:00
void S_StaticSound(sfx_t *sfx, vec3_t origin, float vol, float attenuation)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
channel_t *ss;
sfxcache_t *sc;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(!sfx)
2019-11-24 20:45:15 -08:00
return;
2019-11-25 17:40:18 -08:00
if(total_channels == MAX_CHANNELS)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
Con_Printf("total_channels == MAX_CHANNELS\n");
2019-11-24 20:45:15 -08:00
return;
}
ss = &snd_channels[total_channels];
total_channels++;
2019-11-25 17:40:18 -08:00
sc = S_LoadSound(sfx);
if(!sc)
2019-11-24 20:45:15 -08:00
return;
2019-11-25 17:40:18 -08:00
if(sc->loopstart == -1)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
Con_Printf("Sound %s not looped\n", sfx->name);
2019-11-24 20:45:15 -08:00
return;
}
ss->sfx = sfx;
2019-11-25 17:40:18 -08:00
VectorCopy(origin, ss->origin);
2019-11-25 16:49:58 -08:00
ss->master_vol = (int32_t)vol;
2019-11-24 20:45:15 -08:00
ss->dist_mult = (attenuation / 64) / sound_nominal_clip_dist;
ss->end = paintedtime + sc->length;
2019-11-25 17:40:18 -08:00
SND_Spatialize(ss);
2019-11-24 20:45:15 -08:00
}
//=============================================================================
/*
===================
S_UpdateAmbientSounds
===================
*/
2019-11-25 17:40:18 -08:00
static void S_UpdateAmbientSounds(void)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
mleaf_t *l;
int32_t vol, ambient_channel;
channel_t *chan;
2019-11-24 20:45:15 -08:00
// no ambients when disconnected
2019-11-25 17:40:18 -08:00
if(cls.state != ca_connected)
2019-11-24 20:45:15 -08:00
return;
// calc ambient sound levels
2019-11-25 17:40:18 -08:00
if(!cl.worldmodel)
2019-11-24 20:45:15 -08:00
return;
2019-11-25 17:40:18 -08:00
l = Mod_PointInLeaf(listener_origin, cl.worldmodel);
if(!l || !ambient_level.value)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
for(ambient_channel = 0; ambient_channel < NUM_AMBIENTS; ambient_channel++)
2019-11-24 20:45:15 -08:00
snd_channels[ambient_channel].sfx = NULL;
return;
}
2019-11-25 17:40:18 -08:00
for(ambient_channel = 0; ambient_channel < NUM_AMBIENTS; ambient_channel++)
2019-11-24 20:45:15 -08:00
{
chan = &snd_channels[ambient_channel];
chan->sfx = ambient_sfx[ambient_channel];
2019-11-25 17:40:18 -08:00
vol = (int32_t)(ambient_level.value * l->ambient_sound_level[ambient_channel]);
if(vol < 8)
2019-11-24 20:45:15 -08:00
vol = 0;
2019-11-25 17:40:18 -08:00
// don't adjust volume too fast
if(chan->master_vol < vol)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
chan->master_vol += (int32_t)(host_frametime * ambient_fade.value);
if(chan->master_vol > vol)
2019-11-24 20:45:15 -08:00
chan->master_vol = vol;
}
2019-11-25 17:40:18 -08:00
else if(chan->master_vol > vol)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
chan->master_vol -= (int32_t)(host_frametime * ambient_fade.value);
if(chan->master_vol < vol)
2019-11-24 20:45:15 -08:00
chan->master_vol = vol;
}
chan->leftvol = chan->rightvol = chan->master_vol;
}
}
/*
===================
S_RawSamples (from q2)
2019-11-24 20:45:15 -08:00
Streaming music support. Byte swapping
of data must be handled by the codec.
Expects data in signed 16 bit, or unsigned
8 bit format.
===================
*/
2019-11-25 17:40:18 -08:00
void S_RawSamples(int32_t samples, int32_t rate, int32_t width, int32_t channels, byte *data, float volume)
2019-11-24 20:45:15 -08:00
{
2019-11-25 16:49:58 -08:00
int32_t i;
int32_t src, dst;
2019-11-24 20:45:15 -08:00
float scale;
2019-11-25 16:49:58 -08:00
int32_t intVolume;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(s_rawend < paintedtime)
2019-11-24 20:45:15 -08:00
s_rawend = paintedtime;
scale = (float) rate / shm->speed;
2019-11-25 17:40:18 -08:00
intVolume = (int32_t)(256 * volume);
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(channels == 2 && width == 2)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
for(i = 0; ; i++)
2019-11-24 20:45:15 -08:00
{
src = i * scale;
2019-11-25 17:40:18 -08:00
if(src >= samples)
2019-11-24 20:45:15 -08:00
break;
dst = s_rawend & (MAX_RAW_SAMPLES - 1);
s_rawend++;
2019-11-25 14:15:33 -08:00
s_rawsamples [dst].left = ((int16_t *) data)[src * 2] * intVolume;
s_rawsamples [dst].right = ((int16_t *) data)[src * 2 + 1] * intVolume;
2019-11-24 20:45:15 -08:00
}
}
2019-11-25 17:40:18 -08:00
else if(channels == 1 && width == 2)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
for(i = 0; ; i++)
2019-11-24 20:45:15 -08:00
{
src = i * scale;
2019-11-25 17:40:18 -08:00
if(src >= samples)
2019-11-24 20:45:15 -08:00
break;
dst = s_rawend & (MAX_RAW_SAMPLES - 1);
s_rawend++;
2019-11-25 14:15:33 -08:00
s_rawsamples [dst].left = ((int16_t *) data)[src] * intVolume;
s_rawsamples [dst].right = ((int16_t *) data)[src] * intVolume;
2019-11-24 20:45:15 -08:00
}
}
2019-11-25 17:40:18 -08:00
else if(channels == 2 && width == 1)
2019-11-24 20:45:15 -08:00
{
intVolume *= 256;
2019-11-25 17:40:18 -08:00
for(i = 0; ; i++)
2019-11-24 20:45:15 -08:00
{
src = i * scale;
2019-11-25 17:40:18 -08:00
if(src >= samples)
2019-11-24 20:45:15 -08:00
break;
dst = s_rawend & (MAX_RAW_SAMPLES - 1);
s_rawend++;
2019-11-25 17:40:18 -08:00
// s_rawsamples [dst].left = ((int8_t *) data)[src * 2] * intVolume;
// s_rawsamples [dst].right = ((int8_t *) data)[src * 2 + 1] * intVolume;
2019-11-24 20:45:15 -08:00
s_rawsamples [dst].left = (((byte *) data)[src * 2] - 128) * intVolume;
s_rawsamples [dst].right = (((byte *) data)[src * 2 + 1] - 128) * intVolume;
}
}
2019-11-25 17:40:18 -08:00
else if(channels == 1 && width == 1)
2019-11-24 20:45:15 -08:00
{
intVolume *= 256;
2019-11-25 17:40:18 -08:00
for(i = 0; ; i++)
2019-11-24 20:45:15 -08:00
{
src = i * scale;
2019-11-25 17:40:18 -08:00
if(src >= samples)
2019-11-24 20:45:15 -08:00
break;
dst = s_rawend & (MAX_RAW_SAMPLES - 1);
s_rawend++;
2019-11-25 17:40:18 -08:00
// s_rawsamples [dst].left = ((int8_t *) data)[src] * intVolume;
// s_rawsamples [dst].right = ((int8_t *) data)[src] * intVolume;
2019-11-24 20:45:15 -08:00
s_rawsamples [dst].left = (((byte *) data)[src] - 128) * intVolume;
s_rawsamples [dst].right = (((byte *) data)[src] - 128) * intVolume;
}
}
}
/*
============
S_Update
Called once each time through the main loop
============
*/
2019-11-25 17:40:18 -08:00
void S_Update(vec3_t origin, vec3_t forward, vec3_t right, vec3_t up)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
int32_t i, j;
int32_t total;
channel_t *ch;
channel_t *combine;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(!sound_started || (snd_blocked > 0))
2019-11-24 20:45:15 -08:00
return;
VectorCopy(origin, listener_origin);
VectorCopy(forward, listener_forward);
VectorCopy(right, listener_right);
VectorCopy(up, listener_up);
// update general area ambient sound sources
2019-11-25 17:40:18 -08:00
S_UpdateAmbientSounds();
2019-11-24 20:45:15 -08:00
combine = NULL;
// update spatialization for static and dynamic sounds
ch = snd_channels + NUM_AMBIENTS;
2019-11-25 17:40:18 -08:00
for(i = NUM_AMBIENTS; i < total_channels; i++, ch++)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
if(!ch->sfx)
2019-11-24 20:45:15 -08:00
continue;
2019-11-25 17:40:18 -08:00
SND_Spatialize(ch); // respatialize channel
if(!ch->leftvol && !ch->rightvol)
2019-11-24 20:45:15 -08:00
continue;
2019-11-25 17:40:18 -08:00
// try to combine static sounds with a previous channel of the same
// sound effect so we don't mix five torches every frame
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(i >= MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
// see if it can just use the last one
if(combine && combine->sfx == ch->sfx)
2019-11-24 20:45:15 -08:00
{
combine->leftvol += ch->leftvol;
combine->rightvol += ch->rightvol;
ch->leftvol = ch->rightvol = 0;
continue;
}
2019-11-25 17:40:18 -08:00
// search for one
2019-11-24 20:45:15 -08:00
combine = snd_channels + MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS;
2019-11-25 17:40:18 -08:00
for(j = MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS; j < i; j++, combine++)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
if(combine->sfx == ch->sfx)
2019-11-24 20:45:15 -08:00
break;
}
2019-11-25 17:40:18 -08:00
if(j == total_channels)
2019-11-24 20:45:15 -08:00
{
combine = NULL;
}
else
{
2019-11-25 17:40:18 -08:00
if(combine != ch)
2019-11-24 20:45:15 -08:00
{
combine->leftvol += ch->leftvol;
combine->rightvol += ch->rightvol;
ch->leftvol = ch->rightvol = 0;
}
continue;
}
}
}
//
// debugging output
//
2019-11-25 17:40:18 -08:00
if(snd_show.value)
2019-11-24 20:45:15 -08:00
{
total = 0;
ch = snd_channels;
2019-11-25 17:40:18 -08:00
for(i = 0; i < total_channels; i++, ch++)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
if(ch->sfx && (ch->leftvol || ch->rightvol))
2019-11-24 20:45:15 -08:00
{
total++;
}
}
2019-11-25 17:40:18 -08:00
Con_Printf("----(%" PRIi32 ")----\n", total);
2019-11-24 20:45:15 -08:00
}
// add raw data from streamed samples
2019-11-25 17:40:18 -08:00
// BGM_Update(); // moved to the main loop just before S_Update ()
2019-11-24 20:45:15 -08:00
// mix some sound
S_Update_();
}
2019-11-25 17:40:18 -08:00
static void GetSoundtime(void)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
int32_t samplepos;
static int32_t buffers;
static int32_t oldsamplepos;
int32_t fullsamples;
2019-11-24 20:45:15 -08:00
fullsamples = shm->samples / shm->channels;
// it is possible to miscount buffers if it has wrapped twice between
// calls to S_Update. Oh well.
samplepos = SNDDMA_GetDMAPos();
2019-11-25 17:40:18 -08:00
if(samplepos < oldsamplepos)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
buffers++; // buffer wrapped
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(paintedtime > 0x40000000)
{
// time to chop things off to avoid 32 bit limits
2019-11-24 20:45:15 -08:00
buffers = 0;
paintedtime = fullsamples;
2019-11-25 17:40:18 -08:00
S_StopAllSounds(true);
2019-11-24 20:45:15 -08:00
}
}
oldsamplepos = samplepos;
2019-11-25 17:40:18 -08:00
soundtime = buffers * fullsamples + samplepos / shm->channels;
2019-11-24 20:45:15 -08:00
}
2019-11-25 17:40:18 -08:00
void S_ExtraUpdate(void)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
if(snd_noextraupdate.value)
return; // don't pollute timings
2019-11-24 20:45:15 -08:00
S_Update_();
}
2019-11-25 17:40:18 -08:00
static void S_Update_(void)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
uint32_t endtime;
int32_t samps;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(!sound_started || (snd_blocked > 0))
2019-11-24 20:45:15 -08:00
return;
2019-11-25 17:40:18 -08:00
SNDDMA_LockBuffer();
if(! shm->buffer)
2019-11-24 20:45:15 -08:00
return;
// Updates DMA time
GetSoundtime();
// check to make sure that we haven't overshot
2019-11-25 17:40:18 -08:00
if(paintedtime < soundtime)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
// Con_Printf ("S_Update_ : overflow\n");
2019-11-24 20:45:15 -08:00
paintedtime = soundtime;
}
// mix ahead of current position
2019-12-07 09:27:26 -08:00
endtime = soundtime + (uint32_t)(snd_mixahead.value * shm->speed);
2019-11-24 20:45:15 -08:00
samps = shm->samples >> (shm->channels - 1);
2019-11-25 14:05:12 -08:00
endtime = q_min(endtime, (uint32_t)(soundtime + samps));
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
S_PaintChannels(endtime);
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
SNDDMA_Submit();
2019-11-24 20:45:15 -08:00
}
2019-11-25 17:40:18 -08:00
void S_BlockSound(void)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
/* FIXME: do we really need the blocking at the
* driver level?
*/
if(sound_started && snd_blocked == 0) /* ++snd_blocked == 1 */
2019-11-24 20:45:15 -08:00
{
snd_blocked = 1;
2019-11-25 17:40:18 -08:00
S_ClearBuffer();
if(shm)
2019-11-24 20:45:15 -08:00
SNDDMA_BlockSound();
}
}
2019-11-25 17:40:18 -08:00
void S_UnblockSound(void)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
if(!sound_started || !snd_blocked)
2019-11-24 20:45:15 -08:00
return;
2019-11-25 17:40:18 -08:00
if(snd_blocked == 1) /* --snd_blocked == 0 */
2019-11-24 20:45:15 -08:00
{
snd_blocked = 0;
SNDDMA_UnblockSound();
2019-11-25 17:40:18 -08:00
S_ClearBuffer();
2019-11-24 20:45:15 -08:00
}
}
/*
===============================================================================
console functions
===============================================================================
*/
2019-11-25 17:40:18 -08:00
static void S_Play(void)
2019-11-24 20:45:15 -08:00
{
2019-11-25 16:49:58 -08:00
static int32_t hash = 345;
2019-11-25 17:40:18 -08:00
int32_t i;
char name[256];
sfx_t *sfx;
2019-11-24 20:45:15 -08:00
i = 1;
2019-11-25 17:40:18 -08:00
while(i < Cmd_Argc())
2019-11-24 20:45:15 -08:00
{
q_strlcpy(name, Cmd_Argv(i), sizeof(name));
2019-12-07 09:27:26 -08:00
if(!strrchr(Cmd_Argv(i), '.'))
2019-11-24 20:45:15 -08:00
{
q_strlcat(name, ".wav", sizeof(name));
}
sfx = S_PrecacheSound(name);
S_StartSound(hash++, 0, sfx, listener_origin, 1.0, 1.0);
i++;
}
}
2019-11-25 17:40:18 -08:00
static void S_PlayVol(void)
2019-11-24 20:45:15 -08:00
{
2019-11-25 16:49:58 -08:00
static int32_t hash = 543;
2019-11-25 17:40:18 -08:00
int32_t i;
float vol;
char name[256];
sfx_t *sfx;
2019-11-24 20:45:15 -08:00
i = 1;
2019-11-25 17:40:18 -08:00
while(i < Cmd_Argc())
2019-11-24 20:45:15 -08:00
{
q_strlcpy(name, Cmd_Argv(i), sizeof(name));
2019-12-07 09:27:26 -08:00
if(!strrchr(Cmd_Argv(i), '.'))
2019-11-24 20:45:15 -08:00
{
q_strlcat(name, ".wav", sizeof(name));
}
sfx = S_PrecacheSound(name);
2019-12-07 09:27:26 -08:00
vol = atof(Cmd_Argv(i + 1));
2019-11-24 20:45:15 -08:00
S_StartSound(hash++, 0, sfx, listener_origin, vol, 1.0);
i += 2;
}
}
2019-11-25 17:40:18 -08:00
static void S_SoundList(void)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
int32_t i;
sfx_t *sfx;
sfxcache_t *sc;
int32_t size, total;
2019-11-24 20:45:15 -08:00
total = 0;
2019-11-25 17:40:18 -08:00
for(sfx = known_sfx, i = 0; i < num_sfx; i++, sfx++)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
sc = (sfxcache_t *) Cache_Check(&sfx->cache);
if(!sc)
2019-11-24 20:45:15 -08:00
continue;
2019-11-25 17:40:18 -08:00
size = sc->length * sc->width * (sc->stereo + 1);
2019-11-24 20:45:15 -08:00
total += size;
2019-11-25 17:40:18 -08:00
if(sc->loopstart >= 0)
Con_SafePrintf("L"); //johnfitz -- was Con_Printf
2019-11-24 20:45:15 -08:00
else
2019-11-25 17:40:18 -08:00
Con_SafePrintf(" "); //johnfitz -- was Con_Printf
Con_SafePrintf("(%2" PRIi32 "b) %6" PRIi32 " : %s\n", sc->width * 8, size, sfx->name); //johnfitz -- was Con_Printf
2019-11-24 20:45:15 -08:00
}
Con_Printf("%" PRIi32 " sounds, %" PRIi32 " bytes\n", num_sfx, total); //johnfitz -- added count
2019-11-24 20:45:15 -08:00
}
2019-11-25 17:40:18 -08:00
void S_LocalSound(const char *name)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
sfx_t *sfx;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(nosound.value)
2019-11-24 20:45:15 -08:00
return;
2019-11-25 17:40:18 -08:00
if(!sound_started)
2019-11-24 20:45:15 -08:00
return;
2019-11-25 17:40:18 -08:00
sfx = S_PrecacheSound(name);
if(!sfx)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
Con_Printf("S_LocalSound: can't cache %s\n", name);
2019-11-24 20:45:15 -08:00
return;
}
2019-11-25 17:40:18 -08:00
S_StartSound(cl.viewentity, -1, sfx, vec3_origin, 1, 1);
2019-11-24 20:45:15 -08:00
}
2019-11-25 17:40:18 -08:00
void S_ClearPrecache(void)
2019-11-24 20:45:15 -08:00
{
}
2019-11-25 17:40:18 -08:00
void S_BeginPrecaching(void)
2019-11-24 20:45:15 -08:00
{
}
2019-11-25 17:40:18 -08:00
void S_EndPrecaching(void)
2019-11-24 20:45:15 -08:00
{
}