Solved

Wave File Format

Posted on 1998-05-27
4
804 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
Comment Utility
   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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now