sound.h 14 KB

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