Posts: 31
Threads: 19
Joined: Mar 2018
Reputation:
0
09-19-2019, 09:13 AM
(This post was last modified: 09-19-2019, 09:13 AM by Madammar.)
i have the following code
Code: procedure Tform1.rData(Sender: TObject; const Buffer: Pointer;
BufferSize: Cardinal; var FreeIt: Boolean);
var
Bytes: TIdBytes;
begin
if (Buffer <> nil) and (BufferSize > 0) And (udpreciver.Active) then
begin
udpsender.SendBuffer(ip, port, RawToBytes(Buffer^, Buffersize));
end;
end;
i want to add a text identification to each audio sent and then remove that text when its received
like following as example
Code: if (Buffer <> nil) and (BufferSize > 0) And (udpreciver.Active) then
begin
udpsender.SendBuffer(ip, port, RawToBytes('Audio1'+Buffer^, Buffersize));
end;
how to do this in correct way
Posts: 628
Threads: 2
Joined: Mar 2018
Reputation:
35
Location: USA
(09-19-2019, 09:13 AM)Madammar Wrote: i want to add a text identification to each audio sent and then remove that text when its received
like following as example
Code: if (Buffer <> nil) and (BufferSize > 0) And (udpreciver.Active) then
begin
udpsender.SendBuffer(ip, port, RawToBytes('Audio1'+Buffer^, Buffersize));
end;
how to do this in correct way
There are many different ways you can handle this.
Code: // Delphi XE7 or later only ...
var Bytes: TIdBytes;
Bytes := ToBytes('Audio1') + RawToBytes(Buffer^, Buffersize);
// or: Bytes := Concat(ToBytes('Audio1'), RawToBytes(Buffer^, Buffersize));
udpsender.SendBuffer(ip, port, Bytes);
Code: var Bytes: TidBytes;
Bytes := ToBytes('Audio1');
AppendBytes(Bytes, RawToBytes(Buffer^, Buffersize));
udpsender.SendBuffer(ip, port, Bytes);
Code: var Bytes: TidBytes;
SetLength(Bytes, 6 + Buffersize);
CopyTIdString('Audio1', Bytes, 0);
CopyTIdBytes(RawToBytes(Buffer^, Buffersize), 0, Bytes, 6, Buffersize);
udpsender.SendBuffer(ip, port, Bytes);
Code: var Bytes: TIdBytes;
SetLength(Bytes, 6 + Buffersize);
Move(PAnsiChar('Audio1')^, Bytes[0], 6);
Move(Buffer^, Bytes[6], Buffersize);
udpsender.SendBuffer(ip, port, Bytes);
Posts: 31
Threads: 19
Joined: Mar 2018
Reputation:
0
in receiving part can i delete the Audio1 Text and keep the actual byets ?
Posts: 628
Threads: 2
Joined: Mar 2018
Reputation:
35
Location: USA
09-24-2019, 06:04 PM
(This post was last modified: 09-24-2019, 06:04 PM by rlebeau.)
(09-21-2019, 04:41 PM)Madammar Wrote: in receiving part can i delete the Audio1 Text and keep the actual byets ?
You could, by simply using System.Delete() (XE7+ only) or Indy's RemoveBytes(). Or, you could Copy()/ CopyTIdBytes() the wanted bytes to another array.
But really, there is usually no need to do any of this. If the 1st 6 bytes are 'Audio1', just ignore those bytes and use/save the remaining bytes as needed. What is so hard about that?
Posts: 31
Threads: 19
Joined: Mar 2018
Reputation:
0
sorry for my bad english in my replys i wasn't mean to delete the text i was just asking about splitting this buffer
to differentiate between each audio sent sense i am using Audio1 as identification or more likely a header
thats why i wanted to know a way to split this buffer to check if this audio have a text of audio1 or not
which came to my mind i have to split out the Audio1 to a string variable and do the checking
Posts: 628
Threads: 2
Joined: Mar 2018
Reputation:
35
Location: USA
09-30-2019, 10:35 PM
(This post was last modified: 09-30-2019, 10:37 PM by rlebeau.)
(09-30-2019, 02:48 PM)Madammar Wrote: which came to my mind i have to split out the Audio1 to a string variable and do the checking
Indy has BytesToString() and BytesToStringRaw() functions, which take an optional index and length as input, eg:
Code: if BytesToString(bytes, 0, 6) = 'Audio1' then
begin
// use bytes[6]..bytes[High(bytes)] as needed...
end;
Or, you could simply use the RTL's CompareMem() function, eg:
Code: if (Length(bytes) >= 6) and CompareMem(PByte(bytes), PAnsiChar('Audio1'), 6) = 0 then
begin
// use bytes[6]..bytes[High(bytes)] as needed...
end;
Posts: 31
Threads: 19
Joined: Mar 2018
Reputation:
0
Code: procedure TForm1.udpreciverUDPRead(AThread: TIdUDPListenerThread;
const AData: TIdBytes; ABinding: TIdSocketHandle);
var
AudioDataSize: Integer;
deData : TIdBytes;
straudio : string;
begin
straudio := BytesToString(AData, 6, AData[High(AData)]); // here i have tried to get the orginal Bytes without Audio1
dedata := Tobytes(straudio);
end;
i tried to keep the original bytes after cutting it out
but its always get corrupted what i am doing wrong ?
Posts: 628
Threads: 2
Joined: Mar 2018
Reputation:
35
Location: USA
11-27-2019, 06:39 PM
(This post was last modified: 11-27-2019, 06:41 PM by rlebeau.)
(11-27-2019, 10:29 AM)Madammar Wrote: i tried to keep the original bytes after cutting it out
but its always get corrupted what i am doing wrong ?
Well, for starters, you are not calling BytesToString() correctly. The 3rd parameter is the number of bytes to copy, not a reference to a specific byte. It should be more like this instead:
Code: straudio := BytesToString(AData, 6, Length(AData)-6);
But why are you copying the binary data to a string at all? Converting bytes to a string requires a character encoding, which you are not specifying, so Indy's default encoding will be used, which is US-ASCII by default. So you are still going to get corrupted data for bytes > $7F.
If you are going to use a string, at least use Indy's 8-bit encoding to avoid data loss:
Code: straudio := BytesToString(AData, 6, Length(AData)-6, IndyTextEncoding_8Bit);
// or: straudio := BytesToStringRaw(AData, 6, Length(AData)-6);
deData := ToBytes(straudio, IndyTextEncoding_8Bit);
But really, just get rid of the string altogether, you don't need it at all:
Code: procedure TForm1.udpreciverUDPRead(AThread: TIdUDPListenerThread;
const AData: TIdBytes; ABinding: TIdSocketHandle);
var
deData : TIdBytes;
begin
dedata := Copy(AData, 6, Length(AData)-6);
// use deData as needed...
end;
Or:
Code: procedure TForm1.udpreciverUDPRead(AThread: TIdUDPListenerThread;
const AData: TIdBytes; ABinding: TIdSocketHandle);
var
deData : TIdBytes;
begin
deData := ToBytes(AData, Length(AData)-6, 6);
// use deData as needed...
end;
Or:
Code: procedure TForm1.udpreciverUDPRead(AThread: TIdUDPListenerThread;
const AData: TIdBytes; ABinding: TIdSocketHandle);
var
deData : TIdBytes;
begin
SetLength(deData, Length(AData)-6);
CopyTIdBytes(AData, 6, deData, 0, Length(deData));
// use deData as needed...
end;
Or, simply use a pointer to the data, don't even make a copy:
Code: procedure TForm1.udpreciverUDPRead(AThread: TIdUDPListenerThread;
const AData: TIdBytes; ABinding: TIdSocketHandle);
var
AudioDataSize: Integer;
deData: PByte;
begin
AudioDataSize := Length(AData)-6;
deData := @AData[6];
// use deData up to AudioDataSize bytes as needed...
end;
|