📄 soundsystem.cpp
字号:
//-----------------------------------------------------------------------------
// Name: SoundSystem.cpp
//
// Description: Helper class to play sound files
//
// File Function: Function file for the class
//
// Code:
// Copyright (c) 2002-2003 LostLogic Corporation. All rights reserved.
//
// Libraries Required:
// dxguid.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( m_pLoader );
SAFE_RELEASE( m_pPerformance );
}
//-----------------------------------------------------------------------------
// Name: SoundSystem::SoundSystem()
// Desc: Sound System Class constructor
//-----------------------------------------------------------------------------
SoundSystem::SoundSystem()
{
m_pLoader = NULL;
m_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**)&m_pLoader) )) {
return(SOUNDERROR_MUSICLOADER);
}
// Create the performance
if( FAILED( hResult = CoCreateInstance(CLSID_DirectMusicPerformance, NULL,
CLSCTX_INPROC, IID_IDirectMusicPerformance8,
(void**)&m_pPerformance ))) {
return(SOUNDERROR_MUSICPERFORMANCE);
}
// Initialize the audio
if( FAILED( hResult = m_pPerformance->InitAudio(
NULL,
NULL,
m_hWnd,
DMUS_APATH_DYNAMIC_STEREO,
4,
DMUS_AUDIOF_ALL,
NULL
))) {
return(SOUNDERROR_INITAUDIO);
}
// Get the default path
if( FAILED( m_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( !m_pLoader )
return(SOUNDERROR_MUSICLOADER);
if( !m_pPerformance )
return(SOUNDERROR_MUSICPERFORMANCE);
// Clean up sound if it already exists
if( gs->m_pSound ) {
gs->m_pSound->Unload( m_pPerformance );
gs->m_pSound->Release();
gs->m_pSound = NULL;
}
// Copy in the filename
DXUtil_ConvertGenericStringToWideCch( szWideFileName, szname, 512 );
// Load the sound from a file
if ( FAILED(m_pLoader->LoadObjectFromFile (
CLSID_DirectMusicSegment,
IID_IDirectMusicSegment8,
szWideFileName,
( LPVOID* ) &gs->m_pSound
) ) )
{
return( SOUNDERROR_LOAD );
}
// Set the game sound's performance pointer
gs->m_pPerformance = m_pPerformance;
// Download the data
if ( FAILED ( gs->m_pSound->Download( m_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( !m_pPerformance )
return( SOUNDERROR_MUSICPERFORMANCE );
// Make sure the sound segment exists
if( !gs->m_pSound )
return( SOUNDERROR_NOSEGMENT );
// Play the sound segment
if( FAILED ( m_pPerformance->PlaySegmentEx(
gs->m_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()
{
m_pSound = NULL;
m_pPerformance = NULL;
}
//-----------------------------------------------------------------------------
// Name: SoundSystem::hrInitSoundSystem()
// Desc: Initializes Direct Audio
//-----------------------------------------------------------------------------
GameSound::~GameSound()
{
if( m_pSound ) {
if( m_pPerformance )
m_pSound->Unload( m_pPerformance );
}
SAFE_RELEASE( m_pSound );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -