home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / graphics / directx / dsstream / dstrwave.c < prev    next >
C/C++ Source or Header  |  1997-07-14  |  25KB  |  902 lines

  1. /*==========================================================================
  2.  *
  3.  *  Copyright (C) 1995-1997 Microsoft Corporation. All Rights Reserved.
  4.  *
  5.  *  File:               dstrwave.c
  6.  *  Content:    Wave library routines.
  7.  *
  8.  *      This file is used for loading/saving waves, and reading and
  9.  *      writing waves in smaller blocks.
  10.  *      Uses WaveOpenFile, WaveReadFile and WaveCloseReadFile for
  11.  *      single block access to reading wave files.
  12.  *      Uses WaveCreateFile, WaveWriteFile, WaveCloseWriteFile for
  13.  *      single block access for writing  wave files.
  14.  *      Uses WaveLoadFile to load a whole wave file into memory.
  15.  *      Uses WaveSaveFile to save a whole wave file from memory.
  16.  *
  17.  ***************************************************************************/
  18.  
  19. /* PROTOTYPES */
  20. #include <windows.h>
  21. #include <windowsx.h>
  22. #include <mmsystem.h>
  23. #include "dsstream.h"
  24. #include "wassert.h"
  25.  
  26.  
  27. /* ROUTINES */
  28. /* -------------------------------------------------------*/
  29.  
  30. /* This function will open a wave input file and prepare it for reading,
  31.  * so the data can be easily
  32.  * read with WaveReadFile. Returns 0 if successful, the error code if not.
  33.  *      pszFileName - Input filename to load.
  34.  *      phmmioIn    - Pointer to handle which will be used
  35.  *          for further mmio routines.
  36.  *      ppwfxInfo   - Ptr to ptr to WaveFormatEx structure
  37.  *          with all info about the file.                        
  38.  *      
  39. */
  40. int WaveOpenFile(
  41.     char *pszFileName,                              // (IN)
  42.     HMMIO *phmmioIn,                                // (OUT)
  43.     WAVEFORMATEX **ppwfxInfo,                       // (OUT)
  44.     MMCKINFO *pckInRIFF                             // (OUT)
  45.             )
  46. {
  47.     HMMIO           hmmioIn;
  48.     MMCKINFO        ckIn;           // chunk info. for general use.
  49.     PCMWAVEFORMAT   pcmWaveFormat;  // Temp PCM structure to load in.       
  50.     WORD            cbExtraAlloc;   // Extra bytes for waveformatex 
  51.     int             nError;         // Return value.
  52.  
  53.  
  54.     // Initialization...
  55.     *ppwfxInfo = NULL;
  56.     nError = 0;
  57.     hmmioIn = NULL;
  58.     
  59.     if ((hmmioIn = mmioOpen(pszFileName, NULL, MMIO_ALLOCBUF | MMIO_READ)) == NULL)
  60.         {
  61.         nError = ER_CANNOTOPEN;
  62.         goto ERROR_READING_WAVE;
  63.         }
  64.  
  65.     if ((nError = (int)mmioDescend(hmmioIn, pckInRIFF, NULL, 0)) != 0)
  66.         {
  67.         goto ERROR_READING_WAVE;
  68.         }
  69.  
  70.  
  71.     if ((pckInRIFF->ckid != FOURCC_RIFF) || (pckInRIFF->fccType != mmioFOURCC('W', 'A', 'V', 'E')))
  72.         {
  73.         nError = ER_NOTWAVEFILE;
  74.         goto ERROR_READING_WAVE;
  75.         }
  76.             
  77.     /* Search the input file for for the 'fmt ' chunk.     */
  78.     ckIn.ckid = mmioFOURCC('f', 'm', 't', ' ');
  79.     if ((nError = (int)mmioDescend(hmmioIn, &ckIn, pckInRIFF, MMIO_FINDCHUNK)) != 0)
  80.         {
  81.         goto ERROR_READING_WAVE;                
  82.         }
  83.                     
  84.     /* Expect the 'fmt' chunk to be at least as large as <PCMWAVEFORMAT>;
  85.     * if there are extra parameters at the end, we'll ignore them */
  86.     
  87.     if (ckIn.cksize < (long) sizeof(PCMWAVEFORMAT))
  88.         {
  89.         nError = ER_NOTWAVEFILE;
  90.         goto ERROR_READING_WAVE;
  91.         }
  92.                                                             
  93.     /* Read the 'fmt ' chunk into <pcmWaveFormat>.*/     
  94.     if (mmioRead(hmmioIn, (HPSTR) &pcmWaveFormat, (long) sizeof(pcmWaveFormat)) != (long) sizeof(pcmWaveFormat))
  95.         {
  96.         nError = ER_CANNOTREAD;
  97.         goto ERROR_READING_WAVE;
  98.         }
  99.                             
  100.  
  101.     // Ok, allocate the waveformatex, but if its not pcm
  102.     // format, read the next word, and thats how many extra
  103.     // bytes to allocate.
  104.     if (pcmWaveFormat.wf.wFormatTag == WAVE_FORMAT_PCM)
  105.         cbExtraAlloc = 0;                               
  106.                             
  107.     else
  108.         {
  109.         // Read in length of extra bytes.
  110.         if (mmioRead(hmmioIn, (LPSTR) &cbExtraAlloc,
  111.             (long) sizeof(cbExtraAlloc)) != (long) sizeof(cbExtraAlloc))
  112.             {
  113.             nError = ER_CANNOTREAD;
  114.             goto ERROR_READING_WAVE;
  115.             }
  116.  
  117.         }
  118.                             
  119.     // Ok, now allocate that waveformatex structure.
  120.     if ((*ppwfxInfo = GlobalAlloc(GMEM_FIXED, sizeof(WAVEFORMATEX)+cbExtraAlloc)) == NULL)
  121.         {
  122.         nError = ER_MEM;
  123.         goto ERROR_READING_WAVE;
  124.         }
  125.  
  126.     // Copy the bytes from the pcm structure to the waveformatex structure
  127.     memcpy(*ppwfxInfo, &pcmWaveFormat, sizeof(pcmWaveFormat));
  128.     (*ppwfxInfo)->cbSize = cbExtraAlloc;
  129.  
  130.     // Now, read those extra bytes into the structure, if cbExtraAlloc != 0.
  131.     if (cbExtraAlloc != 0)
  132.         {
  133.         if (mmioRead(hmmioIn, (LPSTR) (((BYTE*)&((*ppwfxInfo)->cbSize))+sizeof(cbExtraAlloc)),
  134.             (long) (cbExtraAlloc)) != (long) (cbExtraAlloc))
  135.             {
  136.             nError = ER_NOTWAVEFILE;
  137.             goto ERROR_READING_WAVE;
  138.             }
  139.         }
  140.  
  141.     /* Ascend the input file out of the 'fmt ' chunk. */                                                            
  142.     if ((nError = mmioAscend(hmmioIn, &ckIn, 0)) != 0)
  143.         {
  144.         goto ERROR_READING_WAVE;
  145.  
  146.         }
  147.     
  148.  
  149.     goto TEMPCLEANUP;               
  150.  
  151. ERROR_READING_WAVE:
  152.     if (*ppwfxInfo != NULL)
  153.         {
  154.         GlobalFree(*ppwfxInfo);
  155.         *ppwfxInfo = NULL;
  156.         }               
  157.  
  158.     if (hmmioIn != NULL)
  159.     {
  160.     mmioClose(hmmioIn, 0);
  161.         hmmioIn = NULL;
  162.         }
  163.     
  164. TEMPCLEANUP:
  165.     *phmmioIn = hmmioIn;
  166.  
  167.     return(nError);
  168.  
  169. }
  170.  
  171. /*      This routine has to be called before WaveReadFile as it searchs for the chunk to descend into for
  172.     reading, that is, the 'data' chunk.  For simplicity, this used to be in the open routine, but was
  173.     taken out and moved to a separate routine so there was more control on the chunks that are before
  174.     the data chunk, such as 'fact', etc... */
  175.  
  176. int WaveStartDataRead(
  177.                     HMMIO *phmmioIn,
  178.                     MMCKINFO *pckIn,
  179.                     MMCKINFO *pckInRIFF
  180.                     )
  181. {
  182.     int                     nError;
  183.  
  184.     nError = 0;
  185.     
  186.     // Do a nice little seek...
  187.     if ((nError = mmioSeek(*phmmioIn, pckInRIFF->dwDataOffset + sizeof(FOURCC), SEEK_SET)) == -1)
  188.         {
  189.         Assert(FALSE);
  190.         }
  191.  
  192.     nError = 0;
  193.     //      Search the input file for for the 'data' chunk.
  194.     pckIn->ckid = mmioFOURCC('d', 'a', 't', 'a');
  195.     if ((nError = mmioDescend(*phmmioIn, pckIn, pckInRIFF, MMIO_FINDCHUNK)) != 0)
  196.         {
  197.         goto ERROR_READING_WAVE;
  198.         }
  199.  
  200.     goto CLEANUP;
  201.  
  202. ERROR_READING_WAVE:
  203.  
  204. CLEANUP:        
  205.     return(nError);
  206. }
  207.  
  208.  
  209. /*      This will read wave data from the wave file.  Makre sure we're descended into
  210.     the data chunk, else this will fail bigtime!
  211.     hmmioIn         - Handle to mmio.
  212.     cbRead          - # of bytes to read.   
  213.     pbDest          - Destination buffer to put bytes.
  214.     cbActualRead- # of bytes actually read.
  215.  
  216.         
  217.  
  218. */
  219.  
  220.  
  221. int WaveReadFile(
  222.         HMMIO hmmioIn,                          // IN
  223.         UINT cbRead,                            // IN           
  224.         BYTE *pbDest,                           // IN
  225.         MMCKINFO *pckIn,                        // IN.
  226.         UINT *cbActualRead                      // OUT.
  227.         
  228.         )
  229. {
  230.  
  231.     MMIOINFO    mmioinfoIn;         // current status of <hmmioIn>
  232.     int                     nError;
  233.     UINT                    cT, cbDataIn, uCopyLength;
  234.  
  235.     nError = 0;
  236.  
  237.     if (nError = mmioGetInfo(hmmioIn, &mmioinfoIn, 0) != 0)
  238.         {
  239.         goto ERROR_CANNOT_READ;
  240.         }
  241.                 
  242.     cbDataIn = cbRead;
  243.     if (cbDataIn > pckIn->cksize) 
  244.         cbDataIn = pckIn->cksize;       
  245.  
  246.     pckIn->cksize -= cbDataIn;
  247.     
  248.     for (cT = 0; cT < cbDataIn; )
  249.         {
  250.         /* Copy the bytes from the io to the buffer. */
  251.         if (mmioinfoIn.pchNext == mmioinfoIn.pchEndRead)
  252.             {
  253.         if ((nError = mmioAdvance(hmmioIn, &mmioinfoIn, MMIO_READ)) != 0)
  254.                 {
  255.         goto ERROR_CANNOT_READ;
  256.                 } 
  257.         if (mmioinfoIn.pchNext == mmioinfoIn.pchEndRead)
  258.                 {
  259.                 nError = ER_CORRUPTWAVEFILE;
  260.         goto ERROR_CANNOT_READ;
  261.                 }
  262.             }
  263.             
  264.         // Actual copy.
  265.         uCopyLength = (UINT)(mmioinfoIn.pchEndRead - mmioinfoIn.pchNext);
  266.         if((cbDataIn - cT) < uCopyLength )
  267.             uCopyLength = cbDataIn - cT;
  268.         memcpy( (BYTE*)(pbDest+cT), (BYTE*)mmioinfoIn.pchNext, uCopyLength );
  269.         cT += uCopyLength;
  270.         mmioinfoIn.pchNext += uCopyLength;
  271.         }
  272.  
  273.     if ((nError = mmioSetInfo(hmmioIn, &mmioinfoIn, 0)) != 0)
  274.         {
  275.         goto ERROR_CANNOT_READ;
  276.         }
  277.  
  278.     *cbActualRead = cbDataIn;
  279.     goto FINISHED_READING;
  280.  
  281. ERROR_CANNOT_READ:
  282.     *cbActualRead = 0;
  283.  
  284. FINISHED_READING:
  285.     return(nError);
  286.  
  287. }
  288.  
  289. /*      This will close the wave file openned with WaveOpenFile.  
  290.     phmmioIn - Pointer to the handle to input MMIO.
  291.     ppwfxSrc - Pointer to pointer to WaveFormatEx structure.
  292.  
  293.     Returns 0 if successful, non-zero if there was a warning.
  294.  
  295. */
  296. int WaveCloseReadFile(
  297.             HMMIO *phmmio,                                  // IN
  298.             WAVEFORMATEX **ppwfxSrc                 // IN
  299.             )
  300. {
  301.  
  302.     if (*ppwfxSrc != NULL)
  303.         {
  304.         GlobalFree(*ppwfxSrc);
  305.         *ppwfxSrc = NULL;
  306.         }
  307.  
  308.     if (*phmmio != NULL)
  309.         {
  310.         mmioClose(*phmmio, 0);
  311.         *phmmio = NULL;
  312.         }
  313.  
  314.     return(0);
  315.  
  316. }
  317.  
  318. /*      This routine will create a wave file for writing.  This will automatically overwrite any
  319.     existing file with the same name, so be careful and check before hand!!!
  320.     pszFileName     - Pointer to filename to write.
  321.     phmmioOut               - Pointer to HMMIO handle that is used for further writes
  322.     pwfxDest                - Valid waveformatex destination structure.
  323.     pckOut                  - Pointer to be set with the MMCKINFO.
  324.     pckOutRIFF              - Pointer to be set with the RIFF info.
  325.  
  326. */
  327. int WaveCreateFile(
  328.             char *pszFileName,                              // (IN)
  329.             HMMIO *phmmioOut,                               // (OUT)
  330.             WAVEFORMATEX *pwfxDest,                 // (IN)
  331.             MMCKINFO *pckOut,                               // (OUT)
  332.             MMCKINFO *pckOutRIFF                    // (OUT)
  333.  
  334.             )
  335. {
  336.         
  337.     int             nError;                         // Return value.
  338.     DWORD           dwFactChunk;            // Contains the actual fact chunk. Garbage until WaveCloseWriteFile.
  339.     MMCKINFO        ckOut1;
  340.  
  341.     dwFactChunk = (DWORD)-1;
  342.     nError = 0;
  343.  
  344.     *phmmioOut = mmioOpen(pszFileName, NULL,
  345.             MMIO_ALLOCBUF | MMIO_READWRITE|MMIO_CREATE);
  346.  
  347.     if (*phmmioOut == NULL)
  348.         {
  349.         nError = ER_CANNOTWRITE;
  350.     goto ERROR_CANNOT_WRITE;    // cannot save WAVE file
  351.         }
  352.  
  353.     /* Create the output file RIFF chunk of form type 'WAVE'.
  354.      */
  355.     pckOutRIFF->fccType = mmioFOURCC('W', 'A', 'V', 'E');       
  356.     pckOutRIFF->cksize = 0; 
  357.     if ((nError = mmioCreateChunk(*phmmioOut, pckOutRIFF, MMIO_CREATERIFF)) != 0)
  358.         {
  359.     goto ERROR_CANNOT_WRITE;    // cannot write file, probably
  360.         }
  361.  
  362.     /* We are now descended into the 'RIFF' chunk we just created.
  363.      * Now create the 'fmt ' chunk. Since we know the size of this chunk,
  364.      * specify it in the MMCKINFO structure so MMIO doesn't have to seek
  365.      * back and set the chunk size after ascending from the chunk.
  366.      */
  367.     pckOut->ckid = mmioFOURCC('f', 'm', 't', ' ');
  368.     pckOut->cksize = sizeof(PCMWAVEFORMAT);   // we know the size of this ck.
  369.     if ((nError = mmioCreateChunk(*phmmioOut, pckOut, 0)) != 0)
  370.         {
  371.     goto ERROR_CANNOT_WRITE;    // cannot write file, probably
  372.         }
  373.  
  374.     /* Write the PCMWAVEFORMAT structure to the 'fmt ' chunk if its that type. */
  375.     if (pwfxDest->wFormatTag == WAVE_FORMAT_PCM)
  376.         {
  377.         if (mmioWrite(*phmmioOut, (HPSTR) pwfxDest, sizeof(PCMWAVEFORMAT))
  378.         != sizeof(PCMWAVEFORMAT))
  379.             {
  380.             nError = ER_CANNOTWRITE;
  381.         goto ERROR_CANNOT_WRITE;    // cannot write file, probably
  382.             }
  383.         }
  384.     
  385.     else 
  386.         {
  387.         // Write the variable length size.
  388.         if ((UINT)mmioWrite(*phmmioOut, (HPSTR) pwfxDest, sizeof(*pwfxDest)+pwfxDest->cbSize)
  389.         != (sizeof(*pwfxDest)+pwfxDest->cbSize))
  390.             {
  391.             nError = ER_CANNOTWRITE;
  392.         goto ERROR_CANNOT_WRITE;    // cannot write file, probably
  393.             } 
  394.  
  395.         }  
  396.  
  397.     /* Ascend out of the 'fmt ' chunk, back into the 'RIFF' chunk.
  398.      */
  399.     if ((nError = mmioAscend(*phmmioOut, pckOut, 0)) != 0)
  400.         {
  401.     goto ERROR_CANNOT_WRITE;    // cannot write file, probably
  402.         }
  403.  
  404.     // Now create the fact chunk, not required for PCM but nice to have.  This is filled
  405.     // in when the close routine is called.
  406.     ckOut1.ckid = mmioFOURCC('f', 'a', 'c', 't');
  407.     ckOut1.cksize = 0;
  408.     if ((nError = mmioCreateChunk(*phmmioOut, &ckOut1, 0)) != 0)
  409.         {
  410.     goto ERROR_CANNOT_WRITE;    // cannot write file, probably
  411.         }
  412.  
  413.     if (mmioWrite(*phmmioOut, (HPSTR)&dwFactChunk, sizeof(dwFactChunk)) != sizeof(dwFactChunk))
  414.         {
  415.         nError = ER_CANNOTWRITE;
  416.         goto ERROR_CANNOT_WRITE;
  417.         }
  418.  
  419.     // Now ascend out of the fact chunk...
  420.     if ((nError = mmioAscend(*phmmioOut, &ckOut1, 0)) != 0)
  421.         {
  422.     nError = ER_CANNOTWRITE;        // cannot write file, probably
  423.         goto ERROR_CANNOT_WRITE;
  424.         }
  425.  
  426.      
  427.    
  428.     goto DONE_CREATE;
  429.  
  430. ERROR_CANNOT_WRITE:
  431.     // Maybe delete the half-written file?  Ah forget it for now, its good to leave the
  432.     // file there for debugging...
  433.  
  434. DONE_CREATE:
  435.     return(nError);
  436.  
  437. }
  438.  
  439. /*      This routine has to be called before any data is written to the wave output file, via wavewritefile.  This
  440.     sets up the data to write, and creates the data chunk.
  441. */
  442.  
  443. int WaveStartDataWrite(
  444.                 HMMIO *phmmioOut,                               // (IN)
  445.                 MMCKINFO *pckOut,                               // (IN)
  446.                 MMIOINFO *pmmioinfoOut                  // (OUT)
  447.                 )
  448. {
  449.  
  450.     int             nError;
  451.  
  452.     nError = 0;
  453.  /* Create the 'data' chunk that holds the waveform samples.  */
  454.     pckOut->ckid = mmioFOURCC('d', 'a', 't', 'a');
  455.     pckOut->cksize = 0;
  456.     if ((nError = mmioCreateChunk(*phmmioOut, pckOut, 0)) != 0)
  457.         {
  458.     goto ERROR_CANNOT_WRITE;    // cannot write file, probably
  459.         }
  460.  
  461.     if ((nError = mmioGetInfo(*phmmioOut, pmmioinfoOut, 0)) != 0)
  462.         {
  463.     goto ERROR_CANNOT_WRITE;
  464.     }
  465.  
  466.     goto CLEANUP;
  467. ERROR_CANNOT_WRITE:     
  468.  
  469. CLEANUP:
  470.     return(nError);
  471. }
  472.  
  473. /* This routine will write out data to a wave file. 
  474.     hmmioOut                - Handle to hmmioOut filled by WaveCreateFile
  475.     cbWrite                 - # of bytes to write out.
  476.     pbSrc                   - Pointer to source.
  477.     pckOut                  - pointer to ckOut filled by WaveCreateFile
  478.     cbActualWrite   - # of actual bytes written.
  479.     pmmioinfoOut    - Pointer to mmioinfoOut filled by WaveCreateFile.
  480.  
  481.     Returns 0 if successful, else the error code.
  482.  
  483.  */
  484.  
  485.  
  486. int WaveWriteFile(
  487.         HMMIO hmmioOut,                         // (IN)
  488.         UINT cbWrite,                           // (IN)
  489.         BYTE *pbSrc,                            // (IN)
  490.         MMCKINFO *pckOut,                       // (IN)
  491.         UINT *cbActualWrite,            // (OUT)
  492.         MMIOINFO *pmmioinfoOut          // (IN)
  493.         )
  494. {
  495.         
  496.     
  497.     int                     nError;
  498.     UINT            cT;
  499.  
  500.     nError = 0;
  501.     
  502.     *cbActualWrite = 0;
  503.  
  504.     for (cT=0; cT < cbWrite; cT++)
  505.         {       
  506.         if (pmmioinfoOut->pchNext == pmmioinfoOut->pchEndWrite)
  507.         {
  508.         pmmioinfoOut->dwFlags |= MMIO_DIRTY;
  509.         if ((nError = mmioAdvance(hmmioOut, pmmioinfoOut, MMIO_WRITE)) != 0)
  510.                 {
  511.             goto ERROR_CANNOT_WRITE;
  512.                 }
  513.         }
  514.         *((BYTE*)pmmioinfoOut->pchNext)++ = *((BYTE*)pbSrc+cT);
  515.         (*cbActualWrite)++;
  516.         }
  517.     
  518.     
  519. ERROR_CANNOT_WRITE:
  520.     // What to do here?  Well, for now, nothing, just return that error.  (maybe delete the
  521.     // file later?
  522.  
  523.     return(nError);
  524.  
  525. }
  526.  
  527.  
  528.  
  529. /*      This routine will close a wave file used for writing.  Returns 0 if successful, else
  530.     the error code.
  531.     phmmioOut       - Pointer to mmio handle for saving.
  532.     pckOut          - Pointer to the MMCKINFO for saving.
  533.     pckOutRiff      - Pointer to the riff MMCKINFO for saving.
  534.     pmmioinfoOut- Pointer to mmioinfo for saving.
  535.     cSamples        - # of samples saved, for the fact chunk.  For PCM, this isn't used but
  536.                   will be written anyway, so this can be zero as long as programs ignore
  537.                   this field when they load PCM formats.
  538.  
  539.  
  540.  
  541. */
  542. int WaveCloseWriteFile(
  543.             HMMIO *phmmioOut,               // (IN)
  544.             MMCKINFO *pckOut,               // (IN)
  545.             MMCKINFO *pckOutRIFF,   // (IN)
  546.             MMIOINFO *pmmioinfoOut, // (IN)
  547.             DWORD cSamples                  // (IN)
  548.             )
  549. {
  550.         
  551.     int                     nError;                         
  552.                                 
  553.     nError = 0;
  554.  
  555.     if (*phmmioOut == NULL)
  556.         return(0);
  557.  
  558.     pmmioinfoOut->dwFlags |= MMIO_DIRTY;
  559.     if ((nError = mmioSetInfo(*phmmioOut, pmmioinfoOut, 0)) != 0)
  560.         {
  561.         // cannot flush, probably...
  562.     goto ERROR_CANNOT_WRITE;                                
  563.         }
  564.  
  565.      /* Ascend the output file out of the 'data' chunk -- this will cause
  566.      * the chunk size of the 'data' chunk to be written.
  567.      */
  568.     if ((nError = mmioAscend(*phmmioOut, pckOut, 0)) != 0)
  569.     goto ERROR_CANNOT_WRITE;    // cannot write file, probably
  570.  
  571.  
  572.     // Do this here instead...
  573.     if ((nError = mmioAscend(*phmmioOut, pckOutRIFF, 0)) != 0)
  574.     goto ERROR_CANNOT_WRITE;    // cannot write file, probably
  575.  
  576.  
  577.     nError = mmioSeek(*phmmioOut, 0, SEEK_SET);
  578.     if ((nError = (int)mmioDescend(*phmmioOut, pckOutRIFF, NULL, 0)) != 0)
  579.         {
  580.         goto ERROR_CANNOT_WRITE;
  581.         }
  582.  
  583.     nError = 0;
  584.     pckOut->ckid = mmioFOURCC('f', 'a', 'c', 't');
  585.     if ((nError = mmioDescend(*phmmioOut, pckOut, pckOutRIFF, MMIO_FINDCHUNK)) == 0)
  586.         {
  587.         // If it didn't fail, write the fact chunk out, if it failed, not critical, just
  588.         // assert (below).
  589.         nError = mmioWrite(*phmmioOut, (HPSTR)&cSamples, sizeof(DWORD));
  590.         nError = mmioAscend(*phmmioOut, pckOut, 0); 
  591.         nError = 0;
  592.         }
  593.     else 
  594.         {
  595.         nError = 0;
  596.         Assert(FALSE);
  597.         }
  598.  
  599. // CANTWRITEFACT:
  600.     /* Ascend the output file out of the 'RIFF' chunk -- this will cause
  601.      * the chunk size of the 'RIFF' chunk to be written.
  602.      */
  603.     if ((nError = mmioAscend(*phmmioOut, pckOutRIFF, 0)) != 0)
  604.     goto ERROR_CANNOT_WRITE;    // cannot write file, probably
  605.     
  606.     
  607.  
  608. ERROR_CANNOT_WRITE:
  609.     if (*phmmioOut != NULL)
  610.         {
  611.     mmioClose(*phmmioOut, 0);
  612.         *phmmioOut = NULL;
  613.         }
  614.  
  615.     return(nError);
  616.  
  617. }
  618.  
  619. /*      This routine will copy from a source wave file to a destination wave file all those useless chunks
  620.     (well, the ones useless to conversions, etc --> apparently people use them!).  The source will be
  621.     seeked to the begining, but the destination has to be at a current pointer to put the new chunks.
  622.     This will also seek     back to the start of the wave riff header at the end of the routine.
  623.  
  624.     phmmioIn                - Pointer to input mmio file handle.
  625.     pckIn                   - Pointer to a nice ckIn to use.
  626.     pckInRiff               - Pointer to the main riff.
  627.     phmmioOut               - Pointer to output mmio file handle.
  628.     pckOut                  - Pointer to nice ckOut to use.
  629.     pckOutRiff              - Pointer to the main riff.
  630.  
  631.  
  632.     Returns 0 if successful, else the error code.  If this routine fails, it still attemps to seek back to
  633.     the start of the wave riff header, though this too could be unsuccessful.
  634. */
  635.  
  636. int WaveCopyUselessChunks(
  637.                     HMMIO *phmmioIn, 
  638.                     MMCKINFO *pckIn, 
  639.                     MMCKINFO *pckInRiff, 
  640.                     HMMIO *phmmioOut, 
  641.                     MMCKINFO *pckOut, 
  642.                     MMCKINFO *pckOutRiff)
  643. {
  644.  
  645.     int                             nError;
  646.  
  647.     nError = 0;
  648.     // First seek to the stinking start of the file, not including the riff header...
  649.     if ((nError = mmioSeek(*phmmioIn, pckInRiff->dwDataOffset + sizeof(FOURCC), SEEK_SET)) == -1)
  650.         {
  651.         nError = ER_CANNOTREAD;
  652.         goto ERROR_IN_PROC;
  653.         }
  654.  
  655.     nError = 0;                     
  656.  
  657.     while (mmioDescend(*phmmioIn, pckIn, pckInRiff, 0) == 0)
  658.     {
  659.     
  660.     //  quickly check for corrupt RIFF file--don't ascend past end!        
  661.     if ((pckIn->dwDataOffset + pckIn->cksize) > (pckInRiff->dwDataOffset + pckInRiff->cksize))
  662.         goto ERROR_IN_PROC;
  663.  
  664.     switch (pckIn->ckid)
  665.     {                   
  666.         //  explicitly skip these...            
  667.         case mmioFOURCC('f', 'm', 't', ' '):
  668.         break;
  669.  
  670.         case mmioFOURCC('d', 'a', 't', 'a'):
  671.         break;
  672.  
  673.         case mmioFOURCC('f', 'a', 'c', 't'):
  674.         break;
  675.  
  676.         case mmioFOURCC('J', 'U', 'N', 'K'):
  677.         break;
  678.  
  679.         case mmioFOURCC('P', 'A', 'D', ' '):
  680.         break;
  681.  
  682.         case mmioFOURCC('c', 'u', 'e', ' '):
  683.         break;                                                  
  684.         
  685.         //  copy chunks that are OK to copy            
  686.         case mmioFOURCC('p', 'l', 's', 't'):
  687.         // although without the 'cue' chunk, it doesn't make much sense
  688.         riffCopyChunk(*phmmioIn, *phmmioOut, pckIn);
  689.         break;
  690.  
  691.         case mmioFOURCC('D', 'I', 'S', 'P'):
  692.         riffCopyChunk(*phmmioIn, *phmmioOut, pckIn);
  693.         break;
  694.  
  695.         
  696.         //  don't copy unknown chunks
  697.         default:
  698.         break;
  699.     }
  700.  
  701.     
  702.     //  step up to prepare for next chunk..        
  703.     mmioAscend(*phmmioIn, pckIn, 0);
  704.     }
  705.  
  706. ERROR_IN_PROC:  
  707.     {
  708.     int nErrorT;
  709.     // Seek back to riff header     
  710.     nErrorT = mmioSeek(*phmmioIn, pckInRiff->dwDataOffset + sizeof(FOURCC), SEEK_SET);
  711.     }
  712.  
  713.     return(nError);
  714. }
  715.  
  716. /** BOOL RIFFAPI riffCopyChunk(HMMIO hmmioSrc, HMMIO hmmioDst, const LPMMCKINFO lpck)
  717.  *
  718.  *  DESCRIPTION:
  719.  *      
  720.  *
  721.  *  ARGUMENTS:
  722.  *      (LPWAVECONVCB lpwc, LPMMCKINFO lpck)
  723.  *
  724.  *  RETURN (BOOL NEAR PASCAL):
  725.  *
  726.  *
  727.  *  NOTES:
  728.  *
  729.  **  */
  730.  
  731. BOOL riffCopyChunk(HMMIO hmmioSrc, HMMIO hmmioDst, const LPMMCKINFO lpck)
  732. {
  733.     MMCKINFO    ck;
  734.     HPSTR       hpBuf;
  735.  
  736.     //
  737.     //
  738.     //
  739.     hpBuf = (HPSTR)GlobalAllocPtr(GHND, lpck->cksize);
  740.     if (!hpBuf)
  741.     return (FALSE);
  742.  
  743.     ck.ckid   = lpck->ckid;
  744.     ck.cksize = lpck->cksize;
  745.     if (mmioCreateChunk(hmmioDst, &ck, 0))
  746.     goto rscc_Error;
  747.     
  748.     if (mmioRead(hmmioSrc, hpBuf, lpck->cksize) != (LONG)lpck->cksize)
  749.     goto rscc_Error;
  750.  
  751.     if (mmioWrite(hmmioDst, hpBuf, lpck->cksize) != (LONG)lpck->cksize)
  752.     goto rscc_Error;
  753.  
  754.     if (mmioAscend(hmmioDst, &ck, 0))
  755.     goto rscc_Error;
  756.  
  757.     if (hpBuf)
  758.     GlobalFreePtr(hpBuf);
  759.  
  760.     return (TRUE);
  761.  
  762. rscc_Error:
  763.  
  764.     if (hpBuf)
  765.     GlobalFreePtr(hpBuf);
  766.  
  767.     return (FALSE);
  768. } /* RIFFSupCopyChunk() */
  769.  
  770.  
  771.  
  772. /*      This routine loads a full wave file into memory.  Be careful, wave files can get
  773.     pretty big these days :).  
  774.     szFileName      -       sz Filename
  775.     cbSize          -       Size of loaded wave (returned)
  776.     cSamples        -       # of samples loaded.
  777.     ppwfxInfo       -       Pointer to pointer to waveformatex structure.  The wfx structure
  778.                     IS ALLOCATED by this routine!  Make sure to free it!
  779.     ppbData         -       Pointer to a byte pointer (globalalloc) which is allocated by this 
  780.                     routine.  Make sure to free it!
  781.  
  782.     Returns 0 if successful, else the error code.
  783. */
  784.  
  785. int WaveLoadFile(
  786.             char *pszFileName,                      // (IN)
  787.             UINT *cbSize,                           // (OUT)
  788.             DWORD *pcSamples,                       // (OUT)
  789.             WAVEFORMATEX **ppwfxInfo,       // (OUT)
  790.             BYTE **ppbData                          // (OUT)
  791.             )
  792. {
  793.  
  794.     HMMIO                           hmmioIn;        
  795.     MMCKINFO                        ckInRiff;
  796.     MMCKINFO                        ckIn;
  797.     int                                     nError;
  798.     UINT                            cbActualRead;
  799.  
  800.     *ppbData = NULL;
  801.     *ppwfxInfo = NULL;
  802.     *cbSize = 0;
  803.     
  804.     if ((nError = WaveOpenFile(pszFileName, &hmmioIn, ppwfxInfo, &ckInRiff)) != 0)
  805.         {
  806.         goto ERROR_LOADING;
  807.         }
  808.  
  809.     if ((nError = WaveStartDataRead(&hmmioIn, &ckIn, &ckInRiff)) != 0)
  810.         {
  811.         goto ERROR_LOADING;
  812.         }
  813.  
  814.     // Ok, size of wave data is in ckIn, allocate that buffer.
  815.     if ((*ppbData = (BYTE *)GlobalAlloc(GMEM_FIXED, ckIn.cksize)) == NULL)
  816.         {
  817.         nError = ER_MEM;
  818.         goto ERROR_LOADING;
  819.         }
  820.  
  821.     if ((nError = WaveReadFile(hmmioIn, ckIn.cksize, *ppbData, &ckIn, &cbActualRead)) != 0)
  822.         {
  823.         goto ERROR_LOADING;
  824.         }        
  825.     
  826.     *cbSize = cbActualRead;
  827.     goto DONE_LOADING;
  828.  
  829. ERROR_LOADING:
  830.     if (*ppbData != NULL)
  831.         {
  832.         GlobalFree(*ppbData);
  833.         *ppbData = NULL;
  834.         }
  835.     if (*ppwfxInfo != NULL)
  836.         {
  837.         GlobalFree(*ppwfxInfo);
  838.         *ppwfxInfo = NULL;
  839.         }
  840.             
  841. DONE_LOADING:
  842.     // Close the wave file. 
  843.     if (hmmioIn != NULL)
  844.         {
  845.         mmioClose(hmmioIn, 0);
  846.         hmmioIn = NULL;
  847.         }
  848.  
  849.     return(nError);
  850.  
  851. }
  852.  
  853. /*      This routine saves a wave file in currently in memory.
  854.     pszFileName -   FileName to save to.  Automatically overwritten, be careful!
  855.     cbSize          -       Size in bytes to write.
  856.     cSamples        -       # of samples to write, used to make the fact chunk. (if !PCM)
  857.     pwfxDest        -       Pointer to waveformatex structure.
  858.     pbData          -       Pointer to the data.
  859. */      
  860.  
  861. int WaveSaveFile(
  862.                 char *pszFileName,                      // (IN)
  863.                 UINT cbSize,                            // (IN)
  864.                 DWORD cSamples,                         // (IN) 
  865.                 WAVEFORMATEX *pwfxDest,         // (IN)
  866.                 BYTE *pbData                            // (IN)
  867.                 )
  868. {
  869.  
  870.     HMMIO           hmmioOut;
  871.     MMCKINFO        ckOut;
  872.     MMCKINFO        ckOutRIFF;
  873.     MMIOINFO        mmioinfoOut;
  874.     UINT            cbActualWrite;
  875.     int                     nError;
  876.  
  877.     if ((nError = WaveCreateFile(pszFileName, &hmmioOut, pwfxDest, &ckOut, &ckOutRIFF)) != 0)
  878.         {
  879.         goto ERROR_SAVING;
  880.         }
  881.  
  882.     if ((nError = WaveStartDataWrite(&hmmioOut, &ckOut, &mmioinfoOut)) != 0)
  883.         {
  884.         goto ERROR_SAVING;
  885.         }
  886.  
  887.     if ((nError = WaveWriteFile(hmmioOut, cbSize, pbData, &ckOut, &cbActualWrite, &mmioinfoOut)) != 0)
  888.         {
  889.         goto ERROR_SAVING;
  890.         }
  891.     
  892.     if ((nError = WaveCloseWriteFile(&hmmioOut, &ckOut, &ckOutRIFF, &mmioinfoOut, cSamples)) != 0)
  893.         {
  894.         goto ERROR_SAVING;
  895.         }       
  896.  
  897. ERROR_SAVING:
  898.     
  899.     return(nError);         
  900.  
  901. }
  902.