Adding a array of float in ressource and use it ?

classic Classic list List threaded Threaded
37 messages Options
12
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Adding a array of float in ressource and use it ?

fredvs
Hello.

Is it possible to add a array of float into ressource ?

For example, there is myarray : array of cfloat.

Is it possible to store that array myarray ressource and, when I want to use it from ressource, I get also a array of float ?

In wiki : http://wiki.freepascal.org/Lazarus_Resources tey speak only how to add files into ressource.

Thanks.

Fre;D
Many thanks ;-)
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Adding a array of float in ressource and use it ?

Free Pascal - General mailing list

Am 04.03.2017 16:06 schrieb "fredvs" <[hidden email]>:
>
> Hello.
>
> Is it possible to add a array of float into ressource ?
>
> For example, there is myarray : array of cfloat.
>
> Is it possible to store that array myarray ressource and, when I want to use
> it from ressource, I get also a array of float ?
>
> In wiki : http://wiki.freepascal.org/Lazarus_Resources tey speak only how to
> add files into ressource.

No, you can't. Resources are not intended for this.

What are you trying to achieve?

Regards,
Sven


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Adding a array of float in ressource and use it ?

fredvs
> What are you trying to achieve?

Hello and thanks for answer.

Huh, I realize that what I ask is stupid.

Of course that array of float must be saved into file to be insered as ressource.

I want to save a buffer of audio samples (a array of float) and use that saved file as ressource for a other program.

That other program will use that ressource that will be converted back into a buffer of audio samples (array of float).

That is what I have now and that I envisage to do (but how?).

But perhaps, I am open for all,  the use a tmemorystream in place of the buffer of audio samples would be easier and will spare a conversion (array of float ---> bytes).

Last but not least, does it exist a way to compress a array of float (or the result in bytes).

Many thanks.

Fre;D





Many thanks ;-)
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Adding a array of float in ressource and use it ?

fredvs
Hello.

I have saved the buffer into file with a TFileStream.

With lot of success for array of cint16 and cint32.

But for array of cfloat --> much less success.

Could it be that TFileStream does accept only integers as data ?
If yes, a conversion would be needed ?

Something like: ?

for x := 0 to length(floatbuffer) -1 do
  begin
   floatbuffer[x] := round(floatbuffer[x] * 2147483647);
   if floatbuffer[x] > 2147483647 then floatbuffer[x] := 2147483647;
   if floatbuffer[x] < -2147483647 then floatbuffer[x] := -2147483647;
  end;
 

Thanks.

Fre;D


Many thanks ;-)
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Adding a array of float in ressource and use it ?

Free Pascal - General mailing list

Am 05.03.2017 18:33 schrieb "fredvs" <[hidden email]>:
>
> Hello.
>
> I have saved the buffer into file with a TFileStream.
>
> With lot of success for array of cint16 and cint32.
>
> But for array of cfloat --> much less success.

Define "much less success". How are you writing the array and how are you reading it?

> Could it be that TFileStream does accept only integers as data ?
> If yes, a conversion would be needed ?

One can write and read any blob of data with the stream classes if one knows what one is doing.

Regards,
Sven


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Adding a array of float in ressource and use it ?

fredvs
Define "much less success". How are you writing the array and how are you reading it?

Hello and thanks for answer.

Here how the array is written:

var
buffer : array of float;
....
FileBuffer.Data := TFileStream.Create(filename,fmCreate);
FileBuffer.Data.Seek(0, soFromBeginning);
FileBuffer.Data.WriteBuffer(buffer[0],Length(Buffer));

The result is OK if integer 16/32 bit inside the buffer.
If data inside the buffer is float 32 bit, result is a file of... 2 octets...

Thanks.

Fre;D

Many thanks ;-)
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Adding a array of float in ressource and use it ?

Michael Van Canneyt


On Mon, 6 Mar 2017, fredvs wrote:

> Define "much less success". How are you writing the array and how are you
> reading it?
>
> Hello and thanks for answer.
>
> Here how the array is written:
>
> var
> buffer : array of float;
> ....
> FileBuffer.Data := TFileStream.Create(filename,fmCreate);
> FileBuffer.Data.Seek(0, soFromBeginning);
> FileBuffer.Data.WriteBuffer(buffer[0],Length(Buffer));
>
> The result is OK if integer 16/32 bit inside the buffer.
> If data inside the buffer is float 32 bit, result is a file of... 2
> octets...

You must write

  FileBuffer.Data.WriteBuffer(buffer[0],Length(Buffer)*Sizeof(Float));

