123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925 |
- #ifndef __SND_LOCAL_H__
- #define __SND_LOCAL_H__
- #ifdef _WIN32
- #include "../openal/include/al.h"
- #include "../openal/include/alc.h"
- #include "../openal/idal.h"
- #define ID_ALCHAR (ALubyte *)
- #elif defined( MACOS_X )
- #include <OpenAL/al.h>
- #include <OpenAL/alc.h>
- #define ID_ALCHAR
- #else
- #include <AL/al.h>
- #include <AL/alc.h>
- #define ID_ALCHAR
- #endif
- #include "../openal/include/efxlib.h"
- typedef enum {
- SCMD_STATE,
- SCMD_PLACE_LISTENER,
- SCMD_ALLOC_EMITTER,
- SCMD_FREE,
- SCMD_UPDATE,
- SCMD_START,
- SCMD_MODIFY,
- SCMD_STOP,
- SCMD_FADE
- } soundDemoCommand_t;
- const int SOUND_MAX_CHANNELS = 8;
- const int SOUND_DECODER_FREE_DELAY = 1000 * MIXBUFFER_SAMPLES / USERCMD_MSEC;
- const int PRIMARYFREQ = 44100;
- const float SND_EPSILON = 1.0f / 32768.0f;
- const int ROOM_SLICES_IN_BUFFER = 10;
- class idAudioHardware;
- class idAudioBuffer;
- class idWaveFile;
- class idSoundCache;
- class idSoundSample;
- class idSampleDecoder;
- class idSoundWorldLocal;
- #ifdef WIN32
- #pragma pack(1)
- #endif
- #ifdef __MWERKS__
- #pragma pack (push, 1)
- #endif
- struct waveformatex_s {
- word wFormatTag;
- word nChannels;
- dword nSamplesPerSec;
- dword nAvgBytesPerSec;
- word nBlockAlign;
- word wBitsPerSample;
- word cbSize;
- } PACKED;
- typedef waveformatex_s waveformatex_t;
- struct waveformat_s {
- word wFormatTag;
- word nChannels;
- dword nSamplesPerSec;
- dword nAvgBytesPerSec;
- word nBlockAlign;
- } PACKED;
- typedef waveformat_s waveformat_t;
- enum {
- WAVE_FORMAT_TAG_PCM = 1,
- WAVE_FORMAT_TAG_OGG = 2
- };
- struct pcmwaveformat_s {
- waveformat_t wf;
- word wBitsPerSample;
- } PACKED;
- typedef pcmwaveformat_s pcmwaveformat_t;
- #ifndef mmioFOURCC
- #define mmioFOURCC( ch0, ch1, ch2, ch3 ) \
- ( (dword)(byte)(ch0) | ( (dword)(byte)(ch1) << 8 ) | \
- ( (dword)(byte)(ch2) << 16 ) | ( (dword)(byte)(ch3) << 24 ) )
- #endif
- #define fourcc_riff mmioFOURCC('R', 'I', 'F', 'F')
- struct waveformatextensible_s {
- waveformatex_t Format;
- union {
- word wValidBitsPerSample;
- word wSamplesPerBlock;
- word wReserved;
- } Samples;
- dword dwChannelMask;
-
- int SubFormat;
- } PACKED;
- typedef waveformatextensible_s waveformatextensible_t;
- typedef dword fourcc;
- struct mminfo_s {
- fourcc ckid;
- dword cksize;
- fourcc fccType;
- dword dwDataOffset;
- } PACKED;
- typedef mminfo_s mminfo_t;
- #ifdef WIN32
- #pragma pack()
- #endif
- #ifdef __MWERKS__
- #pragma pack (pop)
- #endif
- class idWaveFile {
- public:
- idWaveFile( void );
- ~idWaveFile( void );
- int Open( const char* strFileName, waveformatex_t* pwfx = NULL );
- int OpenFromMemory( short* pbData, int ulDataSize, waveformatextensible_t* pwfx );
- int Read( byte* pBuffer, int dwSizeToRead, int *pdwSizeRead );
- int Seek( int offset );
- int Close( void );
- int ResetFile( void );
- int GetOutputSize( void ) { return mdwSize; }
- int GetMemorySize( void ) { return mMemSize; }
- waveformatextensible_t mpwfx;
- private:
- idFile * mhmmio;
- mminfo_t mck;
- mminfo_t mckRiff;
- dword mdwSize;
- dword mMemSize;
- dword mseekBase;
- ID_TIME_T mfileTime;
- bool mbIsReadingFromMemory;
- short * mpbData;
- short * mpbDataCur;
- dword mulDataSize;
- void * ogg;
- bool isOgg;
- private:
- int ReadMMIO( void );
- int OpenOGG( const char* strFileName, waveformatex_t* pwfx = NULL );
- int ReadOGG( byte* pBuffer, int dwSizeToRead, int *pdwSizeRead );
- int CloseOGG( void );
- };
- class idAudioHardware {
- public:
- static idAudioHardware *Alloc();
- virtual ~idAudioHardware();
- virtual bool Initialize( ) = 0;
- virtual bool Lock( void **pDSLockedBuffer, ulong *dwDSLockedBufferSize ) = 0;
- virtual bool Unlock( void *pDSLockedBuffer, dword dwDSLockedBufferSize ) = 0;
- virtual bool GetCurrentPosition( ulong *pdwCurrentWriteCursor ) = 0;
-
-
-
- virtual bool Flush( void ) = 0;
- virtual void Write( bool flushing ) = 0;
- virtual int GetNumberOfSpeakers( void )= 0;
- virtual int GetMixBufferSize( void ) = 0;
- virtual short* GetMixBuffer( void ) = 0;
- };
- class idAudioBuffer {
- public:
- virtual int Play( dword dwPriority=0, dword dwFlags=0 ) = 0;
- virtual int Stop( void ) = 0;
- virtual int Reset( void ) = 0;
- virtual bool IsSoundPlaying( void ) = 0;
- virtual void SetVolume( float x ) = 0;
- };
- typedef enum {
- REMOVE_STATUS_INVALID = -1,
- REMOVE_STATUS_ALIVE = 0,
- REMOVE_STATUS_WAITSAMPLEFINISHED = 1,
- REMOVE_STATUS_SAMPLEFINISHED = 2
- } removeStatus_t;
- class idSoundFade {
- public:
- int fadeStart44kHz;
- int fadeEnd44kHz;
- float fadeStartVolume;
- float fadeEndVolume;
- void Clear();
- float FadeDbAt44kHz( int current44kHz );
- };
- class SoundFX {
- protected:
- bool initialized;
- int channel;
- int maxlen;
- float* buffer;
- float continuitySamples[4];
- float param;
- public:
- SoundFX() { channel = 0; buffer = NULL; initialized = false; maxlen = 0; memset( continuitySamples, 0, sizeof( float ) * 4 ); };
- virtual ~SoundFX() { if ( buffer ) delete buffer; };
- virtual void Initialize() { };
- virtual void ProcessSample( float* in, float* out ) = 0;
- void SetChannel( int chan ) { channel = chan; };
- int GetChannel() { return channel; };
- void SetContinuitySamples( float in1, float in2, float out1, float out2 ) { continuitySamples[0] = in1; continuitySamples[1] = in2; continuitySamples[2] = out1; continuitySamples[3] = out2; };
- void GetContinuitySamples( float& in1, float& in2, float& out1, float& out2 ) { in1 = continuitySamples[0]; in2 = continuitySamples[1]; out1 = continuitySamples[2]; out2 = continuitySamples[3]; };
- void SetParameter( float val ) { param = val; };
- };
- class SoundFX_Lowpass : public SoundFX {
- public:
- virtual void ProcessSample( float* in, float* out );
- };
- class SoundFX_LowpassFast : public SoundFX {
- float freq;
- float res;
- float a1, a2, a3;
- float b1, b2;
- public:
- virtual void ProcessSample( float* in, float* out );
- void SetParms( float p1 = 0, float p2 = 0, float p3 = 0 );
- };
- class SoundFX_Comb : public SoundFX {
- int currentTime;
- public:
- virtual void Initialize();
- virtual void ProcessSample( float* in, float* out );
- };
- class FracTime {
- public:
- int time;
- float frac;
- void Set( int val ) { time = val; frac = 0; };
- void Increment( float val ) { frac += val; while ( frac >= 1.f ) { time++; frac--; } };
- };
- enum {
- PLAYBACK_RESET,
- PLAYBACK_ADVANCING
- };
- class idSoundChannel;
- class idSlowChannel {
- bool active;
- const idSoundChannel* chan;
- int playbackState;
- int triggerOffset;
- FracTime newPosition;
- int newSampleOffset;
- FracTime curPosition;
- int curSampleOffset;
- SoundFX_LowpassFast lowpass;
-
- void GenerateSlowChannel( FracTime& playPos, int sampleCount44k, float* finalBuffer );
- float GetSlowmoSpeed();
- public:
- void AttachSoundChannel( const idSoundChannel *chan );
- void Reset();
- void GatherChannelSamples( int sampleOffset44k, int sampleCount44k, float *dest );
- bool IsActive() { return active; };
- FracTime GetCurrentPosition() { return curPosition; };
- };
- class idSoundChannel {
- public:
- idSoundChannel( void );
- ~idSoundChannel( void );
- void Clear( void );
- void Start( void );
- void Stop( void );
- void GatherChannelSamples( int sampleOffset44k, int sampleCount44k, float *dest ) const;
- void ALStop( void );
- bool triggerState;
- int trigger44kHzTime;
- int triggerGame44kHzTime;
- soundShaderParms_t parms;
- idSoundSample * leadinSample;
- s_channelType triggerChannel;
- const idSoundShader *soundShader;
- idSampleDecoder * decoder;
- float diversity;
- float lastVolume;
- float lastV[6];
- idSoundFade channelFade;
- bool triggered;
- ALuint openalSource;
- ALuint openalStreamingOffset;
- ALuint openalStreamingBuffer[3];
- ALuint lastopenalStreamingBuffer[3];
- bool disallowSlow;
- };
- class idSoundEmitterLocal : public idSoundEmitter {
- public:
- idSoundEmitterLocal( void );
- virtual ~idSoundEmitterLocal( void );
-
-
-
-
- virtual void Free( bool immediate );
-
-
- virtual void UpdateEmitter( const idVec3 &origin, int listenerId, const soundShaderParms_t *parms );
-
- virtual int StartSound( const idSoundShader *shader, const s_channelType channel, float diversity = 0, int shaderFlags = 0, bool allowSlow = true );
-
- virtual void ModifySound( const s_channelType channel, const soundShaderParms_t *parms );
- virtual void StopSound( const s_channelType channel );
- virtual void FadeSound( const s_channelType channel, float to, float over );
- virtual bool CurrentlyPlaying( void ) const;
-
- virtual float CurrentAmplitude( void );
-
- virtual int Index( void ) const;
-
- void Clear( void );
- void OverrideParms( const soundShaderParms_t *base, const soundShaderParms_t *over, soundShaderParms_t *out );
- void CheckForCompletion( int current44kHzTime );
- void Spatialize( idVec3 listenerPos, int listenerArea, idRenderWorld *rw );
- idSoundWorldLocal * soundWorld;
- int index;
- removeStatus_t removeStatus;
- idVec3 origin;
- int listenerId;
- soundShaderParms_t parms;
-
- float maxDistance;
- int lastValidPortalArea;
- bool playing;
- bool hasShakes;
- idVec3 spatializedOrigin;
-
- float realDistance;
- float distance;
-
-
-
- idSoundChannel channels[SOUND_MAX_CHANNELS];
- idSlowChannel slowChannels[SOUND_MAX_CHANNELS];
- idSlowChannel GetSlowChannel( const idSoundChannel *chan );
- void SetSlowChannel( const idSoundChannel *chan, idSlowChannel slow );
- void ResetSlowChannel( const idSoundChannel *chan );
-
-
-
-
- int ampTime;
- float amplitude;
- };
- class s_stats {
- public:
- s_stats( void ) {
- rinuse = 0;
- runs = 1;
- timeinprocess = 0;
- missedWindow = 0;
- missedUpdateWindow = 0;
- activeSounds = 0;
- }
- int rinuse;
- int runs;
- int timeinprocess;
- int missedWindow;
- int missedUpdateWindow;
- int activeSounds;
- };
- typedef struct soundPortalTrace_s {
- int portalArea;
- const struct soundPortalTrace_s *prevStack;
- } soundPortalTrace_t;
- class idSoundWorldLocal : public idSoundWorld {
- public:
- virtual ~idSoundWorldLocal( void );
-
- virtual void ClearAllSoundEmitters( void );
- virtual void StopAllSounds( void );
-
- virtual idSoundEmitter *AllocSoundEmitter( void );
-
- virtual idSoundEmitter *EmitterForIndex( int index );
-
- virtual float CurrentShakeAmplitudeForPosition( const int time, const idVec3 &listererPosition );
-
-
- virtual void PlaceListener( const idVec3 &origin, const idMat3 &axis, const int listenerId, const int gameTime, const idStr& areaName );
-
-
- virtual void FadeSoundClasses( const int soundClass, const float to, const float over );
-
- virtual void StartWritingDemo( idDemoFile *demo );
- virtual void StopWritingDemo( void );
-
- virtual void ProcessDemoCommand( idDemoFile *readDemo );
-
- virtual void PlayShaderDirectly( const char *name, int channel = -1 );
-
- virtual void Pause( void );
- virtual void UnPause( void );
- virtual bool IsPaused( void );
-
- virtual void AVIOpen( const char *path, const char *name );
- virtual void AVIClose( void );
-
- virtual void WriteToSaveGame( idFile *savefile );
- virtual void ReadFromSaveGame( idFile *savefile );
-
- virtual void ReadFromSaveGameSoundChannel( idFile *saveGame, idSoundChannel *ch );
- virtual void ReadFromSaveGameSoundShaderParams( idFile *saveGame, soundShaderParms_t *params );
- virtual void WriteToSaveGameSoundChannel( idFile *saveGame, idSoundChannel *ch );
- virtual void WriteToSaveGameSoundShaderParams( idFile *saveGame, soundShaderParms_t *params );
-
- virtual void SetSlowmo( bool active );
- virtual void SetSlowmoSpeed( float speed );
- virtual void SetEnviroSuit( bool active );
-
- idSoundWorldLocal( void );
- void Shutdown( void );
- void Init( idRenderWorld *rw );
- void ClearBuffer( void );
-
- void ForegroundUpdate( int currentTime );
- void OffsetSoundTime( int offset44kHz );
- idSoundEmitterLocal * AllocLocalSoundEmitter();
- void CalcEars( int numSpeakers, idVec3 realOrigin, idVec3 listenerPos, idMat3 listenerAxis, float ears[6], float spatialize );
- void AddChannelContribution( idSoundEmitterLocal *sound, idSoundChannel *chan,
- int current44kHz, int numSpeakers, float *finalMixBuffer );
- void MixLoop( int current44kHz, int numSpeakers, float *finalMixBuffer );
- void AVIUpdate( void );
- void ResolveOrigin( const int stackDepth, const soundPortalTrace_t *prevStack, const int soundArea, const float dist, const idVec3& soundOrigin, idSoundEmitterLocal *def );
- float FindAmplitude( idSoundEmitterLocal *sound, const int localTime, const idVec3 *listenerPosition, const s_channelType channel, bool shakesOnly );
-
- idRenderWorld * rw;
- idDemoFile * writeDemo;
- idMat3 listenerAxis;
- idVec3 listenerPos;
- int listenerPrivateId;
- idVec3 listenerQU;
- int listenerArea;
- idStr listenerAreaName;
- int listenerEnvironmentID;
- int gameMsec;
- int game44kHz;
- int pause44kHz;
- int lastAVI44kHz;
- idList<idSoundEmitterLocal *>emitters;
- idSoundFade soundClassFade[SOUND_MAX_CLASSES];
-
- idFile * fpa[6];
- idStr aviDemoPath;
- idStr aviDemoName;
- idSoundEmitterLocal * localSound;
- bool slowmoActive;
- float slowmoSpeed;
- bool enviroSuitActive;
- };
- typedef struct {
- ALuint handle;
- int startTime;
- idSoundChannel *chan;
- bool inUse;
- bool looping;
- bool stereo;
- } openalSource_t;
- class idSoundSystemLocal : public idSoundSystem {
- public:
- idSoundSystemLocal( ) {
- isInitialized = false;
- }
-
- virtual void Init( void );
-
- virtual void Shutdown( void );
- virtual void ClearBuffer( void );
-
- virtual bool ShutdownHW( void );
- virtual bool InitHW( void );
-
- virtual int AsyncUpdate( int time );
-
- virtual int AsyncUpdateWrite( int time );
-
- virtual int AsyncMix( int soundTime, float *mixBuffer );
- virtual void SetMute( bool mute );
- virtual cinData_t ImageForTime( const int milliseconds, const bool waveform );
- int GetSoundDecoderInfo( int index, soundDecoderInfo_t &decoderInfo );
-
- virtual idSoundWorld *AllocSoundWorld( idRenderWorld *rw );
-
- virtual void SetPlayingSoundWorld( idSoundWorld *soundWorld );
-
-
- virtual idSoundWorld *GetPlayingSoundWorld( void );
- virtual void BeginLevelLoad( void );
- virtual void EndLevelLoad( const char *mapString );
- virtual void PrintMemInfo( MemInfo_t *mi );
- virtual int IsEAXAvailable( void );
-
- int GetCurrent44kHzTime( void ) const;
- float dB2Scale( const float val ) const;
- int SamplesToMilliseconds( int samples ) const;
- int MillisecondsToSamples( int ms ) const;
- void DoEnviroSuit( float* samples, int numSamples, int numSpeakers );
- ALuint AllocOpenALSource( idSoundChannel *chan, bool looping, bool stereo );
- void FreeOpenALSource( ALuint handle );
- idAudioHardware * snd_audio_hw;
- idSoundCache * soundCache;
- idSoundWorldLocal * currentSoundWorld;
- int olddwCurrentWritePos;
- int buffers;
- int CurrentSoundTime;
- unsigned int nextWriteBlock;
- float realAccum[6*MIXBUFFER_SAMPLES+16];
- float * finalMixBuffer;
- bool isInitialized;
- bool muted;
- bool shutdown;
- s_stats soundStats;
- int meterTops[256];
- int meterTopsTime[256];
- dword * graph;
- float volumesDB[1200];
- idList<SoundFX*> fxList;
- ALCdevice *openalDevice;
- ALCcontext *openalContext;
- ALsizei openalSourceCount;
- openalSource_t openalSources[256];
- EAXSet alEAXSet;
- EAXGet alEAXGet;
- EAXSetBufferMode alEAXSetBufferMode;
- EAXGetBufferMode alEAXGetBufferMode;
- idEFXFile EFXDatabase;
- bool efxloaded;
-
- static bool useOpenAL;
- static bool useEAXReverb;
-
- static int EAXAvailable;
- static idCVar s_noSound;
- static idCVar s_quadraticFalloff;
- static idCVar s_drawSounds;
- static idCVar s_minVolume6;
- static idCVar s_dotbias6;
- static idCVar s_minVolume2;
- static idCVar s_dotbias2;
- static idCVar s_spatializationDecay;
- static idCVar s_showStartSound;
- static idCVar s_maxSoundsPerShader;
- static idCVar s_reverse;
- static idCVar s_showLevelMeter;
- static idCVar s_meterTopTime;
- static idCVar s_volume;
- static idCVar s_constantAmplitude;
- static idCVar s_playDefaultSound;
- static idCVar s_useOcclusion;
- static idCVar s_subFraction;
- static idCVar s_globalFraction;
- static idCVar s_doorDistanceAdd;
- static idCVar s_singleEmitter;
- static idCVar s_numberOfSpeakers;
- static idCVar s_force22kHz;
- static idCVar s_clipVolumes;
- static idCVar s_realTimeDecoding;
- static idCVar s_libOpenAL;
- static idCVar s_useOpenAL;
- static idCVar s_useEAXReverb;
- static idCVar s_muteEAXReverb;
- static idCVar s_decompressionLimit;
- static idCVar s_slowAttenuate;
- static idCVar s_enviroSuitCutoffFreq;
- static idCVar s_enviroSuitCutoffQ;
- static idCVar s_enviroSuitSkipLowpass;
- static idCVar s_enviroSuitSkipReverb;
- static idCVar s_reverbTime;
- static idCVar s_reverbFeedback;
- static idCVar s_enviroSuitVolumeScale;
- static idCVar s_skipHelltimeFX;
- };
- extern idSoundSystemLocal soundSystemLocal;
- const int SCACHE_SIZE = MIXBUFFER_SAMPLES*20;
- class idSoundSample {
- public:
- idSoundSample();
- ~idSoundSample();
- idStr name;
- ID_TIME_T timestamp;
- waveformatex_t objectInfo;
- int objectSize;
- int objectMemSize;
- byte * nonCacheData;
- byte * amplitudeData;
- ALuint openalBuffer;
- bool hardwareBuffer;
- bool defaultSound;
- bool onDemand;
- bool purged;
- bool levelLoadReferenced;
- int LengthIn44kHzSamples() const;
- ID_TIME_T GetNewTimeStamp( void ) const;
- void MakeDefault();
- void Load();
- void Reload( bool force );
- void PurgeSoundSample();
- void CheckForDownSample();
- bool FetchFromCache( int offset, const byte **output, int *position, int *size, const bool allowIO );
- };
- class idSampleDecoder {
- public:
- static void Init( void );
- static void Shutdown( void );
- static idSampleDecoder *Alloc( void );
- static void Free( idSampleDecoder *decoder );
- static int GetNumUsedBlocks( void );
- static int GetUsedBlockMemory( void );
- virtual ~idSampleDecoder( void ) {}
- virtual void Decode( idSoundSample *sample, int sampleOffset44k, int sampleCount44k, float *dest ) = 0;
- virtual void ClearDecoder( void ) = 0;
- virtual idSoundSample * GetSample( void ) const = 0;
- virtual int GetLastDecodeTime( void ) const = 0;
- };
- class idSoundCache {
- public:
- idSoundCache();
- ~idSoundCache();
- idSoundSample * FindSound( const idStr &fname, bool loadOnDemandOnly );
- const int GetNumObjects( void ) { return listCache.Num(); }
- const idSoundSample * GetObject( const int index ) const;
- void ReloadSounds( bool force );
- void BeginLevelLoad();
- void EndLevelLoad();
- void PrintMemInfo( MemInfo_t *mi );
- private:
- bool insideLevelLoad;
- idList<idSoundSample*> listCache;
- };
- #endif /* !__SND_LOCAL_H__ */
|