sound.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 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 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 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 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 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 __SOUND__
  21. #define __SOUND__
  22. /*
  23. ===============================================================================
  24. SOUND SHADER DECL
  25. ===============================================================================
  26. */
  27. // unfortunately, our minDistance / maxDistance is specified in meters, and
  28. // we have far too many of them to change at this time.
  29. const float DOOM_TO_METERS = 0.0254f; // doom to meters
  30. const float METERS_TO_DOOM = (1.0f/DOOM_TO_METERS); // meters to doom
  31. class idSoundSample;
  32. // sound shader flags
  33. static const int SSF_PRIVATE_SOUND = BIT(0); // only plays for the current listenerId
  34. static const int SSF_ANTI_PRIVATE_SOUND =BIT(1); // plays for everyone but the current listenerId
  35. static const int SSF_NO_OCCLUSION = BIT(2); // don't flow through portals, only use straight line
  36. static const int SSF_GLOBAL = BIT(3); // play full volume to all speakers and all listeners
  37. static const int SSF_OMNIDIRECTIONAL = BIT(4); // fall off with distance, but play same volume in all speakers
  38. static const int SSF_LOOPING = BIT(5); // repeat the sound continuously
  39. static const int SSF_PLAY_ONCE = BIT(6); // never restart if already playing on any channel of a given emitter
  40. static const int SSF_UNCLAMPED = BIT(7); // don't clamp calculated volumes at 1.0
  41. static const int SSF_NO_FLICKER = BIT(8); // always return 1.0 for volume queries
  42. static const int SSF_NO_DUPS = BIT(9); // try not to play the same sound twice in a row
  43. // these options can be overriden from sound shader defaults on a per-emitter and per-channel basis
  44. typedef struct {
  45. float minDistance;
  46. float maxDistance;
  47. float volume; // in dB, unfortunately. Negative values get quieter
  48. float shakes;
  49. int soundShaderFlags; // SSF_* bit flags
  50. int soundClass; // for global fading of sounds
  51. } soundShaderParms_t;
  52. const int SOUND_MAX_LIST_WAVS = 32;
  53. // sound classes are used to fade most sounds down inside cinematics, leaving dialog
  54. // flagged with a non-zero class full volume
  55. const int SOUND_MAX_CLASSES = 4;
  56. // it is somewhat tempting to make this a virtual class to hide the private
  57. // details here, but that doesn't fit easily with the decl manager at the moment.
  58. class idSoundShader : public idDecl {
  59. public:
  60. idSoundShader( void );
  61. virtual ~idSoundShader( void );
  62. virtual size_t Size( void ) const;
  63. virtual bool SetDefaultText( void );
  64. virtual const char * DefaultDefinition( void ) const;
  65. virtual bool Parse( const char *text, const int textLength );
  66. virtual void FreeData( void );
  67. virtual void List( void ) const;
  68. virtual const char * GetDescription() const;
  69. // so the editor can draw correct default sound spheres
  70. // this is currently defined as meters, which sucks, IMHO.
  71. virtual float GetMinDistance() const; // FIXME: replace this with a GetSoundShaderParms()
  72. virtual float GetMaxDistance() const;
  73. // returns NULL if an AltSound isn't defined in the shader.
  74. // we use this for pairing a specific broken light sound with a normal light sound
  75. virtual const idSoundShader *GetAltSound() const;
  76. virtual bool HasDefaultSound() const;
  77. virtual const soundShaderParms_t *GetParms() const;
  78. virtual int GetNumSounds() const;
  79. virtual const char * GetSound( int index ) const;
  80. virtual bool CheckShakesAndOgg( void ) const;
  81. private:
  82. friend class idSoundWorldLocal;
  83. friend class idSoundEmitterLocal;
  84. friend class idSoundChannel;
  85. friend class idSoundCache;
  86. // options from sound shader text
  87. soundShaderParms_t parms; // can be overriden on a per-channel basis
  88. bool onDemand; // only load when played, and free when finished
  89. int speakerMask;
  90. const idSoundShader * altSound;
  91. idStr desc; // description
  92. bool errorDuringParse;
  93. float leadinVolume; // allows light breaking leadin sounds to be much louder than the broken loop
  94. idSoundSample * leadins[SOUND_MAX_LIST_WAVS];
  95. int numLeadins;
  96. idSoundSample * entries[SOUND_MAX_LIST_WAVS];
  97. int numEntries;
  98. private:
  99. void Init( void );
  100. bool ParseShader( idLexer &src );
  101. };
  102. /*
  103. ===============================================================================
  104. SOUND EMITTER
  105. ===============================================================================
  106. */
  107. // sound channels
  108. static const int SCHANNEL_ANY = 0; // used in queries and commands to effect every channel at once, in
  109. // startSound to have it not override any other channel
  110. static const int SCHANNEL_ONE = 1; // any following integer can be used as a channel number
  111. typedef int s_channelType; // the game uses its own series of enums, and we don't want to require casts
  112. class idSoundEmitter {
  113. public:
  114. virtual ~idSoundEmitter( void ) {}
  115. // a non-immediate free will let all currently playing sounds complete
  116. // soundEmitters are not actually deleted, they are just marked as
  117. // reusable by the soundWorld
  118. virtual void Free( bool immediate ) = 0;
  119. // the parms specified will be the default overrides for all sounds started on this emitter.
  120. // NULL is acceptable for parms
  121. virtual void UpdateEmitter( const idVec3 &origin, int listenerId, const soundShaderParms_t *parms ) = 0;
  122. // returns the length of the started sound in msec
  123. virtual int StartSound( const idSoundShader *shader, const s_channelType channel, float diversity = 0, int shaderFlags = 0, bool allowSlow = true ) = 0;
  124. // pass SCHANNEL_ANY to effect all channels
  125. virtual void ModifySound( const s_channelType channel, const soundShaderParms_t *parms ) = 0;
  126. virtual void StopSound( const s_channelType channel ) = 0;
  127. // to is in Db (sigh), over is in seconds
  128. virtual void FadeSound( const s_channelType channel, float to, float over ) = 0;
  129. // returns true if there are any sounds playing from this emitter. There is some conservative
  130. // slop at the end to remove inconsistent race conditions with the sound thread updates.
  131. // FIXME: network game: on a dedicated server, this will always be false
  132. virtual bool CurrentlyPlaying( void ) const = 0;
  133. // returns a 0.0 to 1.0 value based on the current sound amplitude, allowing
  134. // graphic effects to be modified in time with the audio.
  135. // just samples the raw wav file, it doesn't account for volume overrides in the
  136. virtual float CurrentAmplitude( void ) = 0;
  137. // for save games. Index will always be > 0
  138. virtual int Index( void ) const = 0;
  139. };
  140. /*
  141. ===============================================================================
  142. SOUND WORLD
  143. There can be multiple independent sound worlds, just as there can be multiple
  144. independent render worlds. The prime example is the editor sound preview
  145. option existing simultaniously with a live game.
  146. ===============================================================================
  147. */
  148. class idSoundWorld {
  149. public:
  150. virtual ~idSoundWorld( void ) {}
  151. // call at each map start
  152. virtual void ClearAllSoundEmitters( void ) = 0;
  153. virtual void StopAllSounds( void ) = 0;
  154. // get a new emitter that can play sounds in this world
  155. virtual idSoundEmitter *AllocSoundEmitter( void ) = 0;
  156. // for load games, index 0 will return NULL
  157. virtual idSoundEmitter *EmitterForIndex( int index ) = 0;
  158. // query sound samples from all emitters reaching a given position
  159. virtual float CurrentShakeAmplitudeForPosition( const int time, const idVec3 &listenerPosition ) = 0;
  160. // where is the camera/microphone
  161. // listenerId allows listener-private and antiPrivate sounds to be filtered
  162. // gameTime is in msec, and is used to time sound queries and removals so that they are independent
  163. // of any race conditions with the async update
  164. virtual void PlaceListener( const idVec3 &origin, const idMat3 &axis, const int listenerId, const int gameTime, const idStr& areaName ) = 0;
  165. // fade all sounds in the world with a given shader soundClass
  166. // to is in Db (sigh), over is in seconds
  167. virtual void FadeSoundClasses( const int soundClass, const float to, const float over ) = 0;
  168. // background music
  169. virtual void PlayShaderDirectly( const char *name, int channel = -1 ) = 0;
  170. // dumps the current state and begins archiving commands
  171. virtual void StartWritingDemo( idDemoFile *demo ) = 0;
  172. virtual void StopWritingDemo() = 0;
  173. // read a sound command from a demo file
  174. virtual void ProcessDemoCommand( idDemoFile *demo ) = 0;
  175. // pause and unpause the sound world
  176. virtual void Pause( void ) = 0;
  177. virtual void UnPause( void ) = 0;
  178. virtual bool IsPaused( void ) = 0;
  179. // Write the sound output to multiple wav files. Note that this does not use the
  180. // work done by AsyncUpdate, it mixes explicitly in the foreground every PlaceOrigin(),
  181. // under the assumption that we are rendering out screenshots and the gameTime is going
  182. // much slower than real time.
  183. // path should not include an extension, and the generated filenames will be:
  184. // <path>_left.raw, <path>_right.raw, or <path>_51left.raw, <path>_51right.raw,
  185. // <path>_51center.raw, <path>_51lfe.raw, <path>_51backleft.raw, <path>_51backright.raw,
  186. // If only two channel mixing is enabled, the left and right .raw files will also be
  187. // combined into a stereo .wav file.
  188. virtual void AVIOpen( const char *path, const char *name ) = 0;
  189. virtual void AVIClose( void ) = 0;
  190. // SaveGame / demo Support
  191. virtual void WriteToSaveGame( idFile *savefile ) = 0;
  192. virtual void ReadFromSaveGame( idFile *savefile ) = 0;
  193. virtual void SetSlowmo( bool active ) = 0;
  194. virtual void SetSlowmoSpeed( float speed ) = 0;
  195. virtual void SetEnviroSuit( bool active ) = 0;
  196. };
  197. /*
  198. ===============================================================================
  199. SOUND SYSTEM
  200. ===============================================================================
  201. */
  202. typedef struct {
  203. idStr name;
  204. idStr format;
  205. int numChannels;
  206. int numSamplesPerSecond;
  207. int num44kHzSamples;
  208. int numBytes;
  209. bool looping;
  210. float lastVolume;
  211. int start44kHzTime;
  212. int current44kHzTime;
  213. } soundDecoderInfo_t;
  214. class idSoundSystem {
  215. public:
  216. virtual ~idSoundSystem( void ) {}
  217. // all non-hardware initialization
  218. virtual void Init( void ) = 0;
  219. // shutdown routine
  220. virtual void Shutdown( void ) = 0;
  221. // call ClearBuffer if there is a chance that the AsyncUpdate won't get called
  222. // for 20+ msec, which would cause a stuttering repeat of the current
  223. // buffer contents
  224. virtual void ClearBuffer( void ) = 0;
  225. // sound is attached to the window, and must be recreated when the window is changed
  226. virtual bool InitHW( void ) = 0;
  227. virtual bool ShutdownHW( void ) = 0;
  228. // asyn loop, called at 60Hz
  229. virtual int AsyncUpdate( int time ) = 0;
  230. // async loop, when the sound driver uses a write strategy
  231. virtual int AsyncUpdateWrite( int time ) = 0;
  232. // it is a good idea to mute everything when starting a new level,
  233. // because sounds may be started before a valid listener origin
  234. // is specified
  235. virtual void SetMute( bool mute ) = 0;
  236. // for the sound level meter window
  237. virtual cinData_t ImageForTime( const int milliseconds, const bool waveform ) = 0;
  238. // get sound decoder info
  239. virtual int GetSoundDecoderInfo( int index, soundDecoderInfo_t &decoderInfo ) = 0;
  240. // if rw == NULL, no portal occlusion or rendered debugging is available
  241. virtual idSoundWorld * AllocSoundWorld( idRenderWorld *rw ) = 0;
  242. // specifying NULL will cause silence to be played
  243. virtual void SetPlayingSoundWorld( idSoundWorld *soundWorld ) = 0;
  244. // some tools, like the sound dialog, may be used in both the game and the editor
  245. // This can return NULL, so check!
  246. virtual idSoundWorld * GetPlayingSoundWorld( void ) = 0;
  247. // Mark all soundSamples as currently unused,
  248. // but don't free anything.
  249. virtual void BeginLevelLoad( void ) = 0;
  250. // Free all soundSamples marked as unused
  251. // We might want to defer the loading of new sounds to this point,
  252. // as we do with images, to avoid having a union in memory at one time.
  253. virtual void EndLevelLoad( const char *mapString ) = 0;
  254. // direct mixing for OSes that support it
  255. virtual int AsyncMix( int soundTime, float *mixBuffer ) = 0;
  256. // prints memory info
  257. virtual void PrintMemInfo( MemInfo_t *mi ) = 0;
  258. // is EAX support present - -1: disabled at compile time, 0: no suitable hardware, 1: ok, 2: failed to load OpenAL DLL
  259. virtual int IsEAXAvailable( void ) = 0;
  260. };
  261. extern idSoundSystem *soundSystem;
  262. #endif /* !__SOUND__ */