CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 1999
    Posts
    18

    Convert a buffer to WAV format?

    Hi,

    I'm currently writing a sound recorder application. I'm using the waveInStart,
    waveInStop, etc... functions to record a sound from the microphone. Now, I want
    to save the sound captured into a .WAV file. Is there an easy way to convert the
    captured buffer to the WAV format without having to care about the WAV file chunk,
    or do I have to create the file myself by manually create the header,the format chunk
    and the data chunk?



  2. #2
    Join Date
    Jun 1999
    Posts
    5

    Re: Convert a buffer to WAV format?

    Hi,
    Folowing code writes sound to disk in WAV format.

    ///////////////////////////////////////////////////////////
    // lpszPathName - Pathname of file
    // pSound - Pointer to sound buffer
    // cbSound - size in bytes of sound buffer
    // pWfx - pointer to WAVEFORMATEX structure
    // cbWfx - size of this structure

    BOOL SaveWAVFile(LPCTSTR lpszPathName, HPSTR pSound, LONG cbSound,
    WAVEFORMATEX pWfx, LONG cbWfx)
    {
    MMIOINFO mmioinfo;
    MMCKINFO ckRIFF;
    MMCKINFO ck;
    HMMIO hmmio;

    ::ZeroMemory(&mmioinfo,sizeof(mmioinfo));

    hmmio = mmioOpen((LPSTR) lpszPathName,&mmioinfo,MMIO_WRITE|MMIO_CREATE);

    if (hmmio == NULL) {
    TRACE("mmioOpen returns NULL\n");
    return FALSE;
    }

    // Creating 'RIFF' and 'WAVE' chunks
    ckRIFF.fccType = mmioFOURCC('W', 'A', 'V', 'E');
    ckRIFF.cksize = 0L;
    ckRIFF.dwFlags = MMIO_DIRTY;
    mmioCreateChunk(hmmio, &ckRIFF, MMIO_CREATERIFF);

    // Creating 'fmt ' chunk
    ck.ckid = mmioFOURCC('f', 'm', 't', ' ');
    ck.cksize = 0L;
    ck.dwFlags = MMIO_DIRTY;
    mmioCreateChunk(hmmio, &ck, 0);
    mmioWrite(hmmio, (HPSTR) pWfx, cbWfx);

    // Goto to 'WAVE' chunk and update 'fmt ' chunk size
    mmioAscend(hmmio, &ck, 0);

    // Creating 'data' chunk
    ck.ckid = mmioFOURCC('d', 'a', 't', 'a');
    ck.cksize = 0L;
    ck.dwFlags = MMIO_DIRTY;
    mmioCreateChunk(hmmio, &ck, 0);
    mmioWrite(hmmio,(HPSTR) pSound,cbSound);

    // Goto to 'WAVE' chunk and update 'data' chunk size
    mmioAscend(hmmio, &ck, 0);

    // Goto to 'RIFF' chunk and update 'WAVE' chunk size
    mmioAscend(hmmio, &ckRIFF, 0);
    mmioClose(hmmio,0);
    }



    That's all.



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured