cSoundInterface.cpp
上传用户:sycq158
上传日期:2008-10-22
资源大小:15361k
文件大小:4k
- // CMAIN LIB - APPLICATION AND DIRECT WRAPPER
- //
- // Written by Mauricio Teichmann Ritter
- //
- // Copyright (C) 2002, Brazil. All rights reserved.
- //
- //
- // cSoundInterface.cpp: implementation of the cSoundInterface class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "cSoundInterface.h"
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- LPDIRECTSOUND8 cSoundInterface::m_pDS = NULL;
- LPDIRECTSOUNDBUFFER cSoundInterface::m_pDSBPrimary = NULL;
- cSoundInterface::cSoundInterface()
- {
- }
- cSoundInterface::~cSoundInterface()
- {
- }
- HRESULT cSoundInterface::Initialize( HWND hWnd,
- DWORD dwCoopLevel,
- DWORD dwPrimaryChannels,
- DWORD dwPrimaryFreq,
- DWORD dwPrimaryBitRate)
- {
- HRESULT hr;
- if( m_pDS )
- {
- m_pDS->Release();
- m_pDS = NULL;
- }
- // Create IDirectSound using the primary sound device
- if( FAILED( hr = DirectSoundCreate8( NULL, &m_pDS, NULL ) ) )
- return DXTRACE_ERR( TEXT("DirectSoundCreate8"), hr );
- // Set DirectSound coop level
- if( FAILED( hr = m_pDS->SetCooperativeLevel( hWnd, dwCoopLevel ) ) )
- return DXTRACE_ERR( TEXT("SetCooperativeLevel"), hr );
-
- // Set primary buffer format
- if( FAILED( hr = SetPrimaryBufferFormat( dwPrimaryChannels, dwPrimaryFreq, dwPrimaryBitRate ) ) )
- return DXTRACE_ERR( TEXT("SetPrimaryBufferFormat"), hr );
- return S_OK;
- }
- HRESULT cSoundInterface::SetPrimaryBufferFormat( DWORD dwPrimaryChannels,
- DWORD dwPrimaryFreq,
- DWORD dwPrimaryBitRate )
- {
- HRESULT hr;
-
- if( m_pDS == NULL )
- return CO_E_NOTINITIALIZED;
- // Get the primary buffer
- DSBUFFERDESC dsbd;
- ZeroMemory( &dsbd, sizeof(DSBUFFERDESC) );
- dsbd.dwSize = sizeof(DSBUFFERDESC);
- dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER | DSBCAPS_CTRL3D;
- dsbd.dwBufferBytes = 0;
- dsbd.lpwfxFormat = NULL;
-
- if( FAILED( hr = m_pDS->CreateSoundBuffer( &dsbd, &m_pDSBPrimary, NULL ) ) )
- return DXTRACE_ERR( TEXT("CreateSoundBuffer"), hr );
- WAVEFORMATEX wfx;
- ZeroMemory( &wfx, sizeof(WAVEFORMATEX) );
- wfx.wFormatTag = WAVE_FORMAT_PCM;
- wfx.nChannels = (WORD) dwPrimaryChannels;
- wfx.nSamplesPerSec = dwPrimaryFreq;
- wfx.wBitsPerSample = (WORD) dwPrimaryBitRate;
- wfx.nBlockAlign = (WORD) (wfx.wBitsPerSample / 8 * wfx.nChannels);
- wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
- if( FAILED( hr = m_pDSBPrimary->SetFormat(&wfx) ) )
- return DXTRACE_ERR( TEXT("SetFormat"), hr );
- return S_OK;
- }
- LPDIRECTSOUND8 cSoundInterface::GetDirectSound()
- {
- return m_pDS;
- }
- void cSoundInterface::Destroy()
- {
- if( m_pDSBPrimary )
- {
- m_pDSBPrimary->Release();
- m_pDSBPrimary = NULL;
- }
- if(m_pDS)
- {
- m_pDS->Release();
- m_pDS = NULL;
- }
- }
- void cSoundInterface::SetListernerPosition(float fX, float fY, float fZ)
- {
- IDirectSound3DListener8 *pListener=NULL;
- m_pDSBPrimary->QueryInterface(IID_IDirectSound3DListener, (void**) &pListener);
-
- if(pListener)
- {
- pListener->SetPosition(fX, fY, fZ, DS3D_IMMEDIATE);
- pListener->SetOrientation(fX, fY, fZ + 1.0f, fX, fY + 1.0f, fZ, DS3D_IMMEDIATE );
- }
- else
- DXTRACE_MSG("NO LISTENER INTERFACE");
- }