Recent

Author Topic: [SOLVED] Playing two sounds at the same time  (Read 12970 times)

gof6yogy

  • New Member
  • *
  • Posts: 45
[SOLVED] Playing two sounds at the same time
« on: March 04, 2016, 05:18:59 pm »
Hello :)
I am trying to play two sounds (a background theme and a jumping sound) at the same time; meaning that the background theme should no be interupted by the jumping sound.
To play the .wav files I am currently using the unit MMSystem and the command sndPlaySound();
If anyone knows a solution, that would be great!
Thanks for any help in advance.
Greetings
gof6yogy
P.S. I have tried it a several times, but did not quite get it... How to set your own topic as [SOLVED], when it is solved?
« Last Edit: March 05, 2016, 09:19:03 pm by gof6yogy »

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Playing two sounds at the same time
« Reply #1 on: March 04, 2016, 05:34:05 pm »
Hello :)
I am trying to play two sounds (a background theme and a jumping sound) at the same time; meaning that the background theme should no be interupted by the jumping sound.
To play the .wav files I am currently using the unit MMSystem and the command sndPlaySound();
If anyone knows a solution, that would be great!
Unfortunately playsound does not mix sounds for you. There is a more complex api required for that.

You could use one of the audio libraries located here http://wiki.freepascal.org/Audio_libraries or read up on multimedia programming with pascal http://wiki.freepascal.org/Multimedia_Programming or use a special component suite here http://wiki.freepascal.org/ACS
-

Quote
P.S. I have tried it a several times, but did not quite get it... How to set your own topic as [SOLVED], when it is solved?
Login the forum, locate you thread, edit your _first_ post and prefix the topic with [solved] (note not always possible if your topic was too lengthy)

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: Playing two sounds at the same time
« Reply #2 on: March 04, 2016, 05:44:12 pm »
hello,

with uos ( see here), it seems that you can play several sounds at the same time (in a demo project, if i click on the button to play a music file twice, i hear the music with an echo effect  :o )   )
« Last Edit: March 04, 2016, 05:51:53 pm by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

JD

  • Hero Member
  • *****
  • Posts: 1848
Re: Playing two sounds at the same time
« Reply #3 on: March 04, 2016, 05:57:27 pm »
hello,

with uos ( see here), it seems that you can play several sounds at the same time (in a demo project, if i click on the button to play a music file twice, i hear the music with an echo effect  :o )   )

So you use UOS too? So do I?

JD
Windows - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe),
Linux Mint - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe)

mORMot; Zeos 8; SQLite, PostgreSQL & MariaDB; VirtualTreeView

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: Playing two sounds at the same time
« Reply #4 on: March 04, 2016, 05:59:08 pm »
i use it only for test   8)
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

gof6yogy

  • New Member
  • *
  • Posts: 45
Re: Playing two sounds at the same time
« Reply #5 on: March 04, 2016, 06:38:47 pm »
@molly: Thanks for the quick response! I looked up, but as I mostly get a bunch of files and no idea, where to put them, I followed Jurassic Pork's idea. Thanks for your help though!

@Jurassic Pork: Thanks for the quick response as well! As I now wanted to test it out with the examples from the other post you related to, I get a error message as
Code: Pascal  [Select][+][-]
  1. res := uos_LoadLib(Pchar(PA_FileName), Pchar(SF_FileName), Pchar(MPG_FileName), nil) ;
is called with 4 parameters instead of 5 (according to the error message):
Code: Pascal  [Select][+][-]
  1. uos_flat.pas(1165,10) Hint: Found declaration: uos_loadlib(PChar,PChar,PChar,PChar,PChar):LongInt;
Has anything changed or am I to bad even at copy and paste  :(
And I do not quite understand the "mechanic" in playing a sound. Where sndPlaySound() just starts to play, uos seems to need a lot more preparation:
Code: Pascal  [Select][+][-]
  1. PlayerIndex1 := 0;
  2.   uos_CreatePlayer(PlayerIndex1); //// Create the player
  3.   In1Index := uos_AddFromFile(PlayerIndex1,(pchar(pathEdit.FileName)));
  4.     uos_AddIntoDevOut(PlayerIndex1, -1, -1, uos_InputGetSampleRate(PlayerIndex1, In1Index), -1, -1, -1);
  5.     // play File
  6.     uos_Play(PlayerIndex1);
So if I want to play file xyz.wav what do I have to do? Nothing more than putting in xyz.wav for pathedit.FileName?
Thanks to anyone helping again :)
Greetings
gof6yogy

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: Playing two sounds at the same time
« Reply #6 on: March 05, 2016, 03:24:10 am »
hello,
sorry but fredvs has added one parameter to the uos_LoadLib function of his library (for aac) since my example.
Now with the new version of uos you can try :
Code: Pascal  [Select][+][-]
  1. // function uos_loadlib(PortAudioFileName, SndFileFileName, Mpg123FileName, Mp4ffFileName,FaadFileName: PChar) : LongInt;
  2.          ////// load libraries... if libraryfilename = nil =>  do not load it...  You may load what and when you want...  
  3.  res := uos_LoadLib(Pchar(PA_FileName), Pchar(SF_FileName), Pchar(MPG_FileName), nil,nil) ;  

Friendly, J.P
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: Playing two sounds at the same time
« Reply #7 on: March 05, 2016, 05:17:46 am »
Under windows , you can use mci commands to play sounds and good news  :) , you can play two sounds at the same time.

Example :
Code: Pascal  [Select][+][-]
  1. // uses Windows,MMSystem;
  2. procedure TForm1.Play2SoundsClick(Sender: TObject);
  3. begin
  4. // Close the sounds if already used
  5.      mciSendString(PChar('close sound1'),nil,0,0);
  6.      mciSendString(PChar('close sound2'),nil,0,0);        
  7. // Set Audio files
  8.     mciSendString(PChar(
  9.     'open "' + utf8tosys('f:\temp\sounds\dog.wav') + '" type waveaudio alias sound1')
  10.    , nil, 0,0);
  11.     mciSendString(PChar(
  12.     'open "' + utf8tosys('f:\temp\sounds\cat.wav') + '" type waveaudio alias sound2')
  13.    , nil, 0,0);
  14. //play   sounds
  15.    mciSendString(PChar('play sound1'), nil, 0, 0);
  16.    mciSendString(PChar('play sound2'), nil, 0, 0);
  17. end;      

Friendly, J.P
« Last Edit: March 05, 2016, 05:36:19 am by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

gof6yogy

  • New Member
  • *
  • Posts: 45
Re: Playing two sounds at the same time
« Reply #8 on: March 05, 2016, 04:23:40 pm »
Quote
sorry but fredvs has added one parameter to the uos_LoadLib function of his library (for aac) since my example.
Ah ok :)

Quote
Under windows , you can use mci commands to play sounds and good news  :) , you can play two sounds at the same time.
That seems to be way more promissing for my usage ;) I have already tested it and it works all quite fine! Now one last question: As the background theme is supposed to play in a loop I wanted to ask if you are able to change the command
Code: Pascal  [Select][+][-]
  1. mciSendString(PChar('play sound1'), nil, 0, 0);
in a way that allows me to let the theme play in a loop without using an extra timer as my current idea would be.
Thanks for your patience with me :)
Greetings
gof6yogy

balazsszekely

  • Guest
Re: Playing two sounds at the same time
« Reply #9 on: March 05, 2016, 04:52:11 pm »
At the following link you can find all multimedia command strings: https://msdn.microsoft.com/en-us/library/windows/desktop/dd743572%28v=vs.85%29.aspx
If you click to "Play": https://msdn.microsoft.com/en-us/library/windows/desktop/dd743667%28v=vs.85%29.aspx you will see the available flags.
So the answer is play command with repeat flag.
« Last Edit: March 05, 2016, 04:53:53 pm by GetMem »

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
Re: Playing two sounds at the same time
« Reply #10 on: March 05, 2016, 06:43:57 pm »
Quote
with uos ( see here), it seems that you can play several sounds at the same time (in a demo project, if i click on the button to play a music file twice, i hear the music with an echo effect  :o )   )

Hello.

Could you explain what you hear ?
Do you mean that if you play 2 x the same song with this code you get a delay?
Code: Pascal  [Select][+][-]
  1. uos_Play(PlayerIndex1);
  2. uos_Play(PlayerIndex2);

With what os ? Have you try with the last release ?
Here I do not get delay nor with Windows, nor Linux nor FreeBSD nor OSX..

Quote
sorry but fredvs has added one parameter to the uos_LoadLib function of his library (for aac) since my example.
Yes, thanks to Frank, uos can deal too now with *.m4a and cd audio files.

Fre;D
« Last Edit: March 05, 2016, 09:42:52 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

gof6yogy

  • New Member
  • *
  • Posts: 45
Re: Playing two sounds at the same time
« Reply #11 on: March 05, 2016, 09:18:42 pm »
@Get Mem: Ah ok! Thanks for the links and the hints to that topic! It all works as it is supposed to do and I learned a lot about playing music in lazarus!
Thanks for all of your help and patience!
Greetings
geisterfurz007

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: [SOLVED] Playing two sounds at the same time
« Reply #12 on: March 06, 2016, 12:29:16 am »
hello Fred vS,
Quote
Do you mean that if you play 2 x the same song with this code you get a delay?
Code: Pascal  [Select]
uos_Play(PlayerIndex1);
uos_Play(PlayerIndex2);

With what os ? Have you try with the last release ?
Here I do not get delay nor with Windows, nor Linux nor FreeBSD nor OSX..

not with a different PlayerIndex, with the same PlayerIndex :
i have a button with this code :
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var  PlayerIndex1: cardinal;
  3.      In1Index : integer;
  4. begin
  5.  if uos_lib_OK then
  6.   begin
  7.   PlayerIndex1 := 0;
  8.   uos_CreatePlayer(PlayerIndex1); //// Create the player
  9.  
  10.   In1Index := uos_AddFromFile(PlayerIndex1,(pchar(pathEdit.FileName)));
  11.  
  12.     uos_AddIntoDevOut(PlayerIndex1, -1, -1, uos_InputGetSampleRate(PlayerIndex1, In1Index), -1, -1, -1);
  13.     // play File
  14.     uos_Play(PlayerIndex1);
  15.    end;
  16. end;

I click on this button , twice with a short delay, and i hear the music file with echo (play twice with delay)
I have the last release and i am under windows 7  Lazarus 1.6 32 bits.

Friendly, J.P
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
Re: [SOLVED] Playing two sounds at the same time
« Reply #13 on: March 06, 2016, 03:29:36 pm »
Hello JP.
Not sure I understood ok.
Do you want to play twice the same file?

Why dont you use 2 players with the same file ?

Code: Pascal  [Select][+][-]
  1.     procedure TForm1.Button1Click(Sender: TObject);
  2.     var  PlayerIndex1: cardinal;
  3.          In1Index : integer;
  4.         PlayerIndex2 cardinal;
  5.          In2Index : integer;
  6.     begin
  7.      if uos_lib_OK then
  8.       begin
  9.       PlayerIndex1 := 0;
  10.       PlayerIndex2 := 1;
  11.    
  12.      uos_CreatePlayer(PlayerIndex1); //// Create the player1
  13.      uos_CreatePlayer(PlayerIndex2); //// Create the player2
  14.      
  15.       In1Index := uos_AddFromFile(PlayerIndex1,(pchar(pathEdit.FileName)));
  16.       In2Index := uos_AddFromFile(PlayerIndex2,(pchar(pathEdit.FileName)));
  17.      
  18.       uos_AddIntoDevOut(PlayerIndex1, -1, -1, uos_InputGetSampleRate(PlayerIndex1, In1Index), -1, -1, -1);
  19.       uos_AddIntoDevOut(PlayerIndex2 -1, -1, uos_InputGetSampleRate(PlayerIndex2, In2Index), -1, -1, -1);
  20.        
  21.  
  22.  // play File
  23.        uos_Play(PlayerIndex1);
  24.        uos_Play(PlayerIndex2);
  25.    
  26.      end;
  27.     end;

Or, maybe, you want say that if you click 2x on the button, the song play twice with echo ?

Thanks.

Fre;D
« Last Edit: March 06, 2016, 03:32:37 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: [SOLVED] Playing two sounds at the same time
« Reply #14 on: March 06, 2016, 03:40:26 pm »
hello Fred Vs,
I don't want to play the same file twice , but i have seen that there is no lock if you play a file, if you launch the same file with the same player index, you have two player running.
It isn't important  ;)
forget this
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

 

TinyPortal © 2005-2018