home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Multimedia Jumpstart 1.1a / CD_ROM.BIN / develpmt / source / wavemix / mixtest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-20  |  11.7 KB  |  460 lines

  1. /****************************************************************************
  2.  
  3.     PROGRAM: MixTest
  4.  
  5. ****************************************************************************/
  6.  
  7. #include <windows.h>            /* required for all Windows applications */
  8. #include <windowsx.h>         /* for message crackers */
  9. #include <mmsystem.h>
  10. #include <stdlib.h>
  11. #include "wavemix.h"
  12. #include "mixtest.h"
  13. #include "resource.h"
  14.  
  15. /************************* GLOBAL VARIABLES ***********************************/
  16. HANDLE ghInst;
  17. HWND ghWnd;
  18. char gszAppName[] = "MixTest";
  19.  
  20. HANDLE ghMixSession;
  21. LPMIXWAVE glpMix1;
  22. LPMIXWAVE glpMix2;
  23. LPMIXWAVE glpMix3;
  24. LPMIXWAVE glpMix4;
  25. LPMIXWAVE glpMix5;
  26. LPMIXWAVE glpMix6;
  27. LPMIXWAVE glpMix7;
  28.  
  29. BOOL gfRepeatPlay=FALSE;
  30. BOOL gfDebug=FALSE;
  31.  
  32. HMENU ghMenu1,ghMenu2;
  33. BOOL gfAuto=FALSE;
  34.  
  35. /************************* GLOBAL VARIABLES ***********************************/
  36.  
  37.  
  38. /****************************** start helper functions ************************/
  39.  
  40. void ODSN(LPSTR sz)
  41. {
  42.     if (!gfDebug)
  43.         return;
  44.     OutputDebugString(sz);
  45.     OutputDebugString("\n\r");
  46. }
  47.  
  48. void UserMessage (LPSTR szMsg)
  49. {
  50.     MessageBox(ghWnd,szMsg,gszAppName,MB_OK|MB_ICONINFORMATION);
  51. }
  52.  
  53. void NiceWait(void)
  54. {
  55.     MSG msg;
  56.  
  57.     if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  58.     {
  59.         TranslateMessage(&msg);
  60.         DispatchMessage (&msg);
  61.     }
  62. }
  63.  
  64. void CloseWaveStuff(void)
  65. {
  66.     if (!ghMixSession)
  67.         return;
  68.     WaveMixCloseChannel(ghMixSession,0,WMIX_ALL);
  69.  
  70.     if (glpMix1) WaveMixFreeWave(ghMixSession,glpMix1);
  71.     if (glpMix2) WaveMixFreeWave(ghMixSession,glpMix2);
  72.     if (glpMix3) WaveMixFreeWave(ghMixSession,glpMix3);
  73.     if (glpMix4) WaveMixFreeWave(ghMixSession,glpMix4);
  74.     if (glpMix5) WaveMixFreeWave(ghMixSession,glpMix5);
  75.     if (glpMix6) WaveMixFreeWave(ghMixSession,glpMix6);
  76.     if (glpMix7) WaveMixFreeWave(ghMixSession,glpMix7);
  77.  
  78.     glpMix1 = glpMix2 = glpMix3 = glpMix4 = glpMix5 = glpMix6 = glpMix7 = NULL;
  79.  
  80.     WaveMixCloseSession(ghMixSession);
  81.     ghMixSession=NULL;
  82. }
  83.  
  84. /****************************** end helper functions **************************/
  85.  
  86. /****************************** start Methods *********************************/
  87.  
  88. void MixTest_OnPaint(HWND hWnd)
  89. {
  90.     HDC hDC;
  91.     PAINTSTRUCT ps;
  92.  
  93.     ODSN("MixTest_OnPaint");
  94.  
  95.     hDC = BeginPaint(hWnd,&ps);
  96.  
  97.     SetTextColor(hDC,RGB(255,255,255));
  98.     SetBkColor(hDC,0);
  99.     TextOut(hDC,10,10,"Press 1,2,3,4 or mouse keys",27);
  100.  
  101.     EndPaint(hWnd,&ps);
  102. }
  103.  
  104. void MixTest_OnKey(HWND hWnd, UINT vk, BOOL fDown, int cRepeat, UINT flags)
  105. {
  106.     MIXPLAYPARAMS MixPlayParams;
  107.  
  108.     ODSN("MixTest_OnKey");
  109.  
  110.     if (!fDown)
  111.         return;
  112.  
  113.     MixPlayParams.wSize = sizeof(MIXPLAYPARAMS);
  114.     MixPlayParams.hMixSession = ghMixSession;
  115.     MixPlayParams.hWndNotify=NULL;
  116.     MixPlayParams.dwFlags=WMIX_CLEARQUEUE|WMIX_HIPRIORITY;
  117.     MixPlayParams.wLoops=0;
  118.  
  119.     switch(vk)
  120.     {
  121.     case '1':
  122.         MixPlayParams.iChannel=1;
  123.         MixPlayParams.lpMixWave=glpMix1;
  124.         break;
  125.     case '2':
  126.         MixPlayParams.iChannel=2;
  127.         MixPlayParams.lpMixWave=glpMix2;
  128.         break;
  129.     case '3':
  130.         MixPlayParams.iChannel=3;
  131. //        MixPlayParams.dwFlags=WMIX_HIPRIORITY;
  132.         MixPlayParams.lpMixWave=glpMix3;
  133.         break;
  134.     case '4':
  135.         MixPlayParams.iChannel=4;
  136.         MixPlayParams.lpMixWave=glpMix4;
  137.         break;
  138.     default:
  139.         if (GetKeyState(VK_CAPITAL)<0)
  140.         {
  141.             gfRepeatPlay=TRUE;
  142.             MixPlayParams.iChannel=7;
  143.             MixPlayParams.lpMixWave=glpMix7;
  144.             MixPlayParams.hWndNotify=hWnd;
  145.         }
  146.         else
  147.         {
  148.             gfRepeatPlay=FALSE;
  149.             return;
  150.         }
  151.         break;
  152.     }
  153.  
  154.     WaveMixPlay(&MixPlayParams);
  155. }
  156.  
  157.  
  158. BOOL MixTest_OnCreate(HWND hWnd, CREATESTRUCT FAR* lpCreateStruct)
  159. {
  160.     BOOL fErr=FALSE;
  161.     WAVEMIXINFO Info;
  162.     ODSN("MixTest_OnCreate");
  163.  
  164.     ghMenu1 = LoadMenu(ghInst,"MixTestMenu1");
  165.     ghMenu2 = LoadMenu(ghInst,"MixTestMenu2");
  166.     SetMenu(hWnd,ghMenu1);
  167.     srand(gfDebug ? 1 : LOWORD(GetTickCount()));    // 1 reintializes, Any other number sets random
  168.  
  169.     Info.wSize=sizeof(WAVEMIXINFO);
  170.     if (WaveMixGetInfo(&Info))
  171.         return FALSE;
  172.  
  173.     if (!(ghMixSession = WaveMixInit()))
  174.         return FALSE;
  175.  
  176.     glpMix1=WaveMixOpenWave(ghMixSession,"1.wav",NULL,0);
  177.     glpMix2=WaveMixOpenWave(ghMixSession,"2.wav",NULL,0);
  178.     glpMix3=WaveMixOpenWave(ghMixSession,"3.wav",NULL,0);
  179.     glpMix4=WaveMixOpenWave(ghMixSession,"4.wav",NULL,0);
  180.     glpMix5=WaveMixOpenWave(ghMixSession,"5.wav",NULL,0);
  181.     glpMix6=WaveMixOpenWave(ghMixSession,"6.wav",NULL,0);
  182.     glpMix7=WaveMixOpenWave(ghMixSession,"7.wav",NULL,0);
  183.  
  184.     fErr=WaveMixOpenChannel(ghMixSession,8,WMIX_OPENCOUNT);
  185.  
  186.     if (fErr || !(glpMix1 && glpMix2 && glpMix3 && glpMix4 && glpMix5 && glpMix6 && glpMix7))
  187.     {
  188.         CloseWaveStuff();
  189.         return FALSE;
  190.     }
  191.  
  192.     return TRUE;
  193. }
  194.  
  195. void MixTest_OnActivate(HWND hwnd, UINT state, HWND hwndActDeact, BOOL fMinimized)
  196. {
  197.     BOOL fActivate = (state != WA_INACTIVE && !fMinimized);
  198.     WaveMixActivate(ghMixSession, fActivate);
  199. }
  200.  
  201. void MixTest_OnLButtonDown(HWND hwnd, BOOL fDoubleClick, int x, int y, UINT keyFlags)
  202. {
  203.     MIXPLAYPARAMS MixPlayParams;
  204.  
  205.     MixPlayParams.wSize = sizeof(MIXPLAYPARAMS);
  206.     MixPlayParams.hMixSession = ghMixSession;
  207.     MixPlayParams.hWndNotify=NULL;
  208.     MixPlayParams.dwFlags=WMIX_CLEARQUEUE|WMIX_HIPRIORITY;
  209.     MixPlayParams.wLoops=0;
  210.     MixPlayParams.iChannel=5;
  211.     MixPlayParams.lpMixWave=glpMix5;
  212.  
  213.     WaveMixFlushChannel(ghMixSession,6,WMIX_NOREMIX);
  214.     WaveMixPlay(&MixPlayParams);
  215. }
  216.  
  217. void MixTest_OnTimer(HWND hwnd, UINT id)
  218. {
  219.     static LPMIXWAVE * waves[7]={&glpMix1,&glpMix2,&glpMix3,&glpMix4,&glpMix5,&glpMix6,&glpMix7};
  220.     int i;
  221.     MIXPLAYPARAMS MixPlayParams;
  222.  
  223.     i = 1+rand()%7;
  224.     
  225.  
  226.     MixPlayParams.wSize = sizeof(MIXPLAYPARAMS);
  227.     MixPlayParams.hMixSession = ghMixSession;
  228.     MixPlayParams.hWndNotify=NULL;
  229.     MixPlayParams.dwFlags=WMIX_CLEARQUEUE|WMIX_HIPRIORITY;
  230.     MixPlayParams.wLoops=0;
  231.     MixPlayParams.iChannel=i;
  232.     MixPlayParams.lpMixWave=*waves[i-1];
  233.  
  234.     WaveMixPlay(&MixPlayParams);
  235. }
  236.  
  237.  
  238. void MixTest_OnRButtonDown(HWND hwnd, BOOL fDoubleClick, int x, int y, UINT keyFlags)
  239. {
  240.     MIXPLAYPARAMS MixPlayParams;
  241.  
  242.     MixPlayParams.wSize = sizeof(MIXPLAYPARAMS);
  243.     MixPlayParams.hMixSession = ghMixSession;
  244.     MixPlayParams.hWndNotify=NULL;
  245.     MixPlayParams.dwFlags=WMIX_CLEARQUEUE|WMIX_HIPRIORITY|WMIX_WAIT;
  246.     MixPlayParams.wLoops=15;
  247.     MixPlayParams.iChannel=6;
  248.     MixPlayParams.lpMixWave=glpMix6;
  249.  
  250.     WaveMixPlay(&MixPlayParams);  // note WMIX_WAIT is set
  251.  
  252.     MixPlayParams.iChannel=7;
  253.     MixPlayParams.wLoops=3;
  254.     MixPlayParams.lpMixWave=glpMix7;
  255.     WaveMixPlay(&MixPlayParams);  // note WMIX_WAIT is set
  256.  
  257.     MixPlayParams.iChannel=1;
  258.     MixPlayParams.lpMixWave=glpMix1;
  259.     WaveMixPlay(&MixPlayParams);  // note WMIX_WAIT is set
  260.  
  261.     MixPlayParams.iChannel=3;
  262.     MixPlayParams.wLoops=0;
  263.     MixPlayParams.lpMixWave=glpMix3;
  264.     MixPlayParams.dwFlags=WMIX_CLEARQUEUE|WMIX_HIPRIORITY; // note WMIX_WAIT is cleared so playing can start
  265.     WaveMixPlay(&MixPlayParams);
  266. }
  267.  
  268. void MixTest_OnMM_WOM_DONE(HWND hWnd, int iChannel, LPMIXWAVE lpMixWave)
  269. {
  270.     MIXPLAYPARAMS MixPlayParams;
  271.  
  272.     if (!gfRepeatPlay)
  273.         return;
  274.  
  275.     MixPlayParams.wSize = sizeof(MIXPLAYPARAMS);
  276.     MixPlayParams.hMixSession = ghMixSession;
  277.     MixPlayParams.hWndNotify=hWnd;
  278.     MixPlayParams.dwFlags=WMIX_QUEUEWAVE;
  279.     MixPlayParams.wLoops=0;
  280.     MixPlayParams.iChannel=iChannel;
  281.     MixPlayParams.lpMixWave=lpMixWave;
  282.  
  283.     WaveMixPlay(&MixPlayParams);
  284. }
  285.  
  286. void MixTest_OnDestroy(HWND hWnd)
  287. {
  288.     ODSN("MixTest_OnDestroy");
  289.  
  290.     CloseWaveStuff();
  291.  
  292.     if (gfAuto)
  293.         KillTimer(hWnd,1);
  294.  
  295.     DestroyMenu(ghMenu1);
  296.     DestroyMenu(ghMenu2);
  297.  
  298.     PostQuitMessage(0);
  299. }
  300.  
  301.  
  302. void MixTest_OnCommand(HWND hWnd, int id, HWND hwndCtl, UINT codeNotify)
  303. {
  304.     ODSN("MixTest_OnCommand");
  305.  
  306.     switch (id)
  307.     {
  308.     case IDM_AUTO:
  309.         gfAuto = !gfAuto;
  310.         if (gfAuto)
  311.         {
  312.             SetMenu(hWnd,ghMenu2);
  313.             SetTimer(hWnd,1,100,NULL);
  314.         }
  315.         else
  316.         {
  317.             KillTimer(hWnd,1);
  318.             SetMenu(hWnd,ghMenu1);
  319.         }
  320.         break;
  321.     case IDM_ABOUT:
  322.         DialogBox(ghInst,"AboutBox",hWnd,AboutDlgProc);
  323.         break;
  324.     default:
  325.         FORWARD_WM_COMMAND(hWnd, id, hwndCtl, codeNotify, DefWindowProc);
  326.     }    
  327. }
  328.  
  329. /****************************** end MixTest_ Methods ******************************************/
  330.  
  331. /****************************** start AboutMixTest_ Methods ***********************************/
  332.  
  333. BOOL AboutMixTest_OnInitDialog(HWND hDlg, HWND hwndFocus, LPARAM lParam)
  334. {
  335.     return TRUE;
  336. }
  337.  
  338. BOOL AboutMixTest_OnCommand(HWND hDlg, int id, HWND hwndCtl, UINT codeNotify)
  339. {
  340.     switch (id)
  341.     {
  342.     case IDOK:
  343.     case IDCANCEL:
  344.         EndDialog(hDlg, TRUE); /* Exits the dialog box          */
  345.         return TRUE;
  346.     }
  347.     return FALSE;
  348. }
  349.  
  350. /****************************** end AboutMixTest_ Methods ***********************************/
  351.  
  352. /************************ start Program Initialization Functions **************************/
  353.  
  354.  
  355. BOOL InitApplication(HINSTANCE hInstance)
  356. {
  357.     WNDCLASS  wc;
  358.  
  359.     wc.style             = CS_BYTEALIGNWINDOW;
  360.     wc.lpfnWndProc         = MixTestWndProc;
  361.     wc.cbClsExtra         = 0;
  362.     wc.cbWndExtra         = 0;
  363.     wc.hInstance          = hInstance;
  364.     wc.hIcon              = LoadIcon(hInstance, "MixTest");
  365.     wc.hCursor            = LoadCursor(NULL, IDC_ARROW);
  366.     wc.hbrBackground    = GetStockObject(BLACK_BRUSH);
  367.     wc.lpszMenuName     = NULL; // "MixTestMenu";
  368.     wc.lpszClassName     = gszAppName;
  369.  
  370.     return (RegisterClass(&wc));
  371. }
  372.  
  373. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  374. {
  375.     ghInst = hInstance;
  376.  
  377.     ghWnd = CreateWindow(
  378.             /* address of registered class name        */    gszAppName,
  379.             /* address of window text                */    gszAppName,
  380.             /* window style                            */    WS_OVERLAPPEDWINDOW,
  381.             /* horizontal position of window        */    CW_USEDEFAULT,
  382.             /* vertical position of window            */    CW_USEDEFAULT,
  383.             /* window width                            */    CW_USEDEFAULT,
  384.             /* window height                        */    CW_USEDEFAULT,
  385.             /* handle of parent window                */    NULL,
  386.             /* handle of menu or child-window id    */    NULL,
  387.             /* handle of application instance        */    hInstance,
  388.             /* address of window-creation data        */    NULL);
  389.     
  390.     if (!ghWnd)
  391.         return (FALSE);
  392.     
  393.     /* Make the window visible; update its client area; and return "success" */
  394.     
  395.     ShowWindow(ghWnd, nCmdShow);  /* Show the window                        */
  396.     UpdateWindow(ghWnd);          /* Sends WM_PAINT message                 */
  397.  
  398.     return (TRUE);
  399. }
  400.  
  401. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  402. {
  403.     MSG msg;
  404.  
  405.     if (!hPrevInstance)
  406.         if (!InitApplication(hInstance))
  407.     return (FALSE);
  408.     
  409.     if (!InitInstance(hInstance, nCmdShow))
  410.         return (FALSE);
  411.  
  412.     while (GetMessage(&msg, NULL, 0, 0))
  413.     {
  414.         TranslateMessage(&msg);
  415.         DispatchMessage(&msg);
  416.     }
  417.  
  418.     return msg.wParam;
  419. }
  420.  
  421. /************************ end Program Initialization Functions **************************/
  422.  
  423. /************************ start exported window Procedures ******************************/
  424.  
  425. BOOL __export CALLBACK AboutDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  426. {
  427.     switch (message)
  428.     {
  429.     case WM_INITDIALOG:
  430.         return (BOOL)HANDLE_WM_INITDIALOG(hDlg, wParam, lParam, AboutMixTest_OnInitDialog);
  431.     
  432.     case WM_COMMAND:
  433.         HANDLE_WM_COMMAND(hDlg,    wParam, lParam, AboutMixTest_OnCommand);
  434.         return TRUE;
  435.     }
  436.     return (FALSE);                  /* Didn't process a message    */
  437. }
  438.  
  439. LRESULT __export CALLBACK MixTestWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  440. {
  441.     switch (message)
  442.     {
  443.         HANDLE_MSG (hWnd, WM_CREATE,        MixTest_OnCreate);
  444.         HANDLE_MSG (hWnd, WM_DESTROY,        MixTest_OnDestroy);
  445.         HANDLE_MSG (hWnd, WM_PAINT,            MixTest_OnPaint);
  446.         HANDLE_MSG (hWnd, WM_COMMAND,        MixTest_OnCommand);
  447.         HANDLE_MSG (hWnd, WM_KEYDOWN,        MixTest_OnKey);
  448.         HANDLE_MSG (hWnd, WM_LBUTTONDOWN,    MixTest_OnLButtonDown);
  449.         HANDLE_MSG (hWnd, WM_RBUTTONDOWN,    MixTest_OnRButtonDown);
  450.         HANDLE_MSG (hWnd, MM_WOM_DONE,        MixTest_OnMM_WOM_DONE);
  451.         HANDLE_MSG (hWnd, WM_ACTIVATE,        MixTest_OnActivate);
  452.         HANDLE_MSG (hWnd, WM_TIMER,            MixTest_OnTimer);
  453.     default:
  454.         return DefWindowProc(hWnd, message, wParam, lParam);
  455.     }
  456. }
  457.  
  458. /************************ end exported window Procedures ******************************/
  459.  
  460.