Wave file operations (15): Merge and crop WAV Files

Source: Internet
Author: User
Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls; Type tform1 = Class (tform) button1: tbutton; button2: tbutton; procedure button1click (Sender: tobject); Procedure button2click (Sender: tobject); end; var form1: tform1; implementation {$ R *. DFM} uses mmsystem; // function for obtaining format information and waveform data from the specified WAV file getwavefmtdata (const path: string; var FMT: Twaveformatex; var Buf: tbytes): Boolean; var hfile: hmmio; ckiriff, ckifmt, ckidata: tmmckinfo; begin result: = false; hfile: = mmioopen (pchar (PATH ), nil, memory); If hfile = 0 Then exit; zeromemory (@ ckiriff, sizeof (tmmckinfo); zeromemory (@ ckifmt, sizeof (tmmckinfo); zeromemory (@ ckidata, sizeof (tmmckinfo); ckiriff. fcctype: = mmiostringtofourcc ('wave ', 0); ckifmt. ckid: = mmiostringtofourcc ('fmt ', 0); ckidata. ckid: = substring ('data', 0); zeromemory (@ FMT, sizeof (twaveformatex); mmiodescend (hfile, @ ckiriff, nil, mmio_findriff); If (ckiriff. ckid = fourcc_riff) and (ckiriff. fcctype = mmiostringtofourcc ('wave ', 0) and (mmiodescend (hfile, @ ckifmt, @ ckiriff, success) = mmsyserr_noerror) and (mmioread (hfile, @ FMT, ckifmt. cksize) = ckifmt. cksize) and (mmioascend (hfile, @ ckifmt, 0) = mmsyserr_noerror) and (mmiodescend (hfile, @ ckidata, @ ckiriff, mmio_findchunk) = mmsyserr_noerror) then begin setlength (BUF, ckidata. cksize); Result: = (mmioread (hfile, pansichar (BUF), ckidata. cksize) = ckidata. cksize); end; mmioclose (hfile, 0); end; // function of creating a WAV file based on the format information and waveform data: function createwave (const path: string; const FMT: twaveformatex; const Buf: tbytes): Boolean; var H: hmmio; ckiriff, ckifm T, ckidata: tmmckinfo; begin zeromemory (@ ckiriff, sizeof (tmmckinfo); ckiriff. cksize: = 44-8 + Length (BUF); ckiriff. fcctype: = mmiostringtofourcc ('wave ', 0); zeromemory (@ ckifmt, sizeof (tmmckinfo); ckifmt. ckid: = mmiostringtofourcc ('fmt', 0); zeromemory (@ ckidata, sizeof (tmmckinfo); ckidata. ckid: = mmiostringtofourcc ('data', 0); ckidata. cksize: = length (BUF); H: = mmioopen (pchar (PATH), nil, mmio _ Create or mmio_write); If (H 0) and (mmiocreatechunk (H, @ ckiriff, timeout) = 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: = (mmiowrite (H, pansichar (BUF), Len Extract (BUF) = length (BUF); mmioclose (H, 0); end; // capture a WAV file. In this example, 1/4 Procedure tform1.button1click (Sender: tobject); const pathsource = 'C: \ windows \ media \ Windows XP start .wav '; pathdest = 'C: \ temp \ new1.wav'; var FMT: twaveformatex; Buf: tbytes; begin getwavefmtdata (pathsource, FMT, Buf); setlength (BUF, length (BUF) Div 4); createwave (pathdest, FMT, Buf); end; // merge the wav file procedure tform1.button2click (send Er: tobject); const path1 = 'C: \ windows \ media \ Windows XP start .wav '; path2 = 'C: \ windows \ media \ Windows XP Shut Down .wav '; pathdest = 'C: \ temp \ new2.wav '; var fmt1, fmt2: twaveformatex; buf1, buf2: tbytes; oldlen: integer; begin getwavefmtdata (path1, fmt1, buf1 ); getwavefmtdata (path2, fmt2, buf2); If comparemem (@ fmt1, @ fmt2, sizeof (twaveformatex) then begin oldlen: = length (buf1); setlength (buf1, length (buf1) + L Ength (buf2); copymemory (@ buf1 [oldlen], pointer (buf2), length (buf2); createwave (pathdest, fmt1, buf1 ); end else showmessage ('the file format is inconsistent, and merging is not performed! '); 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.