
directx8中文教程13声音和音乐.doc
7页- Failed to create the DirectAudio perfomance object."); return false; } else { LogInfo("
- DirectAudio perfomance object created OK."); } //Create the DirectAudio loader object if(CoCreateInstance(CLSID_DirectMusicLoader, NULL, CLSCTX_INPROC, IID_IDirectMusicLoader8, (void**) &m_pDirectAudioLoader) != S_OK) { LogError("
- Failed to create the DirectAudio loader object."); return false; } else { LogInfo("
- DirectAudio loader object created OK."); } //Initialise the performance object if(FAILED(m_pDirectAudioPerformance->InitAudio(NULL, NULL, hWnd, DMUS_APATH_SHARED_STEREOPLUSREVERB, 64, DMUS_AUDIOF_ALL, NULL))) { LogError("
- Failed to initialise the DirectAudio perfomance object."); return false; } else { LogInfo("
- Initialised the DirectAudio perfomance object OK."); } //Get the our applications "sounds" directory. CHAR strSoundPath[MAX_PATH]; GetCurrentDirectory(MAX_PATH, strSoundPath); strcat(strSoundPath, "\\Sounds"); //Convert the path to unicode. WCHAR wstrSoundPath[MAX_PATH]; MultiByteToWideChar(CP_ACP, 0, strSoundPath, -1, wstrSoundPath, MAX_PATH); //Set the search directory. if(FAILED(m_pDirectAudioLoader->SetSearchDirectory(GUID_DirectMusicAllTypes, wstrSoundPath, FALSE))) { LogError("
- Failed to set the search directory '%s'.", strSoundPath); return false; } else { LogInfo("
- Search directory '%s' set OK.", strSoundPath); } return true;}我们用CoCreateInstance函数创建了演奏对象和加载器,然后调用了InitAudio模块来初始化演奏对象。
上面代码中给InitAudio的参数是很典型的,所以你可以沿用它(查看一下SDK中的关于InitAudio函数以及它的参数的内容以获得更多的了解)最后,我们设置了搜索目录搜索目录应该被设置为存放声音文件的目录,以便加载器可以正确的加载声音文件在本例中,搜索目录被设置为“Sounds”文件夹,它应该在本例的项目文件夹当中A new class - CSound (一个新的类——CSound)我们已经为使用DirectX Audio作了必要的准备工作,现在,我们就可以加载、播放声音和音乐了为此,我们做了一个新的类CSound,下面是它的代码,稍后我会解释它是怎样工作的:CSound::CSound(){ m_pDirectAudioPerformance = NULL; m_pDirectAudioLoader = NULL; m_pSegment = NULL; m_pGraph = NULL; m_pMediaControl = NULL; m_pMediaPosition = NULL; m_enumFormat = Unknown; LogInfo("
- Sound created OK");}void CSound::InitialiseForWavMidi(IDirectMusicPerformance8* pDirectAudioPerformance, IDirectMusicLoader8* pDirectAudioLoader){ m_pDirectAudioPerformance = pDirectAudioPerformance; m_pDirectAudioLoader = pDirectAudioLoader; m_enumFormat = WavMidi;}void CSound::InitialiseForMP3(){ 。
DirectX TBy Andy Pike第十三章:声音和音乐Introduction (序)这一章我们学习怎样用DX来播放声音和音乐我们将会使用DirectX Audio来播放WAV和MIDI文件,使用DirectShow来播放MP3文件此章我们仍然有一个简单的程序作例子,它会播放MP3的背景音乐,当用户用鼠标点击某个数字时它会播放一些音效DirectX Audio and DirectShow (DirectX Audio和 DirectShow)我们已经学习过Direct3D和DirectInput了,现在,我要介绍另外两个DirectX组件:DirectX Audio和DirectShow我们用DirectX Audio来播放WAV和MIDI文件;用DirectShow来播放媒体流,例如AVI和MP3在此教程中,我们只学习一下用DirectShow播放MP3的方法Wav, Midi and Mp3 - When should I use what? (WAV,MIDI和MP3——什么时候该用什么?)那末,什么时候用什么格式好呢?嗯,这基本上取决于你的个人爱好下面是这些格式的简介,过后我会说一下我的选择:Wav files (WAV文件)WAV是一种未经压缩的数字音频,CD音质,但是体积非常庞大。
Midi files (MIDI文件)MIDI文件并没有保存音频记录,它实际上更像是一套演奏指令,所以,它的体积是非常小的MIDI的音质完全取决于演奏设备(如我们的声卡),它在高端的设备上能表现出色,而在低端设备上则表现较差Mp3 files (MP3文件)同WAV文件一样,MP3也是一种数字音频格式不同的是,MP3文件是经过压缩的,而且是有损压缩,这意味着它的体积将大大地减小,而音质上将会有一些失真(实际上接近CD音质,基本听不出失真)而且,MP3是一种媒体流,这意味着在播放它的时候不会将它整个的读入,取而代之的是分期的做部分读入My preference (我的选择)你大概会选择MP3或MIDI来作背景音乐,特别是当你希望用户能从网上下载你的程序时MP3和MIDI格式文件的体积都比较小,它们适合做长时间的背景音乐播放而当需要播放简短的声效时,例如爆炸声等,我更趋向于使用WAV文件,品质能稍好一些如果文件的体积不是问题的话,实际上我们可以使用WAV文件来播放所有的声音和音乐Include and Library files (头文件与库文件)我们的项目需要增加以下的头文件和库文件:o dmusici.h o dsound.h o dshow.h o dsound.lib o strmiids.lib Setting up DirectX Audio (设置DirectX Audio)要使用Direct Audio我们需要创建两个对象:演奏对象(Performance Object,有些资料翻译为执行对象)和加载器(Loader)。
演奏对象是Direct Audio中的最高级对象,用于处理从源到合成器的数据流加载器用于把文件(WAV或MIDI)加载到声音段以待播放这两个对象在整个应用程序中我们每样只需要一个,所以,我们已经把它们创建为CGame的成员变量了下面是它们的定义:IDirectMusicPerformance8* m_pDirectAudioPerformance;IDirectMusicLoader8* m_pDirectAudioLoader; 由于Direct Audio是一个纯COM,所以我们需要初始化COM其实很简单,我们只需要在CGame的构造函数中调用CoInitialize函数即可:CoInitialize(NULL); 初始化COM后,我们就可以创建上述的那两个DirectX Audio对象了为此,我们给CGame做了一个新的函数InitialiseDirectAudio,它将会在Initialise函数中被调用代码如下:bool CGame::InitialiseDirectAudio(HWND hWnd){ LogInfo("
Initialise DirectAudio:"); //Create the DirectAudio performance object if(CoCreateInstance(CLSID_DirectMusicPerformance, NULL, CLSCTX_INPROC, IID_IDirectMusicPerformance8, (void**) &m_pDirectAudioPerformance) != S_OK) { LogError("
