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