• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 839
  • Last Modified:

Wave File Format

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
WDB
Asked:
WDB
  • 2
  • 2
1 Solution
 
chensuCommented:
   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
 
WDBAuthor Commented:
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
 
chensuCommented:
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
 
WDBAuthor Commented:
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 learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now