Title: Posting a ByteArray Post by: Walter on October 22, 2012, 08:55:57 PM I'm doing a simple HTTP Post. But I need to send raw audio. I can get the raw audio into the buffer variable (see below). What is best way to send it... I thought I should load a RTCByteArray and then in the BeignRequest send the RTCByteArray.
I don't know how to do this. Any advice is appreciated. Walter Var buffer:array [0..1023] of Byte; bytesRed : integer; -------------- With DataRequest do begin // Is this correct? Request.Info.NewByteArray('wav', WaveStorage1.Wave.DataSize); if WaveStorage1.Wave.BeginRead then begin try BytesRead := WaveStorage1.Wave.Read(Buffer, SizeOf(Buffer)); while BytesRead > 0 do begin // process the buffer // How do I load the Buffer into the RTCByteArray?? BytesRead := WaveStorage1.Wave.Read(Buffer, SizeOf(Buffer)); end; finally WaveStorage1.Wave.EndRead; end; Request.Close := False; Request.Method := RtcString('POST'); Request.Query.Text := RtcString('appId='+appId+'&appKey='+appKey+'&id='+id); Request.Host := RtcString('dictation.service.net:443'); Request.FileName := RtcString('/Cmd/dictation'); Request['Accept'] := RtcString('text/plain'); Request['Content-Type'] := RtcString('audio/x-wav;codec=pcm;bit=16;rate=8000'); Request['Accept-Language'] := RtcString('en-US'); Request['Accept-Topic'] := RtcString('WebSearch'); end; end; How so I send the ByteArray? procedure TForm1.DataRequestBeginRequest(Sender: TRtcConnection); begin with TRtcDataClient(Sender) do begin // Write(Request.Info['wav']); // Send ByteArray end; end; Title: Re: Posting a ByteArray Post by: D.Tkalcec (RTC) on October 22, 2012, 10:37:36 PM Here is the declaration for RtcByteArray:
type RtcByteArray = array of byte; As you can see, it's simply a dynamid array of bytes. You can access elements of a dynamic byte array the same way you would access elements of a static byte array. The only difference between the two is that the size of a static byte array is explicitly set in the declaration section (array[0..1023] of byte), while the size of a dynamic byte array is set at runtime. I've assumed that you want to write directly to the bytearray, since you have set the byte array length up-front. If that is the case, then here is the code which should read the WaveStorage1 content directly into a RtcByteArray and then send it out in the OnBeginRequest event. I've marked all the important changes in bold ... Var buffer : RtcByteArray; bytesRead, bytesIn : integer; -------------- With DataRequest do begin if WaveStorage1.Wave.BeginRead then begin try buffer := Request.Info.NewByteArray('wav', WaveStorage1.Wave.DataSize); bytesIn := 0; // number of bytes already inside the buffer repeat // appending anything you can read from "WaveStorage1", at the end of our "buffer" ... bytesRead := WaveStorage1.Wave.Read (buffer[ bytesIn ], Length(buffer)-bytesIn); // update our buffer pointer "bytesIn" ... Inc(bytesIn, bytesRead); // loop until everything is read, or the read operation returns 0 bytes ... until (bytesRead <= 0) or (bytesIn >= Length(Buffer)); if bytesIn<Length(buffer) then begin // this is just a precaution, in case we don't want to send only part of data out ... Request.Info.isNull['wav']:=True; // clear the byte array raise Exception.Create('Error reading WaveStorage'); end; finally WaveStorage1.Wave.EndRead; end; Request.Close := False; Request.Method := 'POST'; Request.Query.Text := 'appId='+appId+'&appKey='+appKey+'&id='+id; Request.Host := 'dictation.service.net:443'; Request.FileName := '/Cmd/dictation'; Request['Accept'] := 'text/plain'; Request['Content-Type'] := 'audio/x-wav;codec=pcm;bit=16;rate=8000'; Request['Accept-Language'] := 'en-US'; Request['Accept-Topic'] := 'WebSearch'; Post; // post the request to the request queue end; end; procedure TForm1.DataRequestBeginRequest(Sender: TRtcConnection); begin with TRtcDataClient(Sender) do // Do we have a bytearray prepared for sending? if Request.Info.CheckType('wav',rtc_ByteArray) then // send the header and the content WriteEx(Request.Info.asByteArray['wav']) else // no content body, just send the headers WriteHeader; end; Best Regards, Danijel Tkalcec Title: Re: Posting a ByteArray Post by: Walter on October 23, 2012, 12:09:44 AM Thank you does not convey my appreciation for you, and your products!
Walter Title: Re: Posting a ByteArray Post by: D.Tkalcec (RTC) on October 23, 2012, 08:59:54 AM ;D
Title: Re: Posting a ByteArray Post by: Walter on November 02, 2012, 05:23:16 PM Follow up...
How do I manage/handle a Reponse where it is chunked - Response.ChunkedTransferEncoding = True; Walter var sResponse : string; procedure TfrmScratchPadDlg.DataRequestDataReceived(Sender: TRtcConnection); begin with TRtcDataClient(Sender) do if Response.Done then begin if Resonse.ChunkedTransferEncoding then begin // ???? else begin sResponse := Read; // process the response.... end; end; Title: Re: Posting a ByteArray Post by: D.Tkalcec (RTC) on November 02, 2012, 07:20:37 PM Simply use the Read or ReadEx method, just like you would for standard transfer encoding.
Best Regards, Danijel Tkalcec Title: Re: Posting a ByteArray Post by: Walter on November 02, 2012, 08:13:18 PM Does the Response.Done only get set to True after all the Chunks are received?
The DataReceive Event is being called more that once with Response.Done = True. So should I execute Read and concatenate results? - how do I know when the Chunks are completely received? Walter Title: Re: Posting a ByteArray Post by: D.Tkalcec (RTC) on November 02, 2012, 08:45:04 PM The "Response.Done" property should be set to TRUE only once, after the complete response was received. If the Response.Done property was being set to TRUE before the last chunk of data was received, then something is not right (either with the Project, or with the response being received) and I will need an example for testing, to see what exactly is happening.
Best Regards, Danijel Tkalcec Title: Re: Posting a ByteArray Post by: Walter on November 02, 2012, 10:05:03 PM Thank you for confirming. I made a simple version and of the program and I don't see the repeated calls to the DataReceive event... so it must b me! (no suprise there).
Have a great weekend. Walter |