[fpc-pascal] TFileStream.WriteBuffer() and RAM used ?

José Mejuto joshyfun at gmail.com
Mon Dec 12 00:21:50 CET 2016


El 11/12/2016 a las 14:39, fredvs escribió:

> @ Jose : uos uses PortAudio library to grap input from devices. I will
> deeply study your propositions.

Hello,

After a fast view of PortAudio API you can produce a code like:

procedure WriteStreamToFile;
var
   F: TFileStream;
   lAvailableFrames: integer;
   lAvailableBytes: integer;
   lLocalBuffer: array [0..16383] of Byte;
begin
   F:=TFileStream.Create('YourFile.Extension',fmCreate);
   F.write(SomeHeaderData,SizeOf(SomeHeaderData));
   while NotUserBreak_OrLimitReached do
   begin
     lAvailableFrames:=Pa_GetStreamReadAvailable(YourAudioStream);
     lAvailableBytes:=lAvailableFrames * (YourStream_BYTES_PER_FRAME);
     if lAvailableBytes<16384 then
     begin
       // Don't read, just wait a bit.
       Sleep(1);
     end
     else
     begin
       Pa_ReadStream(
         YourAudioStream,
         @lLocalBuffer[0],
         16384 div (YourStream_BYTES_PER_FRAME
       );
       F.WriteBuffer(lLocalBuffer[0],16384);
     end;
   end;
   F.WriteBuffer((SomeTailData,Sizeof(SomeTailData));
   F.Position:=XXXX;
   F.WriteBuffer(SomeDataWithStreamSize,Sizeof(SomeDataWithStreamSize));
   F.Free;
end;

This code should work because PortAudio (and almost 100% APIs of this 
kind) has an internal buffer which is large enough to hold new data 
while you are writing to disk and performing other tasks. Some even use 
a dynamic buffer which can hold audio for several seconds before you 
extract them using the API.

Note: Of course the maths are the expected ones, you must know the 
BYTES_PER_FRAME size and the format of that frames, maybe int16, maybe 
float, maybe ....

-- 



More information about the fpc-pascal mailing list