ds3dbuffer.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. //
  2. // ds3dbuffer.h: low-level classes representing actual DirectSound3D buffers
  3. //
  4. namespace SoundEngine {
  5. // the minimum audible sound we care about (in dB)
  6. const float c_fMinAudible = -60.0f;
  7. // The conversion ratio from min distance to max distance.
  8. const float c_fMinToMaxDistanceRatio = 4000.0f;
  9. //
  10. // A single DirectSound3D sound buffer. This wrapper handles the painful tasks
  11. // of dealing with buffer loss, etc..
  12. //
  13. class DS3DSoundBuffer : public ISoundObject SoundDebugImpl
  14. {
  15. private:
  16. // the default sample rate for this sound (when pitch is set to 1)
  17. DWORD m_dwSampleRate;
  18. //
  19. // cached settings
  20. //
  21. // The current gain of the sound
  22. float m_fGain;
  23. // the current pitch of the sound
  24. float m_fPitch;
  25. // is 3D on at the moment?
  26. bool m_b3D;
  27. // are the positions and velocity listener-relative?
  28. bool m_bListenerRelative;
  29. // the current maximum distance of the sound
  30. float m_fMinimumDistance;
  31. // the current sound cone
  32. float m_fInnerAngle, m_fOuterAngle, m_fOutsideGain;
  33. // the current position of the sound
  34. Vector m_vectPosition;
  35. // the current velocity of the sound
  36. Vector m_vectVelocity;
  37. // the current orientation of the sound
  38. Vector m_vectOrientation;
  39. // convert a gain in the range -100 to 0 dB to a direct sound volume
  40. inline static LONG DSoundVolume(float fGain);
  41. protected:
  42. // The current sound buffer
  43. TRef<IDirectSoundBuffer> m_pdirectsoundbuffer;
  44. TRef<IDirectSound3DBuffer> m_pdirectsound3Dbuffer;
  45. // initializes the object, creating the DSoundBuffer itself and
  46. // initializing local variables.
  47. virtual HRESULT CreateBuffer(IDirectSound* pDirectSound, ISoundPCMData* pdata,
  48. DWORD dwBufferSize, bool bStatic, bool bSupport3D, ISoundEngine::Quality quality,
  49. bool bAllowHardware);
  50. // Use an exisiting to initialize this buffer. Note that the buffers will
  51. // share memory, so this only really works for static buffers.
  52. virtual HRESULT DuplicateBuffer(IDirectSound* pDirectSound, DS3DSoundBuffer* pBuffer);
  53. // Start the buffer, starting as a looping buffer if requested.
  54. HRESULT StartImpl(bool bLooping);
  55. // restore the contents of the buffer after a buffer loss
  56. virtual HRESULT RestoreBuffer() = 0;
  57. public:
  58. DS3DSoundBuffer();
  59. ~DS3DSoundBuffer();
  60. // Sets the volume and pitch iff the new values differ from the old.
  61. HRESULT UpdateState(float fGain, float fPitch, bool bCanDefer = true);
  62. // Sets the 3D info iff the new values differ from the old.
  63. HRESULT UpdateState3D(
  64. const Vector& vectPosition,
  65. const Vector& vectVelocity,
  66. const Vector& vectOrientation,
  67. float fMinimumDistance,
  68. float fInnerAngle,
  69. float fOuterAngle,
  70. float fOutsideGain,
  71. bool b3D,
  72. bool bListenerRelative,
  73. bool bCanDefer = true
  74. );
  75. // Starts the given buffer playing at the given position.
  76. virtual HRESULT Start(DWORD dwPosition, bool bIsStopping = false) = 0;
  77. // stops the given buffer.
  78. virtual HRESULT Stop(bool bForceNow = false) = 0;
  79. // gets the current status of the buffer
  80. HRESULT GetStatus(bool& bPlaying, bool& bBufferLost);
  81. // Gets the current position of the sound
  82. virtual HRESULT GetPosition(DWORD& dwPosition) = 0;
  83. //
  84. // ISoundDebugDump
  85. //
  86. #ifdef _DEBUG
  87. // return a human-readable description of the object, prepending
  88. // strIndent to the beginning of each line.
  89. ZString DebugDump(const ZString& strIndent = "");
  90. #endif
  91. };
  92. //
  93. // A specialization of DS3DSoundBuffer for static buffers
  94. //
  95. class DS3DStaticSoundBuffer : public DS3DSoundBuffer
  96. {
  97. // true if the buffer has been played at least once
  98. bool m_bHasBeenPlayed;
  99. // true iff this sound is a looping sound.
  100. bool m_bLooping;
  101. // The data source used to create this sound
  102. TRef<ISoundPCMData> m_pdata;
  103. // restore the contents of the buffer after a buffer loss
  104. virtual HRESULT RestoreBuffer();
  105. // fills the buffer with data from the data source
  106. virtual HRESULT LoadData();
  107. // a cache of currently playing static sound buffers
  108. class CacheKey
  109. {
  110. bool m_bSupport3D;
  111. ISoundEngine::Quality m_quality;
  112. bool m_bAllowHardware;
  113. ISoundPCMData* m_pdata;
  114. public:
  115. CacheKey(ISoundPCMData* pdata, bool bSupport3D, ISoundEngine::Quality quality, bool bAllowHardware)
  116. : m_pdata(pdata), m_bSupport3D(bSupport3D), m_quality(quality), m_bAllowHardware(bAllowHardware) {};
  117. bool operator < (const CacheKey& key) const
  118. {
  119. return (m_pdata < key.m_pdata) || (m_pdata == key.m_pdata)
  120. && ((!m_bSupport3D && key.m_bSupport3D) || (m_bSupport3D == key.m_bSupport3D)
  121. && ((m_quality < key.m_quality) || (m_quality == key.m_quality)
  122. && (!m_bAllowHardware && key.m_bAllowHardware)));
  123. }
  124. };
  125. typedef std::multimap<CacheKey, DS3DStaticSoundBuffer*> BufferCache;
  126. static BufferCache bufferCache;
  127. BufferCache::iterator m_iterSelf;
  128. public:
  129. // Constructor
  130. DS3DStaticSoundBuffer();
  131. // Destructor
  132. ~DS3DStaticSoundBuffer();
  133. // Initializes this object with the given wave data, 3D support, and sound
  134. // quality.
  135. HRESULT Init(IDirectSound* pDirectSound, ISoundPCMData* pdata,
  136. bool bLooping, bool bSupport3D, ISoundEngine::Quality quality,
  137. bool bAllowHardware
  138. );
  139. // starts the given buffer playing at the given position.
  140. virtual HRESULT Start(DWORD dwPosition, bool bIsStopping = false);
  141. // stops the given buffer.
  142. virtual HRESULT Stop(bool bForceNow = false);
  143. // Gets the current position of the sound
  144. virtual HRESULT GetPosition(DWORD& dwPosition);
  145. };
  146. //
  147. // A specialization of DS3DSoundBuffer for streaming buffers
  148. //
  149. class DS3DStreamingSoundBuffer : public DS3DSoundBuffer, protected TaskListThread::Task
  150. {
  151. protected:
  152. // true if the buffer has been played at least once
  153. bool m_bHasBeenPlayed;
  154. // true iff this sound is a looping sound.
  155. bool m_bLooping;
  156. // The data source used to create this sound
  157. TRef<ISoundPCMData> m_pdata;
  158. // the next source offset to write
  159. DWORD m_dwSourceOffset;
  160. // the next buffer offset at which to write
  161. DWORD m_dwWriteOffset;
  162. // the last buffer offset at which we saw the read pointer
  163. DWORD m_dwLastReadOffset;
  164. // the size of the streaming buffer.
  165. DWORD m_dwBufferSize;
  166. // an offset which, if the read pointer crosses, we know that we have
  167. // finished playing the sound and are now playing silence.
  168. DWORD m_dwStopOffset;
  169. // true iff we are playing silence at this point.
  170. bool m_bPlayingSilence;
  171. // The update thread which handles filling the buffers as they play
  172. static TaskListThread m_threadUpdate;
  173. // determines if a buffer cursor starting at dwStart and ending at dwEnd
  174. // would have crossed event dwTrigger.
  175. inline bool CrossedBoundary(DWORD dwTrigger, DWORD dwStart, DWORD dwEnd);
  176. // updates anything waiting for the play/read pointer to cross a certain
  177. // point in the buffer.
  178. virtual void ReadPointerUpdate(DWORD dwReadOffset);
  179. // gets the last played source offset from the last played buffer offset.
  180. virtual DWORD GetPlayedSourceOffset(DWORD dwLastPlayedPosition);
  181. // restore the contents of the buffer after a buffer loss
  182. virtual HRESULT RestoreBuffer();
  183. // Fills the buffer with as much data as it can handle.
  184. //
  185. // bTrustWritePtr is a bit of a hack for ASR sounds. Basically, the
  186. // problem is that when stop is called, an ASR sound needs to call
  187. // GetCurrentPosition to figure out the first safe place it can start
  188. // writing the sound's release. When UpdateBufferContents calls
  189. // GetCurrentPosition a moment later, however, it may return a slightly
  190. // larger offset for the minimum write position. We want to use the result
  191. // of the first call to GetCurrentPosition in this case, and not assert
  192. // that the write pointer is after the result returned by the second call
  193. // to GetCurrentPosition.
  194. virtual HRESULT UpdateBufferContents(bool bTrustWritePtr = false);
  195. // streams the given length of data out to the buffer pointed to by pvBuffer
  196. virtual HRESULT StreamData(void *pvBuffer, DWORD dwLength);
  197. // Initializes this object with the given wave data, 3D support, sound
  198. // quality, and buffer length (in seconds)
  199. HRESULT Init(IDirectSound* pDirectSound, ISoundPCMData* pdata,
  200. bool bLooping, bool bSupport3D, ISoundEngine::Quality quality,
  201. bool bAllowHardware, float fBufferLength
  202. );
  203. //
  204. // TaskListThread::Execute interface
  205. //
  206. // Updates the contents of the streaming buffer.
  207. virtual bool Execute();
  208. public:
  209. // Constructor
  210. DS3DStreamingSoundBuffer();
  211. // Destructor
  212. ~DS3DStreamingSoundBuffer();
  213. // Initializes this object with the given wave data, 3D support, and sound
  214. // quality.
  215. HRESULT Init(IDirectSound* pDirectSound, ISoundPCMData* pdata,
  216. bool bLooping, bool bSupport3D, ISoundEngine::Quality quality,
  217. bool bAllowHardware
  218. );
  219. // starts the given buffer playing at the given position.
  220. virtual HRESULT Start(DWORD dwPosition, bool bIsStopping = false);
  221. // stops the given buffer.
  222. virtual HRESULT Stop(bool bForceNow = false);
  223. // Gets the current position of the sound
  224. virtual HRESULT GetPosition(DWORD& dwPosition);
  225. // gets the current status of the buffer
  226. HRESULT GetStatus(bool& bPlaying, bool& bBufferLost);
  227. };
  228. //
  229. // A specialization of DS3DSoundBuffer for ASR buffers
  230. //
  231. class DS3DASRSoundBuffer : public DS3DStreamingSoundBuffer
  232. {
  233. private:
  234. // the start of the sustain loop in the data source
  235. DWORD m_dwLoopOffset;
  236. // the length of the sustain loop in the data source
  237. DWORD m_dwLoopLength;
  238. // an offset which, if the read pointer crosses, we know that we have
  239. // finished playing the attack part of the sound.
  240. DWORD m_dwSustainBufferOffset;
  241. // true if we are playing/have played the sustain portion of the sound
  242. bool m_bPlayingSustain;
  243. // an offset which, if the read pointer crosses, we know that we have
  244. // finished playing the sustain part of the sound and are now playing
  245. // the release part of the sound.
  246. DWORD m_dwReleaseBufferOffset;
  247. // true if we are playing/have played the release portion of the sound
  248. bool m_bPlayingRelease;
  249. // true if we are trying to transition to the release portion
  250. bool m_bEnding;
  251. // updates anything waiting for the play/read pointer to cross a certain
  252. // point in the buffer.
  253. virtual void ReadPointerUpdate(DWORD dwReadOffset);
  254. // gets the last played source offset from the last played buffer offset.
  255. virtual DWORD GetPlayedSourceOffset(DWORD dwLastPlayedPosition);
  256. // streams the given length of data out to the buffer pointed to by pvBuffer
  257. virtual HRESULT StreamData(void *pvBuffer, DWORD dwLength);
  258. public:
  259. // Destructor
  260. ~DS3DASRSoundBuffer();
  261. // Initializes this object with the given wave data, 3D support, and sound
  262. // quality.
  263. HRESULT Init(IDirectSound* pDirectSound, ISoundPCMData* pdata,
  264. DWORD dwLoopOffset, DWORD dwLoopLength, bool bSupport3D,
  265. ISoundEngine::Quality quality , bool bAllowHardware
  266. );
  267. // starts the given buffer playing at the given position.
  268. virtual HRESULT Start(DWORD dwPosition, bool bIsStopping = false);
  269. // stops the given buffer.
  270. virtual HRESULT Stop(bool bForceNow = false);
  271. };
  272. };