Waveform Capture: (9) write to WAV file

Source: Internet
Author: User

Write to WAV file

 

Wav files are in the format of Resource Exchange files (riff), including a number of naming blocks, including header information (such as audio sampling format) or data (such as the sample itself ). Windows 32 API provides functions such as opening and disabling the javasf file and searching for blocks. The names of these functions start with "mmio.

The directsound API does not include writing wav files. However, the dxutsound. cpp file implements a cwavefile class, which has the following methods to manage file capturing:
·OpenMethod. Open a file and write it to the header block.
·WriteMethod. Write Data from the buffer to the data block and raise the write pointer.
·CloseMethod. Add the data block case to the header and close the file.

The first step for writing data to a WAV file is to callCwavefile: OpenMethod. This will create a file and write the WAV format block. The parameter is the file name, a pointer to the initialized waveformatex struct and the wavefile_write flag. This method returns an hresult.

The followingCodeA wav file is opened 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 ();
}

ApplicationProgramNow we can start copying data from the capture buffer to this file.

The following example function will be called every time the read pointer reaches the notification location. In this function, the following global variables are used:
· G _ pdsbcapture is a pointer to the idirectsoundcapturebuffer8 interface in the buffer zone.
· G _ dwnextcaptureoffset traces the offset of the next data block in the buffer zone to be copied to the file.
· G _ dwcapturebuffersize indicates the buffer size, which is used to calculate "surround ".

Code
Hresult recordcaptureddata ()
{
Hresult hr;
Void * Pbcapturedata = NULL;
DWORD dwcapturelength;
Void * Pbcapturedata2 = NULL;
DWORD dwcaptureleng2;
Void * Pbplaydata = NULL;
Uint dwdatawrote;
DWORD dwreadpos;
Long llocksize;

If (Null = G_pdsbcapture)
Return S_false;
If (Null = G_pwavefile)
Return S_false;

If (Failed (HR = G_pdsbcapture -> Getcurrentposition (
Null, & Dwreadpos )))
Return HR;

// Lock everything between the private cursor
// And the read cursor, allowing for wraparound.

Llocksize = Dwreadpos - G_dwnextcaptureoffset;
If (Llocksize <   0 ) Llocksize + = G_dwcapturebuffersize;

If (Llocksize =   0 ) Return S_false;

If (Failed (HR = G_pdsbcapture -> Lock (
G_dwnextcaptureoffset, llocksize,
& Pbcapturedata, & Dwcapturelength,
& Pbcapturedata2, & Dwcaptureleng2, 0l )))
Return HR;

// Write the data. This is done in two steps
// To account for wraparound.

If (Failed (HR = G_pwavefile -> Write (dwcapturelength,
(Byte * ) Pbcapturedata, & Dwdatawrote )))
Return HR;

If (Pbcapturedata2 ! = Null)
{
If (Failed (HR = G_pwavefile -> Write (
Dwcaptureleng2, (byte * ) Pbcapturedata2,
& Dwdatawrote )))
Return HR;
}

// Unlock the capture buffer.

G_pdsbcapture -> Unlock (pbcapturedata, dwcapturelength,
Pbcapturedata2, dwcaptureleng2 );

// Move the capture offset forward.

G_dwnextcaptureoffset + = Dwcapturelength;
G_dwnextcaptureoffset % = G_dwcapturebuffersize;
G_dwnextcaptureoffset + = Dwcaptureleng2;
G_dwnextcaptureoffset % = G_dwcapturebuffersize;

Return s_ OK;
}< P>

when capturing is finished, the application closes the wav file.

g_pwavefile -> close ();

View other statements:
statement
1. enumerate "capturing devices"
2. create "capture device object"
3. "Capture device" performance
4. create a "Capture buffer"
5. "Capture buffer" Information
6. "buffer capturing" Notification
7. "buffer capturing" effect
8. use "Capture buffer"
9. write to WAV file

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.