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