type
twavformat = packed record
chunkid: array[0..3] of char; //'riff'
chunksize: longword; // file size - 8 [bytes]
format: array[0..3] of char; // 'wave'
subchunk1id: array[0..3] of char; // '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] of char; //'data'
subchunk2size: longword; //number of data bytes
end;
function concatwavfile(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 not fileexists(awavfile1) then exit;
if not fileexists(awavfile2) then exit;
vfilehandle1 := _lopen(pchar(awavfile1), of_read or of_share_deny_none);
vfilehandle2 := _lopen(pchar(awavfile2), of_read or of_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
if vfilestream1.read(vwavformat1, sizeof(twavformat)) <>
sizeof(twavformat) then exit;
if vfilestream2.read(vwavformat2, sizeof(twavformat)) <>
sizeof(twavformat) then exit;
if vwavformat1.chunkid <> 'riff' then exit;
if vwavformat1.subchunk2id <> 'data' then exit;
vchunksize1 := vwavformat1.subchunk2size;
vchunksize2 := vwavformat2.subchunk2size;
vwavformat1.chunksize := 0;
vwavformat1.subchunk2size := 0;
vwavformat2.chunksize := 0;
vwavformat2.subchunk2size := 0;
if not comparemem(@vwavformat1, @vwavformat2, sizeof(twavformat)) then exit; //格式不相同
with tmemorystream.create do 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 }
procedure tform1.button1click(sender: tobject);
begin
concatwavfile('c:\windows\media\chord.wav', 'c:\windows\media\chimes.wav',
'c:\temp\temp.wav');
end;