SoundPlay takes 400ms to return despite specifying no wait?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
20170201225639
Posts: 144
Joined: 01 Feb 2017, 22:57

SoundPlay takes 400ms to return despite specifying no wait?

26 Aug 2023, 14:44

On my machine, the 1st tooltip shows the time taken to be around 1.5 second, as expected, because SoundPlay is asked to wait until file is finished playing before returning. However, the 2nd tooltip where Wait is omitted ("If [the Wait parameter is] unset or omitted, the script's current thread will move on to the next statement(s) while the file is playing") still shows the time taken to be about 300-400ms. I wonder why this is the case and if it's possible to make SoundPlay return immediately. I'm trying to incorporate more audible feebacks into my workflow and find that having the current thread hang for 300-400ms each time to be drawback.

Code: Select all

	t:=A_TickCount
	SoundPlay("C:\Windows\Media\ding.wav", 1)
	Tooltip("time taken = " A_TickCount-t)

	Sleep(1000)

	t:=A_TickCount
	SoundPlay("C:\Windows\Media\ding.wav")
	Tooltip("time taken = " A_TickCount-t)
There's a workaround, e.g. run an external program (e.g. "nircmd mediaplay"), which returns immediately, but it'd be nice to be able to do this within AHK.
20170201225639
Posts: 144
Joined: 01 Feb 2017, 22:57

Re: SoundPlay takes 400ms to return despite specifying no wait?

26 Aug 2023, 22:53

I guess it could be problems with my sound card or driver? Thanks for trying to reproduce it though.

I'm consistently getting the 300-400ms result on a Dell XPS 17 . I'd be curious to hear if anyone else is getting much faster or similar results.

Code: Select all

Loop 3
{
	t:=A_TickCount
	SoundPlay("C:\Windows\Media\ding.wav", 1)
	FileAppend("time taken = " A_TickCount-t, "*")

	Sleep(2000)

	t:=A_TickCount
	SoundPlay("C:\Windows\Media\ding.wav")
	FileAppend("time taken = " A_TickCount-t, "*")

}
image.png
image.png (194.54 KiB) Viewed 940 times
just me
Posts: 9466
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: SoundPlay takes 400ms to return despite specifying no wait?

28 Aug 2023, 06:25

Code: Select all

R1 := R2 := 0
Loop 3
{
	t:=A_TickCount
	SoundPlay("C:\Windows\Media\ding.wav", 1)
	R1 += A_TickCount - t

	Sleep(2000)

	t:=A_TickCount
	SoundPlay("C:\Windows\Media\ding.wav")
	R2 += A_TickCount - t
}
MsgBox(R1 / 3 . " - " . R2 / 3) ; >>>>> 557.33333333333337 - 46.666666666666664
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: SoundPlay takes 400ms to return despite specifying no wait?

28 Aug 2023, 09:49

Result from @just me's code: 547.0 - 36.333333333333336
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: SoundPlay takes 400ms to return despite specifying no wait?

31 Aug 2023, 22:28

Since this seems to be an issue with the OP's environment and not a feature request, I have moved the topic from Wish List to Ask for Help v2.
User avatar
mikeyww
Posts: 26977
Joined: 09 Sep 2014, 18:38

Re: SoundPlay takes 400ms to return despite specifying no wait?

01 Sep 2023, 05:36

I'm not sure whether this is related, but the native sound card of my laptop computer appears to be very slow in playing sound. When I use an external one, it is fast and seems normal. I recall some programs written to decrease the audio lag. I did try them, and they helped, but they were unreliable for me. If you search the Web for "Windows audio latency", you can find lots of posts and tricks, to update drivers, change settings, and do other things. None of them were permanent fixes for me, but they could be worth trying.

https://www.thewindowsclub.com/sound-delays-lags-and-audio-latency-in-windows-10
iseahound
Posts: 1448
Joined: 13 Aug 2016, 21:04
Contact:

Re: SoundPlay takes 400ms to return despite specifying no wait?

02 Sep 2023, 09:18

Define a custom async function to return it immediately꞉

Code: Select all

async(fn) => SetTimer(fn, -1)
async () => SoundPlay("C:\Windows\Media\ding.wav", 1)

For parallel processing, you could use: "nircmd mediaplay" or a powershell function!

Code: Select all

$PlayWav=New-Object System.Media.SoundPlayer

$PlayWav.SoundLocation=’C:\Foo\Soundfile.wav’

$PlayWav.playsync()
User avatar
mikeyww
Posts: 26977
Joined: 09 Sep 2014, 18:38

Re: SoundPlay takes 400ms to return despite specifying no wait?

02 Sep 2023, 11:52

The problem is not the script but the audio driver, which the script does not address. This is not an AutoHotkey issue.
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: SoundPlay takes 400ms to return despite specifying no wait?

02 Sep 2023, 19:11

Even if SoundPlay was causing the delay, using a timer would only delay the sound until the timer can execute, at which time it would do exactly the same thing as before. :roll:
User avatar
mikeyww
Posts: 26977
Joined: 09 Sep 2014, 18:38

Re: SoundPlay takes 400ms to return despite specifying no wait?

03 Sep 2023, 05:56

No. Interesting script, but it does not resolve the issue with the native audio driver.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: No registered users and 84 guests