Because for a dynamic array, length returns the length in elements, not in bytes.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Adding a array of float in ressource and use it ?

fredvs
Hello Michael and thanks for answer.

>You must write
>  FileBuffer.Data.WriteBuffer(buffer[0],Length(Buffer)*Sizeof(Float));

Ha, perfect, many thanks.

But there is something that I do not catch, why is it working for integer ?

Sizeof Integer is 2 bits so, normally:

FileBuffer.Data.WriteBuffer(buffer[0],Length(Buffer));

should not work (but it works) ---> Length(Buffer) = 1/2 *  Length(Buffer)*Sizeof(integer)

?

Fre;D
Many thanks ;-)
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Adding a array of float in ressource and use it ?

Michael Van Canneyt


On Mon, 6 Mar 2017, fredvs wrote:

> Hello Michael and thanks for answer.
>
>> You must write
>>  FileBuffer.Data.WriteBuffer(buffer[0],Length(Buffer)*Sizeof(Float));
>
> Ha, perfect, many thanks.
>
> But there is something that I do not catch, why is it working for integer ?
>
> Sizeof Integer is 2 bits so, normally:
>
> FileBuffer.Data.WriteBuffer(buffer[0],Length(Buffer));
>
> should not work (but it works) ---> Length(Buffer) = 1/2 *
> Length(Buffer)*Sizeof(integer)

I don't know. By all logic, it should not work either.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Adding a array of float in ressource and use it ?

fredvs
> I don't know. By all logic, it should not work either.

Yes, it is I think too.

Some more explanation:

I use  a "global" buffer of float to store data.
Those data can be int16, int32 or float32.

For example,

If data are integer (16 or 32 bit), I do:

bufferfloat[x] := 127.0 ; (for int16)

bufferfloat[x] := 2147483646.0; (for int32)

If data are float 32 bit, I do:

bufferfloat[x] := 0.2147483646 ; (for float32)

So it seems to me Sizeof(bufferfloat[x]) is the same in the 3 cases (alway sizeof(float)).

Aaargh, sorry, I always come with extreme programming (or extreme dummy) question.

Fre;D
Many thanks ;-)
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Adding a array of float in ressource and use it ?

Free Pascal - General mailing list

Am 06.03.2017 13:42 schrieb "fredvs" <[hidden email]>:
>
> > I don't know. By all logic, it should not work either.
>
> Yes, it is I think too.
>
> Some more explanation:
>
> I useĀ  a "global" buffer of float to store data.
> Those data can be int16, int32 or float32.
>
> For example,
>
> If data are integer (16 or 32 bit), I do:
>
> bufferfloat[x] := 127.0 ; (for int16)
>
> bufferfloat[x] := 2147483646.0; (for int32)
>
> If data are float 32 bit, I do:
>
> bufferfloat[x] := 0.2147483646 ; (for float32)
>
> So it seems to me Sizeof(bufferfloat[x]) is the same in the 3 cases (alway
> sizeof(float)).

Of course it's always the same size as it's an array of cfloat after all and no assignment will change that.

Regards,
Sven


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Adding a array of float in ressource and use it ?

fredvs
> I don't know. By all logic, it should not work either.

OK, It comes from a "lucky" bug (thanks to reveal it).
There was a setlength(buffer, length(buffer) * channels) not needed.

Ok, fixed.

But the problem remain:

If data are int16 or int32: OK, the file is created with +- 1 mega bytes and I can get those data back.

But if data are float32: NOT OK, the file is created but with only 6 octets ;-( .

For example,

for x :=0 to length(bufferfloat) -1 do
bufferfloat[x] := 127 ; // for int16 ---> it works

for x :=0 to length(bufferfloat) -1 do
bufferfloat[x] := 2147483646;  // for int32 ---> it works

for x :=0 to length(bufferfloat) -1 do
bufferfloat[x] := 0.2147483646 ; // for float32 ----> it does not work  


Fre;D
Many thanks ;-)
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Adding a array of float in ressource and use it ?

Lukasz Sokol
On 06/03/17 14:50, fredvs wrote:

