Wave file (7): Create an empty wave file (three methods)

Source: Internet
Author: User
Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs; Type tform1 = Class (tform) Procedure formcreate (Sender: tobject); end; vaR form1: tform1; implementation {$ R *. DFM} uses mmsystem; // Chan: 1 single channel, 2 stereo; // freq: frequency, value: 11025,220 50, 44100 // bit: the size of each sample, value: 8 and 16 function createwav1 (Chan, freq, bit: word; const filepath: string): Boolean; var H: hmmio; ckiriff, ckifmt, ckidata: tmmckinfo; FMT: tpcmwaveformat; begin // This function uses the mmiocreatechunk function to create each block of the wave file separately. {preliminary knowledge-related structure} zeromemory (@ ckiriff, sizeof (tmmckinfo); ckiriff. cksize: = 36; {The mmiocreatechunk function automatically writes the ckid, but the cksize must be manually assigned to} ckiriff. fcctype: = mmiostringtofourcc ('wave ', 0); zeromemory (@ ckifmt, sizeof (tmmckinfo); ckifmt. ckid: = mmiostringtofourcc ('fmt', 0); zeromemory (@ ckidata, sizeof (tmmckinfo); ckidata. ckid: = mmiostringtofourcc ('data', 0); {specify the wave format} FMT. WF. wformattag: = wave_format_pcm; FMT. WF. nchannels: = Chan; FMT. WF. nsamplespersec: = freq; FMT. WF. navgbytespersec: = freq * chan * bit Div 8; FMT. WF. nblockalign: = chan * bit Div 8; FMT. wbitspersample: = bit; H: = mmioopen (pchar (filepath), nil, mmio_create or mmio_write); If H = 0 Then exit (false ); {create riff, FMT, data blocks respectively} If (mmiocreatechunk (H, @ ckiriff, blocks) = mmsyserr_noerror) and (mmiocreatechunk (H, @ ckifmt, 0) = mmsyserr_noerror) and (mmiowrite (H, pansichar (@ FMT), sizeof (tpcmwaveformat) = sizeof (tpcmwaveformat) and (mmioascend (H, @ ckifmt, 0) = mmsyserr_noerror) and (mmiocreatechunk (H, @ ckidata, 0) = mmsyserr_noerror) then result: = true; mmioclose (H, 0); end; // regard the first 44 bytes of the PCM-encoded wave file as a structure for Operation: function createwav2 (Chan, freq, bit: word; const filepath: string): Boolean; type twaveheader = record failed: DWORD; riff_cksize: DWORD; rule: DWORD; fmt_cksize: DWORD; wformattag: word; nchannels: word; nsamplespersec: DWORD; rule: DWORD; nblockalign: word; wbitspersample: word; data_ckid: DWORD; data_cksize: DWORD; end; var wh: twaveheader; hfile: integer; begin Wh. riff_ckid: = fourcc_riff; Wh. riff_cksize: = 36; Wh. riff_fcctype: = mmiostringtofourcc ('wave ', 0); Wh. fmt_ckid: = mmiostringtofourcc ('fmt', 0); Wh. fmt_cksize: = 16; Wh. wformattag: = wave_format_pcm; Wh. nchannels: = Chan; Wh. nsamplespersec: = freq; Wh. navgbytespersec: = freq * chan * bit Div 8; Wh. nblockalign: = chan * bit Div 8; Wh. wbitspersample: = bit; Wh. data_ckid: = mmiostringtofourcc ('data', 0); Wh. data_cksize: = 0; hfile: = filecreate (filepath); Result: = (filewrite (hfile, wh, sizeof (twaveheader)-1); fileclose (hfile); end; // same as above, only use stream to Write File function createwav3 (Chan, freq, bit: word; const filepath: string): Boolean; Type twaveheader = record riff_ckid: DWORD; riff_cksize: DWORD; rule: DWORD; fmt_cksize: DWORD; wformattag: word; nchannels: word; nsamplespersec: DWORD; rule: DWORD; nblockalign: word; wbitspersample: word; data_ckid: DWORD; data_cksize: DWORD; end; var wh: twaveheader; begin Wh. riff_ckid: = fourcc_riff; Wh. riff_cksize: = 36; Wh. riff_fcctype: = mmiostringtofourcc ('wave ', 0); Wh. fmt_ckid: = mmiostringtofourcc ('fmt', 0); Wh. fmt_cksize: = 16; Wh. wformattag: = wave_format_pcm; Wh. nchannels: = Chan; Wh. nsamplespersec: = freq; Wh. navgbytespersec: = freq * chan * bit Div 8; Wh. nblockalign: = chan * bit Div 8; Wh. wbitspersample: = bit; Wh. data_ckid: = mmiostringtofourcc ('data', 0); Wh. data_cksize: = 0; with tfilestream. create (filepath, fmcreate) Do begin result: = (write (Wh, sizeof (twaveheader) = sizeof (twaveheader); free; end; Procedure tform1.formcreate (Sender: tobject); begin createwav1 (1, 11025, 8, 'c: \ temp \ x1.wav '); createwav2 (2, 22050, 16, 'c: \ temp \ x2.wav '); createwav3 (2, 44100, 16, 'c: \ temp \ x3.wav '); end.
 
  
 

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.