Broadcaster -> Receiver -> Disk

Please post bug reports, feature requests, or any question regarding the DELPHI AREA products here.

Broadcaster -> Receiver -> Disk

Postby Lucas » March 6th, 2006, 11:39 am

Hello, i'm trying Wave Audios components with Dephi 7.
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;
Lucas
Member
Member
 
Posts: 2
Joined: March 6th, 2006, 11:34 am

Postby Kambiz » March 6th, 2006, 9:21 pm

I see that you've used the code in the other topic, but totally in a wrong way.

If you want to save audio in to a file, you do not need LiveAudioPlayer in the client part. You can find a solution as attachment.

Please consider that the current implementation of Broadcaster doesn't broadcast silence.
Attachments
ReceiverFile.zip
WaveAudio > Audio Boardcaster/Receiver Demo > Receiver Variation (storing received audio as a wave file)
(223.06 KiB) Downloaded 175 times
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2408
Joined: March 7th, 2003, 7:10 pm

Postby Lucas » March 8th, 2006, 12:04 pm

Thank you, i'll try it.
Lucas
Member
Member
 
Posts: 2
Joined: March 6th, 2006, 11:34 am


Return to DELPHI AREA Products

Who is online

Users browsing this forum: No registered users and 1 guest