>> I don't know. By all logic, it should not work either.
>
> OK, It comes from a "lucky" bug (thanks to reveal it).
> There was a setlength(buffer, length(buffer) * channels) not needed.
>
> Ok, fixed.
>
> But the problem remain:
>
> If data are int16 or int32: OK, the file is created with +- 1 mega bytes and
> I can get those data back.
>
> But if data are float32: NOT OK, the file is created but with only 6 octets
> ;-( .
>
> For example,
>
> for x :=0 to length(bufferfloat) -1 do
> bufferfloat[x] := 127 ; // for int16 ---> it works
>
> for x :=0 to length(bufferfloat) -1 do
> bufferfloat[x] := 2147483646;  // for int32 ---> it works
>
> for x :=0 to length(bufferfloat) -1 do
> bufferfloat[x] := 0.2147483646 ; // for float32 ----> it does not work  
>

Looks like for float you'd need a conversion record

{enable packed records}

TFloatToBuffer = packed record
  case boolean of
    true: FloatField : float32;
    false : BufferField : array[0..3] of byte;
  end;
end;

for x := 0 to length(bufferfloat)-1 do
  bufferfloat[x].FloatField := {float value}

// but then special handling is likely needed to concat the buffer to write to file and reading from file
// so be careful ;)


-L.

>
> Fre;D
>
>
>
> -----
> Many thanks ;-)
> --
> View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Adding-a-array-of-float-in-ressource-and-use-it-tp5727765p5727784.html
> Sent from the Free Pascal - General mailing list archive at Nabble.com.
> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Adding a array of float in ressource and use it ?

Michael Van Canneyt
In reply to this post by fredvs


On Mon, 6 Mar 2017, fredvs wrote:

