【VC开源代码栏目提醒】:网学会员--在 VC开源代码编辑为广大网友搜集整理了:DMUTIL.CPP绩等信息,祝愿广大网友取得需要的信息,参考学习。
//-----------------------------------------------------------------------------
// File: DMUtil.cpp
//
// Desc: DirectMusic framework classes for playing DirectMusic segments and
// DirectMusic scripts. Feel free to use this class as a starting point
// for adding extra functionality.
//
// Copyright (c) 1999-2000 Microsoft Corp. All rights reserved.
//-----------------------------------------------------------------------------
#define STRICT
#include <dmusicc.h>
#include <dmusici.h>
#include <dsound.h>
#include <dxerr8.h>
#include "DMUtil.h"
#include "DXUtil.h"
//-----------------------------------------------------------------------------
// Name: CMusicManager::CMusicManager()
// Desc: Constructs the class
//-----------------------------------------------------------------------------
CMusicManager::CMusicManager()
{
m_pLoader = NULL;
m_pPerformance = NULL;
// Initialize COM
CoInitialize(NULL);
}
//-----------------------------------------------------------------------------
// Name: CMusicManager::~CMusicManager()
// Desc: Destroys the class
//-----------------------------------------------------------------------------
CMusicManager::~CMusicManager()
{
SAFE_RELEASE( m_pLoader );
if( m_pPerformance )
{
// If there is any music playing, stop it.
m_pPerformance->Stop( NULL, NULL, 0, 0 );
m_pPerformance->CloseDown();
SAFE_RELEASE( m_pPerformance );
}
CoUninitialize();
}
//-----------------------------------------------------------------------------
// Name: CMusicManager::Initialize()
// Desc: Inits DirectMusic using a standard audio path
//-----------------------------------------------------------------------------
HRESULT CMusicManager::Initialize( HWND hWnd, DWORD dwPChannels, DWORD dwDefaultPathType )
{
HRESULT hr;
// Create loader object
if( FAILED( hr = CoCreateInstance( CLSID_DirectMusicLoader, NULL, CLSCTX_INPROC,
IID_IDirectMusicLoader8, (void**)&m_pLoader ) ) )
return DXTRACE_ERR( TEXT("CoCreateInstance"), hr );
// Create performance object
if( FAILED( hr = CoCreateInstance( CLSID_DirectMusicPerformance, NULL, CLSCTX_INPROC,
IID_IDirectMusicPerformance8, (void**)&m_pPerformance ) ) )
return DXTRACE_ERR( TEXT("CoCreateInstance"), hr );
// Initialize the performance with the standard audio path.
// This initializes both DirectMusic and DirectSound and
// sets up the synthesizer. Typcially its easist to use an
// audio path for playing music and sound effects.
if( FAILED( hr = m_pPerformance->InitAudio( NULL, NULL, hWnd, dwDefaultPathType,
dwPChannels, DMUS_AUDIOF_ALL, NULL ) ) )
return DXTRACE_ERR( TEXT("InitAudio"), hr );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: CMusicManager::SetSearchDirectory()
// Desc: Sets the search directory. If not called, the current working
// directory is used to load content.
//-----------------------------------------------------------------------------
HRESULT CMusicManager::SetSearchDirectory( const TCHAR* strMediaPath )
{
if( NULL == m_pLoader )
return E_UNEXPECTED;
// DMusic only takes wide strings
WCHAR wstrMediaPath[MAX_PATH];
DXUtil_ConvertGenericStringToWide( wstrMediaPath, strMediaPath );
return m_pLoader->SetSearchDirectory( GUID_DirectMusicAllTypes,
wstrMediaPath, FALSE );
}
//-----------------------------------------------------------------------------
// Name: CMusicManager::GetDefaultAudioPath()
// Desc:
//-----------------------------------------------------------------------------
IDirectMusicAudioPath8* CMusicManager::GetDefaultAudioPath()
{
IDirectMusicAudioPath8* pAudioPath = NULL;
if( NULL == m_pPerformance )
return NULL;
m_pPerformance->GetDefaultAudioPath( &pAudioPath );
return pAudioPath;
}
//-----------------------------------------------------------------------------
// Name: CMusicManager::CollectGarbage()
// Desc: Tells the loader to cleanup any garbage from previously
// released objects.
//-----------------------------------------------------------------------------
VOID CMusicManager::CollectGarbage()
{
if( m_pLoader )
m_pLoader->CollectGarbage();
}
//-----------------------------------------------------------------------------
// Name: CMusicManager::CreateSegmentFromFile()
// Desc:
//-----------------------------------------------------------------------------
HRESULT CMusicManager::CreateSegmentFromFile( CMusicSegment** ppSegment,
TCHAR* strFileName,
BOOL bDownloadNow,
BOOL bIsMidiFile )
{
HRESULT hr;
IDirectMusicSegment8* pSegment = NULL;
// DMusic only takes wide strings
WCHAR wstrFileName[MAX_PATH];
DXUtil_ConvertGenericStringToWide( wstrFileName, strFileName );
if ( FAILED( hr = m_pLoader->LoadObjectFromFile( CLSID_DirectMusicSegment,
IID_IDirectMusicSegment8,
wstrFileName,
(LPVOID*) &pSegment ) ) )
{
if( hr == DMUS_E_LOADER_FAILEDOPEN )
return hr;
return DXTRACE_ERR( TEXT("LoadObjectFromFile"), hr );
}
*ppSegment = new CMusicSegment( m_pPerformance, m_pLoader, pSegment );
if (!*ppSegment)
return E_OUTOFMEMORY;
if( bIsMidiFile )
{
if( FAILED( hr = pSegment->SetParam( GUID_StandardMIDIFile,
0xFFFFFFFF, 0, 0, NULL ) ) )
return DXTRACE_ERR( TEXT("SetParam"), hr );
}
if( bDownloadNow )
{
if( FAILED( hr = (*ppSegment)->Download() ) )
return DXTRACE_ERR( TEXT("Download"), hr );
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: CMusicManager::CreateSegmentFromResource()
// Desc:
//-----------------------------------------------------------------------------
HRESULT CMusicManager::CreateSegmentFromResource( CMusicSegment** ppSegment,
TCHAR* strResource,
TCHAR* strResourceType,
BOOL bDownloadNow,
BOOL bIsMidiFile )
{
HRESULT hr;
IDirectMusicSegment8* pSegment = NULL;
HRSRC hres = NULL;
void* pMem = NULL;
DWORD dwSize = 0;
DMUS_OBJECTDESC objdesc;
// Find the resource
hres = FindResource( NULL,strResource,strResourceType );
if( NULL == hres )
return E_FAIL;
// Load the resource
pMem = (void*)LoadResource( NULL, hres );
if( NULL == pMem )
return E_FAIL;
// Store the size of the resource
dwSize = SizeofResource( NULL, hres );
// Set up our object description
ZeroMemory(&objdesc,sizeof(DMUS_OBJECTDESC));
objdesc.dwSize = sizeof(DMUS_OBJECTDESC);
objdesc.dwValidData = DMUS_OBJ_MEMORY | DMUS_OBJ_CLASS;
objdesc.guidClass = CLSID_DirectMusicSegment;
objdesc.llMemLength =(LONGLONG)dwSize;
objdesc.pbMemData = (BYTE*)pMem;
if (FAILED ( hr = m_pLoader->GetObje
上一篇:
CLLX.pas
下一篇:
英文课程作业:电影库(Piddington library )建立方案分析