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
|

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

fredvs
Re-hello.

Ok, I do not find yet the guilty in my code, I will re-try a other day.
By the way, your code is working perfectly Sven.

Now, last part of the question: how to convert that file stored into ressource but without to write to the disk (only memory stream)?

A TFileStream could be used but it needs a path.
And in ressource there is no path to use in TFileStream.Create('/the/path/filename', fmOpenRead);

What could be the solution ?

Fre;D

Reply | Threaded
Open this post in threaded view
|

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

Free Pascal - General mailing list

Am 06.03.2017 23:57 schrieb "fredvs" <[hidden email]>:
>
> Re-hello.
>
> Ok, I do not find yet the guilty in my code, I will re-try a other day.
> By the way, your code is working perfectly Sven.
>
> Now, last part of the question: how to convert that file stored into
> ressource but without to write to the disk (only memory stream)?
>
> A TFileStream could be used but it needs a path.
> And in ressource there is no path to use in
> TFileStream.Create('/the/path/filename', fmOpenRead);
>
> What could be the solution ?

To access a file stored as a resource you need to use TResourceStream.

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
|

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

fredvs
> To access a file stored as a resource you need to use TResourceStream.

Ho my Dog, I did not know this one.
More than perfect, many thanks Sven.

PS: About array of float into TFileStream, after Googling a while, it s not possible to do directly like fpc does for C compilers.
You need a conversion (like explained by Lukasz Sokol).
And it is the reason why I am (once again) very impressed by fpc.

Many thanks Sven for your help.
Do you agree if I add your name into the list of the "great contributors" of uos (https://github.com/fredvs/uos) ?

Fre;D

Reply | Threaded
Open this post in threaded view
|

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

Free Pascal - General mailing list

Am 07.03.2017 13:49 schrieb "fredvs" <[hidden email]>:
>
> > To access a file stored as a resource you need to use TResourceStream.
>
> Ho my Dog, I did not know this one.
> More than perfect, many thanks Sven.
>
> PS: About array of float into TFileStream, after Googling a while, it s not
> possible to do directly like fpc does for C compilers.
> You need a conversion (like explained by Lukasz Sokol).
> And it is the reason why I am (once again) very impressed by fpc.

Huh? Of course you can do that rather similary in C as well.

> Many thanks Sven for your help.
> Do you agree if I add your name into the list of the "great contributors" of
> uos (https://github.com/fredvs/uos) ?
>

I don't think that's necessary. I'm merely doing my "job"...

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
|

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

Michael Van Canneyt


On Tue, 7 Mar 2017, Sven Barth via fpc-pascal wrote:

> Am 07.03.2017 13:49 schrieb "fredvs" <[hidden email]>:
>>
>>> To access a file stored as a resource you need to use TResourceStream.
>>
>> Ho my Dog, I did not know this one.
>> More than perfect, many thanks Sven.
>>
>> PS: About array of float into TFileStream, after Googling a while, it s
> not
>> possible to do directly like fpc does for C compilers.
>> You need a conversion (like explained by Lukasz Sokol).
>> And it is the reason why I am (once again) very impressed by fpc.
>
> Huh? Of course you can do that rather similary in C as well.

Indeed. You can do exactly the same. You can do this:

{$mode objfpc}
uses classes;


Type
   Float = Double;
   TFloatArray = Array of Float;

Procedure TestRW(X : TFloatArray);

Var
   F : TFileStream;
   Y : TFloatArray;
   I : Integer;

begin
   // Write
   F:=TFileStream.Create('float.dat',fmCreate);
   try
     F.WriteBuffer(X[0],Length(X)*SizeOf(Float));
   finally
     F.Free;
   end;
   // Read
   F:=TFileStream.Create('float.dat',fmOpenRead);
   try
     SetLength(Y,F.Size div SizeOf(Float));
     F.ReadBuffer(Y[0],F.Size);
   finally
     F.Free;
   end;
   // Check
   If Length(Y)<>Length(X) then
     Writeln('Wrong length')
   else For I:=0 to Length(X)-1 do
     if (X[i]<>Y[i]) then
       Writeln('Wrong element at pos ',i,': ',X[i]<>Y[i]);
end;

var
   X : TFloatArray;
   I : Integer;

begin
   SetLength(X,10);
   Writeln('Floats');
   For I:=0 to Length(X)-1 do
      X[i]:=10+(1/(1+I));
   TestRW(X);
   Writeln('integers');
   For I:=0 to Length(X)-1 do
      X[i]:=10+I;
   TestRW(X);
end.

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

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

el_es
Hi,

On 07/03/17 13:23, Michael Van Canneyt wrote:

>
>> Huh? Of course you can do that rather similary in C as well.
>
> Indeed. You can do exactly the same. You can do this:
>
> {$mode objfpc}
> uses classes;
>
>
> Type
>   Float = Double;
>   TFloatArray = Array of Float;
>
> Procedure TestRW(X : TFloatArray);
>
> Var
>   F : TFileStream;
>   Y : TFloatArray;
>   I : Integer;
>
> begin
>   // Write
>   F:=TFileStream.Create('float.dat',fmCreate);
>   try

so here is what I am seeing in Fre;D's code, that he managed to paste in one of the previous
messages :

// "FileBuffer.Data.Seek(0, soFromBeginning);"

      F.Seek(0, soFromBeginning);   /// this it would be in Michaels' code

I don't think it matters if you newly create the stream...
looks like something (probably) performed by the constructor already;

(just a stab in the dark here, unless we take the time to look into Fred's github ?)

-L.

>     F.WriteBuffer(X[0],Length(X)*SizeOf(Float));
>   finally
>     F.Free;
>   end;
>   // Read
>   F:=TFileStream.Create('float.dat',fmOpenRead);
>   try
>     SetLength(Y,F.Size div SizeOf(Float));
>     F.ReadBuffer(Y[0],F.Size);
>   finally
>     F.Free;
>   end;
>   // Check
>   If Length(Y)<>Length(X) then
>     Writeln('Wrong length')
>   else For I:=0 to Length(X)-1 do
>     if (X[i]<>Y[i]) then
>       Writeln('Wrong element at pos ',i,': ',X[i]<>Y[i]);
> end;
>
> var
>   X : TFloatArray;
>   I : Integer;
>
> begin
>   SetLength(X,10);
>   Writeln('Floats');
>   For I:=0 to Length(X)-1 do
>      X[i]:=10+(1/(1+I));
>   TestRW(X);
>   Writeln('integers');
>   For I:=0 to Length(X)-1 do
>      X[i]:=10+I;
>   TestRW(X);
> end.
>
> Michael.


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

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

fredvs
>> Many thanks Sven for your help.
>> Do you agree if I add your name into the list of the "great contributors" of
>> uos (https://github.com/fredvs/uos) ?
> I don't think that's necessary. I'm merely doing my "job"...

OK, I understand. (But I ask it always when I get great help, it is a matter of conscience and respect.)

>> Huh? Of course you can do that rather similary in C as well.
> Indeed. You can do exactly the same. You can do this:

Huh in C, in one shot, like fpc does, without conversion and working?

> (just a stab in the dark here, unless we take the time to look into Fred's github ?)

The code of uos_AddFromRessource(...) is not yet committed, I have to clean and control all before, but, of course it will be a great honor if you take a look in uos code.
And mainly if you find something wrong in the main loop or in the concept of uos.

For example, I got a advice that some data of uos are reference counted and that is bad.
OK, but how to know if data are reference counted and why is it bad?
(I did code for reference counted only for dynamic-loading of libraries).

A other advice was that is not good that each player is a thread.
OK, but how could you make the main program-thread still work if players are not independent thread?

Voila.

PS: There is a uos forum here: http://uos.2369694.n4.nabble.com 

Many thanks for your Sven, Michael and Lukasz.

Fre;D
Reply | Threaded
Open this post in threaded view
|

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

Free Pascal - General mailing list
On 07.03.2017 16:30, fredvs wrote:

>>> Many thanks Sven for your help.
>>> Do you agree if I add your name into the list of the "great contributors"
>>> of
>>> uos (https://github.com/fredvs/uos) ?
>> I don't think that's necessary. I'm merely doing my "job"...
>
> OK, I understand. (But I ask it always when I get great help, it is a matter
> of conscience and respect.)
>
>>> Huh? Of course you can do that rather similary in C as well.
>> Indeed. You can do exactly the same. You can do this:
>
> Huh in C, in one shot, like fpc does, without conversion and working?

The following code reads the floatbuf.txt file that my Pascal example
code generated and correctly prints 3.141500 42 times:

=== code begin ===

#include <stdio.h>

int main()
{
        float* floatbuf;
        FILE* file;

        file = fopen("floatbuf.txt", "rb");
        if (!file)
        {
                printf("Failed to open file\n");
                return 1;
        }

        floatbuf = malloc(sizeof(float) * 42);
        if (fread(floatbuf, sizeof(float), 42, file) != 42)
        {
                printf("Failed to read data\n");
                fclose(file);
                free(floatbuf);
                return 1;
        }

        for (int i = 0; i < 42; i++)
                printf("%f\n", (double)floatbuf[i]);

        fclose(file);
        free(floatbuf);

        return 0;
}

=== 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
|

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

fredvs
OK, Sven you win (and http://stackoverflow.com should follow fpc forum).
But you will not convince me that c does better than fpc, maybe equal but surely not better.

Huh, saving a buffer of float into wav file format, with correct header, --->
loaded by audacity ----> horrible noise.

loaded by fpc (via uos) ----> perfet sound.

But is maybe because wav format accept only in theory data from float.
(and that wav file are (nearly) always encoded from 32 bit integer and converted into float32).

But that is a other story.

Viva fpc.

Fre;D

Reply | Threaded
Open this post in threaded view
|

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

Free Pascal - General mailing list
On 07.03.2017 22:39, fredvs wrote:
> OK, Sven you win (and http://stackoverflow.com should follow fpc forum).
> But you will not convince me that c does better than fpc, maybe equal but
> surely not better.

Despite me definitely favoring Object Pascal as well, I can't you have
spreading incorrect information on C, can I? ;)

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
|

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

vojtech.cihak
In reply to this post by Free Pascal - General mailing list

Hi,

 

there are different informations about *.wav, so maybe there are different impementations too.

See:

http://soundfile.sapp.org/doc/WaveFormat/

and

https://blogs.msdn.microsoft.com/dawate/2009/06/23/intro-to-audio-programming-part-2-demystifying-the-wav-format/

 

The first says that 8-bit is unsigned, the second says signed.

The first does not mention floats, the second says 32-bits but only -1.0 to 1.0.

 

V.

 

______________________________________________________________
> Od: fredvs <[hidden email]>
> Komu: [hidden email]
> Datum: 07.03.2017 22:39
> Předmět: Re: [fpc-pascal] Adding a array of float in ressource and use it ?
>

OK, Sven you win (and http://stackoverflow.com should follow fpc forum).
But you will not convince me that c does better than fpc, maybe equal but
surely not better.

Huh, saving a buffer of float into wav file format, with correct header,
--->
loaded by audacity ----> horrible noise.

loaded by fpc (via uos) ----> perfet sound.

But is maybe because wav format accept only in theory data from float.
(and that wav file are (nearly) always encoded from 32 bit integer and
converted into float32).

But that is a other story.

Viva fpc.

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-tp5727765p5727801.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
|

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

el_es
On 07/03/17 23:03, Vojtěch Čihák wrote:
> Hi,
>
>  

intersting,

>
> there are different informations about *.wav, so maybe there are different impementations too.
>
> See:
>
> http://soundfile.sapp.org/doc/WaveFormat/
>

This also has endiannes information, which is quite cool..

> and
>
> https://blogs.msdn.microsoft.com/dawate/2009/06/23/intro-to-audio-programming-part-2-demystifying-the-wav-format/
>

and this one, seems to be saying the data and file size fields are 32 BYTES (emphasis mine) long.

>  
>
> The first says that 8-bit is unsigned, the second says signed.
>
> The first does not mention floats, the second says 32-bits but only -1.0 to 1.0.
>
>  
>
> V.

-L.

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

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

José Mejuto
In reply to this post by vojtech.cihak
El 08/03/2017 a las 0:03, Vojtěch Čihák escribió:

> there are different informations about *.wav, so maybe there are
> different impementations too.
> http://soundfile.sapp.org/doc/WaveFormat/
> https://blogs.msdn.microsoft.com/dawate/2009/06/23/intro-to-audio-programming-part-2-demystifying-the-wav-format/

Hello,

Wav (AKA RIFF WAVE) is a container (RIFF), not an audio format, inside a
wav you can put many audio formats and other informations like author,
copyright, cues, some of them standard (defined by Microsoft) and others
proprietary.

Usual wav files that most people use are PCM (Pulse Code Modulation) or
in more programmer words int16 per channel. This is the usual format,
but you can put inside floats, mp3, GSM, ADPCM, and many more.

@Fredvs: If your wav is noise in audacity the reason is quite sure
because you are writing float audio buffer with a wav PCM header. Wav
PCM is 4 bytes per sample in stereo while float is 8 bytes and almost
never used for wav. Header and audio data (format, channels, bit depth)
must match.


--

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

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

fredvs
In reply to this post by vojtech.cihak
> The first says that 8-bit is unsigned, the second says signed.
> The first does not mention floats, the second says 32-bits but only -1.0 to 1.0.

Hello.
Yes wav is nebulous and this site:

http://stackoverflow.com/questions/2062620/how-to-write-wav-file-with-32-bit-float-data

says it must set the wFormat tag in the 'fmt' chunk to WAVE_FORMAT_IEEE_FLOAT (3).

I have try but this does not work.

For recording, I will use a "custom" pcm format, using array of float (because with fpc we are not afraid to use float).

Thanks for infos.

> @Fredvs: If your wav is noise in audacity the reason is quite sure  because you are writing float audio buffer with a wav

Ho, it is not a problem, I knew that wav file prefer integers.
It was to see what Audacious has in his body.
For example, with Audacious you can export wav only as 16 bit.

But, now that I have full access to the samples, that fpc deals perfectly array of float in TFileStream, there is no more reason to save the recording into wav format. I will use my own one. 32 bit float.

> I can't you have spreading incorrect information on C, can I? ;)

Maybe but what I think is that C people always want to make things complicated.
And they should, IMO, sometimes think more "Pascalish".
The code of Sven is a perfect example.
He gives the easy-working solution in Pascal code and then translate the pascal code into C.
And it works for c. And all other c solutions I did found on the web were complicate-not-clear-not-working solutions, with lot of conversions.
If they could take a look at Sven code ---> 3 lines and it works.

Fre;D
Reply | Threaded
Open this post in threaded view
|

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

fredvs
In reply to this post by Free Pascal - General mailing list
>  can't you have spreading incorrect information on C

Hello.

Some more infos about the problem of our C friends

It seams that for C compilers (Clang, gcc, c++, VS,...) the way to order the data with a TFileStream differs (not same x-endians) for encoding float.

So you must do it by code and know what compiler was used for the client-program .

For a file created with a TFileStream of gcc but that will be used by a program compiled with VS, you should
convert the data from little endian to native ordering (or reverse) with something like:

        for i := 0 to nSamples-1 do
        b egin
          PCMIn[i] := (pcm_bytes[2*i+1] shl 8) or pcm_bytes[2*i];
        end;

And this the reason why, for storing data, they prefer to use integer 32 bit (where it seams that all compilers use the same order).

Voila.

But it is not our problem, so thank for everything and good luck to our C friends and their endians.

Fre;D


Reply | Threaded
Open this post in threaded view
|

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

Jonas Maebe-3
On 08/03/17 19:57, fredvs wrote:

> For a file created with a TFileStream of gcc but that will be used by a
> program compiled with VS, you should
> convert the data from little endian to native ordering (or reverse) with
> something like:
>
>         for i := 0 to nSamples-1 do
>         b egin
>           PCMIn[i] := (pcm_bytes[2*i+1] shl 8) or pcm_bytes[2*i];
>         end;
>
> And this the reason why, for storing data, they prefer to use integer 32 bit
> (where it seams that all compilers use the same order).

This has nothing to do with C compilers or with 16 vs 32 bit numbers. At
most, it will be different on little and big endian platforms, just like
with FPC. However, there are both little and big endian PCM 16 bit formats.


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

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

fredvs
> This has nothing to do with C compilers or with 16 vs 32 bit numbers.

Ha, ok.
Then I do not understand why C developers are afraid to store float data.

;-)

Fre;D
12