home *** CD-ROM | disk | FTP | other *** search
/ The Developer Connection…ice Driver Kit for OS/2 3 / DEV3-D1.ISO / source / mmpm2src / doubplay / doubapp.c next >
Encoding:
C/C++ Source or Header  |  1993-10-18  |  8.8 KB  |  252 lines

  1. /*******************************************************************************
  2.  * File Name   : DOUPAPP.C
  3.  *
  4.  * Description : This file contains the C source code required for Play
  5.  *               thread processing of the Buffering Playlist sample program.
  6.  *               A separeate thread is created which is responsible for
  7.  *               Playing the wave file from the memory playlist.
  8.  *
  9.  *
  10.  * MMPM/2 API's: List of all MMPM/2 API's that are used in this module.
  11.  *
  12.  *               mciSendCommand
  13.  *                  MCI_PLAY
  14.  *                  MCI_SET
  15.  *               mmioOpen
  16.  *               mmioGetHeader
  17.  *               mmioRead
  18.  *               mmioSeek
  19.  *
  20.  *
  21.  * Copyright (C) IBM  1993
  22.  ******************************************************************************/
  23. #define INCL_DOS                    /* required to use Dos APIs.           */
  24. #define  INCL_OS2MM                 /* required for MCI and MMIO headers   */
  25. #define INCL_WINMESSAGEMGR          /* required to use Win APIs.           */
  26. #define INCL_WIN                    /* required to use Win APIs.           */
  27. #include <os2.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <os2me.h>
  32.  
  33. #include "doubapp.h"
  34. #include "doubplay.h"
  35. #define WAVE_FILE_NAME "mywave.wav"
  36.  
  37.  
  38. /*************************************************************************
  39.  * Name         : PlayTheWave
  40.  *
  41.  * Description : This function initially opens the wave file to be played
  42.  *               and tells the MCD information about the file
  43.  *               that is about to be played.  The Samples Per Second,
  44.  *               Bits Per Sample, and the number of channels with which
  45.  *               the waveform file was created has to be told to the MCD.
  46.  *               This function initially fills up the allocated buffer
  47.  *               of the playlist with the wave file.  For 0 - MAXBUFF
  48.  *               the wave file is filled into the memory.  The Wave file
  49.  *               is read in continously from 0 -MAXBUFF.  So the wave file
  50.  *               may be repeated several times in the allocated buffer.
  51.  *               This same buffer is dynamically re-filled again and again
  52.  *               when the application recieves the MCIPlAYLISTMESSAGE.
  53.  *
  54.  * Concepts     : The wave file is first opened and then continuously read
  55.  *                into the buffer.  If the end of the wave file is reached
  56.  *                then we seek to the starting of the wave and keep on reading
  57.  *                the wave file till the buffer fills up.  This function
  58.  *                is done by creating a separate thread where the MCI_PLAY is
  59.  *                issued.  This thread is only created when the user
  60.  *                presses the Play button. This displays the double buffering
  61.  *                concept because while the playlist is continuously looping
  62.  *                due to the BRANCH instruction and playing the wave file,
  63.  *                the buffer for the wave file is constantly being filled with
  64.  *                the data from the wave file in the MainDialogProc.
  65.  *
  66.  *
  67.  * MMPM/2 API's : mmioOpen
  68.  *                mmioGetHeader
  69.  *                mmioRead
  70.  *                mmioSeek
  71.  *                mciSendCommand
  72.  *                  MCI_SET
  73.  *                  MCI_CUE
  74.  *
  75.  * Parameters   : None.
  76.  *
  77.  * Return       : None.
  78.  *
  79.  *************************************************************************/
  80. void PlayTheWave()
  81. {
  82.  
  83.    MCI_WAVE_SET_PARMS    lpWaveSet;
  84.    ULONG                 ulReturn;
  85.    MCI_PLAY_PARMS        mciPlayParameters;
  86.    MMAUDIOHEADER         mmHeader;
  87.    int i;
  88.    long                  ulBytesRead;
  89.    CHAR                  achError[STRING_SIZE];
  90.  
  91. /* Globals */
  92.    extern PLAY_LIST_STRUCTURE_T aplayList[2*MAXBUFF+1];
  93.    extern char achDataBuffer[MAXBUFF][MAXSIZE];
  94.    extern HMMIO  hmmioFileHandle;
  95.    extern USHORT usWaveDeviceId;
  96.    extern HWND  hwndDiag;
  97.  
  98.    /* Open the Wave File MyWave.Wav for Reading */
  99.    hmmioFileHandle = mmioOpen(WAVE_FILE_NAME,
  100.                              (PMMIOINFO) NULL,
  101.                              MMIO_READ);
  102.    /* If the Wave File could not be opened */
  103.    if (!hmmioFileHandle)
  104.      {
  105.  
  106.       WinPostMsg(hwndDiag,
  107.                  WM_PLAYFAILED,
  108.                  MPFROMLONG((APIRET)(LONG)IDS_CANT_OPEN_WAVE),
  109.                  MPFROMLONG((LONG) FALSE) );
  110.       return;
  111.      }
  112.  
  113.    /*
  114.     * Get the Header Information for the file so that we can set the channels,
  115.     * Samples Per Second and Bits Per Sample to play the memory playlist.
  116.     */
  117.  
  118.    ulReturn = mmioGetHeader(hmmioFileHandle,
  119.                             (PVOID) &mmHeader,
  120.                             sizeof(MMAUDIOHEADER),
  121.                             (PLONG) &ulBytesRead,
  122.                             (ULONG) NULL,
  123.                             (ULONG) NULL);
  124.    if (ulReturn != 0 )
  125.      {
  126.       /* MCI Error, Send a message to the Dialog to display the Error. */
  127.       WinPostMsg(hwndDiag,
  128.                  WM_PLAYFAILED,
  129.                  MPFROMLONG((APIRET)(LONG)ulReturn),
  130.                  MPFROMLONG((LONG) TRUE) );
  131.      return;
  132.      }
  133.  
  134.    /* Set the WaveSet Structure */
  135.    memset(&lpWaveSet, 0, sizeof(lpWaveSet));
  136.    lpWaveSet.ulLevel = 100;
  137.    lpWaveSet.ulSamplesPerSec = mmHeader.mmXWAVHeader.WAVEHeader.ulSamplesPerSec;
  138.    lpWaveSet.usBitsPerSample = mmHeader.mmXWAVHeader.WAVEHeader.usBitsPerSample;
  139.    lpWaveSet.usChannels = mmHeader.mmXWAVHeader.WAVEHeader.usChannels;
  140.    lpWaveSet.ulAudio = MCI_SET_AUDIO_ALL;
  141.    lpWaveSet.hwndCallback = (HWND) NULL;
  142.  
  143.    /* Set the Channels for the MCD */
  144.    ulReturn
  145.         = mciSendCommand(usWaveDeviceId,
  146.                          MCI_SET,
  147.                          MCI_WAIT | MCI_WAVE_SET_CHANNELS,
  148.                          (PVOID) &lpWaveSet,
  149.                          (USHORT)NULL);
  150.    if (ulReturn != 0 )
  151.      {
  152.       /* MCI Error, Send a message to the Dialog to display the Error. */
  153.       WinPostMsg(hwndDiag,
  154.                  WM_PLAYFAILED,
  155.                  MPFROMLONG((APIRET)(LONG)ulReturn),
  156.                  MPFROMLONG((LONG) TRUE) );
  157.      return;
  158.      }
  159.    /* Set the Samples Per Second */
  160.    ulReturn
  161.         = mciSendCommand(usWaveDeviceId,
  162.                          MCI_SET,
  163.                          MCI_WAIT | MCI_WAVE_SET_SAMPLESPERSEC,
  164.                          (PVOID) &lpWaveSet,
  165.                          (USHORT)NULL);
  166.    if (ulReturn != 0 )
  167.      {
  168.       /* MCI Error, Send a message to the Dialog to display the Error. */
  169.       WinPostMsg(hwndDiag,
  170.                  WM_PLAYFAILED,
  171.                  MPFROMLONG((APIRET)(LONG)ulReturn),
  172.                  MPFROMLONG((LONG) TRUE) );
  173.      return;
  174.      }
  175.    /* Set the Bits per Sample */
  176.    ulReturn
  177.         = mciSendCommand(usWaveDeviceId,
  178.                          MCI_SET,
  179.                          MCI_WAIT | MCI_WAVE_SET_BITSPERSAMPLE,
  180.                          (PVOID) &lpWaveSet,
  181.                          (USHORT)NULL);
  182.    if (ulReturn != 0 )
  183.      {
  184.       /* MCI Error, Send a message to the Dialog to display the Error. */
  185.       WinPostMsg(hwndDiag,
  186.                  WM_PLAYFAILED,
  187.                  MPFROMLONG((APIRET)(LONG)ulReturn),
  188.                  MPFROMLONG((LONG) TRUE) );
  189.      return;
  190.      }
  191.  
  192.  
  193.    mciPlayParameters.hwndCallback =  hwndDiag;
  194.  
  195.   /* From 0 - MAXBUFF, fill the memory Playlist buffer with the wave file. */
  196.  
  197.    for (i=0; i<MAXBUFF; i++)
  198.    {
  199.  
  200.      ulBytesRead =
  201.            mmioRead(hmmioFileHandle,
  202.                     (HPSTR) achDataBuffer[i],
  203.                     MAXSIZE);
  204.      /*
  205.       * If the end of the wave file is reached then Seek to the starting
  206.       * of the wave file and start reading the wave into the appropriate
  207.       * buffer
  208.      */
  209.      if (ulBytesRead == 0)
  210.         {
  211.          mmioSeek(hmmioFileHandle, 0, SEEK_SET);
  212.  
  213.          ulBytesRead =
  214.            mmioRead(hmmioFileHandle,
  215.                     (HPSTR) achDataBuffer[i],
  216.                     MAXSIZE);
  217.         }
  218.  
  219.    }
  220.  
  221.    /* Send the Play Command to begin the playing of the memory playlist. */
  222.    ulReturn
  223.             = mciSendCommand(usWaveDeviceId,
  224.                              MCI_PLAY,
  225.                              MCI_NOTIFY,
  226.                              (PVOID) &mciPlayParameters,
  227.                              0);
  228.    if (ulReturn != 0 )
  229.      {
  230.       /* MCI Error, Send a message to the Dialog to display the Error. */
  231.       WinPostMsg(hwndDiag,
  232.                  WM_PLAYFAILED,
  233.                  MPFROMLONG((APIRET)(LONG)ulReturn),
  234.                  MPFROMLONG((LONG) TRUE) );
  235.      return;
  236.      }
  237.    else
  238.      {
  239.      /*
  240.       * Playing of the wave file has started so send a message telling the
  241.       * application to start animation and update the state
  242.       */
  243.      WinPostMsg(hwndDiag,
  244.                 WM_PLAYSTARTED,
  245.                 0,
  246.                 0);
  247.      }
  248.    DosExit(EXIT_THREAD, 0);
  249. }
  250.  
  251.  
  252.