home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / m / m003_1 / sdk_win.ddi / SAMPLE / DLLVOICE / DLLVOICE.C next >
Encoding:
C/C++ Source or Header  |  1991-06-17  |  21.2 KB  |  588 lines

  1. /****************************************************************************
  2.  
  3.     PROGRAM: DLLVoice.c
  4.  
  5.     PURPOSE: DLLVoice template for Sound Blaster applications
  6.  
  7.     FUNCTIONS:
  8.  
  9.     WinMain () - calls initialization function, processes message loop
  10.     InitApplication () - initializes window data and registers window
  11.     InitInstance () - saves instance handle and creates main window
  12.     MainWndProc () - processes messages
  13.     About () - processes messages for "About" dialog box
  14.  
  15.     COMMENTS:
  16.  
  17.         Windows can have several copies of your application running at the
  18.         same time. The variable hInst keeps track of which instance this
  19.         application is so that processing will be to the correct window.
  20.  
  21. ****************************************************************************/
  22.  
  23. #include "windows.h"        /* required for all Windows applications */
  24. #include "dllvoice.h"       /* specific to this program              */
  25. #include <sndblst.h>        /* required for all Sound Blaster applications */
  26.  
  27. #define BUFSIZE 256000
  28.  
  29. WORD    wDLLVersion;        /* verion number of DLL                  */
  30. WORD    wSoundBlasterMsg;   /* Sound Blaster message                 */
  31. LPSTR   lpCardName;         /* name of Sound Blaster card            */
  32. HANDLE  hVoiceMem;          /* handle of allocated memory            */
  33. LPSTR   lpBuffer;           /* buffer to hold voice data             */
  34. HANDLE  hInst;              /* current instance                      */
  35. HWND    hWnd;               /* main window handle.                   */
  36. WORD    wRecordStatus=0;    /* record status                         */
  37. WORD    wPlayStatus=0;      /* play status                           */
  38.  
  39. /****************************************************************************
  40.  
  41.     FUNCTION: WinMain (HANDLE, HANDLE, LPSTR, int)
  42.  
  43.     PURPOSE: calls initialization function, processes message loop
  44.  
  45.     COMMENTS:
  46.  
  47.         Windows recognizes this function by name as the initial entry point 
  48.         for the program.  This function calls the application initialization 
  49.         routine, if no other instance of the program is running, and always 
  50.         calls the instance initialization routine.  It then executes a message 
  51.         retrieval and dispatch loop that is the top-level control structure 
  52.         for the remainder of execution. The loop is terminated when a WM_QUIT
  53.         message is received, at which time this function exits the application 
  54.         instance by returning the value passed by PostQuitMessage(). 
  55.  
  56.         If this function must abort before entering the message loop, it 
  57.         returns the conventional value NULL.  
  58.  
  59. ****************************************************************************/
  60.  
  61. int PASCAL WinMain (hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  62. HANDLE hInstance;                /* current instance             */
  63. HANDLE hPrevInstance;            /* previous instance            */
  64. LPSTR lpCmdLine;                 /* command line                 */
  65. int nCmdShow;                     /* show-window type (open/icon) */
  66. {
  67.     MSG msg;                     /* message                      */
  68.  
  69.     if (!hPrevInstance)               /* Other instances of app running?   */
  70.     if (!InitApplication (hInstance)) /* Initialize shared things          */
  71.         return (FALSE);               /* Exits if unable to initialize     */
  72.  
  73.     /* Perform initializations that apply to a specific instance */
  74.  
  75.     if (!InitInstance (hInstance, nCmdShow))
  76.         return (FALSE);
  77.  
  78.     if ((wDLLVersion = sbcGetDLLVersion ()) == NULL)
  79.         return (FALSE);
  80.  
  81.     if ((lpCardName = sbcGetCardName ()) == NULL)
  82.         return (FALSE);
  83.  
  84.     wSoundBlasterMsg = RegisterWindowMessage ((LPSTR) "SoundBlaster");
  85.  
  86.     /* Acquire and dispatch messages until a WM_QUIT message is received. */
  87.  
  88.     while (GetMessage (&msg,     /* message structure                      */
  89.         NULL,                    /* handle of window receiving the message */
  90.         NULL,                    /* lowest message to examine              */
  91.         NULL))                   /* highest message to examine             */
  92.     {
  93.         TranslateMessage (&msg); /* Translates virtual key codes           */
  94.         DispatchMessage (&msg);  /* Dispatches message to window           */
  95.     }
  96.  
  97.     sbcTerminateDLL ();
  98.  
  99.     return (msg.wParam);        /* Returns the value from PostQuitMessage */
  100. }
  101.  
  102. /****************************************************************************
  103.  
  104.     FUNCTION: InitApplication (HANDLE)
  105.  
  106.     PURPOSE: Initializes window data and registers window class
  107.  
  108.     COMMENTS:
  109.  
  110.         This function is called at initialization time only if no other 
  111.         instances of the application are running.  This function performs 
  112.         initialization tasks that can be done once for any number of running 
  113.         instances.  
  114.  
  115.         In this case, we initialize a window class by filling out a data 
  116.         structure of type WNDCLASS and calling the Windows RegisterClass() 
  117.         function.  Since all instances of this application use the same window 
  118.         class, we only need to do this when the first instance is initialized.  
  119.  
  120. ****************************************************************************/
  121.  
  122. BOOL InitApplication (hInstance)
  123. HANDLE hInstance;                       /* current instance                   */
  124. {
  125.     WNDCLASS  wc;
  126.  
  127.     /* Fill in window class structure with parameters that describe the       */
  128.     /* main window.                                                           */
  129.  
  130.     wc.style         = NULL;             /* Class style(s).                    */
  131.     wc.lpfnWndProc   = MainWndProc;      /* Function to retrieve messages for  */
  132.                                          /* windows of this class.             */
  133.     wc.cbClsExtra    = 0;                /* No per-class extra data.           */
  134.     wc.cbWndExtra    = 0;                /* No per-window extra data.          */
  135.     wc.hInstance     = hInstance;        /* Application that owns the class.   */
  136.     wc.hIcon         = LoadIcon (hInstance, "dllvoice");
  137.     wc.hCursor       = LoadCursor (NULL, IDC_ARROW);
  138.     wc.hbrBackground = GetStockObject(WHITE_BRUSH); 
  139.     wc.lpszMenuName  = "DLLVoiceMenu";   /* Name of menu resource in .RC file. */
  140.     wc.lpszClassName = "DLLVoiceWClass"; /* Name used in call to CreateWindow. */
  141.  
  142.     /* Register the window class and return success/failure code. */
  143.  
  144.     return (RegisterClass (&wc));
  145.  
  146. }
  147.  
  148. /****************************************************************************
  149.  
  150.     FUNCTION:  InitInstance (HANDLE, int)
  151.  
  152.     PURPOSE:  Saves instance handle and creates main window
  153.  
  154.     COMMENTS:
  155.  
  156.         This function is called at initialization time for every instance of 
  157.         this application.  This function performs initialization tasks that 
  158.         cannot be shared by multiple instances.  
  159.  
  160.         In this case, we save the instance handle in a static variable and 
  161.         create and display the main program window.  
  162.         
  163. ****************************************************************************/
  164.  
  165. BOOL InitInstance (hInstance, nCmdShow)
  166.     HANDLE          hInstance;          /* Current instance identifier.       */
  167.     int             nCmdShow;           /* Param for first ShowWindow() call. */
  168. {
  169.     /* Save the instance handle in static variable, which will be used in  */
  170.     /* many subsequence calls from this application to Windows.            */
  171.  
  172.     hInst = hInstance;
  173.  
  174.     /* Create a main window for this application instance.  */
  175.  
  176.     hWnd = CreateWindow
  177.     (
  178.         "DLLVoiceWClass",               /* See RegisterClass() call.          */
  179.         "DLLVOICE Sample Application",  /* Text for window title bar.         */
  180.         WS_OVERLAPPEDWINDOW,            /* Window style.                      */
  181.         CW_USEDEFAULT,                  /* Default horizontal position.       */
  182.         CW_USEDEFAULT,                  /* Default vertical position.         */
  183.         CW_USEDEFAULT,                  /* Default width.                     */
  184.         CW_USEDEFAULT,                  /* Default height.                    */
  185.         NULL,                           /* Overlapped windows have no parent. */
  186.         NULL,                           /* Use the window class menu.         */
  187.         hInstance,                      /* This instance owns this window.    */
  188.         NULL                            /* Pointer not needed.                */
  189.     );
  190.  
  191.     /* If window could not be created, return "failure" */
  192.  
  193.     if (!hWnd)
  194.         return (FALSE);
  195.  
  196.     /* Make the window visible; update its client area; and return "success" */
  197.  
  198.     ShowWindow (hWnd, nCmdShow);   /* Show the window                        */
  199.     UpdateWindow (hWnd);           /* Sends WM_PAINT message                 */
  200.     return (TRUE);                 /* Returns the value from PostQuitMessage */
  201.  
  202. }
  203.  
  204. /****************************************************************************
  205.  
  206.     FUNCTION: MainWndProc (HWND, unsigned, WORD, LONG)
  207.  
  208.     PURPOSE:  Processes messages
  209.  
  210.     MESSAGES:
  211.  
  212.     WM_COMMAND    - application menu (About dialog box)
  213.     WM_DESTROY    - destroy window
  214.  
  215.     COMMENTS:
  216.  
  217.     To process the IDM_ABOUT message, call MakeProcInstance() to get the
  218.     current instance address of the About() function.  Then call Dialog
  219.     box which will create the box according to the information in your
  220.     dllvoice.rc file and turn control over to the About() function.  When
  221.     it returns, free the intance address.
  222.  
  223. ****************************************************************************/
  224.  
  225. long FAR PASCAL MainWndProc (hWnd, message, wParam, lParam)
  226. HWND hWnd;                    /* window handle                   */
  227. unsigned message;             /* type of message                 */
  228. WORD wParam;                  /* additional information          */
  229. LONG lParam;                  /* additional information          */
  230. {
  231.     FARPROC lpProcAbout;      /* pointer to the "About" function */
  232.     WORD wReturnValue;
  233.     PAINTSTRUCT ps;
  234.  
  235.     switch (message)
  236.     {
  237.         case WM_PAINT:
  238.             BeginPaint (hWnd, &ps);
  239.             EndPaint (hWnd, &ps);
  240.             DisplayRecordFlag (wRecordStatus);
  241.             DisplayPlayFlag (wPlayStatus);
  242.             break;
  243.         case WM_COMMAND:       /* message: command from application menu */
  244.             switch (wParam)
  245.             {
  246.                 case IDM_ABOUT:
  247.                     lpProcAbout = MakeProcInstance (About, hInst);
  248.  
  249.                     DialogBox(hInst,         /* current instance         */
  250.                         "AboutBox",          /* resource to use          */
  251.                         hWnd,                /* parent handle            */
  252.                         lpProcAbout);        /* About() instance address */
  253.  
  254.                     FreeProcInstance (lpProcAbout);
  255.                     break;
  256.                 case IDM_START:
  257.                     StartVoiceProcess ();
  258.                     break;
  259.                 case IDM_STOP:
  260.                     StopVoiceProcess ();
  261.                     break;
  262.                 case IDM_PAUSE:
  263.                     PauseVoiceProcess ();
  264.                     break;
  265.                 case IDM_CONTINUE:
  266.                     ContinueVoiceProcess ();
  267.                     break;
  268.                 default:
  269.                     return (DefWindowProc (hWnd, message, wParam, lParam));
  270.             }
  271.             break;
  272.  
  273.         case WM_DESTROY:          /* message: window being destroyed */
  274.             PostQuitMessage(0);
  275.             break;
  276.  
  277.         default:
  278.             if (message == wSoundBlasterMsg)
  279.             {
  280.                 switch (wParam)
  281.                 {
  282.                     case SBM_CALLBACK:
  283.                         sbcCallBack (lParam);
  284.                         break;
  285.                     case SBM_VOC_FLAG:
  286.                         switch (HIWORD (lParam))
  287.                         {
  288.                             case VP_RECORD_MEMUNFMT:
  289.                                 wRecordStatus = LOWORD (lParam);
  290.                                 UpdateMenu ();
  291.                                 DisplayRecordFlag (wRecordStatus);
  292.                                 if (LOWORD (lParam) == 0)
  293.                                 {
  294.                                     vocSetDacSpeaker (1);
  295.                                     wReturnValue = vocPlayMemUnFormat (lpBuffer, 11000,
  296.                                                     vocGetRecordCount ());
  297.                                     if (wReturnValue)
  298.                                     {
  299.                                         vocCloseDevice ();
  300.                                         ReleaseMemory ();
  301.                                         DisplayErrorCode (wReturnValue);
  302.                                     }
  303.                                 }
  304.                                 break;
  305.                             case VP_PLAY_MEMUNFMT:
  306.                                 wPlayStatus = LOWORD (lParam);
  307.                                 UpdateMenu ();
  308.                                 DisplayPlayFlag (wPlayStatus);
  309.                                 if (LOWORD (lParam) == 0)
  310.                                 {
  311.                                     vocCloseDevice ();
  312.                                     ReleaseMemory ();
  313.                                 }
  314.                                 break;
  315.                         }
  316.                         break;
  317.                 }
  318.             }
  319.             else                  /* Passes it on if unproccessed    */
  320.             {
  321.                 return (DefWindowProc(hWnd, message, wParam, lParam));
  322.             }
  323.     }
  324.     return (NULL);
  325. }
  326.  
  327. /****************************************************************************
  328.  
  329.     FUNCTION: ReleaseMemory ()
  330.  
  331.     PURPOSE: Return allocated memory block to windows
  332.  
  333. ****************************************************************************/
  334.  
  335. void ReleaseMemory ()
  336. {
  337.     GlobalPageUnlock (HIWORD (lpBuffer));
  338.     GlobalUnWire (hVoiceMem);
  339.     GlobalFree (hVoiceMem);
  340. }
  341.  
  342. /****************************************************************************
  343.  
  344.     FUNCTION: StartVoiceProcess ()
  345.  
  346.     PURPOSE: Start voice recording
  347.  
  348. ****************************************************************************/
  349.  
  350. void StartVoiceProcess ()
  351. {
  352.     WORD wReturnCode;
  353.  
  354.     if (wReturnCode = vocOpenDevice (hWnd))
  355.         DisplayErrorCode (wReturnCode);
  356.     else
  357.     {
  358.         if ((hVoiceMem = GlobalAlloc (GMEM_MOVEABLE, BUFSIZE)) != NULL)
  359.         {
  360.             lpBuffer = GlobalWire (hVoiceMem);
  361.             GlobalPageLock (HIWORD (lpBuffer));
  362.             vocSetDacSpeaker (0);
  363.             if (wReturnCode = vocRecordMemUnFormat (lpBuffer, 11000, BUFSIZE))
  364.             {
  365.                 vocCloseDevice ();
  366.                 ReleaseMemory ();
  367.                 DisplayErrorCode (wReturnCode);
  368.             }
  369.         }
  370.     }
  371. }
  372.  
  373. /****************************************************************************
  374.  
  375.     FUNCTION: StopVoiceProcess ()
  376.  
  377.     PURPOSE: Stop current active voice process
  378.  
  379. ****************************************************************************/
  380.  
  381. void StopVoiceProcess ()
  382. {
  383.     WORD wReturnValue;
  384.  
  385.     if (wReturnValue = vocStopVoice ())
  386.         DisplayErrorCode (wReturnValue);
  387. }
  388.  
  389. /****************************************************************************
  390.  
  391.     FUNCTION: PauseVoiceProcess ()
  392.  
  393.     PURPOSE: Pause current active voice process
  394.  
  395. ****************************************************************************/
  396.  
  397. void PauseVoiceProcess ()
  398. {
  399.     WORD wReturnValue;
  400.  
  401.     if (wReturnValue = vocPauseVoice ())
  402.         DisplayErrorCode (wReturnValue);
  403. }
  404.  
  405. /****************************************************************************
  406.  
  407.     FUNCTION: ContinueVoiceProcess ()
  408.  
  409.     PURPOSE: Continue previously paused voice process
  410.  
  411. ****************************************************************************/
  412.  
  413. void ContinueVoiceProcess ()
  414. {
  415.     WORD wReturnValue;
  416.  
  417.     if (wReturnValue = vocContinueVoice ())
  418.         DisplayErrorCode (wReturnValue);
  419. }
  420.  
  421. /****************************************************************************
  422.  
  423.     FUNCTION: DisplayErrorCode (WORD)
  424.  
  425.     PURPOSE: Display error code
  426.  
  427. ****************************************************************************/
  428.  
  429. void DisplayErrorCode (error)
  430. WORD error;
  431. {
  432.     char szTmpBuf[20];
  433.  
  434.     wsprintf (szTmpBuf, "Error code = %d", error);
  435.     MessageBeep (0);
  436.     MessageBox (NULL, szTmpBuf, "Error...", MB_ICONINFORMATION | MB_OK);
  437. }
  438.  
  439. /****************************************************************************
  440.  
  441.     FUNCTION: UpdateMenu (void)
  442.  
  443.     PURPOSE: Update status of menu items
  444.  
  445. ****************************************************************************/
  446.  
  447. void UpdateMenu ()
  448. {
  449.     if (wRecordStatus)
  450.     {
  451.             EnableMenuItem (GetMenu (hWnd), IDM_START, MF_DISABLED | MF_GRAYED);
  452.             EnableMenuItem (GetMenu (hWnd), IDM_STOP, MF_ENABLED);
  453.     }
  454.     else
  455.     {
  456.         if (wPlayStatus)
  457.         {
  458.             EnableMenuItem (GetMenu (hWnd), IDM_START, MF_DISABLED | MF_GRAYED);
  459.             EnableMenuItem (GetMenu (hWnd), IDM_STOP, MF_DISABLED | MF_GRAYED);
  460.         }
  461.         else
  462.         {
  463.             EnableMenuItem (GetMenu (hWnd), IDM_START, MF_ENABLED);
  464.             EnableMenuItem (GetMenu (hWnd), IDM_STOP, MF_DISABLED | MF_GRAYED);
  465.         }
  466.     }
  467. }
  468.  
  469. /****************************************************************************
  470.  
  471.     FUNCTION: DisplayRecordFlag (WORD)
  472.  
  473.     PURPOSE: Display status of recording process
  474.  
  475. ****************************************************************************/
  476.  
  477. void DisplayRecordFlag (rec_status)
  478. WORD rec_status;
  479. {
  480.     HDC hDC;
  481.     int x, y;
  482.     char szText[64];
  483.  
  484.     wsprintf (szText, "Voice Record Flag    = %04x    ", rec_status);
  485.     hDC = GetDC (hWnd);
  486.     x = GetDeviceCaps (hDC, LOGPIXELSX)*1.5;
  487.     y = GetDeviceCaps (hDC, LOGPIXELSY)*0.25;
  488.     TextOut (hDC, x, y, szText, strlen (szText));
  489.     ReleaseDC (hWnd, hDC);
  490. }
  491.  
  492. /****************************************************************************
  493.  
  494.     FUNCTION: DisplayPlayFlag (WORD)
  495.  
  496.     PURPOSE: Display status of playback process
  497.  
  498. ****************************************************************************/
  499.  
  500. void DisplayPlayFlag (play_status)
  501. WORD play_status;
  502. {
  503.     HDC hDC;
  504.     int x, y;
  505.     char szText[64];
  506.  
  507.     wsprintf (szText, "Voice Playback Flag = %04x    ", play_status);
  508.     hDC = GetDC (hWnd);
  509.     x = GetDeviceCaps (hDC, LOGPIXELSX)*1.5;
  510.     y = GetDeviceCaps (hDC, LOGPIXELSY)*0.5;
  511.     TextOut (hDC, x, y, szText, strlen (szText));
  512.     ReleaseDC (hWnd, hDC);
  513. }
  514.  
  515. /****************************************************************************
  516.  
  517.     FUNCTION: About (HWND, unsigned, WORD, LONG)
  518.  
  519.     PURPOSE:  Processes messages for "About" dialog box
  520.  
  521.     MESSAGES:
  522.  
  523.     WM_INITDIALOG - initialize dialog box
  524.     WM_COMMAND    - Input received
  525.  
  526.     COMMENTS:
  527.  
  528.     No initialization is needed for this particular dialog box, but TRUE
  529.     must be returned to Windows.
  530.  
  531.     Wait for user to click on "Ok" button, then close the dialog box.
  532.  
  533. ****************************************************************************/
  534.  
  535. BOOL FAR PASCAL About (hDlg, message, wParam, lParam)
  536. HWND hDlg;                                /* window handle of the dialog box */
  537. unsigned message;                         /* type of message                 */
  538. WORD wParam;                              /* message-specific information    */
  539. LONG lParam;
  540. {
  541.     HDC     hDC;
  542.     HDC     hMemoryDC;
  543.     char    szTmpBuf[128];
  544.     BITMAP  Bitmap;
  545.     HBITMAP hBmSBLogo;
  546.     HBITMAP hOldBitmap;
  547.     HANDLE  hMetaFile, hSBlogo;
  548.     PAINTSTRUCT ps;
  549.  
  550.     switch (message)
  551.     {
  552.         case WM_INITDIALOG:           /* message: initialize dialog box */
  553.             wsprintf (szTmpBuf, "Hardware Card: %s", lpCardName);
  554.             SetDlgItemText (hDlg, IDT_CARDNAME, szTmpBuf);
  555.             wsprintf (szTmpBuf, "DLL Version: %d.%02d", HIBYTE (wDLLVersion),
  556.                                                         LOBYTE (wDLLVersion));
  557.             SetDlgItemText (hDlg, IDT_DLLVERSION, szTmpBuf);
  558.             return (TRUE);
  559.  
  560.         case WM_COMMAND:                      /* message: received a command */
  561.             if (wParam == IDOK                /* "OK" box selected?          */
  562.                     || wParam == IDCANCEL)    /* System menu close command?  */
  563.             {
  564.                 EndDialog (hDlg, TRUE);       /* Exits the dialog box        */
  565.                 return (TRUE);
  566.             }
  567.             break;
  568.         case WM_PAINT:
  569.             hBmSBLogo = LoadBitmap (hInst, "sbct");
  570.             GetObject (hBmSBLogo, sizeof (BITMAP), (LPSTR) &Bitmap);
  571.             hDC = GetDC (hDlg);
  572.             hMemoryDC = CreateCompatibleDC (hDC);
  573.             hOldBitmap = SelectObject (hMemoryDC, hBmSBLogo);
  574.             if (hOldBitmap)
  575.             {
  576.                 BitBlt (hDC, 3, 3, Bitmap.bmWidth, Bitmap.bmHeight,
  577.                         hMemoryDC, 0, 0, SRCCOPY);
  578.                 SelectObject (hMemoryDC, hOldBitmap);
  579.             }
  580.             DeleteDC (hMemoryDC);
  581.             ReleaseDC (hDlg, hDC);
  582.             DeleteObject (hBmSBLogo);
  583.             break;
  584.     }
  585.     return (FALSE);                  /* Didn't process a message    */
  586. }
  587.  
  588.