[fpc-pascal] Array as result in function.

silvioprog silvioprog at gmail.com
Fri Jan 20 21:14:17 CET 2017


On Fri, Jan 20, 2017 at 3:45 PM, fredvs <fiens at hotmail.com> wrote:
>
> Here, for example from OpusFile.h =>
>
> OP_WARN_UNUSED_RESULT int op_read_float(OggOpusFile *_of,
>  float *_pcm,int _buf_size,int *_li) OP_ARG_NONNULL(1);
>
> Translated in fpc with:
>
> op_read: function(OpusFile: TOggOpusFile; var pcm; SampleCount: Integer;
> li:
> pointer): Integer;
>
> And used like this:
> BufferIn : array of cfloat;
>
> outframes := op_read_float(HandleOF,BufferIn[0],Wantframes *Channels,
> nil);
>

You should keep the Pascal code syntax closest to the C one, something like
this:

uses ctypes;

POggOpusFile = ^TOggOpusFile
TOggOpusFile = record // please check if original C struct uses some
alignment ...
end;

function op_read_float(_of: POggOpusFile; _pcm: Pcfloat; _buf_size: cint;
_li: pcint): cint; cdecl; ... blah blah blah
// or dynamic loading like you showed (on Delphi you can use the `delayed`
keyword instead of declaring `GetProcedureAddress` by hand :-) )

{$MACRO ON}
{$DEFINE OP_ARG_NONNULL := op_read_float}

It is useful when we need to translate the examples.

It seems that function just increment the address of a (float) buffer, so:

var
...
  buf: pcfloat;
begin
  ... HandleOF/buf initialization etc. ...
  outframes := OP_ARG_NONNULL(HandleOF, buf, Wantframes * Channels, nil);
... code ...
  buf += (Wantframes * Channels) * sizeof(cfloat) // or inc(buf,
(Wantframes * Channels) * sizeof(cfloat));

( you can try "buf += Wantframes * Channels; // or inc(buf, Wantframes *
Channels);" too )

I'm not sure if it works, but you can check it by yourself. :-)


> The nice thing is that BufferIn can be used for portaudio, soundfile,
> mpg123, aac buffers and is working like charm.


Awesome. :-)

Fre;D
>
> -----
> Many thanks ;-)


-- 
Silvio Cl├ęcio
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20170120/a812c7d6/attachment.html>


More information about the fpc-pascal mailing list