📄 soundsystem.cpp
字号:
//-----------------------------------------------------------------------------
// Name: SoundSystem.cpp
//
// Description: Helper class to play sound files
//
// File Function: Function file for the class
//
// Code:
// Copyright (c) 2002 LostLogic Corporation. All rights reserved.
//
// Libraries Required:
// d3d9.lib dxguid.lib d3dx9dt.lib d3dxof.lib comctl32.lib winmm.lib dsound.lib
//
// Local Files Required:
// SoundSystem.h
// SoundSystem.cpp
// DXUtil.cpp
//
// DX Files:
// Copyright (c) 1997-2001 Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#include "SoundSystem.h"
#include "dxutil.h"
#include <stdio.h>
//-----------------------------------------------------------------------------
// Name: SoundSystem::~SoundSystem()
// Desc: Sound System Class destructor
//-----------------------------------------------------------------------------
SoundSystem::~SoundSystem()
{
SAFE_RELEASE(pLoader);
SAFE_RELEASE(pPerformance);
}
//-----------------------------------------------------------------------------
// Name: SoundSystem::SoundSystem()
// Desc: Sound System Class constructor
//-----------------------------------------------------------------------------
SoundSystem::SoundSystem()
{
pLoader = NULL;
pPerformance = NULL;
}
//-----------------------------------------------------------------------------
// Name: SoundSystem::hrInitSoundSystem()
// Desc: Initializes Direct Audio
//-----------------------------------------------------------------------------
HRESULT SoundSystem::hrInitSoundSystem(void)
{
HRESULT hResult;
IDirectMusicAudioPath8 *path;
// Initialize COM
CoInitialize(NULL);
// Create the loader
if( FAILED( hResult = CoCreateInstance(CLSID_DirectMusicLoader, NULL,
CLSCTX_INPROC, IID_IDirectMusicLoader8,
(void**)&pLoader) )) {
return(SOUNDERROR_MUSICLOADER);
}
// Create the performance
if( FAILED( hResult = CoCreateInstance(CLSID_DirectMusicPerformance, NULL,
CLSCTX_INPROC, IID_IDirectMusicPerformance8,
(void**)&pPerformance ))) {
return(SOUNDERROR_MUSICPERFORMANCE);
}
// Initialize the audio
if( FAILED( hResult = pPerformance->InitAudio(
NULL,
NULL,
hWnd,
DMUS_APATH_DYNAMIC_STEREO,
4,
DMUS_AUDIOF_ALL,
NULL
))) {
return(SOUNDERROR_INITAUDIO);
}
// Get the default path
if( FAILED( pPerformance->GetDefaultAudioPath( &path ) ) )
return(SOUNDERROR_PATH);
// Set the default volume
if( FAILED( path->SetVolume(0,0) ) )
return(SOUNDERROR_VOLUME);
return(S_OK);
}
//-----------------------------------------------------------------------------
// Name: SoundSystem::hrLoadSound()
// Desc: Loads a sound and stores it in the GameSound passed
//-----------------------------------------------------------------------------
HRESULT SoundSystem::hrLoadSound(char *szname,GameSound *gs)
{
WCHAR szWideFileName[ 512 ];
// Make sure audio is initialized
if( !pLoader )
return(SOUNDERROR_MUSICLOADER);
if( !pPerformance )
return(SOUNDERROR_MUSICPERFORMANCE);
// Clean up sound if it already exists
if( gs->pSound ) {
gs->pSound->Unload( pPerformance );
gs->pSound->Release();
gs->pSound = NULL;
}
// Copy in the filename
DXUtil_ConvertGenericStringToWideCch( szWideFileName, szname, 512 );
// Load the sound from a file
if ( FAILED(pLoader->LoadObjectFromFile (
CLSID_DirectMusicSegment,
IID_IDirectMusicSegment8,
szWideFileName,
( LPVOID* ) &gs->pSound
) ) )
{
return( SOUNDERROR_LOAD );
}
gs->pPerformance = pPerformance;
// Download the data
if ( FAILED ( gs->pSound->Download( pPerformance ) ) ) {
return(SOUNDERROR_DOWNLOAD);
}
return(S_OK);
}
//-----------------------------------------------------------------------------
// Name: SoundSystem::hrPlaySound()
// Desc: Plays a GameSound segment object
//-----------------------------------------------------------------------------
HRESULT SoundSystem::hrPlaySound(GameSound *gs)
{
// Make sure there is a performance object present
if( !pPerformance )
return( SOUNDERROR_MUSICPERFORMANCE );
if( !gs->pSound )
return( SOUNDERROR_NOSEGMENT );
// Play the sound segment
if( FAILED ( pPerformance->PlaySegmentEx(
gs->pSound,
NULL,
NULL,
DMUS_SEGF_DEFAULT | DMUS_SEGF_SECONDARY,
0,
NULL,
NULL,
NULL
)))
return( SOUNDERROR_PLAYFAIL );
return(S_OK);
}
//-----------------------------------------------------------------------------
// Name: SoundSystem::hrInitSoundSystem()
// Desc: Initializes Direct Audio
//-----------------------------------------------------------------------------
GameSound::GameSound()
{
pSound = NULL;
pPerformance = NULL;
}
//-----------------------------------------------------------------------------
// Name: SoundSystem::hrInitSoundSystem()
// Desc: Initializes Direct Audio
//-----------------------------------------------------------------------------
GameSound::~GameSound()
{
if( pSound ) {
if( pPerformance )
pSound->Unload( pPerformance );
}
SAFE_RELEASE(pSound);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -