soundbase.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. //
  2. // soundbase.h
  3. //
  4. // General interfaces for sounds
  5. //
  6. #pragma once
  7. namespace SoundEngine {
  8. // choose a base class we can change if we decide to, say, go to COM+
  9. class ISoundObject : public IObject
  10. {
  11. };
  12. //
  13. // Tweakable general sound properties
  14. //
  15. class ISoundTweakable : public ISoundObject
  16. {
  17. public:
  18. // Sets the gain, from 0 to -100 dB
  19. virtual HRESULT SetGain(float fGain) = 0;
  20. // Sets the pitch shift, where 1.0 is normal, 0.5 is half of normal speed,
  21. // and 2.0 is twice normal speed.
  22. virtual HRESULT SetPitch(float fPitch) = 0;
  23. // sets the priority - used as a addition to volume when choosing which
  24. // sounds are most important to play.
  25. virtual HRESULT SetPriority(float fPriority) = 0;
  26. };
  27. //
  28. // Tweakable 3D sound properties
  29. //
  30. class ISoundTweakable3D : public ISoundTweakable
  31. {
  32. public:
  33. // toggles 3D Positioning on and off for the given sound.
  34. virtual HRESULT Set3D(bool b3D) = 0;
  35. // Sets the distance at which the sound will be at max volume. This
  36. // effects how quickly the sound drops off with distance.
  37. virtual HRESULT SetMinimumDistance(float fMinimumDistance) = 0;
  38. // Sets a sound cone of size fInnerAngle (in degrees) where the volume is at
  39. // normal levels, outside of which it fades down by fOutsideGain
  40. // (range of 0 to -100 db) at fOuterAngle (degrees) and beyond.
  41. virtual HRESULT SetCone(float fInnerAngle, float fOuterAngle, float fOutsideGain) = 0;
  42. };
  43. //
  44. // Playback controls
  45. //
  46. class ISoundInstance : public ISoundObject
  47. {
  48. public:
  49. // Stops the sound. If bForceNow is true the sound will stop ASAP,
  50. // possibly popping. If it is false some sounds may play a trail-off
  51. // sound or fade away.
  52. virtual HRESULT Stop(bool bForceNow = false) = 0;
  53. // returns S_OK if the sound is currently playing, S_FALSE otherwise.
  54. virtual HRESULT IsPlaying() = 0;
  55. // Gets an event which fires when the sound finishes playing (for any
  56. // reason)
  57. virtual IEventSource* GetFinishEventSource() = 0;
  58. // Gets an interface for tweaking the sound, if supported, NULL otherwise.
  59. virtual TRef<ISoundTweakable> GetISoundTweakable() = 0;
  60. virtual TRef<ISoundTweakable3D> GetISoundTweakable3D() = 0;
  61. };
  62. //
  63. // Provides positioning information for a sound emiter or listener
  64. //
  65. class ISoundPositionSource : public ISoundObject
  66. {
  67. public:
  68. // Gets the position of the sound in space
  69. virtual HRESULT GetPosition(Vector& vectPosition) = 0;
  70. // Gets the velocity of the sound in space
  71. virtual HRESULT GetVelocity(Vector& vectVelocity) = 0;
  72. // Gets the orientation of the sound in space, used for sound cones.
  73. virtual HRESULT GetOrientation(Vector& vectOrientation) = 0;
  74. // Returns S_OK if the position, velocity and orientation reported are
  75. // relative to the listener, S_FALSE otherwise.
  76. virtual HRESULT IsListenerRelative() = 0;
  77. // Returns S_OK if this source is still playing the sound, S_FALSE
  78. // otherwise. This might be false if a sound emitter is destroyed, for
  79. // example, in which case the sound might fade out. Once it returns
  80. // S_FALSE once, it should never return S_OK again.
  81. virtual HRESULT IsPlaying() = 0;
  82. };
  83. //
  84. // Provides positioning info for a listener of a sound
  85. //
  86. class ISoundListener : public ISoundPositionSource
  87. {
  88. public:
  89. // get the "up" vector for the listener
  90. virtual HRESULT GetUpDirection(Vector& vectUp) = 0;
  91. };
  92. //
  93. // A source of PCM wave data
  94. //
  95. class ISoundPCMData : public ISoundObject
  96. {
  97. public:
  98. // Gets the number of channels in the data
  99. virtual unsigned GetNumberOfChannels() = 0;
  100. // Gets the number of bits per sample
  101. virtual unsigned GetBitsPerSample() = 0;
  102. // Gets the default frequency (in samples per second) of the data
  103. virtual unsigned GetSampleRate() = 0;
  104. // Gets the size of the data
  105. virtual unsigned GetSize() = 0;
  106. // Copies the specified portion of the data
  107. virtual void GetData(void* dest, unsigned nOffset, unsigned nLength) = 0;
  108. // calculates the playback rate in bytes per second.
  109. DWORD GetBytesPerSample()
  110. {
  111. return GetBitsPerSample()/8 * GetNumberOfChannels();
  112. }
  113. // calculates the number of bytes per sample
  114. DWORD GetBytesPerSec()
  115. {
  116. return GetBytesPerSample() * GetSampleRate();
  117. }
  118. };
  119. //
  120. // A creation point for raw sound buffers
  121. //
  122. class ISoundBufferSource : public ISoundObject
  123. {
  124. public:
  125. // Creates a static sound buffer of the given wave file. If bLooping is
  126. // true, the sound will loop until stopped.
  127. virtual HRESULT CreateStaticBuffer(TRef<ISoundInstance>& psoundNew,
  128. ISoundPCMData* pcmdata, bool bLooping, ISoundPositionSource* psource = NULL) = 0;
  129. // Creates a sound buffer with a loop in the middle. The sound will play
  130. // the start sound, play the loop sound until it gets a soft stop, then
  131. // play the rest of the sound.
  132. virtual HRESULT CreateASRBuffer(TRef<ISoundInstance>& psoundNew,
  133. ISoundPCMData* pcmdata, unsigned uLoopStart, unsigned uLoopLength,
  134. ISoundPositionSource* psource = NULL) = 0;
  135. // Gets an event which fires each time update is called with the number of
  136. // milliseconds elapsed since the last update. This can be used for some
  137. // of the trickier sounds that change with time.
  138. virtual IIntegerEventSource* GetUpdateEventSource() = 0;
  139. };
  140. //
  141. // The general provider/manager of sound buffers and the sound stage.
  142. //
  143. class ISoundEngine : public ISoundObject
  144. {
  145. public:
  146. // Rebuild the sound stage to reflect any recent changes in sound
  147. virtual HRESULT Update() = 0;
  148. // Gets a buffer source for this object (not guarenteed to keep the sound
  149. // engine alive due to circular reference problems)
  150. virtual ISoundBufferSource* GetBufferSource() = 0;
  151. // Gets the number of virtual sound buffers that are playing at a given
  152. // moment. (no guarantees on how this number changes - it's just a perf.
  153. // number to use.)
  154. virtual HRESULT GetNumPlayingVirtualBuffers(int& nBuffers) = 0;
  155. //
  156. // General environment functions
  157. //
  158. // Sets a general quality of playback (CPU time vs. fidelity)
  159. enum Quality { minQuality, midQuality, maxQuality };
  160. virtual HRESULT SetQuality(Quality quality) = 0;
  161. // Allows/disallows hardware acceleration.
  162. virtual HRESULT EnableHardware(bool bEnable) = 0;
  163. // Sets the listener to use for the current sounds
  164. virtual HRESULT SetListener(ISoundListener* plistener) = 0;
  165. // Sets the conversion from game units to meters
  166. virtual HRESULT SetDistanceFactor(float fMetersPerUnit) = 0;
  167. // Sets the rolloff factor, where 1.0 is the real world attenuation with
  168. // distance, 2.0 is twice the attenuation of the real world, etc..
  169. virtual HRESULT SetRolloffFactor(float fRolloffFactor) = 0;
  170. // Sets the doppler factor, where 1.0 is real-world doppler
  171. virtual HRESULT SetDopplerFactor(float fDopplerFactor) = 0;
  172. };
  173. //
  174. // provides a template for creating new sound instances
  175. //
  176. class ISoundTemplate : public ISoundObject
  177. {
  178. public:
  179. // Creates a new instance of the given sound
  180. virtual HRESULT CreateSound(TRef<ISoundInstance>& psoundNew,
  181. ISoundBufferSource* pbufferSource, ISoundPositionSource* psource = NULL) = 0;
  182. };
  183. //
  184. // Helper functions
  185. //
  186. // create a sound engine for the default sound device
  187. HRESULT CreateSoundEngine(TRef<ISoundEngine>& psoundengine, HWND hwnd);
  188. // create a dummy sound engine
  189. HRESULT CreateDummySoundEngine(TRef<ISoundEngine>& psoundengine);
  190. //
  191. // Debugging facilities
  192. //
  193. #ifdef _DEBUG
  194. // an object which can provide debug info about itself
  195. class ISoundDebugDump
  196. {
  197. public:
  198. // return a human-readable description of the object, prepending
  199. // strIndent to the beginning of each line.
  200. virtual ZString DebugDump(const ZString& strIndent = "") = 0;
  201. };
  202. // a helper function for trying to dump an arbitrary object
  203. ZString SoundDebugDump(ISoundObject* pobject, const ZString& strIndent = "");
  204. #define SoundDebugImpl , public ISoundDebugDump
  205. #else
  206. #define SoundDebugImpl
  207. #endif
  208. }