ds3dengine.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. //
  2. // ds3dengine.h
  3. //
  4. // Classes representing a DirectSound3D implementation of a sound engine
  5. //
  6. namespace SoundEngine {
  7. //
  8. // A sound engine implementation using direct sound 3D that handles basic
  9. // resource management.
  10. //
  11. class DS3DSoundEngine : public ISoundEngine, public ISoundBufferSource SoundDebugImpl
  12. {
  13. // the currently chosen DirectSound implementation
  14. TRef<IDirectSound> m_pDirectSound;
  15. // the primary buffer of this DirectSound instance
  16. TRef<IDirectSoundBuffer> m_pPrimaryBuffer;
  17. // the capabilities of the chosen DirectSound implementation
  18. DSCAPS m_dscaps;
  19. // the listener used to render sound
  20. TRef<ISoundListener> m_plistener;
  21. // the coresponding DirectSound3D listener for this instance.
  22. TRef<IDirectSound3DListener> m_pDSListener;
  23. // the rolloff factor, where 1.0f is the real-world rolloff
  24. float m_fRolloffFactor;
  25. // the doppler factor, where 1.0f is the real-world doppler
  26. float m_fDopplerFactor;
  27. // the distance factor, where 1.0f is the real-world distance factor
  28. float m_fDistanceFactor;
  29. // the update event source, triggered on each call to update.
  30. TRef<IntegerEventSourceImpl> m_peventsourceUpdate;
  31. // a delegate buffer source for this (needed to avoid circular refs)
  32. TRef<ISoundBufferSource> m_pBufferSourceDelegate;
  33. // a list of currently playing virtual sounds
  34. typedef std::list<ZAdapt<TRef<DSVirtualSoundBuffer> > > VirtualSoundList;
  35. VirtualSoundList m_listActiveSounds;
  36. // the time of the last call to update
  37. DWORD m_dwLastUpdateTime;
  38. // the sound quality to use
  39. Quality m_quality;
  40. // whether to allow hardware acceleration
  41. bool m_bAllowHardware;
  42. // the desired number of sounds to play at once. This number may reserve
  43. // a few voices for fading sounds out.
  44. int m_nNumBuffersDesired;
  45. // the maximum number of sounds we can play at once, including sounds that
  46. // are fading in or out.
  47. int m_nNumBuffersMax;
  48. // dumps the capablilites of this DirectSound implementation to the
  49. // debug output.
  50. void DumpCaps();
  51. // Sets the format of the primary buffer to the given sample rate, number
  52. // of bits, and number of channels
  53. HRESULT SetPrimaryBufferFormat(int nSampleRate, int nNumberOfBits, int nChannels);
  54. // updates the listener position, orientation, etc. for direct sound 3D.
  55. HRESULT UpdateListener();
  56. // Is this an error worth killing a virtual sound for, or just something
  57. // transitory? For example, we may lose a sound buffer when being swapped
  58. // to the background, but we want the sound to continue playing when we
  59. // are swapped back in.
  60. static bool IsSeriousError(HRESULT hr);
  61. // provides a complete ordering of virtual sound buffers by priority.
  62. struct SoundPriorityCompare
  63. {
  64. typedef DSVirtualSoundBuffer* first_argument_type;
  65. typedef DSVirtualSoundBuffer* second_argument_type;
  66. typedef bool result_type;
  67. bool operator () (DSVirtualSoundBuffer* psound1, DSVirtualSoundBuffer* psound2) const
  68. {
  69. return psound1->GetDynamicPriority() > psound2->GetDynamicPriority()
  70. || (psound1->GetDynamicPriority() == psound2->GetDynamicPriority()
  71. && psound1 > psound2);
  72. }
  73. };
  74. template <class T>
  75. struct RefToPtr
  76. {
  77. T* operator () (TRef<T>& rt)
  78. {
  79. return rt;
  80. }
  81. };
  82. public:
  83. // Constructor - the real initialization is done in init.
  84. DS3DSoundEngine();
  85. ~DS3DSoundEngine();
  86. // Basic initialization. This was pulled out of the constructor so that we
  87. // can return error values.
  88. HRESULT Init(HWND hwnd);
  89. //
  90. // ISoundEngine Interface
  91. //
  92. // Rebuild the sound stage to reflect any recent changes in sound
  93. virtual HRESULT Update();
  94. // Gets a buffer source for this object (not guarenteed to keep the sound
  95. // engine alive due to circular reference problems)
  96. virtual ISoundBufferSource* GetBufferSource();
  97. // Gets the number of virtual sound buffers that are playing at a given
  98. // moment. (no guarantees on how this number changes - it's just a perf.
  99. // number to use.)
  100. virtual HRESULT GetNumPlayingVirtualBuffers(int& nBuffers);
  101. // Sets a general quality of playback (CPU time vs. fidelity)
  102. HRESULT SetQuality(Quality quality);
  103. // Allows/disallows hardware acceleration.
  104. HRESULT EnableHardware(bool bEnable);
  105. // Sets the listener to use for the current sounds
  106. virtual HRESULT SetListener(ISoundListener* plistener);
  107. // Sets the conversion from game units to meters
  108. virtual HRESULT SetDistanceFactor(float fMetersPerUnit);
  109. // Sets the rolloff factor, where 1.0 is the real world attenuation with
  110. // distance, 2.0 is twice the attenuation of the real world, etc..
  111. virtual HRESULT SetRolloffFactor(float fRolloffFactor);
  112. // Sets the doppler factor, where 1.0 is real-world doppler
  113. virtual HRESULT SetDopplerFactor(float fDopplerFactor);
  114. //
  115. // ISoundBufferSource
  116. //
  117. // Creates a static sound buffer of the given wave file. If bLooping is
  118. // true, the sound will loop until stopped.
  119. virtual HRESULT CreateStaticBuffer(TRef<ISoundInstance>& psoundNew,
  120. ISoundPCMData* pcmdata, bool bLooping, ISoundPositionSource* psource = NULL);
  121. // Creates a sound buffer with a loop in the middle. The sound will play
  122. // the start sound, play the loop sound until it gets a soft stop, then
  123. // play the rest of the sound.
  124. virtual HRESULT CreateASRBuffer(TRef<ISoundInstance>& psoundNew,
  125. ISoundPCMData* pcmdata, unsigned uLoopStart, unsigned uLoopLength,
  126. ISoundPositionSource* psource = NULL);
  127. // Gets an event which fires each time update is called. This can be used
  128. // for some of the trickier sounds that change with time.
  129. virtual IIntegerEventSource* GetUpdateEventSource();
  130. //
  131. // ISoundDebugDump
  132. //
  133. #ifdef _DEBUG
  134. // return a human-readable description of the object, prepending
  135. // strIndent to the beginning of each line.
  136. ZString DebugDump(const ZString& strIndent = "");
  137. #endif
  138. };
  139. };