⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 musictool.cpp

📁 视频采集,一些源码,供大家参考一下.供大家参考一下
💻 CPP
📖 第 1 页 / 共 2 页
字号:
        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:\MSSDK\SAMPLES\DMUSIC\MEDIA)
    if( '\0' == strPath[0] )
    {
        DXUtil_GetDXSDKMediaPathCb( strPath, sizeof(strPath) );
    }

    // 建立OPENFILENAME结构
    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 };

    // 播放片段时,简单调用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] = '\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;
}




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -