sound.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 __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. const float DB_SILENCE = -60.0f;
  32. class idSoundSample;
  33. // sound shader flags
  34. static const int SSF_PRIVATE_SOUND = BIT(0); // only plays for the current listenerId
  35. static const int SSF_ANTI_PRIVATE_SOUND =BIT(1); // plays for everyone but the current listenerId
  36. static const int SSF_NO_OCCLUSION = BIT(2); // don't flow through portals, only use straight line
  37. static const int SSF_GLOBAL = BIT(3); // play full volume to all speakers and all listeners
  38. static const int SSF_OMNIDIRECTIONAL = BIT(4); // fall off with distance, but play same volume in all speakers
  39. static const int SSF_LOOPING = BIT(5); // repeat the sound continuously
  40. static const int SSF_PLAY_ONCE = BIT(6); // never restart if already playing on any channel of a given emitter
  41. static const int SSF_UNCLAMPED = BIT(7); // don't clamp calculated volumes at 1.0
  42. static const int SSF_NO_FLICKER = BIT(8); // always return 1.0 for volume queries
  43. static const int SSF_NO_DUPS = BIT(9); // try not to play the same sound twice in a row
  44. static const int SSF_VO = BIT(10);// VO - direct a portion of the sound through the center channel (set automatically on shaders that contain files that start with "sound/vo/")
  45. static const int SSF_MUSIC = BIT(11);// Music - Muted when the player is playing his own music
  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. 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. // sound classes are used to fade most sounds down inside cinematics, leaving dialog
  56. // flagged with a non-zero class full volume
  57. const int SOUND_MAX_CLASSES = 4;
  58. // it is somewhat tempting to make this a virtual class to hide the private
  59. // details here, but that doesn't fit easily with the decl manager at the moment.
  60. class idSoundShader : public idDecl {
  61. public:
  62. idSoundShader();
  63. virtual ~idSoundShader();
  64. virtual size_t Size() const;
  65. virtual bool SetDefaultText();
  66. virtual const char * DefaultDefinition() const;
  67. virtual bool Parse( const char *text, const int textLength, bool allowBinaryVersion );
  68. virtual void FreeData();
  69. virtual void List() const;
  70. // so the editor can draw correct default sound spheres
  71. // this is currently defined as meters, which sucks, IMHO.
  72. virtual float GetMinDistance() const; // FIXME: replace this with a GetSoundShaderParms()
  73. virtual float GetMaxDistance() const;
  74. // returns NULL if an AltSound isn't defined in the shader.
  75. // we use this for pairing a specific broken light sound with a normal light sound
  76. virtual const idSoundShader *GetAltSound() const;
  77. virtual bool HasDefaultSound() const;
  78. virtual const soundShaderParms_t *GetParms() const;
  79. virtual int GetNumSounds() const;
  80. virtual const char * GetSound( int index ) const;
  81. private:
  82. friend class idSoundWorldLocal;
  83. friend class idSoundEmitterLocal;
  84. friend class idSoundChannel;
  85. // options from sound shader text
  86. soundShaderParms_t parms; // can be overriden on a per-channel basis
  87. int speakerMask;
  88. const idSoundShader * altSound;
  89. bool leadin; // true if this sound has a leadin
  90. float leadinVolume; // allows light breaking leadin sounds to be much louder than the broken loop
  91. idList<idSoundSample *, TAG_AUDIO> entries;
  92. private:
  93. void Init();
  94. bool ParseShader( idLexer &src );
  95. };
  96. /*
  97. ===============================================================================
  98. SOUND EMITTER
  99. ===============================================================================
  100. */
  101. // sound channels
  102. static const int SCHANNEL_ANY = 0; // used in queries and commands to effect every channel at once, in
  103. // startSound to have it not override any other channel
  104. static const int SCHANNEL_ONE = 1; // any following integer can be used as a channel number
  105. typedef int s_channelType; // the game uses its own series of enums, and we don't want to require casts
  106. class idSoundEmitter {
  107. public:
  108. virtual ~idSoundEmitter() {}
  109. // a non-immediate free will let all currently playing sounds complete
  110. // soundEmitters are not actually deleted, they are just marked as
  111. // reusable by the soundWorld
  112. virtual void Free( bool immediate ) = 0;
  113. // the parms specified will be the default overrides for all sounds started on this emitter.
  114. // NULL is acceptable for parms
  115. virtual void UpdateEmitter( const idVec3 &origin, int listenerId, const soundShaderParms_t *parms ) = 0;
  116. // returns the length of the started sound in msec
  117. virtual int StartSound( const idSoundShader *shader, const s_channelType channel, float diversity = 0, int shaderFlags = 0, bool allowSlow = true ) = 0;
  118. // pass SCHANNEL_ANY to effect all channels
  119. virtual void ModifySound( const s_channelType channel, const soundShaderParms_t *parms ) = 0;
  120. virtual void StopSound( const s_channelType channel ) = 0;
  121. // to is in Db, over is in seconds
  122. virtual void FadeSound( const s_channelType channel, float to, float over ) = 0;
  123. // returns true if there are any sounds playing from this emitter. There is some conservative
  124. // slop at the end to remove inconsistent race conditions with the sound thread updates.
  125. // FIXME: network game: on a dedicated server, this will always be false
  126. virtual bool CurrentlyPlaying( const s_channelType channel = SCHANNEL_ANY ) const = 0;
  127. // returns a 0.0 to 1.0 value based on the current sound amplitude, allowing
  128. // graphic effects to be modified in time with the audio.
  129. // just samples the raw wav file, it doesn't account for volume overrides in the
  130. virtual float CurrentAmplitude() = 0;
  131. // for save games. Index will always be > 0
  132. virtual int Index() const = 0;
  133. };
  134. /*
  135. ===============================================================================
  136. SOUND WORLD
  137. There can be multiple independent sound worlds, just as there can be multiple
  138. independent render worlds. The prime example is the editor sound preview
  139. option existing simultaniously with a live game.
  140. ===============================================================================
  141. */
  142. class idSoundWorld {
  143. public:
  144. virtual ~idSoundWorld() {}
  145. // call at each map start
  146. virtual void ClearAllSoundEmitters() = 0;
  147. virtual void StopAllSounds() = 0;
  148. // get a new emitter that can play sounds in this world
  149. virtual idSoundEmitter *AllocSoundEmitter() = 0;
  150. // for load games, index 0 will return NULL
  151. virtual idSoundEmitter *EmitterForIndex( int index ) = 0;
  152. // query sound samples from all emitters reaching a given listener
  153. virtual float CurrentShakeAmplitude() = 0;
  154. // where is the camera/microphone
  155. // listenerId allows listener-private and antiPrivate sounds to be filtered
  156. virtual void PlaceListener( const idVec3 &origin, const idMat3 &axis, const int listenerId ) = 0;
  157. // fade all sounds in the world with a given shader soundClass
  158. // to is in Db, over is in seconds
  159. virtual void FadeSoundClasses( const int soundClass, const float to, const float over ) = 0;
  160. // menu sounds
  161. virtual int PlayShaderDirectly( const char * name, int channel = -1 ) = 0;
  162. // dumps the current state and begins archiving commands
  163. virtual void StartWritingDemo( idDemoFile *demo ) = 0;
  164. virtual void StopWritingDemo() = 0;
  165. // read a sound command from a demo file
  166. virtual void ProcessDemoCommand( idDemoFile *demo ) = 0;
  167. // when cinematics are skipped, we need to advance sound time this much
  168. virtual void Skip( int time ) = 0;
  169. // pause and unpause the sound world
  170. virtual void Pause() = 0;
  171. virtual void UnPause() = 0;
  172. virtual bool IsPaused() = 0;
  173. // Write the sound output to multiple wav files. Note that this does not use the
  174. // work done by AsyncUpdate, it mixes explicitly in the foreground every PlaceOrigin(),
  175. // under the assumption that we are rendering out screenshots and the gameTime is going
  176. // much slower than real time.
  177. // path should not include an extension, and the generated filenames will be:
  178. // <path>_left.raw, <path>_right.raw, or <path>_51left.raw, <path>_51right.raw,
  179. // <path>_51center.raw, <path>_51lfe.raw, <path>_51backleft.raw, <path>_51backright.raw,
  180. // If only two channel mixing is enabled, the left and right .raw files will also be
  181. // combined into a stereo .wav file.
  182. virtual void AVIOpen( const char *path, const char *name ) = 0;
  183. virtual void AVIClose() = 0;
  184. // SaveGame / demo Support
  185. virtual void WriteToSaveGame( idFile *savefile ) = 0;
  186. virtual void ReadFromSaveGame( idFile *savefile ) = 0;
  187. virtual void SetSlowmoSpeed( float speed ) = 0;
  188. virtual void SetEnviroSuit( bool active ) = 0;
  189. };
  190. /*
  191. ===============================================================================
  192. SOUND SYSTEM
  193. ===============================================================================
  194. */
  195. typedef struct {
  196. idStr name;
  197. idStr format;
  198. int numChannels;
  199. int numSamplesPerSecond;
  200. int num44kHzSamples;
  201. int numBytes;
  202. bool looping;
  203. float lastVolume;
  204. int start44kHzTime;
  205. int current44kHzTime;
  206. } soundDecoderInfo_t;
  207. class idSoundSystem {
  208. public:
  209. virtual ~idSoundSystem() {}
  210. // All non-hardware initialization.
  211. virtual void Init() = 0;
  212. // Shutdown routine.
  213. virtual void Shutdown() = 0;
  214. // The renderWorld is used for visualization and light amplitude sampling.
  215. virtual idSoundWorld * AllocSoundWorld( idRenderWorld *rw ) = 0;
  216. virtual void FreeSoundWorld( idSoundWorld *sw ) = 0;
  217. // Specifying NULL will cause silence to be played.
  218. virtual void SetPlayingSoundWorld( idSoundWorld *soundWorld ) = 0;
  219. // Some tools, like the sound dialog, may be used in both the game and the editor
  220. // This can return NULL, so check!
  221. virtual idSoundWorld * GetPlayingSoundWorld() = 0;
  222. // Sends the current playing sound world information to the sound hardware.
  223. virtual void Render() = 0;
  224. virtual void MuteBackgroundMusic( bool mute ) = 0;
  225. // Sets the final output volume to 0.
  226. virtual void SetMute( bool mute ) = 0;
  227. virtual bool IsMuted() = 0;
  228. // Called by the decl system when a sound decl is reloaded
  229. virtual void OnReloadSound( const idDecl* sound ) = 0;
  230. // Called before freeing any sound sample resources
  231. virtual void StopAllSounds() = 0;
  232. // May be called to free memory for level loads
  233. virtual void InitStreamBuffers() = 0;
  234. virtual void FreeStreamBuffers() = 0;
  235. // video playback needs to get this
  236. virtual void * GetIXAudio2() const = 0;
  237. // for the sound level meter window
  238. virtual cinData_t ImageForTime( const int milliseconds, const bool waveform ) = 0;
  239. // Free all sounds loaded during the last map load
  240. virtual void BeginLevelLoad() = 0;
  241. // Load all sounds marked as used this level
  242. virtual void EndLevelLoad() = 0;
  243. virtual void Preload( idPreloadManifest & preload ) = 0;
  244. // prints memory info
  245. virtual void PrintMemInfo( MemInfo_t *mi ) = 0;
  246. };
  247. extern idSoundSystem *soundSystem;
  248. #endif /* !__SOUND__ */