Merge two wav files in the same format

Source: Internet
Author: User

If this problem occurs, search for historical data as usual.

One clue was found: "merging through WAV file format"

Let's take a look at the wav file format first.


Obviously, subchunk2size indicates the audio data size.

If the format is the same, try to merge the data area.

Of course, the subchunk2size audio data size and file size chunksize need to be recalculated

 

Type

Twavformat =Packed Record

Chunkid:Array[0..3]OfChar;// 'Riff'

Chunksize: longword;// File size-8 [bytes]

Format:Array[0..3]OfChar;// 'Wave'

Subchunk1id:Array[0..3]OfChar;// 'Fmt' mind the space!

Subchunk1size: longword;// Hex10

Audioformat: word;// Hex 01

Numofchannels: word;// 1 mono, 2 stereo

Samplerate: longword;// Number of samples/sec

Byterate: longword;// Samplerate * num of channels *

// Bytes per (Mono) Sample

Bytespersample: word;// Size of (Mono) Sample [bytes]

Bitspersample: word;// Bytespersample * 8

Subchunk2id:Array[0..3]OfChar;// 'Data'

Subchunk2size: longword;// Number of data bytes

End;

 

FunctionConcatwavfile (awavfile1, awavfile2, anewfile:String): Boolean;

VaR

Vwavformat1: twavformat;

Vwavformat2: twavformat;

Vfilehandle1: thandle;

Vfilehandle2: thandle;

Vfilestream1: tfilestream;

Vfilestream2: tfilestream;

Vchunksize1, vchunksize2: integer;

Begin

Result: = false;

If NotFileexists (awavfile1)ThenExit;

If NotFileexists (awavfile2)ThenExit;

Vfilehandle1: = _ lopen (pchar (awavfile1), of_readOrOf_share_deny_none );

Vfilehandle2: = _ lopen (pchar (awavfile2), of_readOrOf_share_deny_none );

If(INTEGER (vfilehandle1) <=0)Or(INTEGER (vfilehandle2) <=0)Then

Begin

_ Lclose (vfilehandle1 );

_ Lclose (vfilehandle2 );

Exit;

End;

 

Vfilestream1: = tfilestream. Create (vfilehandle1 );

Vfilestream2: = tfilestream. Create (vfilehandle2 );

Try

IfVfilestream1.read (vwavformat1, sizeof (twavformat) <>

Sizeof (twavformat)ThenExit;

IfVfilestream2.read (vwavformat2, sizeof (twavformat) <>

Sizeof (twavformat)ThenExit;

IfVwavformat1.chunkid <>'Riff' ThenExit;

IfVwavformat1.subchunk2id <>'Data' ThenExit;

Vchunksize1: = vwavformat1.subchunk2size;

Vchunksize2: = vwavformat2.subchunk2size;

Vwavformat1.chunksize: =0;

Vwavformat1.subchunk2size: =0;

Vwavformat2.chunksize: =0;

Vwavformat2.subchunk2size: =0;

If NotComparemem (@ vwavformat1, @ vwavformat2, sizeof (twavformat ))ThenExit;//Different formats

WithTmemorystream. CreateDo Try

Vwavformat1.chunksize: = vchunksize1 + vchunksize2 +

Sizeof (vwavformat1 )-8;

Vwavformat1.subchunk2size: = vchunksize1 + vchunksize2;

Write (vwavformat1, sizeof (twavformat ));

Copyfrom (vfilestream1, vchunksize1 );

Copyfrom (vfilestream2, vchunksize2 );

Try

Savetofile (anewfile );

Except

Exit;

End;

Finally

Free;

End;

Finally

Vfilestream1.free;

Vfilestream2.free;

End;

Result: = true;

End;{Concatwavfile}

 

ProcedureTform1.button1click (Sender: tobject );

Begin

Concatwavfile ('C:/Windows/Media/chord.wav','C:/Windows/Media/chimes.wav',

'C:/temp/temp.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.