?? musictool.cpp
字號:
pPrimarySegment8->SetParam(GUID_TempoParam, 0xffffffff, DMUS_SEG_ALLTRACKS, mtTime, &tempo);
}
return S_OK;
}
//-----------------------------------------------------------------------------
// 函數(shù)名: OnOpenSoundFile()
// 描 述: 當(dāng)用戶需要打開一個聲音文件時,調(diào)用
//-----------------------------------------------------------------------------
VOID OnOpenSoundFile( HWND hDlg )
{
static TCHAR strFileName[MAX_PATH+1] = TEXT("");
static TCHAR strPath[MAX_PATH+1] = TEXT("");
// 得到缺省的多媒體路徑(如C:\MSSDK\SAMPLES\DMUSIC\MEDIA)
if( '\0' == strPath[0] )
{
DXUtil_GetDXSDKMediaPathCb( strPath, sizeof(strPath) );
}
// 建立OPENFILENAME結(jié)構(gòu)
OPENFILENAME ofn = { sizeof(OPENFILENAME), hDlg, NULL,
TEXT("DirectMusic Content Files\0*.sgt;*.mid;*.rmi\0All Files\0*.*\0\0"), NULL,
0, 1, strFileName, MAX_PATH, NULL, 0, strPath,
TEXT("Open Content File"),
OFN_FILEMUSTEXIST|OFN_HIDEREADONLY, 0, 0,
TEXT(".sgt"), 0, NULL, NULL };
// 播放片段時,簡單調(diào)用Stop()不會停止任何MIDI sustain pedals,
// 但調(diào)用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 ) )
{
// 不是一個關(guān)鍵的錯誤,因此僅僅更新狀態(tài)
SetDlgItemText( hDlg, IDC_FILENAME, TEXT("Could not create segment from file.") );
}
// 記憶路徑
strcpy( strPath, strFileName );
char* strLastSlash = strrchr( strPath, '\\' );
if( strLastSlash )
strLastSlash[0] = '\0';
}
//-----------------------------------------------------------------------------
// 函數(shù)名: LoadSegmentFile()
// 描 述: 裝載片段文件
//-----------------------------------------------------------------------------
HRESULT LoadSegmentFile( HWND hDlg, TCHAR* strFileName )
{
HRESULT hr;
SetDlgItemText( hDlg, IDC_FILENAME, TEXT("") );
// 釋放先前的片段,建立一個新的
SAFE_DELETE( g_pMusicSegment );
// 因為老的劇本已釋放,有裝載器收集垃圾
g_pMusicManager->CollectGarbage();
// 為搜索與該文件相關(guān)的DirectMusic內(nèi)容而設(shè)置的基于文件名的多媒體路徑(如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必須知道該文件是否是一個標(biāo)準(zhǔn)的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 ) ) )
{
// 不是一個關(guān)鍵的錯誤,因此僅僅更新狀態(tài)
return S_FALSE;
}
// 為顯示片段被裝載,更新UI控制
SetDlgItemText( hDlg, IDC_FILENAME, strFileName );
EnablePlayUI( hDlg, TRUE );
return S_OK;
}
//-----------------------------------------------------------------------------
// 函數(shù)名: 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;
}
//-----------------------------------------------------------------------------
// 函數(shù)名: 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 )
{
// 設(shè)置片段的重復(fù)次數(shù)
if( FAILED( hr = g_pMusicSegment->SetRepeats( DMUS_SEG_REPEAT_INFINITE ) ) )
return DXTRACE_ERR_MSGBOX( TEXT("SetRepeats"), hr );
}
else
{
// 設(shè)置片段不重復(fù)
if( FAILED( hr = g_pMusicSegment->SetRepeats( 0 ) ) )
return DXTRACE_ERR_MSGBOX( TEXT("SetRepeats"), hr );
}
// 播放片段和等待. 當(dāng)當(dāng)前有一個片段在播放時,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;
}
//-----------------------------------------------------------------------------
// 函數(shù)名: 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 );
}
}
//-----------------------------------------------------------------------------
// 函數(shù)名: 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 )
{
// 從當(dāng)前的表中移除當(dāng)前的工具
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;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -