soundutil.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. //
  2. // soundutil.h
  3. //
  4. // Useful utility classes/functions
  5. //
  6. #pragma once
  7. namespace SoundEngine {
  8. // returns a PCM data object for a given wave file.
  9. HRESULT LoadWaveFile(TRef<ISoundPCMData>& pdata, const ZString& strFilename);
  10. // returns a dummy PCM data object.
  11. HRESULT CreateDummyPCMData(TRef<ISoundPCMData>& pdata);
  12. //
  13. // a generic implementation of a sound source. Its position can be set and
  14. // retrieved, so it can be used if you want to manually set the position and
  15. // velocity of a sound instead of having it polled.
  16. //
  17. class GenericSoundSource : public ISoundPositionSource
  18. {
  19. private:
  20. Vector m_vectPosition;
  21. Vector m_vectVelocity;
  22. Vector m_vectOrientation;
  23. bool m_bListenerRelative;
  24. bool m_bIsPlaying;
  25. public:
  26. // constructors
  27. GenericSoundSource();
  28. GenericSoundSource(
  29. const Vector& vectPosition,
  30. const Vector& vectVelocity = Vector(0, 0, 0),
  31. const Vector& vectOrientation = Vector(0, 0, 1),
  32. bool bListenerRelative = false
  33. );
  34. //
  35. // Position setting functions
  36. //
  37. // Sets the position of the sound in space
  38. void SetPosition(const Vector& vectPosition);
  39. // Sets the velocity of the sound in space
  40. void SetVelocity(const Vector& vectVelocity);
  41. // Sets the orientation of the sound in space, used for sound cones.
  42. void SetOrientation(const Vector& vectOrientation);
  43. // Sets whether or not the sound position source is listener relative
  44. void SetIsListenerRelative(bool bListenerRelative);
  45. // Stops any sounds on this source
  46. void Stop();
  47. //
  48. // ISoundPositionSource
  49. //
  50. // Gets the position of the sound in space
  51. HRESULT GetPosition(Vector& vectPosition);
  52. // Gets the velocity of the sound in space
  53. HRESULT GetVelocity(Vector& vectVelocity);
  54. // Gets the orientation of the sound in space, used for sound cones.
  55. HRESULT GetOrientation(Vector& vectOrientation);
  56. // Returns S_OK if the position, velocity and orientation reported are
  57. // relative to the listener, S_FALSE otherwise.
  58. HRESULT IsListenerRelative();
  59. // Returns S_OK if this source is still playing the sound, S_FALSE
  60. // otherwise. This might be false if a sound emitter is destroyed, for
  61. // example, in which case the sound might fade out. Once it returns
  62. // S_FALSE once, it should never return S_OK again.
  63. HRESULT IsPlaying();
  64. };
  65. // a generic wrapper for an ISoundPositionSource which just delegates everything to
  66. // the object it wraps.
  67. // (not very useful by itself, but useful as a base class)
  68. class SoundPositionSourceWrapper : public ISoundPositionSource
  69. {
  70. protected:
  71. // the object which is wrapped
  72. TRef<ISoundPositionSource> m_pBase;
  73. public:
  74. // constructor
  75. SoundPositionSourceWrapper(ISoundPositionSource* pBase) :
  76. m_pBase(pBase) {};
  77. //
  78. // ISoundPositionSource
  79. //
  80. // Gets the position of the sound in space
  81. HRESULT GetPosition(Vector& vectPosition);
  82. // Gets the velocity of the sound in space
  83. HRESULT GetVelocity(Vector& vectVelocity);
  84. // Gets the orientation of the sound in space, used for sound cones.
  85. HRESULT GetOrientation(Vector& vectOrientation);
  86. // Returns S_OK if this source is still playing the sound, S_FALSE
  87. // otherwise. This might be false if a sound emitter is destroyed, for
  88. // example, in which case the sound might fade out. Once it returns
  89. // S_FALSE once, it should never return S_OK again.
  90. HRESULT IsPlaying();
  91. };
  92. //
  93. // a generic implementation of a listener. Its position can be set and
  94. // retrieved, so it can be used if you want to manually set the position and
  95. // velocity of the listener instead of having it polled.
  96. //
  97. class GenericListener : public GenericSoundSource, public ISoundListener
  98. {
  99. private:
  100. Vector m_vectUp;
  101. public:
  102. // constructor
  103. GenericListener();
  104. // sets the up direction for this listener
  105. void SetUpDirection(const Vector& vectUp);
  106. //
  107. // ISoundListener
  108. //
  109. // get the "up" vector for the listener
  110. HRESULT GetUpDirection(Vector& vectUp);
  111. //
  112. // ISoundPositionSource
  113. //
  114. // Gets the position of the sound in space
  115. HRESULT GetPosition(Vector& vectPosition);
  116. // Gets the velocity of the sound in space
  117. HRESULT GetVelocity(Vector& vectVelocity);
  118. // Gets the orientation of the sound in space, used for sound cones.
  119. HRESULT GetOrientation(Vector& vectOrientation);
  120. // Returns S_OK if this source is still playing the sound, S_FALSE
  121. // otherwise. This might be false if a sound emitter is destroyed, for
  122. // example, in which case the sound might fade out. Once it returns
  123. // S_FALSE once, it should never return S_OK again.
  124. HRESULT IsPlaying();
  125. };
  126. // a generic wrapper for an ISoundInstance which just delegates everything to
  127. // the object it wraps.
  128. // (not very useful by itself, but useful as a base class)
  129. class SoundInstanceWrapper : public ISoundInstance
  130. {
  131. protected:
  132. TRef<ISoundInstance> m_pBase;
  133. public:
  134. SoundInstanceWrapper(ISoundInstance* pBase) :
  135. m_pBase(pBase) { ZAssert(pBase != NULL); };
  136. // Stops the sound. If bForceNow is true the sound will stop ASAP,
  137. // possibly popping. If it is false some sounds may play a trail-off
  138. // sound or fade away.
  139. virtual HRESULT Stop(bool bForceNow = false);
  140. // returns S_OK if the sound is currently playing, S_FALSE otherwise.
  141. virtual HRESULT IsPlaying();
  142. // Gets an event which fires when the sound finishes playing (for any
  143. // reason)
  144. virtual IEventSource* GetFinishEventSource();
  145. // Gets an interface for tweaking the sound, if supported, NULL otherwise.
  146. virtual TRef<ISoundTweakable> GetISoundTweakable();
  147. virtual TRef<ISoundTweakable3D> GetISoundTweakable3D();
  148. };
  149. // a generic wrapper for an ISoundTweakable which just delegates everything to
  150. // the object it wraps.
  151. // (not very useful by itself, but useful as a base class)
  152. class SoundTweakableWrapper : public ISoundTweakable
  153. {
  154. protected:
  155. TRef<ISoundTweakable> m_pBase;
  156. public:
  157. SoundTweakableWrapper(ISoundTweakable* pBase) :
  158. m_pBase(pBase) { ZAssert(pBase != NULL); };
  159. // Sets the gain, from 0 to -100 dB
  160. virtual HRESULT SetGain(float fGain);
  161. // Sets the pitch shift, where 1.0 is normal, 0.5 is half of normal speed,
  162. // and 2.0 is twice normal speed.
  163. virtual HRESULT SetPitch(float fPitch);
  164. // sets the priority - used as a addition to volume when choosing which
  165. // sounds are most important to play.
  166. virtual HRESULT SetPriority(float fPriority);
  167. };
  168. // a generic wrapper for an ISoundTweakable3D which just delegates everything to
  169. // the object it wraps.
  170. // (not very useful by itself, but useful as a base class)
  171. class SoundTweakable3DWrapper : public ISoundTweakable
  172. {
  173. protected:
  174. TRef<ISoundTweakable3D> m_pBase;
  175. public:
  176. SoundTweakable3DWrapper(ISoundTweakable3D* pBase) :
  177. m_pBase(pBase) { ZAssert(pBase != NULL); };
  178. // toggles 3D Positioning on and off for the given sound.
  179. virtual HRESULT Set3D(bool b3D);
  180. // Sets the distance at which the sound will be at max volume. This
  181. // effects how quickly the sound drops off with distance.
  182. virtual HRESULT SetMinimumDistance(float fMinimumDistance);
  183. // Sets a sound cone of size fInnerAngle (in degrees) where the volume is at
  184. // normal levels, outside of which it fades down by fOutsideGain
  185. // (range of 0 to -100 db) at fOuterAngle (degrees) and beyond.
  186. virtual HRESULT SetCone(float fInnerAngle, float fOuterAngle, float fOutsideGain);
  187. };
  188. // A sound wrapper which merely records the settings it's given (a useful base
  189. // class for sounds that may wait before playing).
  190. class StubbedTweakableSoundInstance : public ISoundInstance, public ISoundTweakable3D,
  191. public IIntegerEventSink
  192. {
  193. // The current priority of the sound
  194. float m_fPriority;
  195. bool m_bPrioritySet;
  196. // The current gain of the sound
  197. float m_fGain;
  198. bool m_bGainSet;
  199. // the current pitch of the sound
  200. float m_fPitch;
  201. bool m_bPitchSet;
  202. // is 3D on at the moment?
  203. bool m_b3D;
  204. bool m_b3DSet;
  205. // the current maximum distance of the sound
  206. float m_fMinimumDistance;
  207. bool m_bMinimumDistanceSet;
  208. // the current sound cone
  209. float m_fInnerAngle, m_fOuterAngle, m_fOutsideGain;
  210. bool m_bConeSet;
  211. protected:
  212. // The source of this sound, position-wise
  213. TRef<ISoundPositionSource> m_pposSource;
  214. // this sound has been stopped
  215. bool m_bStopped;
  216. // this sound has been stopped with bForceNow set
  217. bool m_bForcedStop;
  218. // the "stopped" event source
  219. TRef<EventSourceImpl> m_peventsourceStopped;
  220. public:
  221. StubbedTweakableSoundInstance(ISoundPositionSource* pposSource);
  222. // fires the m_peventsourceStopped event if bHasStopped is true
  223. virtual void Update(DWORD dwElapsedTime, bool bHasStopped);
  224. // calls Stop(true) if m_pposSource has stopped. calls Update,
  225. // returns IsPlaying()
  226. virtual bool OnEvent(IIntegerEventSource* pevent, int value);
  227. // copies any tweaks that have been made to the given sound instance
  228. void UpdateSound(ISoundInstance* psound);
  229. //
  230. // ISoundInstance interface
  231. //
  232. // Stops the sound. If bForceNow is true the sound will stop ASAP,
  233. // possibly popping. If it is false some sounds may play a trail-off
  234. // sound or fade away.
  235. virtual HRESULT Stop(bool bForceNow = false);
  236. // returns S_OK if the sound is currently playing, S_FALSE otherwise.
  237. virtual HRESULT IsPlaying();
  238. // Gets an event which fires when the sound finishes playing (for any
  239. // reason)
  240. virtual IEventSource* GetFinishEventSource();
  241. // Gets an interface for tweaking the sound, if supported, NULL otherwise.
  242. virtual TRef<ISoundTweakable> GetISoundTweakable();
  243. virtual TRef<ISoundTweakable3D> GetISoundTweakable3D();
  244. //
  245. // ISoundTweakable3D interface
  246. //
  247. // toggles 3D Positioning on and off for the given sound.
  248. virtual HRESULT Set3D(bool b3D);
  249. // Sets the distance at which the sound will be at max volume. This
  250. // effects how quickly the sound drops off with distance.
  251. virtual HRESULT SetMinimumDistance(float fMinimumDistance);
  252. // Sets a sound cone of size fInnerAngle (in degrees) where the volume is at
  253. // normal levels, outside of which it fades down by fOutsideGain
  254. // (range of 0 to -100 db) at fOuterAngle (degrees) and beyond.
  255. virtual HRESULT SetCone(float fInnerAngle, float fOuterAngle, float fOutsideGain);
  256. //
  257. // ISoundTweakable interface
  258. //
  259. // Sets the gain, from 0 to -100 dB
  260. virtual HRESULT SetGain(float fGain);
  261. // Sets the pitch shift, where 1.0 is normal, 0.5 is half of normal speed,
  262. // and 2.0 is twice normal speed.
  263. virtual HRESULT SetPitch(float fPitch);
  264. // sets the priority - used as a addition to volume when choosing which
  265. // sounds are most important to play.
  266. virtual HRESULT SetPriority(float fPriority);
  267. };
  268. // a delegate for an ISoundTweakable3D which just delegates everything to
  269. // the object it wraps.
  270. class SoundBufferSourceDelegate : public ISoundBufferSource
  271. {
  272. protected:
  273. ISoundBufferSource* m_pBase;
  274. public:
  275. SoundBufferSourceDelegate(ISoundBufferSource* pBase) :
  276. m_pBase(pBase) { ZAssert(pBase != NULL); };
  277. // Creates a static sound buffer of the given wave file. If bLooping is
  278. // true, the sound will loop until stopped.
  279. virtual HRESULT CreateStaticBuffer(TRef<ISoundInstance>& psoundNew,
  280. ISoundPCMData* pcmdata, bool bLooping, ISoundPositionSource* psource = NULL);
  281. // Creates a sound buffer with a loop in the middle. The sound will play
  282. // the start sound, play the loop sound until it gets a soft stop, then
  283. // play the rest of the sound.
  284. virtual HRESULT CreateASRBuffer(TRef<ISoundInstance>& psoundNew,
  285. ISoundPCMData* pcmdata, unsigned uLoopStart, unsigned uLoopLength,
  286. ISoundPositionSource* psource = NULL);
  287. // Gets an event which fires each time update is called. This can be used
  288. // for some of the trickier sounds that change with time.
  289. virtual IIntegerEventSource* GetUpdateEventSource();
  290. };
  291. // a generic wrapper for an ISoundTweakable3D which just delegates everything to
  292. // the object it wraps.
  293. // (not very useful by itself, but useful as a base class)
  294. class SoundBufferSourceWrapper : public SoundBufferSourceDelegate
  295. {
  296. protected:
  297. TRef<ISoundBufferSource> m_pBaseRef;
  298. public:
  299. SoundBufferSourceWrapper(ISoundBufferSource* pBase) :
  300. m_pBaseRef(pBase), SoundBufferSourceDelegate(pBase) {};
  301. };
  302. }