如何在游戏制作中使用MIDI文件演奏音乐

王朝other·作者佚名  2008-05-19
窄屏简体版  字體: |||超大  

下面用 DirectX 8 来演奏音乐(MIDI 文件)。

DirectX 的标准声音文件好象是 *.sgt ,不过 MIDI 格式的文件也可以直接播放。

程序概要:

1.

从 WinMain() 中调用开始演奏的函数(InitDirectMusic),在这个函数中进行演奏音乐的全部处理,返回后只是打开 DialogBox(对话框) 让人欣赏音乐。

2.

关闭 DialogBox 时调用停止演奏的函数(FreeDirectMusic),释放所有 object(对象) 并退出程序。

3.

讲得好象还不是太清楚,这里再附加几条说明。(-.-;) (^_^;

hr=CoInitialize(NULL);

初始化 DirectMusic 的 COM(Component Object Model|组件对象模型)。

InitializeSynth()

初始化 Software Synthesizer(软件合成) 的函数。

LoadSegment()

载入演奏文件的函数。

注意该函数中如下一段代码:

wcscpy( ObjDesc.wszFileName, L"Midi.mid" );

在这里设定要演奏的 MIDI 文件名。

自己准备个喜欢的 MIDI 文件,改名为 Midi.mid ,放进工程文件夹。

下面说明建立工程的步骤:

1.

新建一个 Win32 Application 空白工程,命名为 "Midi"。

2.

向工程中新建一个 C++ Source File ,命名为 "midi" ,向其中键入篇末附带的源程序。

3.

选择菜单 [Insert|插入]-[Resource...|资源...] 打开 [Insert Resource|插入资源] 面板,在左栏内选中 [Dialog|对话框] ,然后点击 [New|新建] :

(参见 §06. 显示 Dialog Box (对话框))

这回的 DialogBox 只需要一个 ID = IDC_CLOSE , Caption = CLOSE 的按扭:

保存资源文件,文件名 "dialog.rc" ,然后选择菜单 [Project|工程]-[Add To Project|添加到工程]-[Files...|文件...] ,向工程中添加下面两个文件:

dialog.rc

resource.h

(参见 §06. 显示 Dialog Box (对话框))

4.

选择菜单 [Project|工程]-[Settings...|设定...] 打开[Project Settings|工程设定] 面板,点击 [Link|链接] 标签,向 [Object/library modules|对象、库模块] 栏内添加下面一个库文件:

dxguid.lib

5.

编译并执行!

源程序:

/****************************************************/

/*★ 演奏音乐(MIDI 文件)

2001-01-23

前田 稔 ★*/

/****************************************************/

#include

#include

#include "resource.h"

#define

ERRMSG(x) MessageBox(NULL, x, "PlayPri", MB_OK);

// Global variables for the DirectMusic sample

IDirectMusicLoader8*

g_pLoader

= NULL;

IDirectMusicPerformance8* g_pPerformance = NULL;

IDirectMusicSegment8*

g_pSegment

= NULL;

// 函数类型说明

HRESULT

InitDirectMusic(LPSTR lpCmdLine );

HRESULT

InitializeSynth();

HRESULT

LoadSegment();

HRESULT

FreeDirectMusic();

//★ DlgProc()

LRESULT CALLBACK DlgProc(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)

{

switch (message)

{

case WM_COMMAND:

switch (LOWORD(wParam))

{

case IDC_CLOSE:

PostQuitMessage(0 );

break;

}

break;

case WM_CLOSE:

EndDialog(hDlg, TRUE );

return (TRUE);

}

return FALSE;

}

//★ Windows 主函数

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,

LPSTR lpCmdLine, int nCmdShow )

{

HRESULT hr;

hr = InitDirectMusic(lpCmdLine );

if (FAILED(hr))

{

FreeDirectMusic();

return FALSE;

}

// Display the main dialog box.

DialogBox(hInstance,MAKEINTRESOURCE(IDD_DIALOG1),NULL,(DLGPROC)DlgProc);

FreeDirectMusic();

return TRUE;

}

//★ InitDirectMusic()

HRESULT InitDirectMusic(LPSTR lpCmdLine )

{

HRESULT hr;

// Initialize COM

hr = CoInitialize(NULL);

if (FAILED(hr))

{

ERRMSG("Could not initialize COM");

return hr;

}

// Create loader object

hr = CoCreateInstance(CLSID_DirectMusicLoader, NULL, CLSCTX_INPROC,

IID_IDirectMusicLoader8, (void**)&g_pLoader );

if (FAILED(hr))

{

ERRMSG("Could not create DMusic Loader");

return hr;

}

// Create performance object

hr = CoCreateInstance(CLSID_DirectMusicPerformance, NULL, CLSCTX_INPROC,

IID_IDirectMusicPerformance8, (void**)&g_pPerformance );

if (FAILED(hr))

{

ERRMSG("Could not create DMusic Performance");

return hr;

}

// Initialize the software synthesizer

hr = InitializeSynth();

if (FAILED(hr))

return hr;

// Load the segment

hr = LoadSegment();

if (FAILED(hr))

{

ERRMSG("Could not load segment");

return hr;

}

// set the segment to repeat many times

g_pSegment-SetRepeats(200);

g_pPerformance-PlaySegment(g_pSegment, DMUS_SEGF_BEAT, 0, NULL );

return S_OK;

}

//★ InitializeSynth()

HRESULT InitializeSynth()

{

HRESULT hr;

hr = g_pPerformance-Init(NULL,NULL,NULL);

if (FAILED(hr))

{

ERRMSG("Could not initialize performance");

return hr;

}

BOOL fAutoDownload = TRUE;

g_pPerformance-SetGlobalParam(GUID_PerfAutoDownload,&fAutoDownload,sizeof(BOOL));

hr = g_pPerformance-AddPort(NULL );

if (FAILED(hr))

{

ERRMSG("Could not add port to performance");

return hr;

}

return S_OK;

}

//★ LoadSegment()

HRESULT LoadSegment()

{

HRESULT

hr;

DMUS_OBJECTDESC ObjDesc; // Object descriptor for pLoader-GetObject()

// sections load as type Segment, as do MIDI files, for example.

ObjDesc.guidClass = CLSID_DirectMusicSegment;

ObjDesc.dwSize = sizeof(DMUS_OBJECTDESC);

//

wcscpy(ObjDesc.wszFileName, L"Sample.sgt" );

wcscpy(ObjDesc.wszFileName, L"Midi.mid" );

ObjDesc.dwValidData = DMUS_OBJ_CLASS | DMUS_OBJ_FILENAME;

hr = g_pLoader-GetObject(&ObjDesc, IID_IDirectMusicSegment, (void**)&g_pSegment );

return hr;

}

//★ FreeDirectMusic()

HRESULT FreeDirectMusic()

{

// Release the Segment

if (NULL != g_pSegment)

{

g_pSegment-Release();

g_pSegment = NULL;

}

// If there is any music playing, stop it.

if (NULL != g_pPerformance)

{

g_pPerformance-Stop(NULL, NULL, 0, 0);

// CloseDown and Release the performance object

g_pPerformance-CloseDown();

g_pPerformance-Release();

g_pPerformance = NULL;

}

// Release the loader object

if (NULL != g_pLoader)

{

g_pLoader-Release();

g_pLoader = NULL;

}

// Release COM

CoUninitialize();

return S_OK;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航