Solved

Wave File Format

Posted on 1998-05-27
4
809 Views
Last Modified: 2013-12-03
I'm creating wave files for my application. I'm using the low level waveIn() and mmio() functions to manage several different formats. I create a RIFF file and am able to add the main RIFF chunk, the fmt chunk and the fact chunks. I also add the data chunk with a size 0 before I start actually writing to the file. I write to the file and everything seems to flow smoothly. The file size seems to be correct but when I attempt to open it with a media player device (i.e. "Sound Recorder") it opens fine but it says it's media length is 0 and it's size is 0. Below is my source.

hmmio = mmioOpen szFileName ,NULL,MMIO_CREATE MMIO_WRITE | MMIO_EXCLUSIVE);

    //
    //  create the RIFF chunk of form type 'WAVE'
    //
    mmckinfo.fccType = mmioFOURCC('W', 'A', 'V', 'E');
    mmckinfo.cksize  = 0L;
    mmioCreateChunk(hmmio, &mmckinfo, MMIO_CREATERIFF);


    //
    //  now create the destination fmt, fact, and data chunks _in that order_
    //
    //  hmmio is now descended into the 'RIFF' chunk--create the format chunk
    //  and write the format header into it
    //

    cbwfx = sizeof(WAVEFORMATEX)+(Acm.pwfx)->cbSize;
    ck.ckid   = mmioFOURCC('f', 'm', 't', ' ');
    ck.cksize = 0L;
    mmioCreateChunk(hmmio, &ck, 0);

    mmioWrite(hmmio, (HPSTR)(Acm.pwfx), cbwfx);
    mmioAscend(hmmio, &ck, 0);

    //
    //  create the 'fact' chunk

    ck.ckid   = mmioFOURCC('f', 'a', 'c', 't');
    ck.cksize = 0L;
    mmioCreateChunk(hmmio, &ck, 0);

    cSamples  = 0L;
    mmioWrite(hmmio, (HPSTR)&cSamples, sizeof(DWORD));
    mmioAscend(hmmio, &ck, 0);

    //
    //  create the data chunk with no data..
    //
    ck.ckid   = mmioFOURCC('d', 'a', 't', 'a');
    ck.cksize = 0L;
    mmioCreateChunk(hmmio, &ck, 0);
    mmioAscend(hmmio, &ck, 0);

    mmioAscend(hmmio, &mmckinfo, 0);

I use the following code with each additional write to the file.

ck.cksize = lpwh->dwBytesRecorded;
ck.ckid = mmioFOURCC('d', 'a', 't', 'a');
mmioCreateChunk(hmmio,&ck,0);
mmioWrite(hmmio,lpwh->lpData,lpwh->dwBytesRecorded);
mmioAscend(hmmio, &ck, 0);

What Am I missing? Thanks for your help!
0
Comment
Question by:WDB
  • 2
  • 2
4 Comments
 
LVL 23

Expert Comment

by:chensu
ID: 1404461
   cbwfx = sizeof(WAVEFORMATEX)+(Acm.pwfx)->cbSize;
    ck.ckid   = mmioFOURCC('f', 'm', 't', ' ');
    ck.cksize = cbwfx; // this should be cbwfx instead of 0L
    mmioCreateChunk(hmmio, &ck, 0);

If the problem still exists, check out the utility RiffWalk to dump the chunks to see what is wrong.

0
 
LVL 2

Author Comment

by:WDB
ID: 1404462
Thanks for your answer chensu but it didn't work for me. I don't think I'm building the RIFF file correclty. I create The main RIFF chunk then I create the frmt chunk and write to it. I ascend out of the fmt chunk and create the fact chunk and write to it. I ascend out of the fact chunk and then create the data chunk. I write to the data chunk several times and then I ascend out of the data chunk and then out of the main RIFF chunk. Is this correct? The fmt and fact chunks or written to correctly but the data chunk still seems to be interpreted as 0 in size?
0
 
LVL 23

Accepted Solution

