使用acm很方便 下面是语音传输代码
unit main;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
ACMWaveIn, ACMWaveOut, StdCtrls, NMUDP, ExtCtrls;
type
TForm1 = class(TForm)
NMUDP1: TNMUDP;
Button1: TButton;
Button2: TButton;
Image1: TImage;
Label1: TLabel;
ACMWaveIn1: TACMWaveIn;
ACMWaveOut1: TACMWaveOut;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
RAddress: TEdit;
Edit1: TEdit;
Label6: TLabel;
Label7: TLabel;
cMode: TRadioGroup;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure ACMWaveIn1Data(data: Pointer;
size: Integer);
procedure NMUDP1DataReceived(Sender: TComponent;
NumberBytes: Integer;
FromIP: String;
Port: Integer);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure cModeClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TGSM610=packed record
wFormatTag: Word;
{ format type }
nChannels: Word;
{ number of channels (i.e. mono, stereo, etc.) }
nSamplesPerSec: DWORD;
{ sample rate }
nAvgBytesPerSec: DWORD;
{ for buffer estimation }
nBlockAlign: Word;
{ block size of data }
wBitsPerSample: Word;
{ number of bits per sample of mono data }
cbSize: Word;
{ the count in bytes of the size of }
wSamplesPerBlock:word;
end;
var
Form1: TForm1;
PlayPackets,ErrorPackets:integer;
format
ointer;
gsm610:TGSM610;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
NMUDP1.Remotehost:=RAddress.text;
ACMWaveIn1.Num:=strtoint(edit1.text);
edit1.text:=inttostr(ACMWaveIn1.Num);
with gsm610do
begin
wFormatTag:=$31;
nChannels:=1;
wBitsPerSample:=0;
nSamplesPerSec:=8000;
nBlockAlign:=65;
nAvgBytesPerSec:=1625;
cbSize:=2;
wSamplesPerBlock:=$140;
end;
format:=@gsm610;
ACMWaveOut1.Open(format);
ACMWaveIn1.Open(format);
Button1.Enabled:=false;
Button2.Enabled:=true;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
ACMWaveIn1.Close;
ACMWaveOut1.Close;
Button1.Enabled:=true;
Button2.Enabled:=false;
end;
procedure TForm1.ACMWaveIn1Data(data: Pointer;
size: Integer);
var
mdata:array[1..10000] of char;
k:integer;
begin
for k:=1 to sizedo
mdata[k]:=pchar(dword(data)+dword(k-1))^;
NMUDP1.SendBuffer(mdata,size);
end;
procedure TForm1.NMUDP1DataReceived(Sender: TComponent;
NumberBytes: Integer;
FromIP: String;
Port: Integer);
var
mdata:array[1..10000] of char;
data
char;
begin
NMUDP1.ReadBuffer(mdata,NumberBytes);
data:=pchar(dword(@mdata));
label1.caption:='Packets:'+inttostr(PlayPackets);
inc(PlayPackets);
if NumberBytes=-1 then
inc(ErrorPackets)
else
ACMWaveOut1.PlayBack(data,NumberBytes);
label4.caption:='Errors:'+inttostr(ErrorPackets);
label7.caption:='Size:'+inttostr(NumberBytes);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ACMWaveOut1.visible:=false;
ACMWaveIn1.visible:=false;
image1.align:=alclient;
NMUDP1.ReportLevel := Status_Basic;
cModeClick(Sender);
PlayPackets:=0;
end;
procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
Button2Click(Sender);
end;
procedure TForm1.cModeClick(Sender: TObject);
begin
if cMode.ItemIndex=0 then
begin
NMUDP1.LocalPort := 6660;
NMUDP1.RemotePort := 6660;
end;
if cMode.ItemIndex=1 then
begin
NMUDP1.LocalPort := 6661;
NMUDP1.RemotePort := 6660;
end;
if cMode.ItemIndex=2 then
begin
NMUDP1.LocalPort := 6661;
NMUDP1.RemotePort := 6661;
end;
label5.caption:=inttostr(NMUDP1.RemotePort)+':'+inttostr(NMUDP1.LocalPort);
end;
end.