delphi mIn/AcmOut 语音采集

2018-10-30


unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ACMOut, ACMConvertor, ACMIn;

type
TForm1 = class(TForm)
   CheckBox1: TCheckBox;
   Button1: TButton;
   GroupBox1: TGroupBox;
   Label1: TLabel;
   ComboBox1: TComboBox;
   ComboBox2: TComboBox;
   Label2: TLabel;
   procedure FormCreate(Sender: TObject);
   procedure FormDestroy(Sender: TObject);
   procedure Button1Click(Sender: TObject);
private
   n, T: integer;
   ACMI: TACMIn;
   ACMO: TACMOut;
   ACMC: TACMConvertor;
   procedure BufferFull(Sender: TObject; Data: Pointer; Size: Integer);
   procedure ShowSize(var Msg: TMessage); message WM_USER;
public
   { Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
ACMO := TACMOut.Create(nil);
ACMC := TACMConvertor.Create;
ACMO.NumBuffers := 0;
ACMO.Open(ACMC.FormatIn);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
ACMC.Active := False;
ACMC.Free;
ACMO.Close;
ACMO.Free;
ACMI.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
ACMI := TACMIn.Create;
ACMI.OnBufferFull := BufferFull;
if ComboBox2.Text = 'Stereo' then ACMC.FormatIn.Format.nChannels := 2
else ACMC.FormatIn.Format.nChannels := 1;
ACMC.FormatIn.Format.nSamplesPerSec := StrToInt(ComboBox1.Text);
  ACMC.FormatIn.Format.wBitsPerSample := 16;
ACMC.FormatIn.Format.nBlockAlign := (ACMC.FormatIn.Format.wBitsPerSample div 8) * ACMC.FormatIn.Format.nChannels;
ACMC.FormatIn.Format.nAvgBytesPerSec := ACMC.FormatIn.Format.nSamplesPerSec * ACMC.FormatIn.Format.nBlockAlign;

ACMC.InputBufferSize := ACMC.FormatIn.Format.nAvgBytesPerSec;
ACMI.BufferSize := ACMC.InputBufferSize;
ACMC.Active := True;
ACMI.Open(ACMC.FormatIn);
t := getTickCount;
end;
{声音充满缓冲区在此得到声音数据DATA……}
procedure Tform1.BufferFull(Sender: TObject; Data: Pointer; Size: Integer);
var
NewSize: Integer;
begin
Move(Data^, ACMC.BufferIn^, Size);
NewSize := ACMC.Convert;
Application.ProcessMessages;
inc(n, newsize);
if getTickCount - t >= 1000 then //每秒数据流量
begin
   SendMessage(Handle, WM_USER, n, 0);
   t := getTickCount;
   n := 0;
end;

//自己的方式传输声音数据 ~.WriteBuffer(ACMC.BufferOut^, NewSize);
   //网络传输数据......//
//接受数据(ACMC.BufferOut^, NewSize)

{声音输出}
if not checkbox1.Checked then ACMO.Play(ACMC.Bufferout^, NewSize);

end;

procedure TForm1.ShowSize(var Msg: TMessage);
begin
Caption := Format('AUDIO %0.1fK/s', [Msg.WParam / 1024]);
end;

end.
///////////////////////

{ WaveFmt.Format.wFormatTag := 49;
  WaveFmt.Format.nChannels := 1;
  WaveFmt.Format.nSamplesPerSec := 22050;
  WaveFmt.Format.nAvgBytesPerSec := 4478;
  WaveFmt.Format.nBlockAlign := 65;
  WaveFmt.Format.wBitsPerSample := 0;
  WaveFmt.Format.cbSize := 2;
  Wavefmt.Rawdata[18] := 64;
  Wavefmt.Rawdata[19] := 1;
}


阅读55