RSS订阅 | 匿名投稿
您的位置:网站首页 > 媒体 > 正文

delphi 低级音频函数 采集音频数据

作者:admin 来源:未知 日期:2010/4/29 23:53:35 人气: 标签:


unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,
MMSystem;{引用mmsystem单元}
const
FNumBuffers = 4;


type TACMWAVEFORMAT = packed record
    case Integer of
      0: (Format: twaveformatex);
      1: (Rawdata: array[0..128] of byte);
end;

type
TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    Label2: TLabel;
    procedure Button1Click(Sender: TObject);
private
    FBufferSize :integer;
    FWaveInHandle: HWaveIn;
    WaveFmt: TACMWAVEFORMAT;
public
    { Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}


procedure WaveInCallback(hWave: HWAVEIN; uMsg: cardinal; pform1: tform1; dwParam1, dwParam2: cardinal) stdcall;

var
Header: PWaveHdr;
Res: Integer;
BytesRecorded: Integer;
Data: Pointer;
begin
if (uMsg = MM_WIM_DATA) then
begin
    header := PWaveHdr(dwParam1);
    Res := WaveInUnPrepareHeader(hWave, Header, sizeof(TWavehdr));
    if Res <> 0 then Exit;
    BytesRecorded := header.dwBytesRecorded;
    begin
      Getmem(Data, BytesRecorded);
      try
        move(header.lpData^, Data^, BytesRecorded);
         {此处进行数据处理 采集的data }
        pform1.Label1.Caption := Format(' %0.1f KB/S', [BytesRecorded / 1024]);

      finally
        Freemem(Data);
      end;

      {清空缓冲区,并添加到录音设备}
      header.dwbufferlength := pform1.FBufferSize;
      header.dwBytesRecorded := 0;
      header.dwUser := 0;
      header.dwflags := 0;
      header.dwloops := 0;
      FillMemory(Header.lpData, pform1.FBufferSize, 0);
      Res := WaveInPrepareHeader(hWave, Header, sizeof(TWavehdr));
      if Res <> 0 then Exit;
      Res := WaveInAddBuffer(hWave, Header, sizeof(TWaveHdr));
      if Res <> 0 then Exit;
    end;
end;
end;


procedure TForm1.Button1Click(Sender: TObject);
var
Res: Integer;
J: Integer;
Header: PWaveHdr;
begin
FWaveInHandle := 0;
/////////////////////////////////////
/////////GSM 6.10////////////
/////////////////////////////////////
WaveFmt.Format.wFormatTag := 49;
WaveFmt.Format.nChannels := 1;
                                                            {1k/s       2k          4k     8k}
WaveFmt.Format.nSamplesPerSec := 8000; //11025//22050//44100
WaveFmt.Format.nAvgBytesPerSec := 1625;//2239//4478//8957
WaveFmt.Format.nBlockAlign := 65;
WaveFmt.Format.wBitsPerSample := 0;
WaveFmt.Format.cbSize := 2;
Wavefmt.Rawdata[18] := 64;
Wavefmt.Rawdata[19] := 1;

FBufferSize :=WaveFmt.Format.nAvgBytesPerSec;
{用回调函数取得采集的音频数据}
res := WaveInOpen(@FWaveInHandle, WAVE_MAPPER, @Wavefmt, cardinal(@WaveInCallback), cardinal(Self), CALLBACK_FUNCTION);
if Res <> 0 then exit;

{为音频输入设备一次添加多个缓冲区,这些缓冲区有系统调度}

for j := 1 to FNumBuffers do
begin
    Getmem(Header, SizeOf(TWaveHDR));
    with Header^ do begin
      Getmem(lpData, FBufferSize);
      dwBufferLength := FBufferSize;
      dwBytesRecorded := 0;
      dwFlags := 0;
      dwLoops := 0;
      Res := WaveInPrepareHeader(FWaveInHandle, Header, sizeof(TWaveHDR));
      if Res <> 0 then Exit;
      Res := WaveInAddBuffer(FWaveInHandle, Header, SizeOf(TWaveHDR));
      if Res <> 0 then Exit;
    end;
end;
Res := WaveInStart(FWaveInHandle);
if Res <> 0 then exit;
end;


读完这篇文章后,您心情如何?
0
0
0
0
0
0
0
0
本文网址: