Waveform Capture: (9) write to WAV file

Source: Internet
Author: User

WAV files are resource Interchange file formats (RIFF), including a number of named blocks that contain header information, such as a sound sampling format, or data (such as the sample itself). The Win API provides functions such as turning off Fiff files, finding blocks, and so on. The names of these functions begin with "Mmio".

The DirectSound API does not include methods for writing WAV files. However, the DXUTsound.cpp file implements a Cwavefile class that has the following methods of managing capture files:

· The Open method. Opens a file and writes a header block.

· Write method. Writes data from a buffer into a block of data and increases the write pointer.

· The Close method. Writes the size of the data block to the head and closes the file.

The first step to writing to a WAV file is to invoke the Cwavefile::open method. This creates a file and writes a WAV format block. The parameter is a filename, a pointer to the initialized WAVEFORMATEX structure, and a wavefile_write flag. This method returns an HRESULT.

The following code opens a WAV file for writing:

Code

CWaveFile  g_pWaveFile;
WAVEFORMATEX wfxInput;

ZeroMemory( &wfxInput, sizeof(wfxInput));
wfxInput.wFormatTag = WAVE_FORMAT_PCM;
wfxInput.nSamplesPerSec = 22050
wfxInput.wBitsPerSample = 8;
wfxInput.nChannels = 1;
wfxInput.nBlockAlign =
wfxInput.nChannels * (wfxInput.wBitsPerSample / 8);
wfxInput.nAvgBytesPerSec =
 wfxInput.nBlockAlign * wfxInput.nSamplesPerSec;

g_pWaveFile = new CWaveFile;
if (FAILED(g_pWaveFile->Open("mywave.wav", &wfxInput,WAVEFILE_WRITE)))
{
 g_pWaveFile->Close();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.