My Project
dsutil.h
1 //-----------------------------------------------------------------------------
2 // File: DSUtil.h
3 //
4 // Desc:
5 //
6 // Copyright (c) Microsoft Corp. All rights reserved.
7 //-----------------------------------------------------------------------------
8 #ifndef DSUTIL_H
9 #define DSUTIL_H
10 
11 #include <windows.h>
12 #include <mmsystem.h>
13 #include <mmreg.h>
14 #include <dsound.h>
15 
16 
17 
18 
19 //-----------------------------------------------------------------------------
20 // Classes used by this header
21 //-----------------------------------------------------------------------------
22 class CSoundManager;
23 class CSound;
24 class CStreamingSound;
25 class CWaveFile;
26 
27 
28 
29 
30 //-----------------------------------------------------------------------------
31 // Typing macros
32 //-----------------------------------------------------------------------------
33 #define WAVEFILE_READ 1
34 #define WAVEFILE_WRITE 2
35 
36 #define DSUtil_StopSound(s) { if(s) s->Stop(); }
37 #define DSUtil_PlaySound(s) { if(s) s->Play( 0, 0 ); }
38 #define DSUtil_PlaySoundLooping(s) { if(s) s->Play( 0, DSBPLAY_LOOPING ); }
39 
40 
41 
42 
43 //-----------------------------------------------------------------------------
44 // Name: class CSoundManager
45 // Desc:
46 //-----------------------------------------------------------------------------
48 {
49 protected:
50  LPDIRECTSOUND8 m_pDS;
51 
52 public:
53  CSoundManager();
54  ~CSoundManager();
55 
56  HRESULT Initialize( HWND hWnd, DWORD dwCoopLevel );
57  inline LPDIRECTSOUND8 GetDirectSound() { return m_pDS; }
58  HRESULT SetPrimaryBufferFormat( DWORD dwPrimaryChannels, DWORD dwPrimaryFreq, DWORD dwPrimaryBitRate );
59  HRESULT Get3DListenerInterface( LPDIRECTSOUND3DLISTENER* ppDSListener );
60 
61  HRESULT Create( CSound** ppSound, LPTSTR strWaveFileName, DWORD dwCreationFlags = 0, GUID guid3DAlgorithm = GUID_NULL, DWORD dwNumBuffers = 1 );
62  HRESULT CreateFromMemory( CSound** ppSound, BYTE* pbData, ULONG ulDataSize, LPWAVEFORMATEX pwfx, DWORD dwCreationFlags = 0, GUID guid3DAlgorithm = GUID_NULL, DWORD dwNumBuffers = 1 );
63  HRESULT CreateStreaming( CStreamingSound** ppStreamingSound, LPTSTR strWaveFileName, DWORD dwCreationFlags, GUID guid3DAlgorithm, DWORD dwNotifyCount, DWORD dwNotifySize, HANDLE hNotifyEvent );
64 };
65 
66 
67 
68 
69 //-----------------------------------------------------------------------------
70 // Name: class CSound
71 // Desc: Encapsulates functionality of a DirectSound buffer.
72 //-----------------------------------------------------------------------------
73 class CSound
74 {
75 protected:
76  LPDIRECTSOUNDBUFFER* m_apDSBuffer;
77  DWORD m_dwDSBufferSize;
78  CWaveFile* m_pWaveFile;
79  DWORD m_dwNumBuffers;
80  DWORD m_dwCreationFlags;
81 
82  HRESULT RestoreBuffer( LPDIRECTSOUNDBUFFER pDSB, BOOL* pbWasRestored );
83 
84 public:
85  CSound( LPDIRECTSOUNDBUFFER* apDSBuffer, DWORD dwDSBufferSize, DWORD dwNumBuffers, CWaveFile* pWaveFile, DWORD dwCreationFlags );
86  virtual ~CSound();
87 
88  HRESULT Get3DBufferInterface( DWORD dwIndex, LPDIRECTSOUND3DBUFFER* ppDS3DBuffer );
89  HRESULT FillBufferWithSound( LPDIRECTSOUNDBUFFER pDSB, BOOL bRepeatWavIfBufferLarger );
90  LPDIRECTSOUNDBUFFER GetFreeBuffer();
91  LPDIRECTSOUNDBUFFER GetBuffer( DWORD dwIndex );
92 
93  HRESULT Play( DWORD dwPriority = 0, DWORD dwFlags = 0, LONG lVolume = 0, LONG lFrequency = -1, LONG lPan = 0 );
94  HRESULT Play3D( LPDS3DBUFFER p3DBuffer, DWORD dwPriority = 0, DWORD dwFlags = 0, LONG lFrequency = 0 );
95  HRESULT Stop();
96  HRESULT Reset();
97  BOOL IsSoundPlaying();
98 };
99 
100 
101 
102 
103 //-----------------------------------------------------------------------------
104 // Name: class CStreamingSound
105 // Desc: Encapsulates functionality to play a wave file with DirectSound.
106 // The Create() method loads a chunk of wave file into the buffer,
107 // and as sound plays more is written to the buffer by calling
108 // HandleWaveStreamNotification() whenever hNotifyEvent is signaled.
109 //-----------------------------------------------------------------------------
110 class CStreamingSound : public CSound
111 {
112 protected:
113  DWORD m_dwLastPlayPos;
114  DWORD m_dwPlayProgress;
115  DWORD m_dwNotifySize;
116  DWORD m_dwNextWriteOffset;
117  BOOL m_bFillNextNotificationWithSilence;
118 
119 public:
120  CStreamingSound( LPDIRECTSOUNDBUFFER pDSBuffer, DWORD dwDSBufferSize, CWaveFile* pWaveFile, DWORD dwNotifySize );
121  ~CStreamingSound();
122 
123  HRESULT HandleWaveStreamNotification( BOOL bLoopedPlay );
124  HRESULT Reset();
125 };
126 
127 
128 
129 
130 //-----------------------------------------------------------------------------
131 // Name: class CWaveFile
132 // Desc: Encapsulates reading or writing sound data to or from a wave file
133 //-----------------------------------------------------------------------------
135 {
136 public:
137  WAVEFORMATEX* m_pwfx; // Pointer to WAVEFORMATEX structure
138  HMMIO m_hmmio; // MM I/O handle for the WAVE
139  MMCKINFO m_ck; // Multimedia RIFF chunk
140  MMCKINFO m_ckRiff; // Use in opening a WAVE file
141  DWORD m_dwSize; // The size of the wave file
142  MMIOINFO m_mmioinfoOut;
143  DWORD m_dwFlags;
144  BOOL m_bIsReadingFromMemory;
145  BYTE* m_pbData;
146  BYTE* m_pbDataCur;
147  ULONG m_ulDataSize;
148  CHAR* m_pResourceBuffer;
149 
150 protected:
151  HRESULT ReadMMIO();
152  HRESULT WriteMMIO( WAVEFORMATEX *pwfxDest );
153 
154 public:
155  CWaveFile();
156  ~CWaveFile();
157 
158  HRESULT Open( LPTSTR strFileName, WAVEFORMATEX* pwfx, DWORD dwFlags );
159  HRESULT OpenFromMemory( BYTE* pbData, ULONG ulDataSize, WAVEFORMATEX* pwfx, DWORD dwFlags );
160  HRESULT Close();
161 
162  HRESULT Read( BYTE* pBuffer, DWORD dwSizeToRead, DWORD* pdwSizeRead );
163  HRESULT Write( UINT nSizeToWrite, BYTE* pbData, UINT* pnSizeWrote );
164 
165  DWORD GetSize();
166  HRESULT ResetFile();
167  WAVEFORMATEX* GetFormat() { return m_pwfx; };
168 };
169 
170 
171 
172 
173 #endif // DSUTIL_H
Definition: dsutil.h:134
Definition: dsutil.h:47
Definition: dsutil.h:73
Definition: dsutil.h:110