I've seen Receiver and Broadcaster which is working good.
I would like to record sound i receive from Receiver to a file like toto.wav.
I've tried this method : http://www.delphiarea.com/forum/viewtop ... light=wave with LiveAudioPlayer but it doesn't work.
My .wav is only 0 Ko. I don't understand why...
These are my modifications of your code.
- Code: Select all
procedure TMainForm.FormCreate(Sender: TObject);
begin
AudioBuffer := TAudioBuffer.Create;
end;
procedure TMainForm.FormDestroy(Sender: TObject);
begin
tcpClient.Active := False;
LiveAudioPlayer.WaitForStop;
AudioBuffer.Free;
end;
procedure TMainForm.tcpClientConnect(Sender: TObject; Socket: TCustomWinSocket);
begin
btnDisconnect.Visible := True;
btnConnect.Visible := False;
edRemoteAddress.Enabled := False;
seRemotePort.Enabled := False;
end;
procedure TMainForm.tcpClientDisconnect(Sender: TObject; Socket: TCustomWinSocket);
begin
AudioBuffer.Clear;
LiveAudioPlayer.Active := False;
btnConnect.Visible := True;
btnDisconnect.Visible := False;
edRemoteAddress.Enabled := True;
seRemotePort.Enabled := True;
edFormat.Text := '';
end;
procedure TMainForm.btnConnectClick(Sender: TObject);
begin
tcpClient.Host := edRemoteAddress.Text;
tcpClient.Port := seRemotePort.Value;
tcpClient.Active := True;
end;
procedure TMainForm.btnDisconnectClick(Sender: TObject);
begin
//tcpClient.Socket.Close;
tcpClient.Active := False;
end;
procedure TMainForm.LiveAudioPlayerActivate(Sender: TObject);
begin
SetPCMAudioFormatS(@WaveFormatEx, LiveAudioPlayer.PCMFormat);
mmIO := CreateStreamWaveAudio(Stream, @WaveFormatEx, ckInfo, ckData);
tcpClient.Socket.SendText('READY');
end;
procedure TMainForm.LiveAudioPlayerDeactivate(Sender: TObject);
begin
tcpClient.Active := False;
CloseWaveAudio(mmio, ckInfo, ckData);
try
Stream.SaveToFile('Name.WAV');
except
end;
end;
procedure TMainForm.LiveAudioPlayerError(Sender: TObject);
begin
tcpClient.Active := False;
MessageDlg(LiveAudioPlayer.LastErrorText, mtError, [mbOK], 0);
end;
procedure TMainForm.LiveAudioPlayerLevel(Sender: TObject; Level: Integer);
begin
pbLevel.Position := Level;
end;
procedure TMainForm.LiveAudioPlayerFormat(Sender: TObject;
out pWaveFormat: PWaveFormatEx; var FreeIt: Boolean);
begin
FreeIt := True;
pWaveFormat := WaveFormat;
edFormat.Text := GetWaveAudioFormat(WaveFormat);
BlockAlign := WaveFormat^.nBlockAlign;
WaveFormat := nil;
end;
function TMainForm.LiveAudioPlayerDataPtr(Sender: TObject;
out Buffer: Pointer; var NumLoops: Cardinal;
var FreeIt: Boolean): Cardinal;
begin
if not tcpClient.Active then
Result := 0 // Stops LiveAudioPlayer
else if AudioBuffer.Get(Buffer, Result) then
FreeIt := True
else
begin
Buffer := nil; // When Buffer is nil,
Result := 10 // Result will be considered as silence milliseconds.
end
end;
procedure TMainForm.tcpClientRead(Sender: TObject; Socket: TCustomWinSocket);
var
WaveFormatSize: Integer;
Data: Pointer;
DataSize: Integer;
begin
try
if not LiveAudioPlayer.Active then
begin
Socket.ReceiveBuf(WaveFormatSize, SizeOf(WaveFormatSize));
while Socket.ReceiveLength < WaveFormatSize do
Sleep(0);
ReallocMem(WaveFormat, WaveFormatSize);
try
Socket.ReceiveBuf(WaveFormat^, WaveFormatSize);
except
ReallocMem(WaveFormat, 0);
raise;
end;
LiveAudioPlayer.Active := True;
end
else
begin
Sleep(0); // giving chance to the player thread for using the audio buffer
DataSize := Socket.ReceiveLength;
if BlockAlign > 1 then
Dec(DataSize, DataSize mod BlockAlign);
if DataSize <> 0 then
begin
Data := AudioBuffer.BeginUpdate(DataSize);
try
Socket.ReceiveBuf(Data^, DataSize);
finally
AudioBuffer.EndUpdate;
end;
end;
end;
except
tcpClient.Active := False;
Application.HandleException(Self);
end;
end;
function TMainForm.LiveAudioPlayerData(Sender: TObject;
const Buffer: Pointer; BufferSize: Cardinal;
var NumLoops: Cardinal): Cardinal;
begin
mmioWrite(mmIO, Buffer, BufferSize);
end;