Re: [fpc-pascal] Array as result in function.
On Fri, Jan 20, 2017 at 9:06 PM, silvioprog wrote: [...] > `_pcm` on original code is an `int16` buffer... consider to using `cint16`: > Damn Gmail's Ctrl+Enter. -.-' I meant: "`_pcm` on original code is an `int16` buffer... consider to using `*pcint16*`:" -- Silvio Clécio ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Array as result in function.
On Fri, Jan 20, 2017 at 7:36 PM, fredvs wrote: > Re-hello. > > Ok, thanks Silvio, I will take this one from your advice, it works like > charm: > > type > TOggOpusFile = THandle; > Hm... you should keep the same C data types. :-) I took a look at `OggOpusFile` type, it is a struct: https://github.com/gcp/opusfile/blob/9a9825df0319138fe845cbb19c250b642965c4b1/include/opusfile.h#L133 so on Pascal it makes more sense declared as: type POggOpusFile = ^OggOpusFile; OggOpusFile = record end; and finally: op_read: function(OpusFile: POggOpusFile; ... `PDArFloat` seems OK to `op_read_float`, but notice last comment regarding `_pcm`. "li: pointer" should be "li: pcint" in both funcs. go slowly: these changes can raise new errors on your code. :-) TDArFloat = array of cfloat; > PDArFloat = ^TDArFloat; > > op_read: function(OpusFile: TOggOpusFile; pcm : PDArFloat; SampleCount: > Integer; li: pointer): Integer; > `_pcm` on original code is an `int16` buffer... consider to using `cint16`: https://github.com/gcp/opusfile/blob/9a9825df0319138fe845cbb19c250b642965c4b1/include/opusfile.h#L1873 op_read_float: function(OpusFile: TOggOpusFile; pcm : PDArFloat; > SampleCount: Integer; li: pointer): Integer; > > Many thanks. > > Fre;D -- Silvio Clécio ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Array as result in function.
On Fri, Jan 20, 2017 at 7:11 PM, fredvs wrote: > Hello Silvio. > > Wow, thanks, I will study it deep. > > By the way, the Opus Pascal wrappers are working. > You may listen, seek, saving to file, apply dsp,.. to Opus files. > You may try SimplePlayer demo in uos (all libraries and Opus-audiofile > included): > > https://github.com/fredvs/uos > > PS: BufferIn is working only if it is a array of cfloat (from ctypes.pas). > With array of double, array of float,.. it does not work. Yes, because C `float`'s size is 4, and Pascal `double`'s, 8. Pascal `cfloat` is just a alias to `single`, 4. :-) Try two tests: $ echo -e '#include \nint main(){printf("size of float: %zu\\n", sizeof(float));return 0;}' > fredvs.c && gcc -o fredvs fredvs.c && clear && ./fredvs # it prints "size of float: 4" on your terminal and: $ echo "program fredvs;begin writeln('sizeof double: ', sizeof(double));writeln('size of single: ', sizeof(single));end." > fredvs.pp && fpc fredvs.pp && clear && ./fredvs # it prints "sizeof double: 8" and "size of single: 4" on your terminal Some useful links: FPC Real types: http://freepascal.org/docs-html/ref/refsu6.html#x28-310003.1.2 C - Data Types: https://www.tutorialspoint.com/cprogramming/c_data_types.htm Delphi to C++ types mapping: http://docwiki.embarcadero.com/RADStudio/Berlin/en/Delphi_to_C%2B%2B_types_mapping Fre;D > > - > Many thanks ;-) -- Silvio Clécio ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Array as result in function.
Re-hello. Ok, thanks Silvio, I will take this one from your advice, it works like charm: type TOggOpusFile = THandle; TDArFloat = array of cfloat; PDArFloat = ^TDArFloat; op_read: function(OpusFile: TOggOpusFile; pcm : PDArFloat; SampleCount: Integer; li: pointer): Integer; op_read_float: function(OpusFile: TOggOpusFile; pcm : PDArFloat; SampleCount: Integer; li: pointer): Integer; Many thanks. Fre;D - Many thanks ;-) -- View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Array-as-result-in-function-tp5727366p5727385.html Sent from the Free Pascal - General mailing list archive at Nabble.com. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Array as result in function.
Hello Silvio. Wow, thanks, I will study it deep. By the way, the Opus Pascal wrappers are working. You may listen, seek, saving to file, apply dsp,.. to Opus files. You may try SimplePlayer demo in uos (all libraries and Opus-audiofile included): https://github.com/fredvs/uos PS: BufferIn is working only if it is a array of cfloat (from ctypes.pas). With array of double, array of float,.. it does not work. Fre;D - Many thanks ;-) -- View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Array-as-result-in-function-tp5727366p5727384.html Sent from the Free Pascal - General mailing list archive at Nabble.com. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Array as result in function.
On Fri, Jan 20, 2017 at 5:14 PM, silvioprog wrote: [...] > > It seems that function just increment the address of a (float) buffer, so: > I meant "It seems that function just fill a (float) buffer". ^^' -- Silvio Clécio ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Array as result in function.
On Fri, Jan 20, 2017 at 3:45 PM, fredvs 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 ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Array as result in function.
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); The nice thing is that BufferIn can be used for portaudio, soundfile, mpg123, aac buffers and is working like charm. Fre;D - Many thanks ;-) -- View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Array-as-result-in-function-tp5727366p5727381.html Sent from the Free Pascal - General mailing list archive at Nabble.com. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Array as result in function.
On Fri, Jan 20, 2017 at 1:12 PM, fredvs wrote: [...] Please look at Sven's warn regarding Pascal arrays mixed with C arrays. Could you show us the original C function? -- Silvio Clécio ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Array as result in function.
Hello. Hum,... I am guilty. ;-( There was a SetLength(Bufferin, x) hidden. So the length of bufferin into the function was not the same as the "pure" one. So, to resume: If using: function arraycopy(arrayin : Tarrayfloat): Tarrayfloat; begin result := arrayin; end; ==> Perfect, the input arrayin = result. ;-( Sorry for the noise (but I m still open for other solutions than dealing with dynamic arrays). (But dynamic arrays are wow). Thanks. Fre;D Fre;D - Many thanks ;-) -- View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Array-as-result-in-function-tp5727366p5727379.html Sent from the Free Pascal - General mailing list archive at Nabble.com. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Array as result in function.
Hello. Some more investigations. Let say we have a array of float of length = 100 (arrayin): setlength(arrayin,100); A external library fill this array with 80 samples (ouframes). When using that arrayin pure (without any dsp) the sound is pure too. If using: function arraycopy(arrayin : Tarrayfloat): Tarrayfloat; begin result := arrayin; end; ==> Bad noisy sound, even using but if using: function arraycopy(arrayin : Tarrayfloat; outfames: integer): Tarrayfloat; begin SetLength(arrayin, outframes); result := arrayin; end; ===> OK, perfect sound. Why ? _ > You shouldn't use Pascal arrays when interfacing with C/C++ code as > especially dynamic arrays have a different format (though you can pass a > pointer to the first array element). Huh, what do you propose instead, I am totally open to new idea ? (But using dynamic arrays gives me lovely results). > Maybe show us how the original C function looks like. All the audio-wrappers use buffers to store the data. I use dynamic arrays for that buffers. In https://github.com/fredvs/uos there are Pascal wrappers to C libraries: uos_Portaudio.pas uos_Mpg123.pas uos_SoundFile.pas uos_Opus.pas uos_AAC.pas uos_SoundTouch.pas ... Thanks. Fre;D Thanks. Fre;D - Many thanks ;-) -- View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Array-as-result-in-function-tp5727366p5727378.html Sent from the Free Pascal - General mailing list archive at Nabble.com. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Array as result in function.
Am 20.01.2017 13:18 schrieb "fredvs" : > > Thanks very much for answers. > > @ silvioprog: > > > using that function as callback with some (C/C++) library? > > Yes and check its parameter calling convention is ok. You shouldn't use Pascal arrays when interfacing with C/C++ code as especially dynamic arrays have a different format (though you can pass a pointer to the first array element). Maybe show us how the original C function looks like. Regards, Sven ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] fpc and voip ?
> Maybe you may think in Opus http://opus-codec.org/ Yep, wrappers for libopus and libopusfile are translated and working for fpc. Also Opus is part now of uos (and working). Libraries for Linux64 and Win32 included (for other os, it will come asap). Updated SimplePlayer demo with Opus sample. https://github.com/fredvs/uos Ok, All the tools for voip and soip (sound over ip) are ready and working. Maybe I will need some help for Opus server-web-streaming. Fre;D - Many thanks ;-) -- View this message in context: http://free-pascal-general.1045716.n5.nabble.com/fpc-and-voip-tp5727307p5727376.html Sent from the Free Pascal - General mailing list archive at Nabble.com. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Array as result in function.
Thanks very much for answers. @ silvioprog: > using that function as callback with some (C/C++) library? Yes and check its parameter calling convention is ok. @ Martin. > result:= copy(arrayin); Ha, I did not know this one. I will try it (and write you asap). Many thanks. Fre;D - Many thanks ;-) -- View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Array-as-result-in-function-tp5727366p5727375.html Sent from the Free Pascal - General mailing list archive at Nabble.com. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Array as result in function.
Am 2017-01-20 um 07:52 schrieb Martin Schreiber: > On Thursday 19 January 2017 22:50:36 fredvs wrote: >> function array_in_out(arrayin: TArFloat): TArFloat; >> begin >> result := arrayin; >> end; > Do you change items of "arrayin" later? If so the items of the result array > will be changed too, dynamic array assignment copies the data pointer only. > http://www.freepascal.org/docs-html/current/ref/refsu15.html#x39-520003.3.1 > Use > " > function array_in_out(arrayin: TArFloat): TArFloat; > begin > result:= copy(arrayin); > end; > " > if the result array must be independent. While the original Pascal language was clear and logical, it has become ambiguous with managed types. In this declaration var X : int64; "X" always means the 8 bytes that hold the integer. "@X" always means the 4 bytes of the *address* where the 8 bytes start. Here you are always aware what is meant: Either the data or the address of the data (pointer). In this declaration: var A : array of int64; "A" means the (elements of the) array if you index it as in "A[7]" but it means the (4 byte) pointer to the first element if you use it as function parameter or in assignments. And even worse, *some* managed types (like strings) have "copy-on-write". IMO this is all very confusing and leads to a lot of hard to spot bugs but it cannot be changed anymore. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Drawing bitmap by pixel
On 2017-01-20 01:50, Ryan Joseph wrote: > The max color value is 65535. I thought it was 1.0 or 255. The FPImage implementation supports 16bit color channels (a bit of future proofing that confuses many). Most programs and images use 8bits per channel. Each color channel is thus a WORD size, not a BYTE size. That is why my example program does the following: // duplicate the data to fill a WORD size channel r := (r shl 8) or r; g := (g shl 8) or g; b := (b shl 8) or b; eg: if r was initial $00AC (by using Random($FF) to populate it), I then duplicate the initial value to fill the WORD size value, and the above code will make r = $ACAC. Strictly not 100% correct thing to do, but seems to work with FPImage. Regards, Graeme -- fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/ My public PGP key: http://tinyurl.com/graeme-pgp ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal