操作Waveファイル(7):空のWaveファイルを作成する(3つの方法)

4339 ワード



unit Unit1;



interface



uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs;



type

  TForm1 = class(TForm)

    procedure FormCreate(Sender: TObject);

  end;



var

  Form1: TForm1;



implementation



{$R *.dfm}



uses MMSystem;



//chan: 1  、2  ;

//freq:  ,  : 11025, 22050, 44100

//bit :  ,   8、16

function CreateWav1(chan, freq, bit: Word; const FilePath: string): Boolean;

var

  h: HMMIO;

  ckiRiff, ckiFmt, ckiData: TMMCKInfo;

  fmt: TPCMWaveFormat;

begin

  //  mmioCreateChunk   Wave  .



  { }

  ZeroMemory(@ckiRiff, SizeOf(TMMCKInfo));

  ckiRiff.cksize := 36; {mmioCreateChunk   ckid,   cksize  }

  ckiRiff.fccType := mmioStringToFOURCC('WAVE', 0);



  ZeroMemory(@ckiFmt, SizeOf(TMMCKInfo));

  ckiFmt.ckid := mmioStringToFOURCC('fmt', 0);



  ZeroMemory(@ckiData, SizeOf(TMMCKInfo));

  ckiData.ckid := mmioStringToFOURCC('data', 0);



  {  Wave  }

  fmt.wf.wFormatTag := WAVE_FORMAT_PCM;

  fmt.wf.nChannels := chan;

  fmt.wf.nSamplesPerSec := freq;

  fmt.wf.nAvgBytesPerSec := freq * chan * bit div 8;

  fmt.wf.nBlockAlign := chan * bit div 8;

  fmt.wBitsPerSample := bit;



  h := mmioOpen(PChar(FilePath), nil, MMIO_CREATE or MMIO_WRITE);

  if h = 0 then Exit(False);



  {  RIFF、fmt、data  }

  if (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 := True;



  mmioClose(h, 0);

end;



//  PCM   WAVE   44  :

function CreateWav2(chan, freq, bit: Word; const FilePath: string): Boolean;

type

  TWaveHeader = record

    Riff_ckid      : DWORD;

    Riff_cksize    : DWORD;

    Riff_fccType   : DWORD;

    fmt_ckid       : DWORD;

    fmt_cksize     : DWORD;

    wFormatTag     : Word;

    nChannels      : Word;

    nSamplesPerSec : DWORD;

    nAvgBytesPerSec: DWORD;

    nBlockAlign    : Word;

    wBitsPerSample : Word;

    data_ckid      : DWORD;

    data_cksize    : DWORD;

  end;

var

  wh: TWaveHeader;

  hFile: Integer;

begin

  wh.Riff_ckid := FOURCC_RIFF;

  wh.Riff_cksize := 36;

  wh.Riff_fccType := mmioStringToFOURCC('WAVE', 0);

  wh.fmt_ckid := mmioStringToFOURCC('fmt', 0);

  wh.fmt_cksize := 16;

  wh.wFormatTag := WAVE_FORMAT_PCM;

  wh.nChannels := chan;

  wh.nSamplesPerSec := freq;

  wh.nAvgBytesPerSec := freq * chan * bit div 8;

  wh.nBlockAlign := chan * bit div 8;

  wh.wBitsPerSample := bit;

  wh.data_ckid := mmioStringToFOURCC('data', 0);

  wh.data_cksize := 0;



  hFile := FileCreate(FilePath);

  Result := (FileWrite(hFile, wh, SizeOf(TWaveHeader)) <> -1);

  FileClose(hFile);

end;



// ,  

function CreateWav3(chan, freq, bit: Word; const FilePath: string): Boolean;

type

  TWaveHeader = record

    Riff_ckid      : DWORD;

    Riff_cksize    : DWORD;

    Riff_fccType   : DWORD;

    fmt_ckid       : DWORD;

    fmt_cksize     : DWORD;

    wFormatTag     : Word;

    nChannels      : Word;

    nSamplesPerSec : DWORD;

    nAvgBytesPerSec: DWORD;

    nBlockAlign    : Word;

    wBitsPerSample : Word;

    data_ckid      : DWORD;

    data_cksize    : DWORD;

  end;

var

  wh: TWaveHeader;

begin

  wh.Riff_ckid := FOURCC_RIFF;

  wh.Riff_cksize := 36;

  wh.Riff_fccType := mmioStringToFOURCC('WAVE', 0);

  wh.fmt_ckid := mmioStringToFOURCC('fmt', 0);

  wh.fmt_cksize := 16;

  wh.wFormatTag := WAVE_FORMAT_PCM;

  wh.nChannels := chan;

  wh.nSamplesPerSec := freq;

  wh.nAvgBytesPerSec := freq * chan * bit div 8;

  wh.nBlockAlign := chan * bit div 8;

  wh.wBitsPerSample := bit;

  wh.data_ckid := mmioStringToFOURCC('data', 0);

  wh.data_cksize := 0;



  with TFileStream.Create(FilePath, fmCreate) do begin

    Result := (Write(wh, SizeOf(TWaveHeader)) = SizeOf(TWaveHeader));

    Free;

  end;

end;



procedure TForm1.FormCreate(Sender: TObject);

begin

  CreateWav1(1, 11025, 8,  'C:\Temp\X1.wav');

  CreateWav2(2, 22050, 16, 'C:\Temp\X2.wav');

  CreateWav3(2, 44100, 16, 'C:\Temp\X3.wav');

end;



end.