musictool.cpp
上传用户:tuheem
上传日期:2007-05-01
资源大小:21889k
文件大小:19k
- //-----------------------------------------------------------------------------
- // 文件名: MusicTool.cpp
- //
- // 描 述: 使用DirectMusic,播放主片段
- //-----------------------------------------------------------------------------
- #define STRICT
- #include <windows.h>
- #include <basetsd.h>
- #include <commdlg.h>
- #include <commctrl.h>
- #include <dmusicc.h>
- #include <dmusici.h>
- #include <dmusicf.h>
- #include <dxerr9.h>
- #include <cguid.h>
- #include <tchar.h>
- #include <stdio.h>
- #include "resource.h"
- #include "DMUtil.h"
- #include "DXUtil.h"
- #include "EchoTool.h"
- #include "MeasTool.h"
- INT_PTR CALLBACK MainDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam );
- HRESULT OnInitDialog( HWND hDlg );
- HRESULT ProcessDirectMusicMessages( HWND hDlg );
- VOID OnOpenSoundFile( HWND hDlg );
- HRESULT LoadSegmentFile( HWND hDlg, TCHAR* strFileName );
- HRESULT OnPlaySegment( HWND hDlg );
- VOID EnablePlayUI( HWND hDlg, BOOL bEnable );
- HRESULT OnChangeTool( HWND hDlg );
- HINSTANCE g_hInst = NULL;
- CMusicManager* g_pMusicManager = NULL;
- CMusicSegment* g_pMusicSegment = NULL;
- CEchoTool* g_pEchoTool = NULL;
- CMeasureTool* g_pMeasureTool1 = NULL;
- CMeasureTool* g_pMeasureTool2 = NULL;
- IDirectMusicTool* g_pCurrentTool = NULL;
- IDirectMusicGraph* g_pGraph = NULL;
- HANDLE g_hDMusicMessageEvent = NULL;
- //-----------------------------------------------------------------------------
- // 函数名: WinMain()
- // 描 述: 应用程序的入口。使用简单的对话框与用户交互
- //-----------------------------------------------------------------------------
- INT APIENTRY WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR pCmdLine,
- INT nCmdShow )
- {
- HWND hDlg = NULL;
- BOOL bDone = FALSE;
- int nExitCode = 0;
- HRESULT hr;
- DWORD dwResult;
- MSG msg;
- g_hInst = hInst;
- // 显示主对话框。
- hDlg = CreateDialog( hInst, MAKEINTRESOURCE(IDD_MAIN),
- NULL, MainDlgProc );
- while( !bDone )
- {
- dwResult = MsgWaitForMultipleObjects( 1, &g_hDMusicMessageEvent,
- FALSE, INFINITE, QS_ALLEVENTS );
- switch( dwResult )
- {
- case WAIT_OBJECT_0 + 0:
- // 发信号给g_hDPMessageEvent,因此有可用的messages消息。
- if( FAILED( hr = ProcessDirectMusicMessages( hDlg ) ) )
- {
- DXTRACE_ERR_MSGBOX( TEXT("ProcessDirectMusicMessages"), hr );
- return FALSE;
- }
- break;
- case WAIT_OBJECT_0 + 1:
- // Windows消息可用。
- while( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
- {
- if( !IsDialogMessage( hDlg, &msg ) )
- {
- TranslateMessage( &msg );
- DispatchMessage( &msg );
- }
- if( msg.message == WM_QUIT )
- {
- nExitCode = (int)msg.wParam;
- bDone = TRUE;
- DestroyWindow( hDlg );
- }
- }
- break;
- }
- }
- return nExitCode;
- }
- //-----------------------------------------------------------------------------
- // 函数名: MainDlgProc()
- // 描 述: 处理对话框消息
- //-----------------------------------------------------------------------------
- INT_PTR CALLBACK MainDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam )
- {
- HRESULT hr;
- switch( msg )
- {
- case WM_INITDIALOG:
- if( FAILED( hr = OnInitDialog( hDlg ) ) )
- {
- DXTRACE_ERR_MSGBOX( TEXT("OnInitDialog"), hr );
- MessageBox( hDlg, "Error initializing DirectMusic. Sample will now exit.",
- "DirectMusic Sample", MB_OK | MB_ICONERROR );
- PostQuitMessage( IDABORT );
- return FALSE;
- }
- break;
- case WM_COMMAND:
- switch( LOWORD(wParam) )
- {
- case IDC_SOUNDFILE:
- OnOpenSoundFile( hDlg );
- break;
- case IDCANCEL:
- PostQuitMessage( IDCANCEL );
- break;
- case IDC_PLAY:
- if( FAILED( hr = OnPlaySegment( hDlg ) ) )
- {
- DXTRACE_ERR_MSGBOX( TEXT("OnPlaySegment"), hr );
- MessageBox( hDlg, "Error playing DirectMusic segment. "
- "Sample will now exit.", "DirectMusic Sample",
- MB_OK | MB_ICONERROR );
- PostQuitMessage( IDABORT );
- }
- break;
- case IDC_STOP:
- // 播放片段时,简单调用Stop()不会停止任何MIDI sustain pedals,
- // 但调用StopAll()可以。
- if( g_pMusicManager )
- g_pMusicManager->StopAll();
- EnablePlayUI( hDlg, TRUE );
- break;
- case IDC_TOOL_COMBO:
- OnChangeTool( hDlg );
- break;
- case IDHELP:
- DXUtil_LaunchReadme( hDlg );
- break;
- default:
- // 不处理消息
- return FALSE;
- }
- break;
- case WM_DESTROY:
- // 清除一切
- SAFE_RELEASE( g_pGraph );
- SAFE_DELETE( g_pMusicSegment );
- SAFE_DELETE( g_pMusicManager );
- SAFE_DELETE( g_pEchoTool );
- SAFE_DELETE( g_pMeasureTool1 );
- SAFE_DELETE( g_pMeasureTool2 );
- CloseHandle( g_hDMusicMessageEvent );
- break;
- default:
- // 不处理消息
- return FALSE;
- }
- return TRUE;
- }
- //-----------------------------------------------------------------------------
- // 函数名: OnInitDialog()
- // 描 述: 初始化对话框(建立UI控制等)
- //-----------------------------------------------------------------------------
- HRESULT OnInitDialog( HWND hDlg )
- {
- HRESULT hr;
- LONG lIndex;
- // 为对话框设置icon
- HICON hIcon = LoadIcon( g_hInst, MAKEINTRESOURCE( IDR_MAINFRAME ) );
- SendMessage( hDlg, WM_SETICON, ICON_BIG, (LPARAM) hIcon ); // 设置大icon
- SendMessage( hDlg, WM_SETICON, ICON_SMALL, (LPARAM) hIcon ); // 设置小icon
- g_hDMusicMessageEvent = CreateEvent( NULL, FALSE, FALSE, NULL );
- g_pMusicManager = new CMusicManager();
- if( NULL == g_pMusicManager )
- return E_OUTOFMEMORY;
- // 使用缺省音轨初始化DirectMusic
- hr = g_pMusicManager->Initialize( hDlg );
- if( FAILED(hr) )
- return DXTRACE_ERR_MSGBOX( TEXT("Initialize"), hr );
-
- IDirectMusicPerformance8* pPerformance = g_pMusicManager->GetPerformance();
- IDirectMusicAudioPath8* pDefaultAudioPath = g_pMusicManager->GetDefaultAudioPath();
- // 产生一个DirectMusicGraph,并告知相关的参数
- hr = pDefaultAudioPath->GetObjectInPath( 0, DMUS_PATH_PERFORMANCE_GRAPH, 0,
- GUID_NULL, 0, IID_IDirectMusicGraph,
- (LPVOID*) &g_pGraph );
- SAFE_RELEASE( pDefaultAudioPath );
- if( FAILED( hr ) )
- return DXTRACE_ERR_MSGBOX( TEXT("GetObjectInPath"), hr );
- // 注册片段通知
- if( FAILED( hr = pPerformance->AddNotificationType( GUID_NOTIFICATION_SEGMENT ) ) )
- return DXTRACE_ERR_MSGBOX( TEXT("AddNotificationType"), hr );
- if( FAILED( hr = pPerformance->AddNotificationType( GUID_NOTIFICATION_MEASUREANDBEAT) ) )
- return DXTRACE_ERR_MSGBOX( TEXT("AddNotificationType"), hr );
- if( FAILED( hr = pPerformance->SetNotificationHandle( g_hDMusicMessageEvent, 0 ) ) )
- return DXTRACE_ERR_MSGBOX( TEXT("SetNotificationHandle"), hr );
- g_pEchoTool = new CEchoTool();
- if( NULL == g_pEchoTool )
- return E_OUTOFMEMORY;
- g_pMeasureTool1 = new CMeasureTool(hDlg);
- if( NULL == g_pMeasureTool1 )
- return E_OUTOFMEMORY;
- g_pMeasureTool2 = new CMeasureTool(hDlg);
- if( NULL == g_pMeasureTool2 )
- return E_OUTOFMEMORY;
- // 初始化UI
- HWND hToolCombo = GetDlgItem( hDlg, IDC_TOOL_COMBO );
- lIndex = (LONG)SendMessage( hToolCombo, CB_ADDSTRING, 0, (LPARAM) TEXT("无") );
- SendMessage( hToolCombo, CB_SETITEMDATA, lIndex, (LPARAM) NULL );
- lIndex = (LONG)SendMessage( hToolCombo, CB_ADDSTRING, 0, (LPARAM) TEXT("Echo工具") );
- SendMessage( hToolCombo, CB_SETITEMDATA, lIndex, (LPARAM) (IDirectMusicTool*) g_pEchoTool );
- lIndex = (LONG)SendMessage( hToolCombo, CB_ADDSTRING, 0, (LPARAM) TEXT("Measure工具") );
- SendMessage( hToolCombo, CB_SETITEMDATA, lIndex, (LPARAM) (IDirectMusicTool*) g_pMeasureTool1 );
- SendMessage( hToolCombo, CB_SETCURSEL, 0, 0 );
- // 装载缺省的音频片段
- TCHAR strFileName[MAX_PATH+1];
- DXUtil_GetDXSDKMediaPathCb( strFileName, sizeof(strFileName) );
- strcat( strFileName, "sample.sgt" );
- if( S_FALSE == LoadSegmentFile( hDlg, strFileName ) )
- {
- // 设置UI控制
- SetDlgItemText( hDlg, IDC_FILENAME, TEXT("No file loaded.") );
- }
- else
- {
- IDirectMusicSegment8* pPrimarySegment8;
- DMUS_TEMPO_PARAM tempo;
- MUSIC_TIME mtTime = 0;
- pPrimarySegment8 = g_pMusicSegment->GetSegment();
- tempo.mtTime = 0;
- tempo.dblTempo = 70.;
- pPrimarySegment8->SetParam(GUID_TempoParam, 0xffffffff, DMUS_SEG_ALLTRACKS, mtTime, &tempo);
- }
- return S_OK;
- }
- //-----------------------------------------------------------------------------
- // 函数名: OnOpenSoundFile()
- // 描 述: 当用户需要打开一个声音文件时,调用
- //-----------------------------------------------------------------------------
- VOID OnOpenSoundFile( HWND hDlg )
- {
- static TCHAR strFileName[MAX_PATH+1] = TEXT("");
- static TCHAR strPath[MAX_PATH+1] = TEXT("");
- // 得到缺省的多媒体路径(如C:MSSDKSAMPLESDMUSICMEDIA)
- if( ' ' == strPath[0] )
- {
- DXUtil_GetDXSDKMediaPathCb( strPath, sizeof(strPath) );
- }
- // 建立OPENFILENAME结构
- OPENFILENAME ofn = { sizeof(OPENFILENAME), hDlg, NULL,
- TEXT("DirectMusic Content Files *.sgt;*.mid;*.rmi All Files *.* "), NULL,
- 0, 1, strFileName, MAX_PATH, NULL, 0, strPath,
- TEXT("Open Content File"),
- OFN_FILEMUSTEXIST|OFN_HIDEREADONLY, 0, 0,
- TEXT(".sgt"), 0, NULL, NULL };
- // 播放片段时,简单调用Stop()不会停止任何MIDI sustain pedals,
- // 但调用StopAll()可以。
- if( g_pMusicManager )
- g_pMusicManager->StopAll();
- // 为播放装载的文件更新UI控制
- EnableWindow( GetDlgItem( hDlg, IDC_PLAY ), FALSE);
- EnableWindow( GetDlgItem( hDlg, IDC_STOP ), FALSE);
- SetDlgItemText( hDlg, IDC_FILENAME, TEXT("Loading file...") );
- // 显示OpenFileName对话框。然后尝试装载指定的文件
- if( TRUE != GetOpenFileName( &ofn ) )
- {
- SetDlgItemText( hDlg, IDC_FILENAME, TEXT("Load aborted.") );
- return;
- }
- if( S_FALSE == LoadSegmentFile( hDlg, strFileName ) )
- {
- // 不是一个关键的错误,因此仅仅更新状态
- SetDlgItemText( hDlg, IDC_FILENAME, TEXT("Could not create segment from file.") );
- }
- // 记忆路径
- strcpy( strPath, strFileName );
- char* strLastSlash = strrchr( strPath, '\' );
- if( strLastSlash )
- strLastSlash[0] = ' ';
- }
- //-----------------------------------------------------------------------------
- // 函数名: LoadSegmentFile()
- // 描 述: 装载片段文件
- //-----------------------------------------------------------------------------
- HRESULT LoadSegmentFile( HWND hDlg, TCHAR* strFileName )
- {
- HRESULT hr;
- SetDlgItemText( hDlg, IDC_FILENAME, TEXT("") );
- // 释放先前的片段,建立一个新的
- SAFE_DELETE( g_pMusicSegment );
- // 因为老的剧本已释放,有装载器收集垃圾
- g_pMusicManager->CollectGarbage();
- // 为搜索与该文件相关的DirectMusic内容而设置的基于文件名的多媒体路径(如C:MEDIA)
- TCHAR strMediaPath[MAX_PATH+1];
- _tcsncpy( strMediaPath, strFileName, MAX_PATH );
- strMediaPath[MAX_PATH-1] = 0;
- TCHAR* strLastSlash = _tcsrchr(strMediaPath, TEXT('\'));
- if( strLastSlash )
- {
- *strLastSlash = 0;
- if( FAILED( hr = g_pMusicManager->SetSearchDirectory( strMediaPath ) ) )
- return DXTRACE_ERR_MSGBOX( TEXT("SetSearchDirectory"), hr );
- }
-
- // 为装载正确的乐器,DirectMusic必须知道该文件是否是一个标准的MIDI文件
- BOOL bMidiFile = FALSE;
- if( strstr( strFileName, ".mid" ) != NULL ||
- strstr( strFileName, ".rmi" ) != NULL )
- {
- bMidiFile = TRUE;
- }
- // 将文件载入一个DirectMusic片段中
- if( FAILED( g_pMusicManager->CreateSegmentFromFile( &g_pMusicSegment, strFileName,
- TRUE, bMidiFile ) ) )
- {
- // 不是一个关键的错误,因此仅仅更新状态
- return S_FALSE;
- }
- // 为显示片段被装载,更新UI控制
- SetDlgItemText( hDlg, IDC_FILENAME, strFileName );
- EnablePlayUI( hDlg, TRUE );
- return S_OK;
- }
- //-----------------------------------------------------------------------------
- // 函数名: ProcessDirectMusicMessages()
- // 描 述: 处理DirectMusic音符消息
- //-----------------------------------------------------------------------------
- HRESULT ProcessDirectMusicMessages( HWND hDlg )
- {
- HRESULT hr;
- IDirectMusicPerformance8* pPerf = NULL;
- DMUS_NOTIFICATION_PMSG* pPMsg;
-
- if( NULL == g_pMusicManager )
- return S_OK;
- pPerf = g_pMusicManager->GetPerformance();
- // Get waiting notification message from the performance
- while( S_OK == pPerf->GetNotificationPMsg( &pPMsg ) )
- {
- switch( pPMsg->dwNotificationOption )
- {
- case DMUS_NOTIFICATION_SEGEND:
- if( pPMsg->punkUser )
- {
- IDirectMusicSegmentState8* pSegmentState = NULL;
- IDirectMusicSegment* pNotifySegment = NULL;
- IDirectMusicSegment8* pNotifySegment8 = NULL;
- IDirectMusicSegment8* pPrimarySegment8 = NULL;
- // pPMsg->punkUser包括一个IDirectMusicSegmentState8,
- // 从而我们能够查询SegmentState引用的片段
- if( FAILED( hr = pPMsg->punkUser->QueryInterface( IID_IDirectMusicSegmentState8,
- (VOID**) &pSegmentState ) ) )
- return DXTRACE_ERR_MSGBOX( TEXT("QueryInterface"), hr );
- if( SUCCEEDED( hr = pSegmentState->GetSegment( &pNotifySegment ) ) )
- {
- if( FAILED( hr = pNotifySegment->QueryInterface( IID_IDirectMusicSegment8,
- (VOID**) &pNotifySegment8 ) ) )
- return DXTRACE_ERR_MSGBOX( TEXT("QueryInterface"), hr );
- // 为主要的片段得到IDirectMusicSegment
- pPrimarySegment8 = g_pMusicSegment->GetSegment();
- // 推断这是什么片段
- if( pNotifySegment8 == pPrimarySegment8 )
- {
- // 为显示停止音频,更新UI控制
- EnablePlayUI( hDlg, TRUE );
- }
- }
- // 清除
- SAFE_RELEASE( pSegmentState );
- SAFE_RELEASE( pNotifySegment );
- SAFE_RELEASE( pNotifySegment8 );
- }
- break;
- }
- pPerf->FreePMsg( (DMUS_PMSG*)pPMsg );
- }
- return S_OK;
- }
- //-----------------------------------------------------------------------------
- // 函数名: OnPlaySegment()
- // 描 述: 播放片段
- //-----------------------------------------------------------------------------
- HRESULT OnPlaySegment( HWND hDlg )
- {
- HRESULT hr;
- HWND hLoopButton = GetDlgItem( hDlg, IDC_LOOP_CHECK );
- BOOL bLooped = ( SendMessage( hLoopButton, BM_GETSTATE, 0, 0 ) == BST_CHECKED );
- if( bLooped )
- {
- // 设置片段的重复次数
- if( FAILED( hr = g_pMusicSegment->SetRepeats( DMUS_SEG_REPEAT_INFINITE ) ) )
- return DXTRACE_ERR_MSGBOX( TEXT("SetRepeats"), hr );
- }
- else
- {
- // 设置片段不重复
- if( FAILED( hr = g_pMusicSegment->SetRepeats( 0 ) ) )
- return DXTRACE_ERR_MSGBOX( TEXT("SetRepeats"), hr );
- }
- // 播放片段和等待. 当当前有一个片段在播放时,DMUS_SEGF_BEAT表示在下一次敲打时播放
- if( FAILED( hr = g_pMusicSegment->Play( DMUS_SEGF_BEAT ) ) )
- return DXTRACE_ERR_MSGBOX( TEXT("Play"), hr );
- EnablePlayUI( hDlg, FALSE );
- return S_OK;
- }
- //-----------------------------------------------------------------------------
- // 函数名: EnablePlayUI( )
- // 描 述: 是否允许UI控制的表演
- //-----------------------------------------------------------------------------
- VOID EnablePlayUI( HWND hDlg, BOOL bEnable )
- {
- if( bEnable )
- {
- EnableWindow( GetDlgItem( hDlg, IDC_LOOP_CHECK ), TRUE );
- EnableWindow( GetDlgItem( hDlg, IDC_STOP ), FALSE );
- EnableWindow( GetDlgItem( hDlg, IDC_PLAY ), TRUE );
- SetFocus( GetDlgItem( hDlg, IDC_PLAY ) );
- }
- else
- {
- EnableWindow( GetDlgItem( hDlg, IDC_LOOP_CHECK ), FALSE );
- EnableWindow( GetDlgItem( hDlg, IDC_STOP ), TRUE );
- SetFocus( GetDlgItem( hDlg, IDC_STOP ) );
- EnableWindow( GetDlgItem( hDlg, IDC_PLAY ), FALSE );
- }
- }
- //-----------------------------------------------------------------------------
- // 函数名: OnChangeTool()
- // 描 述: 改变工具
- //-----------------------------------------------------------------------------
- HRESULT OnChangeTool( HWND hDlg )
- {
- HRESULT hr;
- IDirectMusicTool* pSelectedTool;
- LONG lCurSelection;
- lCurSelection = (LONG)SendDlgItemMessage( hDlg, IDC_TOOL_COMBO, CB_GETCURSEL, 0, 0 );
- pSelectedTool = (IDirectMusicTool*) SendDlgItemMessage( hDlg, IDC_TOOL_COMBO,
- CB_GETITEMDATA, lCurSelection, 0 );
- if( pSelectedTool != g_pCurrentTool )
- {
- // 从当前的表中移除当前的工具
- if( g_pCurrentTool != NULL )
- {
- if( FAILED( hr = g_pGraph->RemoveTool( g_pCurrentTool ) ) )
- return DXTRACE_ERR_MSGBOX( TEXT("RemoveTool"), hr );
- }
- // 对所有的通道或初始化表时,增加工具到表中
- if( pSelectedTool != NULL )
- {
- if( FAILED( hr = g_pGraph->InsertTool( pSelectedTool, NULL, 0, 0 ) ) )
- return DXTRACE_ERR_MSGBOX( TEXT("InsertTool"), hr );
- }
- g_pCurrentTool = pSelectedTool;
- }
- return S_OK;
- }