by:
chensu earned 150 total points
ID: 1404463
Here is the code I used. By the way, the utility RiffWalk can help you determine which chunk is wrong. It can be found in the Windows Platform SDK (http://www.microsoft.com/msdn/sdk) or MSDN CDs.

//-----------------------------------------------------------------------------
UINT WINAPI WriteWaveToFile(LPCSTR lpszWaveFilename,
                            LPCPCMWAVEFORMAT lppcmWaveFormat,
                            HPCSTR hpWaveData, DWORD dwWaveDataSize)
{
    ASSERT(lpszWaveFilename != NULL);
    ASSERT(::AfxIsValidString(lpszWaveFilename));
    ASSERT(lppcmWaveFormat != NULL);
    ASSERT(::IsValidPCMWaveFormat(lppcmWaveFormat));
   
    MMCKINFO mmckinfoParent, mmckinfoSubchunk;
    HMMIO hmmio;
   
    // Open the given file for writing using buffered I/O.
    if ((hmmio = ::mmioOpen((LPSTR)lpszWaveFilename, NULL,
                            MMIO_CREATE | MMIO_WRITE | MMIO_EXCLUSIVE))
        == NULL)
        // Failed to create wave file.
        return 1;
   
    // Create a 'RIFF' chunk with a 'WAVE' form type
    mmckinfoParent.fccType = mmioFOURCC('W', 'A', 'V', 'E');
    if (::mmioCreateChunk(hmmio, &mmckinfoParent, MMIO_CREATERIFF) != 0)
    {
        // Failed create 'RIFF' chunk.
        ::mmioClose(hmmio, 0);
        return 2;
    }
   
    // Create the format subchunk (form type 'fmt ').
    mmckinfoSubchunk.ckid = mmioFOURCC('f', 'm', 't', ' ');
    mmckinfoSubchunk.cksize = sizeof(PCMWAVEFORMAT);
    if (::mmioCreateChunk(hmmio, &mmckinfoSubchunk, 0) != 0)
    {
        // Failed create the format subchunk.
        ::mmioClose(hmmio, 0);
        return 3;
    }
   
    // Write the format subchunk.
    if (::mmioWrite(hmmio, (HPSTR)lppcmWaveFormat, sizeof(PCMWAVEFORMAT))
        != (LONG)sizeof(PCMWAVEFORMAT))
    {
        // Failed to write the format subchunk.
        ::mmioClose(hmmio, 0);
        return 4;
    }
   
    // Ascend out of the format subchunk.
    if (::mmioAscend(hmmio, &mmckinfoSubchunk, 0) != 0)
    {
        // Failed to write the format subchunk.
        ::mmioClose(hmmio, 0);
        return 4;
    }
   
    // Create the data subchunk that holds the waveform samples.
    mmckinfoSubchunk.ckid = mmioFOURCC('d', 'a', 't', 'a');
    mmckinfoSubchunk.cksize = dwWaveDataSize;
    if (::mmioCreateChunk(hmmio, &mmckinfoSubchunk, 0) != 0)
    {
        // Failed to create the data subchunk.
        ::mmioClose(hmmio, 0);
        return 5;
    }
   
    if (hpWaveData != NULL && dwWaveDataSize != 0)
        // Write the waveform data subchunk.
        if (::mmioWrite(hmmio, hpWaveData, (LONG)dwWaveDataSize)
            != (LONG)dwWaveDataSize)
        {
            // Failed to write the data subchunk.
            ::mmioClose(hmmio, 0);
            return 6;
        }
   
    // Ascend out of the data subchunk.
    if (::mmioAscend(hmmio, &mmckinfoSubchunk, 0) != 0)
    {
        // Failed to write the data subchunk.
        ::mmioClose(hmmio, 0);
        return 6;
    }
   
    // Ascend out of the 'RIFF' chunk.
    if (::mmioAscend(hmmio, &mmckinfoParent, 0) != 0)
    {
        // Failed create 'RIFF' chunk.
        ::mmioClose(hmmio, 0);
        return 2;
    }
   
    // We're done with the file, close it.
    ::mmioClose(hmmio, 0);
   
    return 0;    // successful
}
//-----------------------------------------------------------------------------
0
 
LVL 2

Author Comment

by:WDB
ID: 1404464
Thanks chensu. I needed to change the way I was using the waveInPrepareHeader() and waveInUnprepareHeader() functions. Your source didn't help since I'm using formats other than PCM. I appreciate you offering your source. Thanks Again!
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question