📄 unitaudio.pas
字号:
unit UnitAudio;
interface
uses
ShareMem,
Windows,
Messages,
SysUtils,
Variants,
Classes,
Graphics,
Controls,
Forms,
Dialogs,
StdCtrls,
Sockets,
ACMOut,
ACMConvertor,
mmsystem,
CompressionStreamUnitForms,
ComCtrls;
type
TAudio = class(TForm)
Label1: TLabel;
ComboBox1: TComboBox;
Label2: TLabel;
ComboBox2: TComboBox;
CheckBox1: TCheckBox;
CheckBox2: TCheckBox;
StatusBar1: TStatusBar;
procedure FormCreate(Sender: TObject);
procedure CheckBox1Click(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormDeactivate(Sender: TObject);
procedure FormCanResize(Sender: TObject; var NewWidth,
NewHeight: Integer; var Resize: Boolean);
private
{ Private declarations }
DataSocket: TCustomWinSocket;
Connected: Boolean;
ACMO: TACMOut;
ACMC: TACMConvertor;
Total: dword;
ConnectNotifyInfo: TNotifyInfo;
ReadNotifyInfo: TNotifyInfo;
DisconnectNotifyInfo: TNotifyInfo;
procedure Connect(var Socket: TCustomWinSocket; Data: Pointer);
procedure Read(var Socket: TCustomWinSocket; CommandFrame: TCommandFrame; Stream: TMemoryStream; Data: Pointer);
procedure Disconnect(var Socket: TCustomWinSocket; Data: Pointer);
function SocketConnected: Boolean;
public
{ Public declarations }
RemoteAddress: string;
WindowItem: TListItem;
end;
implementation
uses UnitMain;
{$R *.dfm}
const
A_TYPE = 3;
A_START = 1;
A_STOP = 2;
function TAudio.SocketConnected: Boolean;
begin
if ((Connected) and (DataSocket <> nil)) then
begin
Connected := DataSocket.Connected;
if not Connected then StatusBar1.Panels[0].Text := ' 断开连接';
end;
Result := Connected;
end;
procedure TAudio.Connect(var Socket: TCustomWinSocket; Data: Pointer);
var
Audio: TAudio;
ConnectionInfo: TConnectionInfo;
begin
Audio := TAudio(Data);
if TStreamRecord(Socket.Data).LocalAddress <> Audio.RemoteAddress then Exit;
if Audio.DataSocket = nil then Audio.DataSocket := Socket else Exit;
ConnectionInfo.ConnectionType := A_TYPE;
Socket.SendBuf(ConnectionInfo, SizeOf(TConnectionInfo));
Audio.Connected := True;
Audio.StatusBar1.Panels.Items[0].Text := ' 已连接: ' + Socket.RemoteAddress;
Audio.CheckBox1.Enabled := True;
Socket := nil;
end;
procedure TAudio.Read(var Socket: TCustomWinSocket; CommandFrame: TCommandFrame; Stream: TMemoryStream; Data: Pointer);
var
Audio: TAudio;
Buffer: Pointer;
begin
Audio := TAudio(Data);
if Audio.DataSocket = Socket then
begin
try
Buffer := Stream.Memory;
case CommandFrame.Command of
A_START:
begin
Application.ProcessMessages;
if not Audio.Visible then Exit;
if not Audio.CheckBox2.Checked then Audio.ACMO.Play(Buffer^, Stream.Size);
end;
end;
finally
Socket := nil;
end;
end;
end;
procedure TAudio.Disconnect(var Socket: TCustomWinSocket; Data: Pointer);
var
Audio: TAudio;
begin
Audio := TAudio(Data);
if Audio.DataSocket = Socket then
begin
Audio.Connected := False;
Audio.StatusBar1.Panels[0].Text := ' 断开连接';
Audio.CheckBox1.Enabled := False;
Audio.CheckBox1.Checked := False;
Socket := nil;
Audio.DataSocket := nil;
end;
end;
procedure TAudio.FormCreate(Sender: TObject);
begin
DataSocket := nil;
ConnectNotifyInfo := TNotifyInfo.Create;
ConnectNotifyInfo.Data := Self;
ConnectNotifyInfo.Callback := @TAudio.Connect;
Main.NotifyConnectList.Add(ConnectNotifyInfo);
ReadNotifyInfo := TNotifyInfo.Create;
ReadNotifyInfo.Data := Self;
ReadNotifyInfo.Callback := @TAudio.Read;
Main.NotifyReadList.Add(ReadNotifyInfo);
DisconnectNotifyInfo := TNotifyInfo.Create;
DisconnectNotifyInfo.Data := Self;
DisconnectNotifyInfo.Callback := @TAudio.Disconnect;
Main.NotifyDisconnectList.Add(DisconnectNotifyInfo);
ACMO := TACMOut.Create(nil);
ACMC := TACMConvertor.Create;
ACMO.NumBuffers := 0;
ACMO.Open(ACMC.FormatIn);
end;
procedure TAudio.CheckBox1Click(Sender: TObject);
var
CommandFrame: TCommandFrame;
ReplyStream: TMemoryStream;
Format: TWaveFormatEx;
begin
CheckBox2.Enabled := CheckBox1.Checked;
if not SocketConnected then Exit;
if not CheckBox1.Enabled then Exit;
if CheckBox1.Checked then
begin
Total := 0;
CommandFrame.len := SizeOf(TWaveFormatEx);
CommandFrame.Command := A_START;
CommandFrame.ID := FRAME_ID;
ReplyStream := TMemoryStream.Create;
ReplyStream.WriteBuffer(CommandFrame, SizeOf(TCommandFrame));
if ComboBox2.Text = '设备' then
Format.nChannels := 2
else
Format.nChannels := 1;
Format.nSamplesPerSec := StrToInt(ComboBox1.Text);
Format.wBitsPerSample := 16;
Format.nAvgBytesPerSec := Format.nSamplesPerSec * Format.nChannels * 2;
Format.nBlockAlign := Format.nChannels * 2;
ACMC.FormatIn.Format.nChannels := Format.nChannels;
ACMC.FormatIn.Format.nSamplesPerSec := Format.nSamplesPerSec;
ACMC.FormatIn.Format.nAvgBytesPerSec := Format.nAvgBytesPerSec;
ACMC.FormatIn.Format.nBlockAlign := Format.nBlockAlign;
ACMC.FormatIn.Format.wBitsPerSample := Format.wBitsPerSample;
ReplyStream.WriteBuffer(Format, SizeOf(TWaveFormatEx));
Main.SendStream(DataSocket, ReplyStream);
end
else
begin
CommandFrame.len := 0;
CommandFrame.Command := A_STOP;
CommandFrame.ID := FRAME_ID;
ReplyStream := TMemoryStream.Create;
ReplyStream.WriteBuffer(CommandFrame, SizeOf(TCommandFrame));
Main.SendStream(DataSocket, ReplyStream);
end;
end;
procedure TAudio.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
CanClose := not CheckBox1.Checked;
end;
procedure TAudio.FormClose(Sender: TObject; var Action: TCloseAction);
var
Socket: TCustomWinSocket;
begin
ACMC.Active := False;
ACMC.Free;
ACMO.Free;
Main.NotifyConnectList.Delete(Main.NotifyConnectList.IndexOf(ConnectNotifyInfo));
Main.NotifyReadList.Delete(Main.NotifyReadList.IndexOf(ReadNotifyInfo));
Main.NotifyDisconnectList.Delete(Main.NotifyDisconnectList.IndexOf(DisconnectNotifyInfo));
WindowItem.Delete;
if DataSocket <> nil then
begin
if not SocketConnected then
begin
Socket := DataSocket;
DataSocket := nil;
Connected := False;
Socket.Close;
end;
end;
end;
procedure TAudio.FormDeactivate(Sender: TObject);
begin
if WindowState = wsMinimized then Hide;
end;
procedure TAudio.FormCanResize(Sender: TObject; var NewWidth,
NewHeight: Integer; var Resize: Boolean);
begin
Resize := False;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -