📄 snd_dma.c
字号:
/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.
This file is part of Quake III Arena source code.
Quake III Arena source code 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.
Quake III Arena source code 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 Foobar; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
/*****************************************************************************
* name: snd_dma.c
*
* desc: main control for any streaming sound output device
*
* $Archive: /MissionPack/code/client/snd_dma.c $
*
*****************************************************************************/
#include "snd_local.h"
#include "client.h"
void S_Play_f(void);
void S_SoundList_f(void);
void S_Music_f(void);
void S_Update_();
void S_StopAllSounds(void);
void S_UpdateBackgroundTrack( void );
static fileHandle_t s_backgroundFile;
static wavinfo_t s_backgroundInfo;
//int s_nextWavChunk;
static int s_backgroundSamples;
static char s_backgroundLoop[MAX_QPATH];
//static char s_backgroundMusic[MAX_QPATH]; //TTimo: unused
// =======================================================================
// Internal sound data & structures
// =======================================================================
// only begin attenuating sound volumes when outside the FULLVOLUME range
#define SOUND_FULLVOLUME 80
#define SOUND_ATTENUATE 0.0008f
channel_t s_channels[MAX_CHANNELS];
channel_t loop_channels[MAX_CHANNELS];
int numLoopChannels;
static int s_soundStarted;
static qboolean s_soundMuted;
dma_t dma;
static int listener_number;
static vec3_t listener_origin;
static vec3_t listener_axis[3];
int s_soundtime; // sample PAIRS
int s_paintedtime; // sample PAIRS
// MAX_SFX may be larger than MAX_SOUNDS because
// of custom player sounds
#define MAX_SFX 4096
sfx_t s_knownSfx[MAX_SFX];
int s_numSfx = 0;
#define LOOP_HASH 128
static sfx_t *sfxHash[LOOP_HASH];
cvar_t *s_volume;
cvar_t *s_testsound;
cvar_t *s_khz;
cvar_t *s_show;
cvar_t *s_mixahead;
cvar_t *s_mixPreStep;
cvar_t *s_musicVolume;
cvar_t *s_separation;
cvar_t *s_doppler;
static loopSound_t loopSounds[MAX_GENTITIES];
static channel_t *freelist = NULL;
int s_rawend;
portable_samplepair_t s_rawsamples[MAX_RAW_SAMPLES];
// ====================================================================
// User-setable variables
// ====================================================================
void S_SoundInfo_f(void) {
Com_Printf("----- Sound Info -----\n" );
if (!s_soundStarted) {
Com_Printf ("sound system not started\n");
} else {
if ( s_soundMuted ) {
Com_Printf ("sound system is muted\n");
}
Com_Printf("%5d stereo\n", dma.channels - 1);
Com_Printf("%5d samples\n", dma.samples);
Com_Printf("%5d samplebits\n", dma.samplebits);
Com_Printf("%5d submission_chunk\n", dma.submission_chunk);
Com_Printf("%5d speed\n", dma.speed);
Com_Printf("0x%x dma buffer\n", dma.buffer);
if ( s_backgroundFile ) {
Com_Printf("Background file: %s\n", s_backgroundLoop );
} else {
Com_Printf("No background file.\n" );
}
}
Com_Printf("----------------------\n" );
}
/*
================
S_Init
================
*/
void S_Init( void ) {
cvar_t *cv;
qboolean r;
Com_Printf("\n------- sound initialization -------\n");
s_volume = Cvar_Get ("s_volume", "0.8", CVAR_ARCHIVE);
s_musicVolume = Cvar_Get ("s_musicvolume", "0.25", CVAR_ARCHIVE);
s_separation = Cvar_Get ("s_separation", "0.5", CVAR_ARCHIVE);
s_doppler = Cvar_Get ("s_doppler", "1", CVAR_ARCHIVE);
s_khz = Cvar_Get ("s_khz", "22", CVAR_ARCHIVE);
s_mixahead = Cvar_Get ("s_mixahead", "0.2", CVAR_ARCHIVE);
s_mixPreStep = Cvar_Get ("s_mixPreStep", "0.05", CVAR_ARCHIVE);
s_show = Cvar_Get ("s_show", "0", CVAR_CHEAT);
s_testsound = Cvar_Get ("s_testsound", "0", CVAR_CHEAT);
cv = Cvar_Get ("s_initsound", "1", 0);
if ( !cv->integer ) {
Com_Printf ("not initializing.\n");
Com_Printf("------------------------------------\n");
return;
}
Cmd_AddCommand("play", S_Play_f);
Cmd_AddCommand("music", S_Music_f);
Cmd_AddCommand("s_list", S_SoundList_f);
Cmd_AddCommand("s_info", S_SoundInfo_f);
Cmd_AddCommand("s_stop", S_StopAllSounds);
r = SNDDMA_Init();
Com_Printf("------------------------------------\n");
if ( r ) {
s_soundStarted = 1;
s_soundMuted = 1;
// s_numSfx = 0;
Com_Memset(sfxHash, 0, sizeof(sfx_t *)*LOOP_HASH);
s_soundtime = 0;
s_paintedtime = 0;
S_StopAllSounds ();
S_SoundInfo_f();
}
}
void S_ChannelFree(channel_t *v) {
v->thesfx = NULL;
*(channel_t **)v = freelist;
freelist = (channel_t*)v;
}
channel_t* S_ChannelMalloc() {
channel_t *v;
if (freelist == NULL) {
return NULL;
}
v = freelist;
freelist = *(channel_t **)freelist;
v->allocTime = Com_Milliseconds();
return v;
}
void S_ChannelSetup() {
channel_t *p, *q;
// clear all the sounds so they don't
Com_Memset( s_channels, 0, sizeof( s_channels ) );
p = s_channels;;
q = p + MAX_CHANNELS;
while (--q > p) {
*(channel_t **)q = q-1;
}
*(channel_t **)q = NULL;
freelist = p + MAX_CHANNELS - 1;
Com_DPrintf("Channel memory manager started\n");
}
// =======================================================================
// Shutdown sound engine
// =======================================================================
void S_Shutdown( void ) {
if ( !s_soundStarted ) {
return;
}
SNDDMA_Shutdown();
s_soundStarted = 0;
Cmd_RemoveCommand("play");
Cmd_RemoveCommand("music");
Cmd_RemoveCommand("stopsound");
Cmd_RemoveCommand("soundlist");
Cmd_RemoveCommand("soundinfo");
}
// =======================================================================
// Load a sound
// =======================================================================
/*
================
return a hash value for the sfx name
================
*/
static long S_HashSFXName(const char *name) {
int i;
long hash;
char letter;
hash = 0;
i = 0;
while (name[i] != '\0') {
letter = tolower(name[i]);
if (letter =='.') break; // don't include extension
if (letter =='\\') letter = '/'; // damn path names
hash+=(long)(letter)*(i+119);
i++;
}
hash &= (LOOP_HASH-1);
return hash;
}
/*
==================
S_FindName
Will allocate a new sfx if it isn't found
==================
*/
static sfx_t *S_FindName( const char *name ) {
int i;
int hash;
sfx_t *sfx;
if (!name) {
Com_Error (ERR_FATAL, "S_FindName: NULL\n");
}
if (!name[0]) {
Com_Error (ERR_FATAL, "S_FindName: empty name\n");
}
if (strlen(name) >= MAX_QPATH) {
Com_Error (ERR_FATAL, "Sound name too long: %s", name);
}
hash = S_HashSFXName(name);
sfx = sfxHash[hash];
// see if already loaded
while (sfx) {
if (!Q_stricmp(sfx->soundName, name) ) {
return sfx;
}
sfx = sfx->next;
}
// find a free sfx
for (i=0 ; i < s_numSfx ; i++) {
if (!s_knownSfx[i].soundName[0]) {
break;
}
}
if (i == s_numSfx) {
if (s_numSfx == MAX_SFX) {
Com_Error (ERR_FATAL, "S_FindName: out of sfx_t");
}
s_numSfx++;
}
sfx = &s_knownSfx[i];
Com_Memset (sfx, 0, sizeof(*sfx));
strcpy (sfx->soundName, name);
sfx->next = sfxHash[hash];
sfxHash[hash] = sfx;
return sfx;
}
/*
=================
S_DefaultSound
=================
*/
void S_DefaultSound( sfx_t *sfx ) {
int i;
sfx->soundLength = 512;
sfx->soundData = SND_malloc();
sfx->soundData->next = NULL;
for ( i = 0 ; i < sfx->soundLength ; i++ ) {
sfx->soundData->sndChunk[i] = i;
}
}
/*
===================
S_DisableSounds
Disables sounds until the next S_BeginRegistration.
This is called when the hunk is cleared and the sounds
are no longer valid.
===================
*/
void S_DisableSounds( void ) {
S_StopAllSounds();
s_soundMuted = qtrue;
}
/*
=====================
S_BeginRegistration
=====================
*/
void S_BeginRegistration( void ) {
s_soundMuted = qfalse; // we can play again
if (s_numSfx == 0) {
SND_setup();
s_numSfx = 0;
Com_Memset( s_knownSfx, 0, sizeof( s_knownSfx ) );
Com_Memset(sfxHash, 0, sizeof(sfx_t *)*LOOP_HASH);
S_RegisterSound("sound/feedback/hit.wav", qfalse); // changed to a sound in baseq3
}
}
/*
==================
S_RegisterSound
Creates a default buzz sound if the file can't be loaded
==================
*/
sfxHandle_t S_RegisterSound( const char *name, qboolean compressed ) {
sfx_t *sfx;
compressed = qfalse;
if (!s_soundStarted) {
return 0;
}
if ( strlen( name ) >= MAX_QPATH ) {
Com_Printf( "Sound name exceeds MAX_QPATH\n" );
return 0;
}
sfx = S_FindName( name );
if ( sfx->soundData ) {
if ( sfx->defaultSound ) {
Com_Printf( S_COLOR_YELLOW "WARNING: could not find %s - using default\n", sfx->soundName );
return 0;
}
return sfx - s_knownSfx;
}
sfx->inMemory = qfalse;
sfx->soundCompressed = compressed;
S_memoryLoad(sfx);
if ( sfx->defaultSound ) {
Com_Printf( S_COLOR_YELLOW "WARNING: could not find %s - using default\n", sfx->soundName );
return 0;
}
return sfx - s_knownSfx;
}
void S_memoryLoad(sfx_t *sfx) {
// load the sound file
if ( !S_LoadSound ( sfx ) ) {
// Com_Printf( S_COLOR_YELLOW "WARNING: couldn't load sound: %s\n", sfx->soundName );
sfx->defaultSound = qtrue;
}
sfx->inMemory = qtrue;
}
//=============================================================================
/*
=================
S_SpatializeOrigin
Used for spatializing s_channels
=================
*/
void S_SpatializeOrigin (vec3_t origin, int master_vol, int *left_vol, int *right_vol)
{
vec_t dot;
vec_t dist;
vec_t lscale, rscale, scale;
vec3_t source_vec;
vec3_t vec;
const float dist_mult = SOUND_ATTENUATE;
// calculate stereo seperation and distance attenuation
VectorSubtract(origin, listener_origin, source_vec);
dist = VectorNormalize(source_vec);
dist -= SOUND_FULLVOLUME;
if (dist < 0)
dist = 0; // close enough to be at full volume
dist *= dist_mult; // different attenuation levels
VectorRotate( source_vec, listener_axis, vec );
dot = -vec[1];
if (dma.channels == 1)
{ // no attenuation = no spatialization
rscale = 1.0;
lscale = 1.0;
}
else
{
rscale = 0.5 * (1.0 + dot);
lscale = 0.5 * (1.0 - dot);
//rscale = s_separation->value + ( 1.0 - s_separation->value ) * dot;
//lscale = s_separation->value - ( 1.0 - s_separation->value ) * dot;
if ( rscale < 0 ) {
rscale = 0;
}
if ( lscale < 0 ) {
lscale = 0;
}
}
// add in distance effect
scale = (1.0 - dist) * rscale;
*right_vol = (master_vol * scale);
if (*right_vol < 0)
*right_vol = 0;
scale = (1.0 - dist) * lscale;
*left_vol = (master_vol * scale);
if (*left_vol < 0)
*left_vol = 0;
}
// =======================================================================
// Start a sound effect
// =======================================================================
/*
====================
S_StartSound
Validates the parms and ques the sound up
if pos is NULL, the sound will be dynamically sourced from the entity
Entchannel 0 will never override a playing sound
====================
*/
void S_StartSound(vec3_t origin, int entityNum, int entchannel, sfxHandle_t sfxHandle ) {
channel_t *ch;
sfx_t *sfx;
int i, oldest, chosen, time;
int inplay, allowed;
if ( !s_soundStarted || s_soundMuted ) {
return;
}
if ( !origin && ( entityNum < 0 || entityNum > MAX_GENTITIES ) ) {
Com_Error( ERR_DROP, "S_StartSound: bad entitynum %i", entityNum );
}
if ( sfxHandle < 0 || sfxHandle >= s_numSfx ) {
Com_Printf( S_COLOR_YELLOW, "S_StartSound: handle %i out of range\n", sfxHandle );
return;
}
sfx = &s_knownSfx[ sfxHandle ];
if (sfx->inMemory == qfalse) {
S_memoryLoad(sfx);
}
if ( s_show->integer == 1 ) {
Com_Printf( "%i : %s\n", s_paintedtime, sfx->soundName );
}
time = Com_Milliseconds();
// Com_Printf("playing %s\n", sfx->soundName);
// pick a channel to play on
allowed = 4;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -