home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / DirectMusic / PlayMotif / PlayMotif.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-31  |  28.6 KB  |  759 lines

  1. //-----------------------------------------------------------------------------
  2. // File: PlayMotif.cpp
  3. //
  4. // Desc: Lets the user play a primary segment using DirectMusic as well as 
  5. //       any of motifs contained inside the segment.  
  6. //
  7. // Copyright (c) 2000-2001 Microsoft Corporation. All rights reserved.
  8. //-----------------------------------------------------------------------------
  9. #define STRICT
  10. #include <windows.h>
  11. #include <basetsd.h>
  12. #include <commdlg.h>
  13. #include <commctrl.h>
  14. #include <objbase.h>
  15. #include <conio.h>
  16. #include <direct.h>
  17. #include <dmusicc.h>
  18. #include <dmusici.h>
  19. #include <dxerr8.h>
  20. #include <tchar.h>
  21. #include <commctrl.h>
  22. #include "resource.h"
  23. #include "DMUtil.h"
  24. #include "DXUtil.h"
  25.  
  26.  
  27.  
  28.  
  29. //-----------------------------------------------------------------------------
  30. // Function-prototypes
  31. //-----------------------------------------------------------------------------
  32. INT_PTR CALLBACK MainDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam );
  33. HRESULT OnInitDialog( HWND hDlg );
  34. HRESULT ProcessDirectMusicMessages( HWND hDlg );
  35. VOID    OnOpenSegmentFile( HWND hDlg );
  36. HRESULT LoadSegmentFile( HWND hDlg, TCHAR* strFileName );
  37. HRESULT OnPlaySegment( HWND hDlg );
  38. HRESULT OnPlayMotif( HWND hDlg );
  39. VOID    EnablePlayUI( HWND hDlg, BOOL bEnable );
  40.  
  41. struct MOTIF_NODE
  42. {
  43.     IDirectMusicSegment* pMotif;
  44.     DWORD dwPlayCount;
  45. };
  46.  
  47.  
  48.  
  49.  
  50. //-----------------------------------------------------------------------------
  51. // Defines, constants, and global variables
  52. //-----------------------------------------------------------------------------
  53. CMusicManager*     g_pMusicManager          = NULL;
  54. CMusicSegment*     g_pMusicSegment          = NULL;
  55. HINSTANCE          g_hInst                  = NULL;
  56. HANDLE             g_hDMusicMessageEvent    = NULL;
  57.  
  58.  
  59.  
  60.  
  61. //-----------------------------------------------------------------------------
  62. // Name: WinMain()
  63. // Desc: Entry point for the application.  Since we use a simple dialog for 
  64. //       user interaction we don't need to pump messages.
  65. //-----------------------------------------------------------------------------
  66. INT APIENTRY WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR pCmdLine, 
  67.                       INT nCmdShow )
  68. {
  69.     HWND    hDlg = NULL;
  70.     BOOL    bDone = FALSE;
  71.     int     nExitCode;
  72.     HRESULT hr; 
  73.     DWORD   dwResult;
  74.     MSG     msg;
  75.  
  76.     g_hInst = hInst;
  77.  
  78.     // Display the main dialog box.
  79.     hDlg = CreateDialog( hInst, MAKEINTRESOURCE(IDD_MAIN), 
  80.                          NULL, MainDlgProc );
  81.  
  82.     while( !bDone ) 
  83.     { 
  84.         dwResult = MsgWaitForMultipleObjects( 1, &g_hDMusicMessageEvent, 
  85.                                               FALSE, INFINITE, QS_ALLEVENTS );
  86.         switch( dwResult )
  87.         {
  88.             case WAIT_OBJECT_0 + 0:
  89.                 // g_hDPMessageEvent is signaled, so there are
  90.                 // DirectPlay messages available
  91.                 if( FAILED( hr = ProcessDirectMusicMessages( hDlg ) ) ) 
  92.                 {
  93.                     DXTRACE_ERR( TEXT("ProcessDirectMusicMessages"), hr );
  94.                     return FALSE;
  95.                 }
  96.                 break;
  97.  
  98.             case WAIT_OBJECT_0 + 1:
  99.                 // Windows messages are available
  100.                 while( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ) 
  101.                 { 
  102.                     if( !IsDialogMessage( hDlg, &msg ) )  
  103.                     {
  104.                         TranslateMessage( &msg ); 
  105.                         DispatchMessage( &msg ); 
  106.                     }
  107.  
  108.                     if( msg.message == WM_QUIT )
  109.                     {
  110.                         nExitCode = (int)msg.wParam;
  111.                         bDone     = TRUE;
  112.                         DestroyWindow( hDlg );
  113.                     }
  114.                 }
  115.                 break;
  116.         }
  117.     }
  118.  
  119.     return nExitCode;
  120. }
  121.  
  122.  
  123.  
  124.  
  125. //-----------------------------------------------------------------------------
  126. // Name: MainDlgProc()
  127. // Desc: Handles dialog messages
  128. //-----------------------------------------------------------------------------
  129. INT_PTR CALLBACK MainDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam )
  130. {
  131.     HRESULT hr;
  132.  
  133.     switch( msg ) 
  134.     {
  135.         case WM_INITDIALOG:
  136.             if( FAILED( hr = OnInitDialog( hDlg ) ) )
  137.             {
  138.                 DXTRACE_ERR( TEXT("OnInitDialog"), hr );
  139.                 MessageBox( hDlg, "Error initializing DirectMusic.  Sample will now exit.", 
  140.                                   "DirectMusic Sample", MB_OK | MB_ICONERROR );
  141.                 EndDialog( hDlg, 0 );
  142.                 return TRUE;
  143.             }
  144.             break;
  145.  
  146.         case WM_COMMAND:
  147.             switch( LOWORD(wParam) )
  148.             {
  149.                 case IDC_SOUNDFILE:
  150.                     OnOpenSegmentFile( hDlg );
  151.                     break;
  152.  
  153.                 case IDCANCEL:
  154.                     PostQuitMessage( IDCANCEL );
  155.                     break;
  156.  
  157.                 case IDC_MOTIF_LIST:
  158.                     if (HIWORD(wParam) == LBN_DBLCLK )
  159.                     {
  160.                     if( FAILED( hr = OnPlayMotif( hDlg ) ) )
  161.                     {
  162.                         DXTRACE_ERR( TEXT("OnPlayMotif"), hr );
  163.                         MessageBox( hDlg, "Error playing DirectMusic motif. "
  164.                                     "Sample will now exit.", "DirectMusic Sample", 
  165.                                     MB_OK | MB_ICONERROR );
  166.                         PostQuitMessage( IDABORT );
  167.                     }
  168.                     break;
  169.                     }
  170.  
  171.                     if( g_pMusicSegment )
  172.                     {
  173.                         if( g_pMusicSegment->IsPlaying() )
  174.                         {
  175.                             HWND hListBox = GetDlgItem( hDlg, IDC_MOTIF_LIST );
  176.                             int nIndex = (int)SendMessage( hListBox, LB_GETCURSEL, 0, 0 );
  177.  
  178.                             if( nIndex == LB_ERR )
  179.                                 EnableWindow( GetDlgItem( hDlg, IDC_PLAY_MOTIF ), FALSE );
  180.                             else
  181.                                 EnableWindow( GetDlgItem( hDlg, IDC_PLAY_MOTIF ), TRUE );
  182.                         }
  183.                     }
  184.                     break;
  185.  
  186.                 case IDC_PLAY:
  187.                     if( FAILED( hr = OnPlaySegment( hDlg ) ) )
  188.                     {
  189.                         DXTRACE_ERR( TEXT("OnPlaySegment"), hr );
  190.                         MessageBox( hDlg, "Error playing DirectMusic segment. "
  191.                                     "Sample will now exit.", "DirectMusic Sample", 
  192.                                     MB_OK | MB_ICONERROR );
  193.                         PostQuitMessage( IDABORT );
  194.                     }
  195.                     break;
  196.  
  197.                 case IDC_STOP:
  198.                     g_pMusicSegment->Stop( DMUS_SEGF_BEAT ); 
  199.                     SetDlgItemText( hDlg, IDC_STATUS, "Primary segment stopped." );
  200.                     EnablePlayUI( hDlg, TRUE );
  201.                     break;
  202.  
  203.                 case IDC_PLAY_MOTIF:
  204.                     if( FAILED( hr = OnPlayMotif( hDlg ) ) )
  205.                     {
  206.                         DXTRACE_ERR( TEXT("OnPlayMotif"), hr );
  207.                         MessageBox( hDlg, "Error playing DirectMusic motif. "
  208.                                     "Sample will now exit.", "DirectMusic Sample", 
  209.                                     MB_OK | MB_ICONERROR );
  210.                         PostQuitMessage( IDABORT );
  211.                     }
  212.                     break;
  213.  
  214.                 default:
  215.                     return FALSE; // Didn't handle message
  216.             }
  217.             break;
  218.  
  219.         case WM_DESTROY:
  220.         {            
  221.             // Cleanup everything
  222.             HWND hListBox = GetDlgItem( hDlg, IDC_MOTIF_LIST );
  223.             DWORD dwCount = (DWORD)SendMessage( hListBox, LB_GETCOUNT, 0, 0 );                 
  224.             for( DWORD i=0; i<dwCount; i++ )
  225.             {
  226.                 MOTIF_NODE* pMotifNode = (MOTIF_NODE*) SendMessage( hListBox, LB_GETITEMDATA, i, 0 );
  227.                 if( pMotifNode )
  228.                 {
  229.                     SAFE_RELEASE( pMotifNode->pMotif );
  230.                     SAFE_DELETE( pMotifNode );
  231.                 }
  232.             }
  233.  
  234.             CloseHandle( g_hDMusicMessageEvent );
  235.             SAFE_DELETE( g_pMusicSegment );
  236.             SAFE_DELETE( g_pMusicManager );
  237.             break; 
  238.         }
  239.  
  240.         default:
  241.             return FALSE; // Didn't handle message
  242.     }
  243.  
  244.     return TRUE; // Handled message
  245. }
  246.  
  247.  
  248.  
  249.  
  250. //-----------------------------------------------------------------------------
  251. // Name: OnInitDialog()
  252. // Desc: Initializes the dialogs (sets up UI controls, etc.)
  253. //-----------------------------------------------------------------------------
  254. HRESULT OnInitDialog( HWND hDlg )
  255. {
  256.     HRESULT hr;
  257.  
  258.     // Load the icon
  259.     HICON hIcon = LoadIcon( g_hInst, MAKEINTRESOURCE( IDR_MAINFRAME ) );
  260.  
  261.     // Set the icon for this dialog.
  262.     SendMessage( hDlg, WM_SETICON, ICON_BIG,   (LPARAM) hIcon );  // Set big icon
  263.     SendMessage( hDlg, WM_SETICON, ICON_SMALL, (LPARAM) hIcon );  // Set small icon
  264.  
  265.     g_hDMusicMessageEvent = CreateEvent( NULL, FALSE, FALSE, NULL );
  266.     g_pMusicManager = new CMusicManager();
  267.  
  268.     if( FAILED( hr = g_pMusicManager->Initialize( hDlg ) ) )
  269.         return DXTRACE_ERR( TEXT("Initialize"), hr );
  270.  
  271.     // Register segment notification
  272.     IDirectMusicPerformance* pPerf = g_pMusicManager->GetPerformance();
  273.     GUID guid = GUID_NOTIFICATION_SEGMENT;
  274.     if( FAILED( hr = pPerf->AddNotificationType( guid ) ) )
  275.         return DXTRACE_ERR( TEXT("AddNotificationType"), hr );
  276.  
  277.     if( FAILED( hr = pPerf->SetNotificationHandle( g_hDMusicMessageEvent, 0 ) ) )
  278.         return DXTRACE_ERR( TEXT("SetNotificationHandle"), hr );
  279.  
  280.     EnableWindow( GetDlgItem( hDlg, IDC_PLAY_MOTIF ), FALSE );
  281.     CheckRadioButton( hDlg, IDC_RADIO_DEFAULT, IDC_RADIO_MEASURE, IDC_RADIO_MEASURE );
  282.  
  283.     // Load a default music segment 
  284.     TCHAR strFileName[MAX_PATH];
  285.     strcpy( strFileName, DXUtil_GetDXSDKMediaPath() );
  286.     strcat( strFileName, "sample.sgt" );
  287.     if( S_FALSE == LoadSegmentFile( hDlg, strFileName ) )
  288.     {
  289.         // Set the UI controls
  290.         SetDlgItemText( hDlg, IDC_FILENAME, TEXT("") );
  291.         SetDlgItemText( hDlg, IDC_STATUS, TEXT("No file loaded.") );
  292.     }
  293.  
  294.     return S_OK;
  295. }
  296.  
  297.  
  298.  
  299.  
  300. //-----------------------------------------------------------------------------
  301. // Name: OnOpenSegmentFile()
  302. // Desc: Called when the user requests to open a sound file
  303. //-----------------------------------------------------------------------------
  304. VOID OnOpenSegmentFile( HWND hDlg ) 
  305. {
  306.     static TCHAR strFileName[MAX_PATH] = TEXT("");
  307.     static TCHAR strPath[MAX_PATH] = TEXT("");
  308.     HWND hListBox = GetDlgItem( hDlg, IDC_MOTIF_LIST );
  309.  
  310.     // Get the default media path (something like C:\MSSDK\SAMPLES\MULTIMEDIA\MEDIA)
  311.     if( '\0' == strPath[0] )
  312.     {
  313.         const TCHAR* szDir = DXUtil_GetDXSDKMediaPath();
  314.         strcpy( strPath, szDir );
  315.     }
  316.  
  317.     // Setup the OPENFILENAME structure
  318.     OPENFILENAME ofn = { sizeof(OPENFILENAME), hDlg, NULL,
  319.                          TEXT("DirectMusic Content Files\0*.sgt;*.mid;*.rmi\0All Files\0*.*\0\0"), NULL,
  320.                          0, 1, strFileName, MAX_PATH, NULL, 0, strPath,
  321.                          TEXT("Open Segment File"),
  322.                          OFN_FILEMUSTEXIST|OFN_HIDEREADONLY, 0, 0,
  323.                          TEXT(".sgt"), 0, NULL, NULL };
  324.  
  325.     if( g_pMusicSegment )
  326.         g_pMusicSegment->Stop( 0 );
  327.  
  328.     // Update the UI controls to show the sound as loading a file
  329.     EnableWindow( GetDlgItem( hDlg, IDC_PLAY ), FALSE);
  330.     EnableWindow( GetDlgItem( hDlg, IDC_STOP ), FALSE);
  331.     EnableWindow( GetDlgItem( hDlg, IDC_PLAY_MOTIF ), FALSE );
  332.     SetDlgItemText( hDlg, IDC_FILENAME, TEXT("") );
  333.     SetDlgItemText( hDlg, IDC_STATUS, TEXT("Loading file...") );
  334.  
  335.     // Cleanup motif listbox
  336.     DWORD dwCount = (DWORD)SendMessage( hListBox, LB_GETCOUNT, 0, 0 );                 
  337.     for( DWORD i=0; i<dwCount; i++ )
  338.     {
  339.         MOTIF_NODE* pMotifNode = (MOTIF_NODE*) SendMessage( hListBox, LB_GETITEMDATA, i, 0 );
  340.         if( pMotifNode )
  341.         {
  342.             SAFE_RELEASE( pMotifNode->pMotif );
  343.             SAFE_DELETE( pMotifNode );
  344.         }
  345.     }
  346.     SendMessage( hListBox, LB_RESETCONTENT, 0, 0 );
  347.  
  348.     // Display the OpenFileName dialog. Then, try to load the specified file
  349.     if( TRUE != GetOpenFileName( &ofn ) )
  350.     {
  351.         SetDlgItemText( hDlg, IDC_STATUS, TEXT("Load aborted.") );
  352.         return;
  353.     }
  354.  
  355.     if( S_FALSE == LoadSegmentFile( hDlg, strFileName ) )
  356.     {
  357.         SetDlgItemText( hDlg, IDC_FILENAME, TEXT("") );
  358.         SetDlgItemText( hDlg, IDC_STATUS, TEXT("Could not create segment from file.") );
  359.     }
  360.  
  361.     // Remember the path for next time
  362.     strcpy( strPath, strFileName );
  363.     char* strLastSlash = strrchr( strPath, '\\' );
  364.     strLastSlash[0] = '\0';
  365. }
  366.  
  367.  
  368.  
  369.  
  370. //-----------------------------------------------------------------------------
  371. // Name: LoadSegmentFile()
  372. // Desc: 
  373. //-----------------------------------------------------------------------------
  374. HRESULT LoadSegmentFile( HWND hDlg, TCHAR* strFileName )
  375. {
  376.     HRESULT hr;
  377.  
  378.     HWND hListBox = GetDlgItem( hDlg, IDC_MOTIF_LIST );
  379.     SetDlgItemText( hDlg, IDC_FILENAME, TEXT("") );
  380.  
  381.     SAFE_DELETE( g_pMusicSegment );
  382.  
  383.     // Have the loader collect any garbage now that the old 
  384.     // script has been released
  385.     g_pMusicManager->CollectGarbage();
  386.  
  387.     // Set the media path based on the file name (something like C:\MEDIA)
  388.     // to be used as the search directory for finding DirectMusic content
  389.     // related to this file.
  390.     TCHAR strMediaPath[MAX_PATH];
  391.     _tcscpy( strMediaPath, strFileName );
  392.     TCHAR* strLastSlash = _tcsrchr(strMediaPath, TEXT('\\'));
  393.     *strLastSlash = 0;
  394.     if( FAILED( hr = g_pMusicManager->SetSearchDirectory( strMediaPath ) ) )
  395.         return DXTRACE_ERR( TEXT("SetSearchDirectory"), hr );
  396.  
  397.     // For DirectMusic must know if the file is a standard MIDI file or not
  398.     // in order to load the correct instruments.
  399.     BOOL bMidiFile = FALSE;
  400.     if( strstr( strFileName, ".mid" ) != NULL ||
  401.         strstr( strFileName, ".rmi" ) != NULL ) 
  402.     {
  403.         bMidiFile = TRUE;
  404.     }
  405.  
  406.     // Load the file into a DirectMusic segment 
  407.     if( FAILED( g_pMusicManager->CreateSegmentFromFile( &g_pMusicSegment, strFileName, 
  408.                                                         TRUE, bMidiFile ) ) )
  409.     {
  410.         // Not a critical failure, so just update the status
  411.         return S_FALSE;
  412.     }
  413.  
  414.     IDirectMusicStyle8* pStyle = NULL;
  415.     DWORD dwStyleIndex = 0;
  416.  
  417.     while( TRUE )
  418.     {
  419.         // Get the style from the segment
  420.         // Segments may have any number of styles.
  421.         hr = g_pMusicSegment->GetStyle( &pStyle, dwStyleIndex );
  422.         if( FAILED(hr) )
  423.             break;
  424.  
  425.         // Get the names of the motifs from the style. 
  426.         // Styles may have any number of motifs.
  427.         DWORD dwIndex = 0;
  428.         while( TRUE )
  429.         {
  430.             WCHAR wstrMotifName[MAX_PATH];
  431.             CHAR  strMotifName[MAX_PATH];
  432.             if( S_FALSE == pStyle->EnumMotif( dwIndex, wstrMotifName ) )
  433.                 break;
  434.  
  435.             wcstombs( strMotifName, wstrMotifName, wcslen( wstrMotifName ) + 1 );
  436.             int nIndex = (int)SendMessage( hListBox, LB_ADDSTRING, 0, (LPARAM) strMotifName );
  437.  
  438.             MOTIF_NODE* pMotifNode = new MOTIF_NODE;
  439.             if( FAILED( hr = pStyle->GetMotif( wstrMotifName, &pMotifNode->pMotif ) ) )
  440.                 return DXTRACE_ERR( TEXT("GetMotif"), hr );
  441.             pMotifNode->dwPlayCount = 0;
  442.             SendMessage( hListBox, LB_SETITEMDATA, nIndex, (LPARAM) pMotifNode );
  443.  
  444.             dwIndex++;
  445.         }
  446.  
  447.         SAFE_RELEASE( pStyle );
  448.         dwStyleIndex++;
  449.     }
  450.  
  451.     SendMessage( hListBox, LB_SETCURSEL, 0, 0 );
  452.  
  453.     // Update the UI controls to show the segment is loaded
  454.     SetDlgItemText( hDlg, IDC_FILENAME, strFileName );
  455.     SetDlgItemText( hDlg, IDC_STATUS, TEXT("File loaded.") );
  456.     EnablePlayUI( hDlg, TRUE );
  457.  
  458.     return S_OK;
  459. }
  460.  
  461.  
  462.  
  463.  
  464. //-----------------------------------------------------------------------------
  465. // Name: ProcessDirectMusicMessages()
  466. // Desc: Handle DirectMusic notification messages
  467. //-----------------------------------------------------------------------------
  468. HRESULT ProcessDirectMusicMessages( HWND hDlg )
  469. {
  470.     HRESULT hr;
  471.     IDirectMusicPerformance* pPerf = NULL;
  472.     DMUS_NOTIFICATION_PMSG* pPMsg;
  473.         
  474.     if( NULL == g_pMusicManager )
  475.         return S_OK;
  476.  
  477.     pPerf = g_pMusicManager->GetPerformance();
  478.  
  479.     // Get waiting notification message from the performance
  480.     while( S_OK == pPerf->GetNotificationPMsg( &pPMsg ) )
  481.     {
  482.         switch( pPMsg->dwNotificationOption )
  483.         {
  484.         case DMUS_NOTIFICATION_SEGSTART:
  485.             if( pPMsg->punkUser )
  486.             {
  487.                 IDirectMusicSegment*       pPrimarySegment  = NULL;
  488.                 IDirectMusicSegmentState8* pSegmentState    = NULL;
  489.                 IDirectMusicSegment*       pNotifySegment   = NULL;
  490.                 IDirectMusicSegment8*      pPrimarySegment8 = NULL;
  491.  
  492.                 // The pPMsg->punkUser contains a IDirectMusicSegmentState8, 
  493.                 // which we can query for the segment that segment it refers to.
  494.                 if( FAILED( hr = pPMsg->punkUser->QueryInterface( IID_IDirectMusicSegmentState8,
  495.                                                                   (VOID**) &pSegmentState ) ) )
  496.                     return DXTRACE_ERR( TEXT("QueryInterface"), hr );
  497.  
  498.                 if( FAILED( hr = pSegmentState->GetSegment( &pNotifySegment ) ) )
  499.                     return DXTRACE_ERR( TEXT("GetSegment"), hr );
  500.  
  501.                 // Get the IDirectMusicSegment for the primary segment
  502.                 pPrimarySegment8 = g_pMusicSegment->GetSegment();
  503.                 if( FAILED( hr = pPrimarySegment8->QueryInterface( IID_IDirectMusicSegment,
  504.                                                                    (VOID**) &pPrimarySegment ) ) )
  505.                     return DXTRACE_ERR( TEXT("QueryInterface"), hr );
  506.                 // Figure out which segment this is
  507.                 if( pNotifySegment == pPrimarySegment )
  508.                 {
  509.                     SetDlgItemText( hDlg, IDC_STATUS, "Primary segment playing." );
  510.                 }
  511.                 else
  512.                 {
  513.                     // Look through the motfs and see if they are playing 
  514.                     HWND hListBox = GetDlgItem( hDlg, IDC_MOTIF_LIST );
  515.                     DWORD dwCount = (DWORD)SendMessage( hListBox, LB_GETCOUNT, 0, 0 );                 
  516.                     for( DWORD i=0; i<dwCount; i++ )
  517.                     {
  518.                         MOTIF_NODE* pMotifNode = (MOTIF_NODE*) SendMessage( hListBox, LB_GETITEMDATA, i, 0 );
  519.                         if( pNotifySegment == pMotifNode->pMotif )
  520.                         {
  521.                             // If its motif segment update the UI
  522.                             TCHAR strMotifName[MAX_PATH];
  523.                             SendMessage( hListBox, LB_GETTEXT, i, (LPARAM) strMotifName );
  524.  
  525.                             TCHAR strStatus[MAX_PATH];
  526.                             if( pMotifNode->dwPlayCount > 0 )
  527.                                 strMotifName[ strlen(strMotifName) - strlen(" (Playing)") ] = 0;
  528.  
  529.                             wsprintf( strStatus, "%s motif started playing.", strMotifName );
  530.                             SetDlgItemText( hDlg, IDC_STATUS, strStatus );
  531.  
  532.                             pMotifNode->dwPlayCount++;
  533.                             if( pMotifNode->dwPlayCount == 1 )
  534.                             {
  535.                                 int nCurSel = (int)SendMessage( hListBox, LB_GETCURSEL, 0, 0 );
  536.                                 SendMessage( hListBox, LB_DELETESTRING, i, 0 );
  537.                                 strcat( strMotifName, " (Playing)" );
  538.                                 SendMessage( hListBox, LB_INSERTSTRING, i, (LPARAM) strMotifName );
  539.                                 SendMessage( hListBox, LB_SETITEMDATA,  i, (LPARAM) pMotifNode );
  540.                                 SendMessage( hListBox, LB_SETCURSEL, nCurSel, 0 );
  541.                             }
  542.                         }
  543.                     }
  544.                 }
  545.  
  546.                 // Cleanup
  547.                 SAFE_RELEASE( pSegmentState );
  548.                 SAFE_RELEASE( pNotifySegment );
  549.                 SAFE_RELEASE( pPrimarySegment );
  550.             }
  551.             break;
  552.  
  553.         case DMUS_NOTIFICATION_SEGEND:
  554.             if( pPMsg->punkUser )
  555.             {
  556.                 IDirectMusicSegment*       pPrimarySegment  = NULL;
  557.                 IDirectMusicSegmentState8* pSegmentState    = NULL;
  558.                 IDirectMusicSegment*       pNotifySegment   = NULL;
  559.                 IDirectMusicSegment8*      pPrimarySegment8 = NULL;
  560.  
  561.                 // The pPMsg->punkUser contains a IDirectMusicSegmentState8, 
  562.                 // which we can query for the segment that segment it refers to.
  563.                 if( FAILED( hr = pPMsg->punkUser->QueryInterface( IID_IDirectMusicSegmentState8,
  564.                                                                   (VOID**) &pSegmentState ) ) )
  565.                     return DXTRACE_ERR( TEXT("QueryInterface"), hr );
  566.  
  567.                 if( FAILED( hr = pSegmentState->GetSegment( &pNotifySegment ) ) )
  568.                 {
  569.                     // Sometimes the segend arrives after the segment is gone
  570.                     // This can happen when you load another segment as 
  571.                     // a motif or the segment is ending
  572.                     if( hr == DMUS_E_NOT_FOUND )
  573.                     {
  574.                         SAFE_RELEASE( pSegmentState );
  575.                         return S_OK;
  576.                     }
  577.  
  578.                     return DXTRACE_ERR( TEXT("GetSegment"), hr );
  579.                 }
  580.  
  581.                 // Get the IDirectMusicSegment for the primary segment
  582.                 pPrimarySegment8 = g_pMusicSegment->GetSegment();
  583.                 if( FAILED( hr = pPrimarySegment8->QueryInterface( IID_IDirectMusicSegment,
  584.                                                                    (VOID**) &pPrimarySegment ) ) )
  585.                     return DXTRACE_ERR( TEXT("QueryInterface"), hr );
  586.  
  587.                 // Figure out which segment this is
  588.                 if( pNotifySegment == pPrimarySegment )
  589.                 {
  590.                     // Update the UI controls to show the sound as stopped
  591.                     SetDlgItemText( hDlg, IDC_STATUS, "Primary segment stopped." );
  592.                     EnablePlayUI( hDlg, TRUE );
  593.                 }
  594.                 else
  595.                 {
  596.                     HWND hListBox = GetDlgItem( hDlg, IDC_MOTIF_LIST );
  597.                     DWORD dwCount = (DWORD)SendMessage( hListBox, LB_GETCOUNT, 0, 0 );                 
  598.                     for( DWORD i=0; i<dwCount; i++ )
  599.                     {
  600.                         MOTIF_NODE* pMotifNode = (MOTIF_NODE*) SendMessage( hListBox, LB_GETITEMDATA, i, 0 );
  601.                         if( pNotifySegment == pMotifNode->pMotif )
  602.                         {
  603.                             // If its motif segment update the UI
  604.                             TCHAR strMotifName[MAX_PATH];
  605.                             SendMessage( hListBox, LB_GETTEXT, i, (LPARAM) strMotifName );
  606.                             strMotifName[ strlen(strMotifName) - strlen(" (Playing)") ] = 0;
  607.  
  608.                             pMotifNode->dwPlayCount--;
  609.                             if( pMotifNode->dwPlayCount == 0 )
  610.                             {
  611.                                 int nCurSel = (int)SendMessage( hListBox, LB_GETCURSEL, 0, 0 );
  612.                                 SendMessage( hListBox, LB_DELETESTRING, i, 0 );
  613.                                 SendMessage( hListBox, LB_INSERTSTRING, i, (LPARAM) strMotifName );
  614.                                 SendMessage( hListBox, LB_SETITEMDATA,  i, (LPARAM) pMotifNode );
  615.                                 SendMessage( hListBox, LB_SETCURSEL, nCurSel, 0 );
  616.                             }
  617.  
  618.                             TCHAR strStatus[MAX_PATH];
  619.                             wsprintf( strStatus, "%s motif stopped playing.", strMotifName );
  620.                             SetDlgItemText( hDlg, IDC_STATUS, strStatus );
  621.                         }
  622.                     }
  623.                 }
  624.  
  625.                 // Cleanup
  626.                 SAFE_RELEASE( pSegmentState );
  627.                 SAFE_RELEASE( pNotifySegment );
  628.                 SAFE_RELEASE( pPrimarySegment );
  629.             }
  630.             break;
  631.         }
  632.  
  633.         pPerf->FreePMsg( (DMUS_PMSG*)pPMsg ); 
  634.     }
  635.  
  636.     return S_OK;
  637. }
  638.  
  639.  
  640.  
  641.  
  642. //-----------------------------------------------------------------------------
  643. // Name: OnPlaySegment()
  644. // Desc: 
  645. //-----------------------------------------------------------------------------
  646. HRESULT OnPlaySegment( HWND hDlg )
  647. {
  648.     HRESULT hr;
  649.  
  650.     HWND hLoopButton = GetDlgItem( hDlg, IDC_LOOP_CHECK );
  651.     BOOL bLooped = ( SendMessage( hLoopButton, BM_GETSTATE, 0, 0 ) == BST_CHECKED );
  652.  
  653.     if( bLooped )
  654.     {
  655.         // Set the segment to repeat many times
  656.         if( FAILED( hr = g_pMusicSegment->SetRepeats( DMUS_SEG_REPEAT_INFINITE ) ) )
  657.             return DXTRACE_ERR( TEXT("SetRepeats"), hr );
  658.     }
  659.     else
  660.     {
  661.         // Set the segment to not repeat
  662.         if( FAILED( hr = g_pMusicSegment->SetRepeats( 0 ) ) )
  663.             return DXTRACE_ERR( TEXT("SetRepeats"), hr );
  664.     }
  665.  
  666.     // Play the segment and wait. The DMUS_SEGF_BEAT indicates to play on the 
  667.     // next beat if there is a segment currently playing. The first 0 indicates 
  668.     // to play (on the next beat from) now.  
  669.     if( FAILED( hr = g_pMusicSegment->Play( DMUS_SEGF_BEAT ) ) )
  670.         return DXTRACE_ERR( TEXT("Play"), hr );
  671.  
  672.     EnablePlayUI( hDlg, FALSE );
  673.  
  674.     return S_OK;
  675. }
  676.  
  677.  
  678.  
  679.  
  680. //-----------------------------------------------------------------------------
  681. // Name: OnPlayMotif()
  682. // Desc: 
  683. //-----------------------------------------------------------------------------
  684. HRESULT OnPlayMotif( HWND hDlg )
  685. {
  686.     HRESULT hr;
  687.     DWORD dwSegFlags = DMUS_SEGF_SECONDARY;
  688.  
  689.     HWND hListBox = GetDlgItem( hDlg, IDC_MOTIF_LIST );
  690.     int nIndex = (int)SendMessage( hListBox, LB_GETCURSEL, 0, 0 );
  691.     if( nIndex == LB_ERR )
  692.         return S_FALSE;
  693.  
  694.     if( IsDlgButtonChecked( hDlg, IDC_RADIO_DEFAULT ) == BST_CHECKED )
  695.         dwSegFlags |= DMUS_SEGF_DEFAULT;
  696.     else if( IsDlgButtonChecked( hDlg, IDC_RADIO_IMMEDIATE ) == BST_CHECKED )
  697.         dwSegFlags |= 0;
  698.     else if( IsDlgButtonChecked( hDlg, IDC_RADIO_GRID ) == BST_CHECKED )
  699.         dwSegFlags |= DMUS_SEGF_GRID;
  700.     else if( IsDlgButtonChecked( hDlg, IDC_RADIO_BEAT ) == BST_CHECKED )
  701.         dwSegFlags |= DMUS_SEGF_BEAT;
  702.     else if( IsDlgButtonChecked( hDlg, IDC_RADIO_MEASURE ) == BST_CHECKED )
  703.         dwSegFlags |= DMUS_SEGF_MEASURE;
  704.  
  705.     TCHAR strMotifName[ MAX_PATH ];
  706.     SendMessage( hListBox, LB_GETTEXT, nIndex, (LPARAM) strMotifName );
  707.  
  708.     WCHAR wstrMotifName[ MAX_PATH ];
  709.     mbstowcs( wstrMotifName, strMotifName, MAX_PATH );
  710.  
  711.     IDirectMusicSegment* pMotif = NULL;
  712.     MOTIF_NODE* pMotifNode = (MOTIF_NODE*) SendMessage( hListBox, LB_GETITEMDATA, nIndex, 0 );
  713.     IDirectMusicPerformance* pPerformance = g_pMusicManager->GetPerformance();
  714.  
  715.     if( FAILED( hr = pPerformance->PlaySegment( pMotifNode->pMotif, dwSegFlags, 
  716.                                                 0, NULL ) ) )
  717.         return DXTRACE_ERR( TEXT("PlaySegment"), hr );
  718.  
  719.     return S_OK;
  720. }
  721.  
  722.  
  723.  
  724.  
  725. //-----------------------------------------------------------------------------
  726. // Name: EnablePlayUI( hDlg,)
  727. // Desc: Enables or disables the Play UI controls 
  728. //-----------------------------------------------------------------------------
  729. VOID EnablePlayUI( HWND hDlg, BOOL bEnable )
  730. {
  731.     if( bEnable )
  732.     {
  733.         EnableWindow(   GetDlgItem( hDlg, IDC_LOOP_CHECK ), TRUE );
  734.         EnableWindow(   GetDlgItem( hDlg, IDC_STOP ),       FALSE );
  735.  
  736.         EnableWindow(   GetDlgItem( hDlg, IDC_PLAY_MOTIF ), FALSE );
  737.         EnableWindow(   GetDlgItem( hDlg, IDC_PLAY ),       TRUE );
  738.         SetFocus(       GetDlgItem( hDlg, IDC_PLAY ) );
  739.     }
  740.     else
  741.     {
  742.         EnableWindow(  GetDlgItem( hDlg, IDC_LOOP_CHECK ), FALSE );
  743.         EnableWindow(  GetDlgItem( hDlg, IDC_STOP ),       TRUE );
  744.         SetFocus(      GetDlgItem( hDlg, IDC_STOP ) );
  745.         EnableWindow(  GetDlgItem( hDlg, IDC_PLAY ),       FALSE );
  746.  
  747.         HWND hListBox = GetDlgItem( hDlg, IDC_MOTIF_LIST );
  748.         int nIndex = (int)SendMessage( hListBox, LB_GETCURSEL, 0, 0 );
  749.  
  750.         if( nIndex == LB_ERR )
  751.             EnableWindow(   GetDlgItem( hDlg, IDC_PLAY_MOTIF ), FALSE );
  752.         else
  753.             EnableWindow(   GetDlgItem( hDlg, IDC_PLAY_MOTIF ), TRUE );
  754.     }
  755. }
  756.  
  757.  
  758.  
  759.