>> I don't know. By all logic, it should not work either.
>
> OK, It comes from a "lucky" bug (thanks to reveal it).
> There was a setlength(buffer, length(buffer) * channels) not needed.
>
> Ok, fixed.
>
> But the problem remain:
>
> If data are int16 or int32: OK, the file is created with +- 1 mega bytes and
> I can get those data back.
>
> But if data are float32: NOT OK, the file is created but with only 6 octets
> ;-( .
>
> For example,
>
> for x :=0 to length(bufferfloat) -1 do
> bufferfloat[x] := 127 ; // for int16 ---> it works
>
> for x :=0 to length(bufferfloat) -1 do
> bufferfloat[x] := 2147483646;  // for int32 ---> it works
>
> for x :=0 to length(bufferfloat) -1 do
> bufferfloat[x] := 0.2147483646 ; // for float32 ----> it does not work

If the bufferfloat is the same variable in all cases, there should be no difference
whatsoever. The write operation doesn't care a fig about the actual contents of the buffer.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Adding a array of float in ressource and use it ?

Michael Van Canneyt
In reply to this post by Lukasz Sokol


On Mon, 6 Mar 2017, Lukasz Sokol wrote:

> On 06/03/17 14:50, fredvs wrote:
>>> I don't know. By all logic, it should not work either.
>>
>> OK, It comes from a "lucky" bug (thanks to reveal it).
>> There was a setlength(buffer, length(buffer) * channels) not needed.
>>
>> Ok, fixed.
>>
>> But the problem remain:
>>
>> If data are int16 or int32: OK, the file is created with +- 1 mega bytes and
>> I can get those data back.
>>
>> But if data are float32: NOT OK, the file is created but with only 6 octets
>> ;-( .
>>
>> For example,
>>
>> for x :=0 to length(bufferfloat) -1 do
>> bufferfloat[x] := 127 ; // for int16 ---> it works
>>
>> for x :=0 to length(bufferfloat) -1 do
>> bufferfloat[x] := 2147483646;  // for int32 ---> it works
>>
>> for x :=0 to length(bufferfloat) -1 do
>> bufferfloat[x] := 0.2147483646 ; // for float32 ----> it does not work
>>
>
> Looks like for float you'd need a conversion record

There should be absolutely no need for that ?

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Adding a array of float in ressource and use it ?

Lukasz Sokol
In reply to this post by Lukasz Sokol
Or, quite comically maybe: use a text file... write/writeln and read/readln,
and use zip compression on the text file to compress data...

-L.

On 06/03/17 15:59, Lukasz Sokol wrote:

> On 06/03/17 14:50, fredvs wrote:
>>> I don't know. By all logic, it should not work either.
>>
>> OK, It comes from a "lucky" bug (thanks to reveal it). There was a
>> setlength(buffer, length(buffer) * channels) not needed.
>>
>> Ok, fixed.
>>
>> But the problem remain:
>>
>> If data are int16 or int32: OK, the file is created with +- 1 mega
>> bytes and I can get those data back.
>>
>> But if data are float32: NOT OK, the file is created but with only
>> 6 octets ;-( .
>>
>> For example,
>>
>> for x :=0 to length(bufferfloat) -1 do bufferfloat[x] := 127 ; //
>> for int16 ---> it works
>>
>> for x :=0 to length(bufferfloat) -1 do bufferfloat[x] :=
>> 2147483646;  // for int32 ---> it works
>>
>> for x :=0 to length(bufferfloat) -1 do bufferfloat[x] :=
>> 0.2147483646 ; // for float32 ----> it does not work
>>
>
> Looks like for float you'd need a conversion record
>
> {enable packed records}
>
> TFloatToBuffer = packed record case boolean of true: FloatField :
> float32; false : BufferField : array[0..3] of byte; end; end;
>
> for x := 0 to length(bufferfloat)-1 do bufferfloat[x].FloatField :=
> {float value}
>
> // but then special handling is likely needed to concat the buffer to
> write to file and reading from file // so be careful ;)
>
>
> -L.
>>
>> Fre;D
>>
>>
>>
>> ----- Many thanks ;-) -- View this message in context:
>> http://free-pascal-general.1045716.n5.nabble.com/Adding-a-array-of-float-in-ressource-and-use-it-tp5727765p5727784.html
>>
>>
Sent from the Free Pascal - General mailing list archive at Nabble.com.

>> _______________________________________________ fpc-pascal maillist
>> -  [hidden email]
>> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>>
>
>
> _______________________________________________ fpc-pascal maillist
> -  [hidden email]
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Adding a array of float in ressource and use it ?

fredvs
> Or, quite comically maybe: use a text file

Or maybe, like in my second post, convert float32 ---> integer32

for x := 0 to length(floatbuffer) -1 do
  begin
   floatbuffer[x] := round(floatbuffer[x] * 2147483647);
   if floatbuffer[x] > 2147483647 then floatbuffer[x] := 2147483647;
   if floatbuffer[x] < -2147483647 then floatbuffer[x] := -2147483647;
  end;

And do the reverse when reading from the file ?

Fre;D
Many thanks ;-)
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Adding a array of float in ressource and use it ?

Free Pascal - General mailing list
On 06.03.2017 19:31, fredvs wrote:

>> Or, quite comically maybe: use a text file
>
> Or maybe, like in my second post, convert float32 ---> integer32
>
> for x := 0 to length(floatbuffer) -1 do
>   begin
>    floatbuffer[x] := round(floatbuffer[x] * 2147483647);
>    if floatbuffer[x] > 2147483647 then floatbuffer[x] := 2147483647;
>    if floatbuffer[x] < -2147483647 then floatbuffer[x] := -2147483647;
>   end;
>
> And do the reverse when reading from the file ?

I don't know what you're doing wrong, but the following works:

=== code begin ===

program tfloatbuf;

{$mode objfpc}

uses
  SysUtils, Classes, ctypes;

var
  fbuf: array of cfloat;
  i: LongInt;
  fs: TFileStream;
begin
  SetLength(fbuf, 42);
  for i := Low(fbuf) to High(fbuf) do
    fbuf[i] := 3.1415;

  Writeln('Writing data to ', ParamStr(1));
  fs := TFileStream.Create(ParamStr(1), fmOpenWrite or fmCreate);
  try
    fs.WriteBuffer(fbuf[0], Length(fbuf) * SizeOf(fbuf[0]));
  finally
    fs.Free;
  end;

  SetLength(fbuf, 0);
  SetLength(fbuf, 42);

  Writeln('Reading data from ', ParamStr(1));
  fs := TFileStream.Create(ParamStr(1), fmOpenRead);
  try
    fs.ReadBuffer(fbuf[0], Length(fbuf) * SizeOf(fbuf[0]));
  finally
    fs.Free;
  end;

  for i := Low(fbuf) to High(fbuf) do
    Writeln(fbuf[i]);
end.

=== code end ===

Regards,
Sven
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Adding a array of float in ressource and use it ?

fredvs
> I don't know what you're doing wrong, but the following works:

Huh, it is exactly what I (think to) do.
And re-reading hundred times my code does not see any difference.

OK, I will re-read thousand times the code, maybe I will find what is wrong.

Many thanks for your code Sven.

Fre;D
Many thanks ;-)
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Adding a array of float in ressource and use it ?

Free Pascal - General mailing list

Am 06.03.2017 22:45 schrieb "fredvs" <[hidden email]>:
>
> > I don't know what you're doing wrong, but the following works:
>
> Huh, it is exactly what I (think to) do.
> And re-reading hundred times my code does not see any difference.
>
> OK, I will re-read thousand times the code, maybe I will find what is wrong.

Then try to minimize your code as much as possible and then show it here (an as small as possible compileable example that shows your problem).

Regards,
Sven


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
12
Loading...