home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Game Programming for Teens / VBGPFT.cdr / DirectX8 / dx8a_sdk.exe / samples / multimedia / directmusic / playmotif / playmotif.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-11-04  |  28.1 KB  |  746 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 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( g_pMusicSegment )
  159.                     {
  160.                         if( g_pMusicSegment->IsPlaying() )
  161.                         {
  162.                             HWND hListBox = GetDlgItem( hDlg, IDC_MOTIF_LIST );
  163.                             int nIndex = (int)SendMessage( hListBox, LB_GETCURSEL, 0, 0 );
  164.  
  165.                             if( nIndex == LB_ERR )
  166.                                 EnableWindow( GetDlgItem( hDlg, IDC_PLAY_MOTIF ), FALSE );
  167.                             else
  168.                                 EnableWindow( GetDlgItem( hDlg, IDC_PLAY_MOTIF ), TRUE );
  169.                         }
  170.                     }
  171.                     break;
  172.  
  173.                 case IDC_PLAY:
  174.                     if( FAILED( hr = OnPlaySegment( hDlg ) ) )
  175.                     {
  176.                         DXTRACE_ERR( TEXT("OnPlaySegment"), hr );
  177.                         MessageBox( hDlg, "Error playing DirectMusic segment. "
  178.                                     "Sample will now exit.", "DirectMusic Sample", 
  179.                                     MB_OK | MB_ICONERROR );
  180.                         PostQuitMessage( IDABORT );
  181.                     }
  182.                     break;
  183.  
  184.                 case IDC_STOP:
  185.                     g_pMusicSegment->Stop( DMUS_SEGF_BEAT ); 
  186.                     SetDlgItemText( hDlg, IDC_STATUS, "Primary segment stopped." );
  187.                     EnablePlayUI( hDlg, TRUE );
  188.                     break;
  189.  
  190.                 case IDC_PLAY_MOTIF:
  191.                     if( FAILED( hr = OnPlayMotif( hDlg ) ) )
  192.                     {
  193.                         DXTRACE_ERR( TEXT("OnPlayMotif"), hr );
  194.                         MessageBox( hDlg, "Error playing DirectMusic motif. "
  195.                                     "Sample will now exit.", "DirectMusic Sample", 
  196.                                     MB_OK | MB_ICONERROR );
  197.                         PostQuitMessage( IDABORT );
  198.                     }
  199.                     break;
  200.  
  201.                 default:
  202.                     return FALSE; // Didn't handle message
  203.             }
  204.             break;
  205.  
  206.         case WM_DESTROY:
  207.         {            
  208.             // Cleanup everything
  209.             HWND hListBox = GetDlgItem( hDlg, IDC_MOTIF_LIST );
  210.             DWORD dwCount = (DWORD)SendMessage( hListBox, LB_GETCOUNT, 0, 0 );                 
  211.             for( DWORD i=0; i<dwCount; i++ )
  212.             {
  213.                 MOTIF_NODE* pMotifNode = (MOTIF_NODE*) SendMessage( hListBox, LB_GETITEMDATA, i, 0 );
  214.                 if( pMotifNode )
  215.                 {
  216.                     SAFE_RELEASE( pMotifNode->pMotif );
  217.                     SAFE_DELETE( pMotifNode );
  218.                 }
  219.             }
  220.  
  221.             CloseHandle( g_hDMusicMessageEvent );
  222.             SAFE_DELETE( g_pMusicSegment );
  223.             SAFE_DELETE( g_pMusicManager );
  224.             break; 
  225.         }
  226.  
  227.         default:
  228.             return FALSE; // Didn't handle message
  229.     }
  230.  
  231.     return TRUE; // Handled message
  232. }
  233.  
  234.  
  235.  
  236.  
  237. //-----------------------------------------------------------------------------
  238. // Name: OnInitDialog()
  239. // Desc: Initializes the dialogs (sets up UI controls, etc.)
  240. //-----------------------------------------------------------------------------
  241. HRESULT OnInitDialog( HWND hDlg )
  242. {
  243.     HRESULT hr;
  244.  
  245.     // Load the icon
  246.     HICON hIcon = LoadIcon( g_hInst, MAKEINTRESOURCE( IDR_MAINFRAME ) );
  247.  
  248.     // Set the icon for this dialog.
  249.     SendMessage( hDlg, WM_SETICON, ICON_BIG,   (LPARAM) hIcon );  // Set big icon
  250.     SendMessage( hDlg, WM_SETICON, ICON_SMALL, (LPARAM) hIcon );  // Set small icon
  251.  
  252.     g_hDMusicMessageEvent = CreateEvent( NULL, FALSE, FALSE, NULL );
  253.     g_pMusicManager = new CMusicManager();
  254.  
  255.     if( FAILED( hr = g_pMusicManager->Initialize( hDlg ) ) )
  256.         return DXTRACE_ERR( TEXT("Initialize"), hr );
  257.  
  258.     // Register segment notification
  259.     IDirectMusicPerformance* pPerf = g_pMusicManager->GetPerformance();
  260.     GUID guid = GUID_NOTIFICATION_SEGMENT;
  261.     if( FAILED( hr = pPerf->AddNotificationType( guid ) ) )
  262.         return DXTRACE_ERR( TEXT("AddNotificationType"), hr );
  263.  
  264.     if( FAILED( hr = pPerf->SetNotificationHandle( g_hDMusicMessageEvent, 0 ) ) )
  265.         return DXTRACE_ERR( TEXT("SetNotificationHandle"), hr );
  266.  
  267.     EnableWindow( GetDlgItem( hDlg, IDC_PLAY_MOTIF ), FALSE );
  268.     CheckRadioButton( hDlg, IDC_RADIO_DEFAULT, IDC_RADIO_MEASURE, IDC_RADIO_MEASURE );
  269.  
  270.     // Load a default music segment 
  271.     TCHAR strFileName[MAX_PATH];
  272.     strcpy( strFileName, DXUtil_GetDXSDKMediaPath() );
  273.     strcat( strFileName, "sample.sgt" );
  274.     if( S_FALSE == LoadSegmentFile( hDlg, strFileName ) )
  275.     {
  276.         // Set the UI controls
  277.         SetDlgItemText( hDlg, IDC_FILENAME, TEXT("") );
  278.         SetDlgItemText( hDlg, IDC_STATUS, TEXT("No file loaded.") );
  279.     }
  280.  
  281.     return S_OK;
  282. }
  283.  
  284.  
  285.  
  286.  
  287. //-----------------------------------------------------------------------------
  288. // Name: OnOpenSegmentFile()
  289. // Desc: Called when the user requests to open a sound file
  290. //-----------------------------------------------------------------------------
  291. VOID OnOpenSegmentFile( HWND hDlg ) 
  292. {
  293.     static TCHAR strFileName[MAX_PATH] = TEXT("");
  294.     static TCHAR strPath[MAX_PATH] = TEXT("");
  295.     HWND hListBox = GetDlgItem( hDlg, IDC_MOTIF_LIST );
  296.  
  297.     // Get the default media path (something like C:\MSSDK\SAMPLES\MULTIMEDIA\MEDIA)
  298.     if( '\0' == strPath[0] )
  299.     {
  300.         const TCHAR* szDir = DXUtil_GetDXSDKMediaPath();
  301.         strcpy( strPath, szDir );
  302.     }
  303.  
  304.     // Setup the OPENFILENAME structure
  305.     OPENFILENAME ofn = { sizeof(OPENFILENAME), hDlg, NULL,
  306.                          TEXT("DirectMusic Content Files\0*.sgt;*.mid;*.rmi\0All Files\0*.*\0\0"), NULL,
  307.                          0, 1, strFileName, MAX_PATH, NULL, 0, strPath,
  308.                          TEXT("Open Segment File"),
  309.                          OFN_FILEMUSTEXIST|OFN_HIDEREADONLY, 0, 0,
  310.                          TEXT(".sgt"), 0, NULL, NULL };
  311.  
  312.     if( g_pMusicSegment )
  313.         g_pMusicSegment->Stop( 0 );
  314.  
  315.     // Update the UI controls to show the sound as loading a file
  316.     EnableWindow( GetDlgItem( hDlg, IDC_PLAY ), FALSE);
  317.     EnableWindow( GetDlgItem( hDlg, IDC_STOP ), FALSE);
  318.     EnableWindow( GetDlgItem( hDlg, IDC_PLAY_MOTIF ), FALSE );
  319.     SetDlgItemText( hDlg, IDC_FILENAME, TEXT("") );
  320.     SetDlgItemText( hDlg, IDC_STATUS, TEXT("Loading file...") );
  321.  
  322.     // Cleanup motif listbox
  323.     DWORD dwCount = (DWORD)SendMessage( hListBox, LB_GETCOUNT, 0, 0 );                 
  324.     for( DWORD i=0; i<dwCount; i++ )
  325.     {
  326.         MOTIF_NODE* pMotifNode = (MOTIF_NODE*) SendMessage( hListBox, LB_GETITEMDATA, i, 0 );
  327.         if( pMotifNode )
  328.         {
  329.             SAFE_RELEASE( pMotifNode->pMotif );
  330.             SAFE_DELETE( pMotifNode );
  331.         }
  332.     }
  333.     SendMessage( hListBox, LB_RESETCONTENT, 0, 0 );
  334.  
  335.     // Display the OpenFileName dialog. Then, try to load the specified file
  336.     if( TRUE != GetOpenFileName( &ofn ) )
  337.     {
  338.         SetDlgItemText( hDlg, IDC_STATUS, TEXT("Load aborted.") );
  339.         return;
  340.     }
  341.  
  342.     if( S_FALSE == LoadSegmentFile( hDlg, strFileName ) )
  343.     {
  344.         SetDlgItemText( hDlg, IDC_FILENAME, TEXT("") );
  345.         SetDlgItemText( hDlg, IDC_STATUS, TEXT("Could not create segment from file.") );
  346.     }
  347.  
  348.     // Remember the path for next time
  349.     strcpy( strPath, strFileName );
  350.     char* strLastSlash = strrchr( strPath, '\\' );
  351.     strLastSlash[0] = '\0';
  352. }
  353.  
  354.  
  355.  
  356.  
  357. //-----------------------------------------------------------------------------
  358. // Name: LoadSegmentFile()
  359. // Desc: 
  360. //-----------------------------------------------------------------------------
  361. HRESULT LoadSegmentFile( HWND hDlg, TCHAR* strFileName )
  362. {
  363.     HRESULT hr;
  364.  
  365.     HWND hListBox = GetDlgItem( hDlg, IDC_MOTIF_LIST );
  366.     SetDlgItemText( hDlg, IDC_FILENAME, TEXT("") );
  367.  
  368.     SAFE_DELETE( g_pMusicSegment );
  369.  
  370.     // Have the loader collect any garbage now that the old 
  371.     // script has been released
  372.     g_pMusicManager->CollectGarbage();
  373.  
  374.     // Set the media path based on the file name (something like C:\MEDIA)
  375.     // to be used as the search directory for finding DirectMusic content
  376.     // related to this file.
  377.     TCHAR strMediaPath[MAX_PATH];
  378.     _tcscpy( strMediaPath, strFileName );
  379.     TCHAR* strLastSlash = _tcsrchr(strMediaPath, TEXT('\\'));
  380.     *strLastSlash = 0;
  381.     if( FAILED( hr = g_pMusicManager->SetSearchDirectory( strMediaPath ) ) )
  382.         return DXTRACE_ERR( TEXT("SetSearchDirectory"), hr );
  383.  
  384.     // For DirectMusic must know if the file is a standard MIDI file or not
  385.     // in order to load the correct instruments.
  386.     BOOL bMidiFile = FALSE;
  387.     if( strstr( strFileName, ".mid" ) != NULL ||
  388.         strstr( strFileName, ".rmi" ) != NULL ) 
  389.     {
  390.         bMidiFile = TRUE;
  391.     }
  392.  
  393.     // Load the file into a DirectMusic segment 
  394.     if( FAILED( g_pMusicManager->CreateSegmentFromFile( &g_pMusicSegment, strFileName, 
  395.                                                         TRUE, bMidiFile ) ) )
  396.     {
  397.         // Not a critical failure, so just update the status
  398.         return S_FALSE;
  399.     }
  400.  
  401.     IDirectMusicStyle8* pStyle = NULL;
  402.     DWORD dwStyleIndex = 0;
  403.  
  404.     while( TRUE )
  405.     {
  406.         // Get the style from the segment
  407.         // Segments may have any number of styles.
  408.         hr = g_pMusicSegment->GetStyle( &pStyle, dwStyleIndex );
  409.         if( FAILED(hr) )
  410.             break;
  411.  
  412.         // Get the names of the motifs from the style. 
  413.         // Styles may have any number of motifs.
  414.         DWORD dwIndex = 0;
  415.         while( TRUE )
  416.         {
  417.             WCHAR wstrMotifName[MAX_PATH];
  418.             CHAR  strMotifName[MAX_PATH];
  419.             if( S_FALSE == pStyle->EnumMotif( dwIndex, wstrMotifName ) )
  420.                 break;
  421.  
  422.             wcstombs( strMotifName, wstrMotifName, wcslen( wstrMotifName ) + 1 );
  423.             int nIndex = (int)SendMessage( hListBox, LB_ADDSTRING, 0, (LPARAM) strMotifName );
  424.  
  425.             MOTIF_NODE* pMotifNode = new MOTIF_NODE;
  426.             if( FAILED( hr = pStyle->GetMotif( wstrMotifName, &pMotifNode->pMotif ) ) )
  427.                 return DXTRACE_ERR( TEXT("GetMotif"), hr );
  428.             pMotifNode->dwPlayCount = 0;
  429.             SendMessage( hListBox, LB_SETITEMDATA, nIndex, (LPARAM) pMotifNode );
  430.  
  431.             dwIndex++;
  432.         }
  433.  
  434.         SAFE_RELEASE( pStyle );
  435.         dwStyleIndex++;
  436.     }
  437.  
  438.     SendMessage( hListBox, LB_SETCURSEL, 0, 0 );
  439.  
  440.     // Update the UI controls to show the segment is loaded
  441.     SetDlgItemText( hDlg, IDC_FILENAME, strFileName );
  442.     SetDlgItemText( hDlg, IDC_STATUS, TEXT("File loaded.") );
  443.     EnablePlayUI( hDlg, TRUE );
  444.  
  445.     return S_OK;
  446. }
  447.  
  448.  
  449.  
  450.  
  451. //-----------------------------------------------------------------------------
  452. // Name: ProcessDirectMusicMessages()
  453. // Desc: Handle DirectMusic notification messages
  454. //-----------------------------------------------------------------------------
  455. HRESULT ProcessDirectMusicMessages( HWND hDlg )
  456. {
  457.     HRESULT hr;
  458.     IDirectMusicPerformance* pPerf = NULL;
  459.     DMUS_NOTIFICATION_PMSG* pPMsg;
  460.         
  461.     if( NULL == g_pMusicManager )
  462.         return S_OK;
  463.  
  464.     pPerf = g_pMusicManager->GetPerformance();
  465.  
  466.     // Get waiting notification message from the performance
  467.     while( S_OK == pPerf->GetNotificationPMsg( &pPMsg ) )
  468.     {
  469.         switch( pPMsg->dwNotificationOption )
  470.         {
  471.         case DMUS_NOTIFICATION_SEGSTART:
  472.             if( pPMsg->punkUser )
  473.             {
  474.                 IDirectMusicSegment*       pPrimarySegment  = NULL;
  475.                 IDirectMusicSegmentState8* pSegmentState    = NULL;
  476.                 IDirectMusicSegment*       pNotifySegment   = NULL;
  477.                 IDirectMusicSegment8*      pPrimarySegment8 = NULL;
  478.  
  479.                 // The pPMsg->punkUser contains a IDirectMusicSegmentState8, 
  480.                 // which we can query for the segment that segment it refers to.
  481.                 if( FAILED( hr = pPMsg->punkUser->QueryInterface( IID_IDirectMusicSegmentState8,
  482.                                                                   (VOID**) &pSegmentState ) ) )
  483.                     return DXTRACE_ERR( TEXT("QueryInterface"), hr );
  484.  
  485.                 if( FAILED( hr = pSegmentState->GetSegment( &pNotifySegment ) ) )
  486.                     return DXTRACE_ERR( TEXT("GetSegment"), hr );
  487.  
  488.                 // Get the IDirectMusicSegment for the primary segment
  489.                 pPrimarySegment8 = g_pMusicSegment->GetSegment();
  490.                 if( FAILED( hr = pPrimarySegment8->QueryInterface( IID_IDirectMusicSegment,
  491.                                                                    (VOID**) &pPrimarySegment ) ) )
  492.                     return DXTRACE_ERR( TEXT("QueryInterface"), hr );
  493.                 // Figure out which segment this is
  494.                 if( pNotifySegment == pPrimarySegment )
  495.                 {
  496.                     SetDlgItemText( hDlg, IDC_STATUS, "Primary segment playing." );
  497.                 }
  498.                 else
  499.                 {
  500.                     // Look through the motfs and see if they are playing 
  501.                     HWND hListBox = GetDlgItem( hDlg, IDC_MOTIF_LIST );
  502.                     DWORD dwCount = (DWORD)SendMessage( hListBox, LB_GETCOUNT, 0, 0 );                 
  503.                     for( DWORD i=0; i<dwCount; i++ )
  504.                     {
  505.                         MOTIF_NODE* pMotifNode = (MOTIF_NODE*) SendMessage( hListBox, LB_GETITEMDATA, i, 0 );
  506.                         if( pNotifySegment == pMotifNode->pMotif )
  507.                         {
  508.                             // If its motif segment update the UI
  509.                             TCHAR strMotifName[MAX_PATH];
  510.                             SendMessage( hListBox, LB_GETTEXT, i, (LPARAM) strMotifName );
  511.  
  512.                             TCHAR strStatus[MAX_PATH];
  513.                             if( pMotifNode->dwPlayCount > 0 )
  514.                                 strMotifName[ strlen(strMotifName) - strlen(" (Playing)") ] = 0;
  515.  
  516.                             wsprintf( strStatus, "%s motif started playing.", strMotifName );
  517.                             SetDlgItemText( hDlg, IDC_STATUS, strStatus );
  518.  
  519.                             pMotifNode->dwPlayCount++;
  520.                             if( pMotifNode->dwPlayCount == 1 )
  521.                             {
  522.                                 int nCurSel = (int)SendMessage( hListBox, LB_GETCURSEL, 0, 0 );
  523.                                 SendMessage( hListBox, LB_DELETESTRING, i, 0 );
  524.                                 strcat( strMotifName, " (Playing)" );
  525.                                 SendMessage( hListBox, LB_INSERTSTRING, i, (LPARAM) strMotifName );
  526.                                 SendMessage( hListBox, LB_SETITEMDATA,  i, (LPARAM) pMotifNode );
  527.                                 SendMessage( hListBox, LB_SETCURSEL, nCurSel, 0 );
  528.                             }
  529.                         }
  530.                     }
  531.                 }
  532.  
  533.                 // Cleanup
  534.                 SAFE_RELEASE( pSegmentState );
  535.                 SAFE_RELEASE( pNotifySegment );
  536.                 SAFE_RELEASE( pPrimarySegment );
  537.             }
  538.             break;
  539.  
  540.         case DMUS_NOTIFICATION_SEGEND:
  541.             if( pPMsg->punkUser )
  542.             {
  543.                 IDirectMusicSegment*       pPrimarySegment  = NULL;
  544.                 IDirectMusicSegmentState8* pSegmentState    = NULL;
  545.                 IDirectMusicSegment*       pNotifySegment   = NULL;
  546.                 IDirectMusicSegment8*      pPrimarySegment8 = NULL;
  547.  
  548.                 // The pPMsg->punkUser contains a IDirectMusicSegmentState8, 
  549.                 // which we can query for the segment that segment it refers to.
  550.                 if( FAILED( hr = pPMsg->punkUser->QueryInterface( IID_IDirectMusicSegmentState8,
  551.                                                                   (VOID**) &pSegmentState ) ) )
  552.                     return DXTRACE_ERR( TEXT("QueryInterface"), hr );
  553.  
  554.                 if( FAILED( hr = pSegmentState->GetSegment( &pNotifySegment ) ) )
  555.                 {
  556.                     // Sometimes the segend arrives after the segment is gone
  557.                     // This can happen when you load another segment as 
  558.                     // a motif or the segment is ending
  559.                     if( hr == DMUS_E_NOT_FOUND )
  560.                     {
  561.                         SAFE_RELEASE( pSegmentState );
  562.                         return S_OK;
  563.                     }
  564.  
  565.                     return DXTRACE_ERR( TEXT("GetSegment"), hr );
  566.                 }
  567.  
  568.                 // Get the IDirectMusicSegment for the primary segment
  569.                 pPrimarySegment8 = g_pMusicSegment->GetSegment();
  570.                 if( FAILED( hr = pPrimarySegment8->QueryInterface( IID_IDirectMusicSegment,
  571.                                                                    (VOID**) &pPrimarySegment ) ) )
  572.                     return DXTRACE_ERR( TEXT("QueryInterface"), hr );
  573.  
  574.                 // Figure out which segment this is
  575.                 if( pNotifySegment == pPrimarySegment )
  576.                 {
  577.                     // Update the UI controls to show the sound as stopped
  578.                     SetDlgItemText( hDlg, IDC_STATUS, "Primary segment stopped." );
  579.                     EnablePlayUI( hDlg, TRUE );
  580.                 }
  581.                 else
  582.                 {
  583.                     HWND hListBox = GetDlgItem( hDlg, IDC_MOTIF_LIST );
  584.                     DWORD dwCount = (DWORD)SendMessage( hListBox, LB_GETCOUNT, 0, 0 );                 
  585.                     for( DWORD i=0; i<dwCount; i++ )
  586.                     {
  587.                         MOTIF_NODE* pMotifNode = (MOTIF_NODE*) SendMessage( hListBox, LB_GETITEMDATA, i, 0 );
  588.                         if( pNotifySegment == pMotifNode->pMotif )
  589.                         {
  590.                             // If its motif segment update the UI
  591.                             TCHAR strMotifName[MAX_PATH];
  592.                             SendMessage( hListBox, LB_GETTEXT, i, (LPARAM) strMotifName );
  593.                             strMotifName[ strlen(strMotifName) - strlen(" (Playing)") ] = 0;
  594.  
  595.                             pMotifNode->dwPlayCount--;
  596.                             if( pMotifNode->dwPlayCount == 0 )
  597.                             {
  598.                                 int nCurSel = (int)SendMessage( hListBox, LB_GETCURSEL, 0, 0 );
  599.                                 SendMessage( hListBox, LB_DELETESTRING, i, 0 );
  600.                                 SendMessage( hListBox, LB_INSERTSTRING, i, (LPARAM) strMotifName );
  601.                                 SendMessage( hListBox, LB_SETITEMDATA,  i, (LPARAM) pMotifNode );
  602.                                 SendMessage( hListBox, LB_SETCURSEL, nCurSel, 0 );
  603.                             }
  604.  
  605.                             TCHAR strStatus[MAX_PATH];
  606.                             wsprintf( strStatus, "%s motif stopped playing.", strMotifName );
  607.                             SetDlgItemText( hDlg, IDC_STATUS, strStatus );
  608.                         }
  609.                     }
  610.                 }
  611.  
  612.                 // Cleanup
  613.                 SAFE_RELEASE( pSegmentState );
  614.                 SAFE_RELEASE( pNotifySegment );
  615.                 SAFE_RELEASE( pPrimarySegment );
  616.             }
  617.             break;
  618.         }
  619.  
  620.         pPerf->FreePMsg( (DMUS_PMSG*)pPMsg ); 
  621.     }
  622.  
  623.     return S_OK;
  624. }
  625.  
  626.  
  627.  
  628.  
  629. //-----------------------------------------------------------------------------
  630. // Name: OnPlaySegment()
  631. // Desc: 
  632. //-----------------------------------------------------------------------------
  633. HRESULT OnPlaySegment( HWND hDlg )
  634. {
  635.     HRESULT hr;
  636.  
  637.     HWND hLoopButton = GetDlgItem( hDlg, IDC_LOOP_CHECK );
  638.     BOOL bLooped = ( SendMessage( hLoopButton, BM_GETSTATE, 0, 0 ) == BST_CHECKED );
  639.  
  640.     if( bLooped )
  641.     {
  642.         // Set the segment to repeat many times
  643.         if( FAILED( hr = g_pMusicSegment->SetRepeats( DMUS_SEG_REPEAT_INFINITE ) ) )
  644.             return DXTRACE_ERR( TEXT("SetRepeats"), hr );
  645.     }
  646.     else
  647.     {
  648.         // Set the segment to not repeat
  649.         if( FAILED( hr = g_pMusicSegment->SetRepeats( 0 ) ) )
  650.             return DXTRACE_ERR( TEXT("SetRepeats"), hr );
  651.     }
  652.  
  653.     // Play the segment and wait. The DMUS_SEGF_BEAT indicates to play on the 
  654.     // next beat if there is a segment currently playing. The first 0 indicates 
  655.     // to play (on the next beat from) now.  
  656.     if( FAILED( hr = g_pMusicSegment->Play( DMUS_SEGF_BEAT ) ) )
  657.         return DXTRACE_ERR( TEXT("Play"), hr );
  658.  
  659.     EnablePlayUI( hDlg, FALSE );
  660.  
  661.     return S_OK;
  662. }
  663.  
  664.  
  665.  
  666.  
  667. //-----------------------------------------------------------------------------
  668. // Name: OnPlayMotif()
  669. // Desc: 
  670. //-----------------------------------------------------------------------------
  671. HRESULT OnPlayMotif( HWND hDlg )
  672. {
  673.     HRESULT hr;
  674.     DWORD dwSegFlags = DMUS_SEGF_SECONDARY;
  675.  
  676.     HWND hListBox = GetDlgItem( hDlg, IDC_MOTIF_LIST );
  677.     int nIndex = (int)SendMessage( hListBox, LB_GETCURSEL, 0, 0 );
  678.     if( nIndex == LB_ERR )
  679.         return S_FALSE;
  680.  
  681.     if( IsDlgButtonChecked( hDlg, IDC_RADIO_DEFAULT ) == BST_CHECKED )
  682.         dwSegFlags |= DMUS_SEGF_DEFAULT;
  683.     else if( IsDlgButtonChecked( hDlg, IDC_RADIO_IMMEDIATE ) == BST_CHECKED )
  684.         dwSegFlags |= 0;
  685.     else if( IsDlgButtonChecked( hDlg, IDC_RADIO_GRID ) == BST_CHECKED )
  686.         dwSegFlags |= DMUS_SEGF_GRID;
  687.     else if( IsDlgButtonChecked( hDlg, IDC_RADIO_BEAT ) == BST_CHECKED )
  688.         dwSegFlags |= DMUS_SEGF_BEAT;
  689.     else if( IsDlgButtonChecked( hDlg, IDC_RADIO_MEASURE ) == BST_CHECKED )
  690.         dwSegFlags |= DMUS_SEGF_MEASURE;
  691.  
  692.     TCHAR strMotifName[ MAX_PATH ];
  693.     SendMessage( hListBox, LB_GETTEXT, nIndex, (LPARAM) strMotifName );
  694.  
  695.     WCHAR wstrMotifName[ MAX_PATH ];
  696.     mbstowcs( wstrMotifName, strMotifName, MAX_PATH );
  697.  
  698.     IDirectMusicSegment* pMotif = NULL;
  699.     MOTIF_NODE* pMotifNode = (MOTIF_NODE*) SendMessage( hListBox, LB_GETITEMDATA, nIndex, 0 );
  700.     IDirectMusicPerformance* pPerformance = g_pMusicManager->GetPerformance();
  701.  
  702.     if( FAILED( hr = pPerformance->PlaySegment( pMotifNode->pMotif, dwSegFlags, 
  703.                                                 0, NULL ) ) )
  704.         return DXTRACE_ERR( TEXT("PlaySegment"), hr );
  705.  
  706.     return S_OK;
  707. }
  708.  
  709.  
  710.  
  711.  
  712. //-----------------------------------------------------------------------------
  713. // Name: EnablePlayUI( hDlg,)
  714. // Desc: Enables or disables the Play UI controls 
  715. //-----------------------------------------------------------------------------
  716. VOID EnablePlayUI( HWND hDlg, BOOL bEnable )
  717. {
  718.     if( bEnable )
  719.     {
  720.         EnableWindow(   GetDlgItem( hDlg, IDC_LOOP_CHECK ), TRUE );
  721.         EnableWindow(   GetDlgItem( hDlg, IDC_STOP ),       FALSE );
  722.  
  723.         EnableWindow(   GetDlgItem( hDlg, IDC_PLAY_MOTIF ), FALSE );
  724.         EnableWindow(   GetDlgItem( hDlg, IDC_PLAY ),       TRUE );
  725.         SetFocus(       GetDlgItem( hDlg, IDC_PLAY ) );
  726.     }
  727.     else
  728.     {
  729.         EnableWindow(  GetDlgItem( hDlg, IDC_LOOP_CHECK ), FALSE );
  730.         EnableWindow(  GetDlgItem( hDlg, IDC_STOP ),       TRUE );
  731.         SetFocus(      GetDlgItem( hDlg, IDC_STOP ) );
  732.         EnableWindow(  GetDlgItem( hDlg, IDC_PLAY ),       FALSE );
  733.  
  734.         HWND hListBox = GetDlgItem( hDlg, IDC_MOTIF_LIST );
  735.         int nIndex = (int)SendMessage( hListBox, LB_GETCURSEL, 0, 0 );
  736.  
  737.         if( nIndex == LB_ERR )
  738.             EnableWindow(   GetDlgItem( hDlg, IDC_PLAY_MOTIF ), FALSE );
  739.         else
  740.             EnableWindow(   GetDlgItem( hDlg, IDC_PLAY_MOTIF ), TRUE );
  741.     }
  742. }
  743.  
  744.  
  745.  
  746.