程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 操作 Wave 文件(15): 合並與剪裁 wav 文件

操作 Wave 文件(15): 合並與剪裁 wav 文件

編輯:Delphi

unit Unit1; 
 
interface 
 
uses 
 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; 
 
//從指定 wav 文件中獲取格式信息和波形數據的函數 
function 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, MMIO_READ); 
 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 := mmiOStringToFOURCC('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, MMIO_FINDCHUNK) = 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; 
 
//根據格式信息和波形數據建立 wav 文件的函數 
function CreateWave(const path: string; const fmt: TWaveFormatEx; const buf: TBytes): Boolean; 
var 
 h: HMMIO; 
 ckiRiff, ckiFmt, 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, MMIO_CREATERIFF) = 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), Length(buf)) = Length(buf)); 
 mmioClose(h, 0); 
end; 
 
 
//截取 wav 文件, 本例截留了文件的 1/4 
procedure TForm1.Button1Click(Sender: TObject); 
const 
 pathSource = 'C:\WINDOWS\Media\Windows XP 啟動.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; 
 
//合並 wav 文件 
procedure TForm1.Button2Click(Sender: TObject); 
const 
 path1 = 'C:\WINDOWS\Media\Windows XP 啟動.wav'; 
 path2 = 'C:\WINDOWS\Media\Windows XP 關機.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) + Length(buf2)); 
  CopyMemory(PByte(Integer(buf1) + oldLen), PByte(buf2), Length(buf2)); 
  CreateWave(pathDest, fmt1, buf1); 
 end else ShowMessage('文件格式不一致, 沒有執行合並!'); 
end; 
 
end. 


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved