Recent

Author Topic: Use music (wav) from resource  (Read 637 times)

tonse

  • New member
  • *
  • Posts: 5
Use music (wav) from resource
« on: April 25, 2022, 07:56:37 pm »
Hi
I need some audio feedback when the user makes (or forgets to make) a choice. Therefor i have soms simple wav sounds. I am testing it with a buttonclick on the form, as the audio needs to be played instantly. When I do this with PlaySound, then there is a delay. One of the answers i read on the forum was to use  resources. And play the sound from the resource. At this point it gets confusing for me. Because there is *.lrs, or *.res. And i can add resources at the project options. I have read wiki's and forums, but things get even more confusing for me.
My questions are:
- What is the best way to approach this?
- How can i edit a resource file (so i can alter or add sounds in a later stage of development)

Thanks

Sieben

  • Sr. Member
  • ****
  • Posts: 302
Re: Use music (wav) from resource
« Reply #1 on: April 25, 2022, 08:20:36 pm »
I'd add the wav sounds via project options, because it is the most convenient way and you don't need any additional tools. A file 'MySound.wav' would be added as resource MYSOUND - ie in upper case and without extension. Type should evaluate to RCDATA. You can then access the resource like this:

Code: Pascal  [Select][+][-]
  1. var Stream: TResourceStream;
  2.     ResName: string;
  3.     hNd: THandle;
  4.   // ...
  5.   ResName := UpperCase('MySound');
  6.   hNd := FindResource(hInstance,PChar(ResName),RT_RCDATA);
  7.   if (hNd <> 0) then
  8.   begin
  9.     Stream := TResourceStream.Create(hInstance,ResName,RT_RCDATA);
  10.     try
  11.      // feed stream to player
  12.     finally
  13.       Stream.Free;
  14.     end;
  15.   end;
  16.  

Using FindResource prior to creating the resource stream prevents an exception being thrown in case the resource is missing. You can alter or add sounds by simply replacing them in project options or by adding new ones.
Lazarus 2.2.0, FPC 3.2.2, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

tonse

  • New member
  • *
  • Posts: 5
Re: Use music (wav) from resource
« Reply #2 on: April 25, 2022, 10:00:49 pm »
THanks, but i have got one problem left: How do i feed the stream to a player. Both the PlaySound and the sndPlaySound want a name, not a stream.
I can investigate other players, but that seems so "heavy" as i only want to use beeps or other short tones.
By the way: THe Windows.beep function comes with a very long delay that is also interupting the program.
Let me clarify a bit more. I need sounds on different positions of a Progressbar that is progressing with a timer.

wp

  • Hero Member
  • *****
  • Posts: 10060
Re: Use music (wav) from resource
« Reply #3 on: April 25, 2022, 10:11:32 pm »

tonse

  • New member
  • *
  • Posts: 5
Re: Use music (wav) from resource
« Reply #4 on: April 25, 2022, 11:06:36 pm »
the problem is not really solved. I got a sound from the resource on a buttonclick, but there is a real big delay. Almost a second. I need it to be almost instant.
Is there a way to do this. (playing from resource didn't do the trick)

wp

  • Hero Member
  • *****
  • Posts: 10060
Re: Use music (wav) from resource
« Reply #5 on: April 25, 2022, 11:31:17 pm »
For me the sound starts to play instantly. I am rather sure that the reason for your delay is somewhere else. Please check out the attached demo which simply executes Remy's code from the other thread. Button1 reads the stream from the resource and plays the sound. Button2 assumes that the stream already has been read when the application starts - this should be a tiny bit faster, but I am sure that this cannot be noticed.

tonse

  • New member
  • *
  • Posts: 5
Re: Use music (wav) from resource
« Reply #6 on: April 26, 2022, 11:51:07 am »
hmmm... it seems i am a beginner.. :( but ....
There remains a delay. Even with your program. It is short but too long. I want a click with the response time of a typewriter click feedback. That must be somewhere below 100msec, or it gets inconvenient. Your example has a longer delay. Almost the same as i experienced with Remy's code which i (for demo purposes) also programmed on a form with just one button.
I am using Lazarus 2.2.0 on Win10Pro 64bit.
- So. If you have a shorter delay then that, there is something wrong at my side. Any suggestions? Is there a short silence at the beginning of your sound?
- Then the real beginner question: How to create a res file with multiple files in it. All I find is ways to put a single file in a single .res. Is there a way to put all needed sounds in one res file.
- Are there advantages/disadvantages to use *.lrs?
- Why not add the resources in the Project Options?

wp

  • Hero Member
  • *****
  • Posts: 10060
Re: Use music (wav) from resource
« Reply #7 on: April 26, 2022, 12:36:10 pm »
I tried again, and I added a 440-Hz wav file written by myself since I know then that there is not a short silent passage at the beginning of the file. Well, maybe a short delay - but the OnClick handler becomes active only when the mouse button is released. So, if you want to ignore the time the mouse button is down call the PlaySound in the OnMouseDown event rather than in OnClick.

wp

  • Hero Member
  • *****
  • Posts: 10060
Re: Use music (wav) from resource
« Reply #8 on: April 26, 2022, 12:49:07 pm »
The attached batch file adds the file wav440.wav to the sounds.res file of the test application in my previous post. There is absolutely no silence at the beginning of the file. You must compile the lazres tool which you find in the tools folder of your Lazarus application, and copy it to some directory in your path (or to the project directory). Also, rename the resource in the application: replace the "ALARM02.wav" by "WAV440.wav".

lazres is my method of choice to create resource files. Just add all files needed, or you can also put the file names, line by line, into a text file which you specify with a preceding @, e.g:
Code: [Select]
dir /b *.wav > filelist.txt
lazres sounds.res @filelist.txt
del filelist.txt
This way you can simply edit the contents of the res file - simply by recreating it after having edited the batch script.

I never used .rc files (which used to require the windres resource compiler - this has changed now), and I almost never add the resouces in the Project Options. The main reason is that the individual files to be added to the resource file must be available at the time when the application is built - but that's a matter of taste...

tonse

  • New member
  • *
  • Posts: 5
Re: Use music (wav) from resource
« Reply #9 on: April 26, 2022, 04:17:47 pm »
Solved!!!
- i now know how to make a res file with multiple soundsfiles
- The delay seemed to have something to do with my soundcard settings... Everything is working fine now

Thanks!

 

TinyPortal © 2005-2018