dsutil.h
上传用户:luhy168
上传日期:2022-01-10
资源大小:240k
文件大小:6k
源码类别:

模拟服务器

开发平台:

Visual C++

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