snd_local.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #ifndef __SND_LOCAL_H__
  21. #define __SND_LOCAL_H__
  22. #include "WaveFile.h"
  23. // Maximum number of voices we can have allocated
  24. #define MAX_HARDWARE_VOICES 48
  25. // A single voice can play multiple channels (up to 5.1, but most commonly stereo)
  26. // This is the maximum number of channels which can play simultaneously
  27. // This is limited primarily by seeking on the optical drive, secondarily by memory consumption, and tertiarily by CPU time spent mixing
  28. #define MAX_HARDWARE_CHANNELS 64
  29. // We may need up to 3 buffers for each hardware voice if they are all long sounds
  30. #define MAX_SOUND_BUFFERS ( MAX_HARDWARE_VOICES * 3 )
  31. // Maximum number of channels in a sound sample
  32. #define MAX_CHANNELS_PER_VOICE 8
  33. /*
  34. ========================
  35. MsecToSamples
  36. SamplesToMsec
  37. ========================
  38. */
  39. ID_INLINE_EXTERN uint32 MsecToSamples( uint32 msec, uint32 sampleRate ) { return ( msec * ( sampleRate / 100 ) ) / 10; }
  40. ID_INLINE_EXTERN uint32 SamplesToMsec( uint32 samples, uint32 sampleRate ) { return sampleRate < 100 ? 0 : ( samples * 10 ) / ( sampleRate / 100 ); }
  41. /*
  42. ========================
  43. DBtoLinear
  44. LinearToDB
  45. ========================
  46. */
  47. ID_INLINE_EXTERN float DBtoLinear( float db ) { return idMath::Pow( 2.0f, db * ( 1.0f / 6.0f ) ); }
  48. ID_INLINE_EXTERN float LinearToDB( float linear ) { return ( linear > 0.0f ) ? ( idMath::Log( linear ) * ( 6.0f / 0.693147181f ) ) : -999.0f; }
  49. // demo sound commands
  50. typedef enum {
  51. SCMD_STATE, // followed by a load game state
  52. SCMD_PLACE_LISTENER,
  53. SCMD_ALLOC_EMITTER,
  54. SCMD_FREE,
  55. SCMD_UPDATE,
  56. SCMD_START,
  57. SCMD_MODIFY,
  58. SCMD_STOP,
  59. SCMD_FADE
  60. } soundDemoCommand_t;
  61. #include "SoundVoice.h"
  62. #define OPERATION_SET 1
  63. #include <dxsdkver.h>
  64. #include <xaudio2.h>
  65. #include <xaudio2fx.h>
  66. #include <X3DAudio.h>
  67. #include <xma2defs.h>
  68. #include "XAudio2/XA2_SoundSample.h"
  69. #include "XAudio2/XA2_SoundVoice.h"
  70. #include "XAudio2/XA2_SoundHardware.h"
  71. //------------------------
  72. // Listener data
  73. //------------------------
  74. struct listener_t {
  75. idMat3 axis; // orientation of the listener
  76. idVec3 pos; // position in meters
  77. int id; // the entity number, used to detect when a sound is local
  78. int area; // area number the listener is in
  79. };
  80. class idSoundFade {
  81. public:
  82. int fadeStartTime;
  83. int fadeEndTime;
  84. float fadeStartVolume;
  85. float fadeEndVolume;
  86. public:
  87. idSoundFade() { Clear(); }
  88. void Clear();
  89. void SetVolume( float to );
  90. void Fade( float to, int length, int soundTime );
  91. float GetVolume( int soundTime ) const;
  92. };
  93. /*
  94. ================================================
  95. idSoundChannel
  96. ================================================
  97. */
  98. class idSoundChannel {
  99. public:
  100. bool CanMute() const;
  101. void Mute();
  102. bool CheckForCompletion( int currentTime );
  103. void UpdateVolume( int currentTime );
  104. void UpdateHardware( float volumeAdd, int currentTime );
  105. // returns true if this channel is marked as looping
  106. bool IsLooping() const;
  107. class idSoundEmitterLocal * emitter;
  108. int startTime;
  109. int endTime;
  110. int logicalChannel;
  111. bool allowSlow;
  112. soundShaderParms_t parms; // combines shader parms and per-channel overrides
  113. const idSoundShader * soundShader;
  114. idSoundSample * leadinSample;
  115. idSoundSample * loopingSample;
  116. idSoundFade volumeFade;
  117. float volumeDB; // last volume at which this channel will play (calculated in UpdateVolume)
  118. float currentAmplitude; // current amplitude on the hardware voice
  119. // hardwareVoice will be freed and NULL'd when a sound is out of range,
  120. // and reallocated when it comes back in range
  121. idSoundVoice * hardwareVoice;
  122. // only allocated by the soundWorld block allocator
  123. idSoundChannel();
  124. ~idSoundChannel();
  125. };
  126. // Maximum number of SoundChannels for a single SoundEmitter.
  127. // This is probably excessive...
  128. const int MAX_CHANNELS_PER_EMITTER = 16;
  129. /*
  130. ===================================================================================
  131. idSoundWorldLocal
  132. ===================================================================================
  133. */
  134. class idSoundWorldLocal : public idSoundWorld {
  135. public:
  136. idSoundWorldLocal();
  137. virtual ~idSoundWorldLocal();
  138. //------------------------
  139. // Functions from idSoundWorld, implemented in SoundWorld.cpp
  140. //------------------------
  141. // Called at map start
  142. virtual void ClearAllSoundEmitters();
  143. // stop all playing sounds
  144. virtual void StopAllSounds();
  145. // get a new emitter that can play sounds in this world
  146. virtual idSoundEmitter *AllocSoundEmitter();
  147. // for load games
  148. virtual idSoundEmitter *EmitterForIndex( int index );
  149. // query data from all emitters in the world
  150. virtual float CurrentShakeAmplitude();
  151. // where is the camera
  152. virtual void PlaceListener( const idVec3 &origin, const idMat3 &axis, const int listenerId );
  153. // fade all sounds in the world with a given shader soundClass
  154. // to is in Db, over is in seconds
  155. virtual void FadeSoundClasses( const int soundClass, const float to, const float over );
  156. // dumps the current state and begins archiving commands
  157. virtual void StartWritingDemo( idDemoFile *demo );
  158. virtual void StopWritingDemo();
  159. // read a sound command from a demo file
  160. virtual void ProcessDemoCommand( idDemoFile *readDemo );
  161. // menu sounds
  162. virtual int PlayShaderDirectly( const char *name, int channel = -1 );
  163. virtual void Skip( int time );
  164. virtual void Pause();
  165. virtual void UnPause();
  166. virtual bool IsPaused() { return isPaused; }
  167. virtual int GetSoundTime();
  168. // avidump
  169. virtual void AVIOpen( const char *path, const char *name );
  170. virtual void AVIClose();
  171. // SaveGame Support
  172. virtual void WriteToSaveGame( idFile *savefile );
  173. virtual void ReadFromSaveGame( idFile *savefile );
  174. virtual void SetSlowmoSpeed( float speed );
  175. virtual void SetEnviroSuit( bool active );
  176. //=======================================
  177. //------------------------
  178. // Random stuff that's not exposed outside the sound system
  179. //------------------------
  180. void Update();
  181. void OnReloadSound( const idDecl *decl );
  182. idSoundChannel * AllocSoundChannel();
  183. void FreeSoundChannel( idSoundChannel * );
  184. public:
  185. // even though all these variables are public, nobody outside the sound system includes SoundWorld_local.h
  186. // so this is equivalent to making it private and friending all the other classes in the sound system
  187. idSoundFade volumeFade; // master volume knob for the entire world
  188. idSoundFade soundClassFade[SOUND_MAX_CLASSES];
  189. idRenderWorld * renderWorld; // for debug visualization and light amplitude sampling
  190. idDemoFile * writeDemo; // if not NULL, archive commands here
  191. float currentCushionDB; // channels at or below this level will be faded to 0
  192. float shakeAmp; // last calculated shake amplitude
  193. listener_t listener;
  194. idList<idSoundEmitterLocal *, TAG_AUDIO> emitters;
  195. idSoundEmitter * localSound; // for PlayShaderDirectly()
  196. idBlockAlloc<idSoundEmitterLocal, 16> emitterAllocator;
  197. idBlockAlloc<idSoundChannel, 16> channelAllocator;
  198. idSoundFade pauseFade;
  199. int pausedTime;
  200. int accumulatedPauseTime;
  201. bool isPaused;
  202. float slowmoSpeed;
  203. bool enviroSuitActive;
  204. public:
  205. struct soundPortalTrace_t {
  206. int portalArea;
  207. const soundPortalTrace_t * prevStack;
  208. };
  209. void ResolveOrigin( const int stackDepth, const soundPortalTrace_t * prevStack, const int soundArea, const float dist, const idVec3 & soundOrigin, idSoundEmitterLocal * def );
  210. };
  211. /*
  212. ================================================
  213. idSoundEmitterLocal
  214. ================================================
  215. */
  216. class idSoundEmitterLocal : public idSoundEmitter {
  217. public:
  218. virtual void Free( bool immediate );
  219. virtual void Reset();
  220. virtual void UpdateEmitter( const idVec3 &origin, int listenerId, const soundShaderParms_t *parms );
  221. virtual int StartSound( const idSoundShader *shader, const s_channelType channel, float diversity = 0, int shaderFlags = 0, bool allowSlow = true );
  222. virtual void ModifySound( const s_channelType channel, const soundShaderParms_t *parms );
  223. virtual void StopSound( const s_channelType channel );
  224. virtual void FadeSound( const s_channelType channel, float to, float over );
  225. virtual bool CurrentlyPlaying( const s_channelType channel = SCHANNEL_ANY ) const;
  226. virtual float CurrentAmplitude();
  227. virtual int Index() const;
  228. //----------------------------------------------
  229. void Init( int i, idSoundWorldLocal * sw );
  230. // Returns true if the emitter should be freed.
  231. bool CheckForCompletion( int currentTime );
  232. void OverrideParms( const soundShaderParms_t * base, const soundShaderParms_t * over, soundShaderParms_t * out );
  233. void Update( int currentTime );
  234. void OnReloadSound( const idDecl *decl );
  235. //----------------------------------------------
  236. idSoundWorldLocal * soundWorld; // the world that holds this emitter
  237. int index; // in world emitter list
  238. bool canFree; // if true, this emitter can be canFree (once channels.Num() == 0)
  239. // a single soundEmitter can have many channels playing from the same point
  240. idStaticList<idSoundChannel *, MAX_CHANNELS_PER_EMITTER> channels;
  241. //----- set by UpdateEmitter -----
  242. idVec3 origin;
  243. soundShaderParms_t parms;
  244. int emitterId; // sounds will be full volume when emitterId == listenerId
  245. //----- set by Update -----
  246. int lastValidPortalArea;
  247. float directDistance;
  248. float spatializedDistance;
  249. idVec3 spatializedOrigin;
  250. // sound emitters are only allocated by the soundWorld block allocator
  251. idSoundEmitterLocal();
  252. virtual ~idSoundEmitterLocal();
  253. };
  254. /*
  255. ===================================================================================
  256. idSoundSystemLocal
  257. ===================================================================================
  258. */
  259. class idSoundSystemLocal : public idSoundSystem {
  260. public:
  261. // all non-hardware initialization
  262. virtual void Init();
  263. // shutdown routine
  264. virtual void Shutdown();
  265. virtual idSoundWorld * AllocSoundWorld( idRenderWorld *rw );
  266. virtual void FreeSoundWorld( idSoundWorld *sw );
  267. // specifying NULL will cause silence to be played
  268. virtual void SetPlayingSoundWorld( idSoundWorld *soundWorld );
  269. // some tools, like the sound dialog, may be used in both the game and the editor
  270. // This can return NULL, so check!
  271. virtual idSoundWorld * GetPlayingSoundWorld();
  272. // sends the current playing sound world information to the sound hardware
  273. virtual void Render();
  274. // Mutes the SSG_MUSIC group
  275. virtual void MuteBackgroundMusic( bool mute ) { musicMuted = mute; }
  276. // sets the final output volume to 0
  277. // This should only be used when the app is deactivated
  278. // Since otherwise there will be problems with different subsystems muting and unmuting at different times
  279. virtual void SetMute( bool mute ) { muted = mute; }
  280. virtual bool IsMuted() { return muted; }
  281. virtual void OnReloadSound( const idDecl * sound );
  282. virtual void StopAllSounds();
  283. virtual void InitStreamBuffers();
  284. virtual void FreeStreamBuffers();
  285. virtual void * GetIXAudio2() const;
  286. // for the sound level meter window
  287. virtual cinData_t ImageForTime( const int milliseconds, const bool waveform );
  288. // Free all sounds loaded during the last map load
  289. virtual void BeginLevelLoad();
  290. // We might want to defer the loading of new sounds to this point
  291. virtual void EndLevelLoad();
  292. // prints memory info
  293. virtual void PrintMemInfo( MemInfo_t *mi );
  294. //-------------------------
  295. // Before a sound is reloaded, any active voices using it must
  296. // be stopped. Returns true if any were playing, and should be
  297. // restarted after the sound is reloaded.
  298. void StopVoicesWithSample( const idSoundSample * const sample );
  299. void Restart();
  300. void SetNeedsRestart() { needsRestart = true; }
  301. int SoundTime() const;
  302. // may return NULL if there are no more voices left
  303. idSoundVoice * AllocateVoice( const idSoundSample * leadinSample, const idSoundSample * loopingSample );
  304. void FreeVoice( idSoundVoice * );
  305. idSoundSample * LoadSample( const char * name );
  306. virtual void Preload( idPreloadManifest & preload );
  307. struct bufferContext_t {
  308. bufferContext_t() :
  309. voice( NULL ),
  310. sample( NULL ),
  311. bufferNumber( 0 )
  312. { }
  313. idSoundVoice_XAudio2 * voice;
  314. idSoundSample_XAudio2 * sample;
  315. int bufferNumber;
  316. };
  317. // Get a stream buffer from the free pool, returns NULL if none are available
  318. bufferContext_t * ObtainStreamBufferContext();
  319. void ReleaseStreamBufferContext( bufferContext_t * p );
  320. idSysMutex streamBufferMutex;
  321. idStaticList< bufferContext_t *, MAX_SOUND_BUFFERS > freeStreamBufferContexts;
  322. idStaticList< bufferContext_t *, MAX_SOUND_BUFFERS > activeStreamBufferContexts;
  323. idStaticList< bufferContext_t, MAX_SOUND_BUFFERS > bufferContexts;
  324. idSoundWorldLocal * currentSoundWorld;
  325. idStaticList<idSoundWorldLocal *, 32> soundWorlds;
  326. idList<idSoundSample *, TAG_AUDIO> samples;
  327. idHashIndex sampleHash;
  328. idSoundHardware hardware;
  329. idRandom2 random;
  330. int soundTime;
  331. bool muted;
  332. bool musicMuted;
  333. bool needsRestart;
  334. bool insideLevelLoad;
  335. //-------------------------
  336. idSoundSystemLocal() :
  337. soundTime( 0 ),
  338. currentSoundWorld( NULL ),
  339. muted( false ),
  340. musicMuted( false ),
  341. needsRestart( false )
  342. {}
  343. };
  344. extern idSoundSystemLocal soundSystemLocal;
  345. #endif /* !__SND_LOCAL_H__ */