• 合并两个 Wav 文件的函数


    unit Unit1;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;
    
    type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    {合并两个 Wav 文件的函数}
    function ConWavFile(AWavFile1, AWavFile2, ANewFile: string): Boolean;
    type
      TWavFormat = packed record
        ChunkID: array[0..3] of AnsiChar;     {'RIFF'}
        ChunkSize: Longword;                  {size-8}
        Format: array[0..3] of AnsiChar;      {'WAVE'}
        SubChunk1ID: array[0..3] of AnsiChar; {'fmt '}
        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}
        BitsPerSample: Word;                  {BytesperSample *8}
        SubChunk2ID: array[0..3] of AnsiChar; {'data'}
        SubChunk2Size: Longword;              {number of data bytes}
      end;
    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(PAnsiChar(AnsiString(AWavFile1)), OF_READ or OF_SHARE_DENY_NONE);
      vFileHandle2 := _lopen(PAnsiChar(AnsiString(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; { ConWavFile End}
    
    
    {测试}
    procedure TForm1.Button1Click(Sender: TObject);
    var
      Wav1,Wav2,WavDest: string;
    begin
      Wav1 := 'c:\temp\1.wav';
      Wav2 := 'c:\temp\2.wav';
      WavDest := 'c:\temp\12.wav';
      if ConWavFile(Wav1, Wav2, WavDest) then
        ShowMessageFmt('''%s'' 和 ''%s'' 已成功合并到 ''%s''', [Wav1,Wav2,WavDest]);
    end;
    
    end.
    
  • 相关阅读:
    mysql 触发器 插入
    【经验】STL的list vector在iterator迭代器的循环中 使用erase 造成的BUG
    C/C++ 关于 for循环 的第二个表达式右侧非常量的时候
    MySQL C API的一个让我头疼的问题,获得一行记录中包括NULL
    vim粘贴代码的时候,恶心的缩进.
    [转]分析MySQL数据类型的长度【mysql数据字段 中length和decimals的作用!熟悉mysql必看】
    [转]对于孩子:旅行的意义何在?
    libc中的标准函数 localtime和localtime_r 的用法
    【腾讯面试题目】非循环方式 计算一个32位整数中被置1的位数
    C++对带有分隔符的字符串 分割为数字的通用解决方案
  • 原文地址:https://www.cnblogs.com/del/p/1319576.html
Copyright © 2020-2023  润新知