This repository has been archived on 2022-06-27. You can view files and clone it, but cannot push or open issues or pull requests.
Xash3DArchive/engine/client/s_load.c

410 lines
8.9 KiB
C
Raw Normal View History

2011-05-09 22:00:00 +02:00
/*
s_load.c - sounds managment
Copyright (C) 2007 Uncle Mike
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 3 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.
*/
2009-08-07 22:00:00 +02:00
2010-11-20 22:00:00 +01:00
#include "common.h"
2009-08-07 22:00:00 +02:00
#include "sound.h"
2009-10-02 22:00:00 +02:00
// during registration it is possible to have more sounds
// than could actually be referenced during gameplay,
// because we don't want to free anything until we are
// sure we won't need it.
2010-06-27 22:00:00 +02:00
#define MAX_SFX 8192
2009-11-23 22:00:00 +01:00
#define MAX_SFX_HASH (MAX_SFX/4)
2009-10-02 22:00:00 +02:00
2010-06-27 22:00:00 +02:00
static int s_numSfx = 0;
static sfx_t s_knownSfx[MAX_SFX];
static sfx_t *s_sfxHashList[MAX_SFX_HASH];
static string s_sentenceImmediateName; // keep dummy sentence name
2010-10-26 22:00:00 +02:00
qboolean s_registering = false;
2010-06-27 22:00:00 +02:00
int s_registration_sequence = 0;
2009-08-07 22:00:00 +02:00
/*
=================
S_SoundList_f
=================
*/
void S_SoundList_f( void )
{
2009-10-02 22:00:00 +02:00
sfx_t *sfx;
2010-04-20 22:00:00 +02:00
wavdata_t *sc;
int i, totalSfx = 0;
2009-10-02 22:00:00 +02:00
int totalSize = 0;
2009-08-07 22:00:00 +02:00
2010-06-27 22:00:00 +02:00
for( i = 0, sfx = s_knownSfx; i < s_numSfx; i++, sfx++ )
2009-08-07 22:00:00 +02:00
{
2018-09-29 23:00:00 +02:00
if( !sfx->name[0] )
2009-10-02 22:00:00 +02:00
continue;
2009-08-07 22:00:00 +02:00
2009-10-02 22:00:00 +02:00
sc = sfx->cache;
if( sc )
2009-08-07 22:00:00 +02:00
{
2010-04-20 22:00:00 +02:00
totalSize += sc->size;
2009-10-02 22:00:00 +02:00
2018-03-03 22:00:00 +01:00
if( sc->loopStart >= 0 ) Con_Printf( "L" );
else Con_Printf( " " );
2018-09-29 23:00:00 +02:00
if( sfx->name[0] == '*' )
Con_Printf( " (%2db) %s : %s\n", sc->width * 8, Q_memprint( sc->size ), sfx->name );
else Con_Printf( " (%2db) %s : %s%s\n", sc->width * 8, Q_memprint( sc->size ), DEFAULT_SOUNDPATH, sfx->name );
2009-10-02 22:00:00 +02:00
totalSfx++;
2009-08-07 22:00:00 +02:00
}
}
2018-03-03 22:00:00 +01:00
Con_Printf( "-------------------------------------------\n" );
Con_Printf( "%i total sounds\n", totalSfx );
Con_Printf( "%s total memory\n", Q_memprint( totalSize ));
Con_Printf( "\n" );
2009-08-07 22:00:00 +02:00
}
2009-10-11 22:00:00 +02:00
// return true if char 'c' is one of 1st 2 characters in pch
2010-10-26 22:00:00 +02:00
qboolean S_TestSoundChar( const char *pch, char c )
2009-10-11 22:00:00 +02:00
{
char *pcht = (char *)pch;
2016-11-15 22:00:00 +01:00
int i;
2009-10-11 22:00:00 +02:00
2012-01-27 21:00:00 +01:00
if( !pch || !*pch )
return false;
2009-10-11 22:00:00 +02:00
// check first 2 characters
for( i = 0; i < 2; i++ )
{
if( *pcht == c )
return true;
pcht++;
}
return false;
}
2010-04-21 22:00:00 +02:00
// return pointer to first valid character in file name
char *S_SkipSoundChar( const char *pch )
{
char *pcht = (char *)pch;
// check first character
if( *pcht == '!' )
pcht++;
return pcht;
}
2009-08-07 22:00:00 +02:00
/*
=================
S_CreateDefaultSound
=================
*/
2010-04-20 22:00:00 +02:00
static wavdata_t *S_CreateDefaultSound( void )
2009-08-07 22:00:00 +02:00
{
2010-04-20 22:00:00 +02:00
wavdata_t *sc;
2009-08-07 22:00:00 +02:00
2018-05-26 23:00:00 +02:00
sc = Mem_Calloc( sndpool, sizeof( wavdata_t ));
2009-08-07 22:00:00 +02:00
2010-04-20 22:00:00 +02:00
sc->width = 2;
sc->channels = 1;
sc->loopStart = -1;
2010-10-02 22:00:00 +02:00
sc->rate = SOUND_DMA_SPEED;
sc->samples = SOUND_DMA_SPEED;
2010-04-20 22:00:00 +02:00
sc->size = sc->samples * sc->width * sc->channels;
2018-05-26 23:00:00 +02:00
sc->buffer = Mem_Calloc( sndpool, sc->size );
2009-08-07 22:00:00 +02:00
2010-04-20 22:00:00 +02:00
return sc;
2009-08-07 22:00:00 +02:00
}
/*
=================
S_LoadSound
=================
*/
2010-04-20 22:00:00 +02:00
wavdata_t *S_LoadSound( sfx_t *sfx )
2009-08-07 22:00:00 +02:00
{
2011-09-03 22:00:00 +02:00
wavdata_t *sc = NULL;
2009-08-07 22:00:00 +02:00
2009-10-02 22:00:00 +02:00
if( !sfx ) return NULL;
2009-08-07 22:00:00 +02:00
2018-09-28 23:00:00 +02:00
// see if still in memory
if( sfx->cache )
return sfx->cache;
if( !COM_CheckString( sfx->name ))
return NULL;
2009-08-07 22:00:00 +02:00
2018-09-28 23:00:00 +02:00
// load it from disk
if( Q_stricmp( sfx->name, "*default" ))
2018-10-16 23:00:00 +02:00
{
// load it from disk
if( sfx->name[0] == '*' )
sc = FS_LoadSound( sfx->name + 1, NULL, 0 );
else sc = FS_LoadSound( sfx->name, NULL, 0 );
}
2010-06-24 22:00:00 +02:00
if( !sc ) sc = S_CreateDefaultSound();
2011-09-03 22:00:00 +02:00
if( sc->rate < SOUND_11k ) // some bad sounds
Sound_Process( &sc, SOUND_11k, sc->width, SOUND_RESAMPLE );
else if( sc->rate > SOUND_11k && sc->rate < SOUND_22k ) // some bad sounds
Sound_Process( &sc, SOUND_22k, sc->width, SOUND_RESAMPLE );
else if( sc->rate > SOUND_22k && sc->rate <= SOUND_32k ) // some bad sounds
Sound_Process( &sc, SOUND_44k, sc->width, SOUND_RESAMPLE );
2016-11-15 22:00:00 +01:00
2010-09-10 22:00:00 +02:00
sfx->cache = sc;
2009-08-07 22:00:00 +02:00
2009-10-02 22:00:00 +02:00
return sfx->cache;
2009-08-07 22:00:00 +02:00
}
// =======================================================================
// Load a sound
// =======================================================================
/*
2009-10-02 22:00:00 +02:00
==================
2010-06-27 22:00:00 +02:00
S_FindName
2009-10-02 22:00:00 +02:00
==================
2009-08-07 22:00:00 +02:00
*/
2011-10-14 22:00:00 +02:00
sfx_t *S_FindName( const char *pname, int *pfInCache )
2009-08-07 22:00:00 +02:00
{
2009-10-02 22:00:00 +02:00
sfx_t *sfx;
2011-10-14 22:00:00 +02:00
uint i, hash;
string name;
2009-08-07 22:00:00 +02:00
2018-03-14 22:00:00 +01:00
if( !COM_CheckString( pname ) || !dma.initialized )
2010-11-20 22:00:00 +01:00
return NULL;
2018-09-28 23:00:00 +02:00
if( Q_strlen( pname ) >= sizeof( sfx->name ))
2009-08-07 22:00:00 +02:00
return NULL;
2011-10-14 22:00:00 +02:00
Q_strncpy( name, pname, sizeof( name ));
COM_FixSlashes( name );
2009-10-02 22:00:00 +02:00
// see if already loaded
2018-02-28 22:00:00 +01:00
hash = COM_HashKey( name, MAX_SFX_HASH );
2010-06-27 22:00:00 +02:00
for( sfx = s_sfxHashList[hash]; sfx; sfx = sfx->hashNext )
2009-10-02 22:00:00 +02:00
{
2011-03-09 22:00:00 +01:00
if( !Q_strcmp( sfx->name, name ))
2009-08-07 22:00:00 +02:00
{
2010-06-27 22:00:00 +02:00
if( pfInCache )
{
// indicate whether or not sound is currently in the cache.
*pfInCache = ( sfx->cache != NULL ) ? true : false;
}
2009-08-07 22:00:00 +02:00
// prolonge registration
2018-03-07 22:00:00 +01:00
sfx->servercount = s_registration_sequence;
2009-08-07 22:00:00 +02:00
return sfx;
}
2009-10-02 22:00:00 +02:00
}
2009-08-07 22:00:00 +02:00
// find a free sfx slot spot
2010-06-27 22:00:00 +02:00
for( i = 0, sfx = s_knownSfx; i < s_numSfx; i++, sfx++)
2009-10-02 22:00:00 +02:00
if( !sfx->name[0] ) break; // free spot
2011-10-06 22:00:00 +02:00
2010-06-27 22:00:00 +02:00
if( i == s_numSfx )
2009-08-07 22:00:00 +02:00
{
2010-06-27 22:00:00 +02:00
if( s_numSfx == MAX_SFX )
2009-08-07 22:00:00 +02:00
return NULL;
2010-06-27 22:00:00 +02:00
s_numSfx++;
2009-08-07 22:00:00 +02:00
}
2009-10-02 22:00:00 +02:00
2010-06-27 22:00:00 +02:00
sfx = &s_knownSfx[i];
2016-11-17 22:00:00 +01:00
memset( sfx, 0, sizeof( *sfx ));
2010-06-27 22:00:00 +02:00
if( pfInCache ) *pfInCache = false;
2011-03-09 22:00:00 +01:00
Q_strncpy( sfx->name, name, MAX_STRING );
2018-03-07 22:00:00 +01:00
sfx->servercount = s_registration_sequence;
2018-02-28 22:00:00 +01:00
sfx->hashValue = COM_HashKey( sfx->name, MAX_SFX_HASH );
2009-11-23 22:00:00 +01:00
// link it in
2010-06-27 22:00:00 +02:00
sfx->hashNext = s_sfxHashList[sfx->hashValue];
s_sfxHashList[sfx->hashValue] = sfx;
2009-11-23 22:00:00 +01:00
2009-08-07 22:00:00 +02:00
return sfx;
}
2009-11-23 22:00:00 +01:00
/*
==================
S_FreeSound
==================
*/
2010-09-12 22:00:00 +02:00
void S_FreeSound( sfx_t *sfx )
2009-11-23 22:00:00 +01:00
{
sfx_t *hashSfx;
sfx_t **prev;
2018-09-28 23:00:00 +02:00
if( !sfx || !sfx->name[0] )
return;
2009-11-23 22:00:00 +01:00
// de-link it from the hash tree
2010-06-27 22:00:00 +02:00
prev = &s_sfxHashList[sfx->hashValue];
2009-11-23 22:00:00 +01:00
while( 1 )
{
hashSfx = *prev;
if( !hashSfx )
break;
if( hashSfx == sfx )
{
*prev = hashSfx->hashNext;
break;
}
prev = &hashSfx->hashNext;
}
2018-09-28 23:00:00 +02:00
if( sfx->cache )
FS_FreeSound( sfx->cache );
2016-11-17 22:00:00 +01:00
memset( sfx, 0, sizeof( *sfx ));
2009-11-23 22:00:00 +01:00
}
2009-08-07 22:00:00 +02:00
/*
=====================
S_BeginRegistration
2009-10-02 22:00:00 +02:00
2009-08-07 22:00:00 +02:00
=====================
*/
void S_BeginRegistration( void )
{
2011-09-03 22:00:00 +02:00
int i;
2010-06-27 22:00:00 +02:00
s_registration_sequence++;
2011-09-03 22:00:00 +02:00
snd_ambient = false;
// check for automatic ambient sounds
for( i = 0; i < NUM_AMBIENTS; i++ )
{
if( !GI->ambientsound[i][0] )
continue; // empty slot
ambient_sfx[i] = S_RegisterSound( GI->ambientsound[i] );
if( ambient_sfx[i] ) snd_ambient = true; // allow auto-ambients
}
2018-09-28 23:00:00 +02:00
s_registering = true;
2009-08-07 22:00:00 +02:00
}
/*
=====================
S_EndRegistration
2009-10-02 22:00:00 +02:00
2009-08-07 22:00:00 +02:00
=====================
*/
void S_EndRegistration( void )
{
sfx_t *sfx;
int i;
2018-03-14 22:00:00 +01:00
if( !s_registering || !dma.initialized )
return;
2010-11-20 22:00:00 +01:00
2009-08-07 22:00:00 +02:00
// free any sounds not from this registration sequence
2010-06-27 22:00:00 +02:00
for( i = 0, sfx = s_knownSfx; i < s_numSfx; i++, sfx++ )
2009-08-07 22:00:00 +02:00
{
2018-10-16 23:00:00 +02:00
if( !sfx->name[0] || !Q_stricmp( sfx->name, "*default" ))
2018-09-28 23:00:00 +02:00
continue; // don't release default sound
2018-03-07 22:00:00 +01:00
if( sfx->servercount != s_registration_sequence )
2009-11-23 22:00:00 +01:00
S_FreeSound( sfx ); // don't need this sound
2009-08-07 22:00:00 +02:00
}
// load everything in
2010-06-27 22:00:00 +02:00
for( i = 0, sfx = s_knownSfx; i < s_numSfx; i++, sfx++ )
2009-08-07 22:00:00 +02:00
{
2018-09-28 23:00:00 +02:00
if( !sfx->name[0] )
continue;
2009-10-11 22:00:00 +02:00
S_LoadSound( sfx );
2009-08-07 22:00:00 +02:00
}
2010-06-27 22:00:00 +02:00
s_registering = false;
2009-08-07 22:00:00 +02:00
}
/*
2009-10-02 22:00:00 +02:00
==================
2009-08-07 22:00:00 +02:00
S_RegisterSound
2009-10-02 22:00:00 +02:00
==================
2009-08-07 22:00:00 +02:00
*/
sound_t S_RegisterSound( const char *name )
{
sfx_t *sfx;
2018-03-14 22:00:00 +01:00
if( !COM_CheckString( name ) || !dma.initialized )
return -1;
2010-11-20 22:00:00 +01:00
2010-06-27 22:00:00 +02:00
if( S_TestSoundChar( name, '!' ))
{
2011-03-09 22:00:00 +01:00
Q_strncpy( s_sentenceImmediateName, name, sizeof( s_sentenceImmediateName ));
2010-06-27 22:00:00 +02:00
return SENTENCE_INDEX;
}
2012-05-10 22:00:00 +02:00
// some stupid mappers used leading '/' or '\' in path to models or sounds
if( name[0] == '/' || name[0] == '\\' ) name++;
if( name[0] == '/' || name[0] == '\\' ) name++;
2010-06-27 22:00:00 +02:00
sfx = S_FindName( name, NULL );
2009-08-07 22:00:00 +02:00
if( !sfx ) return -1;
2018-03-07 22:00:00 +01:00
sfx->servercount = s_registration_sequence;
2010-06-27 22:00:00 +02:00
if( !s_registering ) S_LoadSound( sfx );
2009-08-07 22:00:00 +02:00
2010-06-27 22:00:00 +02:00
return sfx - s_knownSfx;
2009-08-07 22:00:00 +02:00
}
sfx_t *S_GetSfxByHandle( sound_t handle )
{
2018-09-28 23:00:00 +02:00
if( !dma.initialized )
2010-11-20 22:00:00 +01:00
return NULL;
2018-09-28 23:00:00 +02:00
// create new sfx
2010-06-27 22:00:00 +02:00
if( handle == SENTENCE_INDEX )
return S_FindName( s_sentenceImmediateName, NULL );
if( handle < 0 || handle >= s_numSfx )
2009-08-07 22:00:00 +02:00
return NULL;
2018-09-28 23:00:00 +02:00
2010-06-27 22:00:00 +02:00
return &s_knownSfx[handle];
2009-08-07 22:00:00 +02:00
}
2018-09-28 23:00:00 +02:00
/*
=================
S_InitSounds
=================
*/
void S_InitSounds( void )
{
// create unused 0-entry
Q_strncpy( s_knownSfx->name, "*default", MAX_QPATH );
s_knownSfx->hashValue = COM_HashKey( s_knownSfx->name, MAX_SFX_HASH );
s_knownSfx->hashNext = s_sfxHashList[s_knownSfx->hashValue];
s_sfxHashList[s_knownSfx->hashValue] = s_knownSfx;
s_knownSfx->cache = S_CreateDefaultSound();
s_numSfx = 1;
}
2009-08-07 22:00:00 +02:00
/*
=================
S_FreeSounds
=================
*/
void S_FreeSounds( void )
{
sfx_t *sfx;
int i;
2010-11-20 22:00:00 +01:00
if( !dma.initialized )
return;
2009-08-07 22:00:00 +02:00
// stop all sounds
2017-06-23 23:00:00 +02:00
S_StopAllSounds( true );
2009-08-07 22:00:00 +02:00
// free all sounds
2010-06-27 22:00:00 +02:00
for( i = 0, sfx = s_knownSfx; i < s_numSfx; i++, sfx++ )
2009-11-23 22:00:00 +01:00
S_FreeSound( sfx );
2009-08-07 22:00:00 +02:00
2016-11-17 22:00:00 +01:00
memset( s_knownSfx, 0, sizeof( s_knownSfx ));
memset( s_sfxHashList, 0, sizeof( s_sfxHashList ));
2010-04-20 22:00:00 +02:00
2010-06-27 22:00:00 +02:00
s_numSfx = 0;
2009-08-07 22:00:00 +02:00
}