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_dsp.c

161 lines
3.9 KiB
C
Raw Normal View History

2009-10-10 22:00:00 +02:00
//=======================================================================
// Copyright XashXT Group 2009 <20>
2009-10-11 22:00:00 +02:00
// s_dsp.c - digital signal processing algorithms for audio FX
2009-10-10 22:00:00 +02:00
//=======================================================================
2010-11-20 22:00:00 +01:00
#include "common.h"
2009-10-10 22:00:00 +02:00
#include "sound.h"
2011-01-09 22:00:00 +01:00
#include "room_int.h"
2011-02-22 22:00:00 +01:00
#include "client.h"
2011-01-09 22:00:00 +01:00
convar_t *dsp_room; // room dsp preset - sounds more distant from player (1ch)
convar_t *dsp_room_type;
convar_t *dsp_stereo; // set to 1 for true stereo processing. 2x perf hit.
int ipset_room_prev;
int ipset_room_typeprev;
int idsp_room;
static DSP_FUNCTIONS gDspFuncs;
2011-03-11 22:00:00 +01:00
static dll_info_t room_dll = { "room.dll", NULL, false };
2011-01-09 22:00:00 +01:00
// engine callbacks
static dsp_enginefuncs_t gEngfuncs =
{
2011-02-28 22:00:00 +01:00
Com_RandomFloat,
Com_RandomLong,
2011-01-09 22:00:00 +01:00
Con_Printf,
Con_DPrintf,
};
2009-10-10 22:00:00 +02:00
2011-01-09 22:00:00 +01:00
float DSP_GetGain( int idsp )
2010-09-16 22:00:00 +02:00
{
2011-01-09 22:00:00 +01:00
if( !room_dll.link ) return 0.0f;
return gDspFuncs.pfnDSP_GetGain( idsp );
2010-09-16 22:00:00 +02:00
}
2009-10-10 22:00:00 +02:00
2011-01-09 22:00:00 +01:00
void DSP_ClearState( void )
2010-09-16 22:00:00 +02:00
{
2011-01-09 22:00:00 +01:00
if( !room_dll.link ) return;
2010-09-16 22:00:00 +02:00
2011-01-09 22:00:00 +01:00
Cvar_SetFloat( "dsp_room", 0.0f );
Cvar_SetFloat( "room_type", 0.0f );
2010-09-16 22:00:00 +02:00
2011-01-09 22:00:00 +01:00
CheckNewDspPresets();
gDspFuncs.pfnVidInit();
2009-10-10 22:00:00 +02:00
}
2011-01-09 22:00:00 +01:00
void DSP_Process( int idsp, portable_samplepair_t *pbfront, int sampleCount )
2009-10-10 22:00:00 +02:00
{
2011-01-09 22:00:00 +01:00
ASSERT( pbfront );
2009-10-10 22:00:00 +02:00
2011-01-09 22:00:00 +01:00
// return right away if fx processing is turned off
if( !room_dll.link || dsp_off->integer ) return;
2009-10-10 22:00:00 +02:00
2011-01-09 22:00:00 +01:00
gDspFuncs.pfnDSP_Process( idsp, pbfront, sampleCount );
2010-09-16 22:00:00 +02:00
}
2009-10-10 22:00:00 +02:00
2011-01-09 22:00:00 +01:00
// free all dsp processors
void FreeDsps( void )
2010-09-16 22:00:00 +02:00
{
2011-01-09 22:00:00 +01:00
if( !room_dll.link ) return;
2009-10-10 22:00:00 +02:00
2011-01-09 22:00:00 +01:00
gDspFuncs.pfnDSP_Free( idsp_room );
idsp_room = 0;
2010-09-16 22:00:00 +02:00
2011-01-09 22:00:00 +01:00
gDspFuncs.pfnShutdown();
2009-10-10 22:00:00 +02:00
2011-01-09 22:00:00 +01:00
Sys_FreeLibrary( &room_dll );
2011-03-10 22:00:00 +01:00
Q_memset( &gDspFuncs, 0, sizeof( gDspFuncs ));
2009-10-10 22:00:00 +02:00
}
2011-01-09 22:00:00 +01:00
// alloc dsp processors
qboolean AllocDsps( void )
2009-10-10 22:00:00 +02:00
{
2011-01-09 22:00:00 +01:00
static ROOMAPI InitDSP;
static dsp_enginefuncs_t gpEngfuncs;
2009-10-10 22:00:00 +02:00
2011-03-11 22:00:00 +01:00
if( !Sys_LoadLibrary( &room_dll ))
2009-11-13 22:00:00 +01:00
{
2011-01-09 22:00:00 +01:00
MsgDev( D_INFO, "DSP: disabled\n" );
2010-09-16 22:00:00 +02:00
return false;
}
2011-01-09 22:00:00 +01:00
InitDSP = (ROOMAPI)Sys_GetProcAddress( &room_dll, "InitDSP" );
2010-09-16 22:00:00 +02:00
2011-01-09 22:00:00 +01:00
if( !InitDSP )
2009-10-10 22:00:00 +02:00
{
2011-01-09 22:00:00 +01:00
Sys_FreeLibrary( &room_dll );
MsgDev( D_ERROR, "AllocDsps: failed to get address of InitDSP proc\n" );
return false;
2010-09-16 22:00:00 +02:00
}
2009-10-10 22:00:00 +02:00
2011-01-09 22:00:00 +01:00
// make local copy of engfuncs to prevent overwrite it with user dll
2011-03-10 22:00:00 +01:00
Q_memcpy( &gpEngfuncs, &gEngfuncs, sizeof( gpEngfuncs ));
2009-10-10 22:00:00 +02:00
2011-01-09 22:00:00 +01:00
if( !InitDSP( &gDspFuncs, &gpEngfuncs, DSP_VERSION ))
2010-09-16 22:00:00 +02:00
{
2011-01-09 22:00:00 +01:00
Sys_FreeLibrary( &room_dll );
MsgDev( D_ERROR, "AllocDsps: can't init DSP\n" );
return false;
2009-10-10 22:00:00 +02:00
}
2011-01-09 22:00:00 +01:00
gDspFuncs.pfnInit();
2009-10-10 22:00:00 +02:00
2010-09-16 22:00:00 +02:00
idsp_room = -1.0;
2009-10-10 22:00:00 +02:00
2011-01-06 22:00:00 +01:00
// initialize DSP cvars
dsp_room = Cvar_Get( "dsp_room", "0", 0, "room dsp preset - sounds more distant from player (1ch)" );
dsp_room_type = Cvar_Get( "room_type", "0", 0, "duplicate for dsp_room cvar for backward compatibility" );
dsp_stereo = Cvar_Get( "dsp_stereo", "0", 0, "set to 1 for true stereo processing. 2x perf hits" );
2010-09-16 22:00:00 +02:00
// alloc dsp room channel (mono, stereo if dsp_stereo is 1)
2009-10-10 22:00:00 +02:00
2010-09-16 22:00:00 +02:00
// dsp room is mono, 300ms fade time
2011-01-09 22:00:00 +01:00
idsp_room = gDspFuncs.pfnDSP_Alloc( dsp_room->integer, 300, dsp_stereo->integer * 2 );
2009-10-10 22:00:00 +02:00
2010-09-16 22:00:00 +02:00
// init prev values
ipset_room_prev = dsp_room->integer;
ipset_room_typeprev = dsp_room_type->integer;
2009-10-10 22:00:00 +02:00
2010-09-16 22:00:00 +02:00
if( idsp_room < 0 )
{
MsgDev( D_WARN, "DSP processor failed to initialize! \n" );
2009-10-10 22:00:00 +02:00
2010-09-16 22:00:00 +02:00
FreeDsps();
return false;
}
return true;
}
2009-10-10 22:00:00 +02:00
2010-09-16 22:00:00 +02:00
void CheckNewDspPresets( void )
2009-10-10 22:00:00 +02:00
{
2011-01-09 22:00:00 +01:00
int iroomtype;
2010-09-16 22:00:00 +02:00
int iroom;
2009-10-10 22:00:00 +02:00
2011-01-09 22:00:00 +01:00
if( !room_dll.link ) return;
iroomtype = dsp_room_type->integer;
2010-06-27 22:00:00 +02:00
if( s_listener.waterlevel > 2 )
2010-09-16 22:00:00 +02:00
iroom = 15;
2011-02-22 22:00:00 +01:00
else if( s_listener.inmenu && !cl.background )
2010-09-16 22:00:00 +02:00
iroom = 0;
else iroom = dsp_room->integer;
2009-10-10 22:00:00 +02:00
2010-09-16 22:00:00 +02:00
// legacy code support for "room_type" Cvar
if( iroomtype != ipset_room_typeprev )
2009-10-10 22:00:00 +02:00
{
2010-09-16 22:00:00 +02:00
// force dsp_room = room_type
ipset_room_typeprev = iroomtype;
2010-10-31 22:00:00 +01:00
Cvar_SetFloat( "dsp_room", iroomtype );
2009-10-10 22:00:00 +02:00
}
2010-09-16 22:00:00 +02:00
if( iroom != ipset_room_prev )
2009-10-10 22:00:00 +02:00
{
2011-01-09 22:00:00 +01:00
gDspFuncs.pfnDSP_SetPreset( idsp_room, iroom );
2010-09-16 22:00:00 +02:00
ipset_room_prev = iroom;
2009-10-10 22:00:00 +02:00
2010-09-16 22:00:00 +02:00
// force room_type = dsp_room
2010-10-31 22:00:00 +01:00
Cvar_SetFloat( "room_type", iroom );
2010-09-16 22:00:00 +02:00
ipset_room_typeprev = iroom;
}
2009-10-10 22:00:00 +02:00
}