Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cDirectSound.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cDirectSound_H
7 #define _INC_cDirectSound_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 #include "../Gray3D.h"
13 
19 
23 
24 namespace Gray3D
25 {
26  typedef float SOUND_POWER_t;
27  static const SOUND_POWER_t k_SOUND_POWER_DEF = 1e10f;
28  static const SOUND_POWER_t k_SOUND_POWER_DEFW = 2.5f;
29 };
30 
31 #ifdef USE_DX9
32 #include <dsound.h>
33 
34 MIDL_INTERFACE("279AFA86-4981-11CE-A521-0020AF0BE560") IDirectSound3DBuffer; // SAME GUID ! THIS SEEMS wrong!
35 MIDL_INTERFACE("279AFA85-4981-11CE-A521-0020AF0BE560") IDirectSoundBuffer;
36 
37 namespace Gray3D
38 {
39  template class GRAY3D_LINK cIUnkPtr<IDirectSoundBuffer>;
40  template class GRAY3D_LINK cIUnkPtr<IDirectSound3DBuffer>;
41  template class GRAY3D_LINK cIUnkPtr<ISoundObject>;
42 
43  class GRAY3D_LINK cDirectSoundInst : public cRefBase, public cHeapObject
44  {
48  friend class cDirectSoundMgr;
49  public:
50  cDirectSoundInst( ISoundObject* pSoundObj, bool bUse3D=true, SOUND_POWER_t fPower=k_SOUND_POWER_DEF );
51  cDirectSoundInst( const cWaveFormatEx* pwf, size_t dwDataSize, const void* pWaveData, bool bUse3D=true, SOUND_POWER_t fPower=k_SOUND_POWER_DEF );
52  virtual ~cDirectSoundInst();
53 
54  bool isValid() const noexcept
55  {
56  return m_pDSBuffer.isValidPtr();
57  }
58 
59  HRESULT SetupBuffer( const cWaveFormatEx* pwf, size_t dwDataSize, const void* pWaveData );
60  HRESULT SetupBuffer( ISoundObject* pSoundObj );
61 
62  DWORD get_StatusFlags() const; // DSBSTATUS_PLAYING
63  bool isPlaying() const
64  {
65  return(( get_StatusFlags() & DSBSTATUS_PLAYING ) ? true : false );
66  }
67  bool isLooping() const
68  {
69  return(( get_StatusFlags() & DSBSTATUS_LOOPING ) ? true : false );
70  }
71 
72  bool Play( const cVector3f* ppt=nullptr, SOUND_VOL_t nVolMax=cSoundBase::k_VOL_MAX, int iRepeat=0 );
73  bool Stop();
74 
75  IDirectSoundBuffer* get_Buffer() const
76  { return m_pDSBuffer;}
77  IDirectSound3DBuffer* get_Buffer3D() const // bUseDX3D
78  { return m_pDS3DBuffer; }
79 
80  bool UpdatePos( const cVector3f& vPos, const cVector3f& vVelocity );
81  void put_VolumeDefer( SOUND_VOL_t nVol );
82 
83  protected:
84  HRESULT WriteBuffer( const void* pWaveData, size_t& dwDataSize );
85  HRESULT CreateBuffer( const cWaveFormatEx* pwf, size_t dwDataSize, bool bUseDX3D );
86 
87  void Update3D( bool bFirst ); // called by cDirectSoundMgr
88 
89  protected:
91 
92  // config params
93  bool m_bUse3D;
94  cVector3f m_vPos;
95  cVector3f m_vVelocity;
96 
97  // Attenuation of the sound for 1. constant offset 2. distance.
98  float m_fVolumeMax;
99  SOUND_POWER_t m_fPower;
100 
101  // implementation
102  cIUnkPtr<IDirectSoundBuffer> m_pDSBuffer;
103  cIUnkPtr<IDirectSound3DBuffer> m_pDS3DBuffer;
104  cIUnkPtr<ISoundObject> m_pSoundObj;
105  };
106 
107  typedef cRefPtr<cDirectSoundInst> cDirectSoundInstPtr;
108 
109  class GRAY3D_LINK cDirectSoundMgr
110  : public cSingletonStatic<cDirectSoundMgr>
111  {
115  friend class cDirectSoundInst;
116  public:
117  cDirectSoundMgr( bool bUseDX3D=false, int iMaxSounds=8 );
118  ~cDirectSoundMgr();
119 
120  HRESULT InitSoundMgr( HWND hWnd, const cVector3f* ptEarPosition );
121  void CloseSoundMgr();
122  void ClearAll();
123  void put_VolEffects( SOUND_VOL_t nVol );
124 
125  const cVector3f& get_EarPosition() const
126  {
127  return m_vEarPosition; // Last update for the view for sounds.
128  }
129  void put_EarPosition( const cVector3f& ptEarPosition );
130  void put_EarFrontDir( const cVector3f& ptEarOrient );
131  void put_EarTopDir( const cVector3f& ptEarOrient );
132  void put_EarVelocity( const cVector3f& ptEarVelocity );
133 
134  cDirectSoundInstPtr PlaySound( ISoundObject* pSoundObj, SOUND_POWER_t fPower=k_SOUND_POWER_DEF, const cVector3f* ppt=nullptr, int iRepeat=0, SOUND_VOL_t nVolMax=cSoundBase::k_VOL_MAX );
135  bool isOpen() const
136  {
137  return( m_pDS != nullptr );
138  }
139  IDirectSound* get_DS() const
140  { return m_pDS; }
141 
142  bool UpdateSounds( bool bForceUpdate = false );
143 
144  private:
145  bool CheckMaxSounds();
146 
147  public:
148  // config
149  int m_iMaxSounds;
150  TIMESYSD_t m_iUpdate3DPeriod;
151  bool m_bUseDX3D;
152 
153  private:
154  // dynamic
155  cVector3f m_vEarPosition;
156  cVector3f m_vEarVelocity;
157  cVector3f m_vEarFrontDir;
158  cVector3f m_vEarTopDir;
159 
160  cTimeSys m_TimeLastUpdate;
161  bool m_bChange3D;
162 
163  private:
164  cIUnkPtr<IDirectSound8> m_pDS;
165  cIUnkPtr<IDirectSound3DListener> m_pDS3DListener;
166  cArrayRef<cDirectSoundInst> m_aSoundInsts;
167  };
168 };
169 #endif // USE_DX9
170 #endif // _INC_CDSOUND_H
MIDL_INTERFACE("7ED943DD-52E8-40b5-A8D8-76685C406330") ID3DXBaseMesh
#define GRAY3D_LINK
Definition: Gray3D.h:15
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
#define CHEAPOBJECT_IMPL
Definition: cHeapObject.h:32
Definition: Gray3D.cpp:12
float SOUND_POWER_t
Power of the sound. This roughly corresponds to hearing distance, but THIS IS NOT CUT-OFF DISTANCE.
Definition: cDirectSound.h:26
WORD SOUND_VOL_t
Mono Attenuation of the audio. linear perception 0 to k_VOL_MAX(0xffff). NOT decibels.
Definition: cSoundBase.h:29
INT32 TIMESYSD_t
Time delta. signed milli-Seconds Span. cTimeSys::k_DMAX, cTimeSys::k_INF = MAILSLOT_WAIT_FOREVER.
Definition: cTimeSys.h:28