procedure LinkWave(waveFiles, NewWaveFile: string);//WaveFiles:原文件串(必须是完整路径);NewWaveFile:生成的文件名
var
Files:TStringList;
n1,n2,n3,n,WaveLen:Integer;
WaveFile,SoureFile:TFileStream;
a1,a2,a3:byte;
//联接WAV声音文件函数。程序源码:周栋梁(snappy)
//调用方法:将所要联接的声音文件的完整路径用“,”号组成字符串。
begin
Files:=TStringList.Create;
Files.CommaText:=WaveFiles;
WaveFile:=TFileStream.Create(NewWaveFile,fmCreate);
SoureFile:=TFileStream.Create(Files.Strings[0],fmOpenRead);
//打开其中一个声音文件
WaveFile.CopyFrom(SoureFile,59);
//读取声音文件头
WaveLen:=0;
//记录声音长度
for n:=0 to Files.Count-1do
begin
SoureFile.Free;
SoureFile:=TFileStream.Create(Files.Strings[n],fmOpenRead);
SoureFile.Position:=54;
SoureFile.read(a1,1);
SoureFile.read(a2,1);
SoureFile.read(a3,1);
WaveLen:=WaveLen+(ord(a3)*65536)+(ord(a2)*256)+ord(a1);
SoureFile.Position:=59;
WaveFile.CopyFrom(SoureFile,SoureFile.Size-60);
end;
WaveFile.Position:=4;
n1:=(WaveFile.Size-8) div 65536;
n2:=(WaveFile.Size-8-n1*65536) div 256;
n3:=WaveFile.Size-8-n1-n2*256;
WaveFile.WriteBuffer(n3,1);
WaveFile.WriteBuffer(n2,1);
WaveFile.WriteBuffer(n1,1);
n1:=WaveLen div 65536;
n2:=(WaveLen-n1*65536) div 256;
n3:=WaveLen-n1-n2*256;
WaveFile.Position:=54;
WaveFile.WriteBuffer(n3,1);
WaveFile.WriteBuffer(n2,1);
WaveFile.WriteBuffer(n1,1);
WaveFile.Position:=46;
WaveFile.WriteBuffer(n3,1);
WaveFile.WriteBuffer(n2,1);
WaveFile.WriteBuffer(n1,1);
Files.Free;
WaveFile.Free;
SoureFile.Free;
end;
调用例子:
procedure TForm1.Button1Click(Sender: TObject);
begin
LinkWave('e:/1.wav,e:/2.wav','e:/ss.wav');
playsound('e:/ss.wav',0,0);//必须在DELPHI中播放,Windows播放器可能不认识生成的声音文件格式
end;