Recent

Author Topic: sndplaysound not applying snd_async  (Read 970 times)

lazer

  • Full Member
  • ***
  • Posts: 215
sndplaysound not applying snd_async
« on: November 02, 2022, 10:02:56 am »
Hi, I need to play two sound
Code: Pascal  [Select][+][-]
  1.  files one after the other.
  2.  
  3. SND_SYNC  returns immediately, allowing synchronous execution of the rest of the program. SND_ASYNC presumably does the opposite and returns when finished.
  4.  
  5. If I do two subsequent calls to sndPlaySound, I only hear the second one.
  6.  
  7. How should I be using this?  
  8.  
  9. BTW, I do not want alternative ways to play sounds, I want to get this to work, or know that it is impossible.
  10.  
  11. TIA.
  12.  
  13.  
fpc -v
Free Pascal Compiler version 3.3.1 [2022/10/22] for x86_64

avk

  • Hero Member
  • *****
  • Posts: 752
Re: sndplaysound not applying snd_async
« Reply #1 on: November 02, 2022, 11:09:38 am »
This just works for me:
Code: Pascal  [Select][+][-]
  1.   sndPlaySound(PChar('chimes.wav'), SND_SYNC);
  2.   sndPlaySound(PChar('logon.wav'), SND_SYNC);  
  3.  

FPC version=3.3.1-11268-g430f1910ed

lazer

  • Full Member
  • ***
  • Posts: 215
Re: sndplaysound not applying snd_async
« Reply #2 on: November 02, 2022, 12:57:36 pm »
Thanks,

synchronous means at the same time as

https://www.dictionary.com/browse/synchronous
occurring at the same time; coinciding in time; contemporaneous; simultaneous:

It seems that this is something else Microsoft chose to redefine. For MS this is what async means, sync mean sequential.

thanks for the explanation.
« Last Edit: November 02, 2022, 01:08:06 pm by lazer »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2068
  • Fifty shades of code.
    • Delphi & FreePascal
Re: sndplaysound not applying snd_async
« Reply #3 on: November 02, 2022, 01:21:17 pm »
I really do not know where you take your knowledge from but this is the correct definition
Quote
SND_ASYNC   The sound is played asynchronously and PlaySound returns immediately after beginning the sound. To terminate an asynchronously played waveform sound, call PlaySound with pszSound set to NULL.
Quote
SND_SYNC   The sound is played synchronously, and PlaySound returns after the sound event completes. This is the default behavior.
https://learn.microsoft.com/en-us/previous-versions//dd743680(v=vs.85)
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

VisualLab

  • Sr. Member
  • ****
  • Posts: 321
Re: sndplaysound not applying snd_async
« Reply #4 on: November 02, 2022, 01:32:17 pm »
From the description of the sndPlaySound function, which is available on the website:

https://learn.microsoft.com/en-us/previous-versions/dd798676(v=vs.85)

it follows that this is more about the operation of the function itself than about the way the content is reproduced from the sound file. A synchronous call locks the current code until the function executes. Only then is the next instruction (or subroutine) executed. By contrast, an async call is made in the background while the main code continues.

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
Re: sndplaysound not applying snd_async
« Reply #5 on: November 02, 2022, 01:45:29 pm »
@Lazer: If you want to play sound-files at same instant and if you absolutely want to use sndPlaySound, you could create 2 independent threads and in one add sndPlaySound(PChar('sound1.wav'), SND_SYNC) and in the other sndPlaySound(PChar('sound2.wav'), SND_SYNC).

To play the 2 sounds together, just do:

Code: Pascal  [Select][+][-]
  1. thread1.execute;
  2. thread2.execute;
« Last Edit: November 02, 2022, 01:57:28 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

lazer

  • Full Member
  • ***
  • Posts: 215
Re: sndplaysound not applying snd_async
« Reply #6 on: November 02, 2022, 05:29:38 pm »
Thanks Fred. The object is not to play two sounds at the same time but determine whether a sound plays and execution returns immediately after the call to sndPlaySound  (ie execution of the program is synchronous with the sound playing ) or whether it waits until the sound has terminated before returning control to the rest of the code.

The tip on using threads is handy to know about.


lazer

  • Full Member
  • ***
  • Posts: 215
Re: sndplaysound not applying snd_async
« Reply #7 on: November 02, 2022, 05:32:40 pm »
I really do not know where you take your knowledge from but this is the correct definition
Quote
SND_ASYNC   The sound is played asynchronously and PlaySound returns immediately after beginning the sound. To terminate an asynchronously played waveform sound, call PlaySound with pszSound set to NULL.
Quote
SND_SYNC   The sound is played synchronously, and PlaySound returns after the sound event completes. This is the default behavior.
https://learn.microsoft.com/en-us/previous-versions//dd743680(v=vs.85)

Well if you read post you are replying to you would know where I got the definition of the word synchronous and you would have read that I was recognising "correct definition" according to MS and saying that it was another thing they have chosen to redefine.


Quote
The sound is played synchronously, and PlaySound returns after the sound event completes.
That is NOT what the word synchronous means, that is called sequential.
« Last Edit: November 02, 2022, 05:34:26 pm by lazer »

 

TinyPortal © 2005-2018