操作 Wave 文件(6): 获取 Wave 文件的波形数据


读取函数及测试代码:


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}



uses MMSystem;



function GetWaveData(FilePath: string; var stream: TMemoryStream): Boolean;

var

  hFile: HMMIO;

  ckiRIFF,ckiData: TMMCKInfo;

begin

  Result := False;

  hFile := mmioOpen(PChar(FilePath), nil, MMIO_READ);

  if hFile = 0 then Exit;



  ZeroMemory(@ckiRIFF, SizeOf(TMMCKInfo));

  ZeroMemory(@ckiData, SizeOf(TMMCKInfo));

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



  //先获取主块的信息

  mmioDescend(hFile, @ckiRIFF, nil, MMIO_FINDRIFF);



  //再获取 data 块的信息后, 指针将自动指向 data 数据的起点; 然后读出数据

  if (ckiRIFF.ckid = FOURCC_RIFF) and

     (ckiRIFF.fccType = mmioStringToFOURCC('WAVE',0)) and

     (mmioDescend(hFile, @ckiData, @ckiRIFF, MMIO_FINDCHUNK) = MMSYSERR_NOERROR) then

    begin

      stream.Size := ckiData.cksize;

      Result := (mmioRead(hFile, stream.Memory, ckiData.cksize) = ckiData.cksize);

    end;

  mmioClose(hFile, 0);

end;



//调用测试

procedure TForm1.Button1Click(Sender: TObject);

const

  FilePath = 'C:\WINDOWS\Media\Windows XP 启动.wav';

var

  stream: TMemoryStream;

begin

  stream := TMemoryStream.Create;



  if GetWaveData(FilePath, stream) then

    ShowMessageFmt('读出的数据大小是: %d', [stream.Size]); {424600}



  stream.Free;

end;



end.


 
   

你可能感兴趣的:(文件)