Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to set TidByets to Tbyets ?
#2
(01-02-2019, 08:58 PM)Madammar Wrote: i want to get its Adata as Tbyets how can i do that ??

You have two choices:

1. make a separate copy of the bytes as needed:

Code:
procedure Taudiofrm.recorderData(Sender: TObject; const Buffer: Pointer; BufferSize: Cardinal; var FreeIt: Boolean);
var
  Bytes: TBytes;
begin
  if (Buffer <> nil) and (BufferSize > 0) And (udpreciver.Active) then
  begin
    SetLength(Bytes, BufferSize);
    Move(Buffer^, Bytes[0], BufferSize);
    Bytes := Encodeaudio(Bytes);
    udpreciver.SendBuffer(ip, port, RawToBytes(Bytes[0], Length(Bytes)));
  end;
end;

procedure Taudiofrm.udpreciverUDPRead(AThread: TIdUDPListenerThread; const AData: TIdBytes; ABinding: TIdSocketHandle);
var
  Bytes: TBytes;
  AudioDataSize: Integer;
begin
  SetLength(Bytes, Length(AData));
  BytesToRaw(AData, Bytes[0], Length(AData));
  Bytes := Decodeaudio(Bytes);
  AudioDataSize := Length(Bytes);
  if (AudioDataSize > 10) then
  begin
    if BlockAlign > 1 then
      Dec(AudioDataSize, AudioDataSize mod BlockAlign);

    TThread.Queue(nil,
      procedure
      var
        AudioData : Pointer;
      begin
        try
          if not Player.Active then
          begin
            Player.Active := True;
            Player.WaitForStart;
          end;
        except
        end;

        AudioData := AudioBuffer.BeginUpdate(AudioDataSize);
        try
          Move(Bytes[0], AudioData^, AudioDataSize);
        finally
          AudioBuffer.EndUpdate;
        end;
      end
    );
  end else
  begin
    TThread.Queue(nil,
      procedure
      begin
        Player.Active := False;
        Player.WaitForStop;
      end
    );
  end;
end;

2. type-cast, since TBytes and TIdBytes are both dynamic arrays of bytes, so they are essentially the same thing in memory even though they are different types:

Code:
procedure Taudiofrm.recorderData(Sender: TObject; const Buffer: Pointer; BufferSize: Cardinal; var FreeIt: Boolean);
type
  PIdBytes = ^TIdBytes;
var
  Bytes: TBytes;
begin
  if (Buffer <> nil) and (BufferSize > 0) And (udpreciver.Active) then
  begin
    SetLength(Bytes, BufferSize);
    Move(Buffer^, Bytes[0], BufferSize);
    Bytes := Encodeaudio(Bytes);
    udpreciver.SendBuffer(ip, port, PIdBytes(@Bytes)^);
  end;
end;

procedure Taudiofrm.udpreciverUDPRead(AThread: TIdUDPListenerThread; const AData: TIdBytes; ABinding: TIdSocketHandle);
type
  PBytes = ^TBytes;
var
  Bytes: TBytes;
  AudioDataSize: Integer;
begin
  Bytes := Decodeaudio(PBytes(@AData)^);
  AudioDataSize := Length(Bytes);
  if (AudioDataSize > 10) then
  begin
    if BlockAlign > 1 then
      Dec(AudioDataSize, AudioDataSize mod BlockAlign);

    TThread.Queue(nil,
      procedure
      var
        AudioData : Pointer;
      begin
        try
          if not Player.Active then
          begin
            Player.Active := True;
            Player.WaitForStart;
          end;
        except
        end;

        AudioData := AudioBuffer.BeginUpdate(AudioDataSize);
        try
          Move(Bytes[0], AudioData^, AudioDataSize);
        finally
          AudioBuffer.EndUpdate;
        end;
      end
    );
  end else
  begin
    TThread.Queue(nil,
      procedure
      begin
        Player.Active := False;
        Player.WaitForStop;
      end
    );
  end;
end;

Reply


Messages In This Thread
how to set TidByets to Tbyets ? - by Madammar - 01-02-2019, 08:58 PM
RE: how to set TidByets to Tbyets ? - by rlebeau - 01-03-2019, 01:18 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)