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/sound/s_load.c

344 lines
6.9 KiB
C
Raw Normal View History

2009-08-07 22:00:00 +02:00
//=======================================================================
// Copyright XashXT Group 2007 <20>
// s_load.c - sound managment
//=======================================================================
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
{
2009-11-23 22:00:00 +01:00
if( !sfx->touchFrame )
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
2010-04-20 22:00:00 +02:00
if( sc->loopStart >= 0 ) Msg( "L" );
else Msg( " " );
Msg( " (%2db) %s : sound/%s\n", sc->width * 8, memprint( sc->size ), sfx->name );
2009-10-02 22:00:00 +02:00
totalSfx++;
2009-08-07 22:00:00 +02:00
}
}
2010-04-20 22:00:00 +02:00
Msg( "-------------------------------------------\n" );
Msg( "%i total sounds\n", totalSfx );
Msg( "%s total memory\n", memprint( totalSize ));
Msg( "\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
{
int i;
char *pcht = (char *)pch;
// 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
2010-11-20 22:00:00 +01:00
sc = Mem_Alloc( 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;
2010-11-20 22:00:00 +01:00
sc->buffer = Mem_Alloc( 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
{
2010-04-20 22:00:00 +02:00
wavdata_t *sc;
2009-08-07 22:00:00 +02:00
2009-10-02 22:00:00 +02:00
if( !sfx ) return NULL;
if( sfx->cache ) return sfx->cache; // see if still in memory
2009-08-07 22:00:00 +02:00
// load it from disk
2010-06-28 22:00:00 +02:00
if( sfx->name[0] == '*' )
sc = FS_LoadSound( sfx->name + 1, NULL, 0 );
else sc = FS_LoadSound( sfx->name, NULL, 0 );
2009-08-07 22:00:00 +02:00
2010-06-24 22:00:00 +02:00
if( !sc ) sc = S_CreateDefaultSound();
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
*/
2010-06-27 22:00:00 +02:00
sfx_t *S_FindName( const char *name, int *pfInCache )
2009-08-07 22:00:00 +02:00
{
int i;
2009-10-02 22:00:00 +02:00
sfx_t *sfx;
2009-11-23 22:00:00 +01:00
uint hash;
2009-08-07 22:00:00 +02:00
2010-11-20 22:00:00 +01:00
if( !name || !name[0] || !dma.initialized )
return NULL;
2009-10-02 22:00:00 +02:00
if( com.strlen( name ) >= MAX_STRING )
2009-08-07 22:00:00 +02:00
{
MsgDev( D_ERROR, "S_FindSound: sound name too long: %s", name );
return NULL;
}
2009-10-02 22:00:00 +02:00
// see if already loaded
2009-11-23 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
{
if( !com.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
2010-06-27 22:00:00 +02:00
sfx->touchFrame = 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-08-07 22:00:00 +02:00
{
2009-10-02 22:00:00 +02:00
if( !sfx->name[0] ) break; // free spot
2009-08-07 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
{
MsgDev( D_ERROR, "S_FindName: MAX_SFX limit exceeded\n" );
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];
2009-10-02 22:00:00 +02:00
Mem_Set( sfx, 0, sizeof( *sfx ));
2010-06-27 22:00:00 +02:00
if( pfInCache ) *pfInCache = false;
2009-08-07 22:00:00 +02:00
com.strncpy( sfx->name, name, MAX_STRING );
2010-06-27 22:00:00 +02:00
sfx->touchFrame = s_registration_sequence;
2009-11-23 22:00:00 +01:00
sfx->hashValue = Com_HashKey( sfx->name, MAX_SFX_HASH );
// 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;
if( !sfx || !sfx->name[0] ) return;
// 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;
}
2010-04-20 22:00:00 +02:00
if( sfx->cache ) FS_FreeSound( sfx->cache );
2009-11-23 22:00:00 +01:00
Mem_Set( sfx, 0, sizeof( *sfx ));
}
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 )
{
2010-06-27 22:00:00 +02:00
s_registration_sequence++;
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;
2010-11-20 22:00:00 +01:00
if( !dma.initialized ) return;
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
{
if( !sfx->name[0] ) continue;
2010-06-27 22:00:00 +02:00
if( sfx->touchFrame != 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
{
2009-10-02 22: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;
2010-11-20 22:00:00 +01:00
if( !dma.initialized ) return 0;
2010-06-27 22:00:00 +02:00
if( S_TestSoundChar( name, '!' ))
{
com.strncpy( s_sentenceImmediateName, name, sizeof( s_sentenceImmediateName ));
return SENTENCE_INDEX;
}
sfx = S_FindName( name, NULL );
2009-08-07 22:00:00 +02:00
if( !sfx ) return -1;
2010-06-27 22:00:00 +02:00
sfx->touchFrame = s_registration_sequence;
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 )
{
2010-11-20 22:00:00 +01:00
if( !dma.initialized )
return NULL;
2010-06-27 22:00:00 +02:00
if( handle == SENTENCE_INDEX )
{
// create new sfx
return S_FindName( s_sentenceImmediateName, NULL );
}
if( handle < 0 || handle >= s_numSfx )
2009-08-07 22:00:00 +02:00
{
2010-06-27 22:00:00 +02:00
MsgDev( D_ERROR, "S_GetSfxByHandle: handle %i out of range (%i)\n", handle, s_numSfx );
2009-08-07 22:00:00 +02:00
return NULL;
}
2010-06-27 22:00:00 +02:00
return &s_knownSfx[handle];
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
S_StopAllSounds();
// 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
2010-06-27 22:00:00 +02:00
Mem_Set( s_knownSfx, 0, sizeof( s_knownSfx ));
Mem_Set( 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
}