How to play two sounds at once?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
jaka1
Posts: 17
Joined: 27 Nov 2022, 08:09

How to play two sounds at once?

22 Mar 2024, 08:48

Can somebody tell me if there is possibility to play two sounds at once by AHK-v1? Is there any simple way how to achieve this in the way of SoundPlay (example below)? Not through Run.

Code: Select all

SoundPlay, C:\Example-Sound_1.mp3
Sleep, 3000
SoundPlay, C:\Example-Sound_2.mp3
Last edited by jaka1 on 22 Mar 2024, 09:44, edited 1 time in total.
User avatar
mikeyww
Posts: 26979
Joined: 09 Sep 2014, 18:38

Re: How to play two sounds at once?

22 Mar 2024, 08:57

You could try Windows Media Player. I did not test it.

viewtopic.php?p=81003#p81003
User avatar
jaka1
Posts: 17
Joined: 27 Nov 2022, 08:09

Re: How to play two sounds at once?

22 Mar 2024, 09:51

Thank you for your quick answer, but I do not want some kind of player to appear, but just like via SoundPlay, and it does not look like it is handled that way on the given link.
User avatar
V0RT3X
Posts: 236
Joined: 20 May 2023, 21:59
Contact:

Re: How to play two sounds at once?

22 Mar 2024, 10:07

Just a thought, but if it's going to be the same 2 sounds, maybe you could merge them with an audio editor into one new sound, then play that?
Otherwise, I think the idiosyncrasies of timings will prevent playing the 2 sounds at the same time. Even the Sleep command isn't precise with timings. The computer is always doing things in the background.
User avatar
jaka1
Posts: 17
Joined: 27 Nov 2022, 08:09

Re: How to play two sounds at once?

22 Mar 2024, 11:06

Thank you V0RT3X, but processing in audio editor is not solution, and I thought about this option. And if I do not manage it by expected way, then, maybe, I will do it in this way. But I hope that it could be done in the mentioned way. As to precise timing, audio editor is surely better for this, but this is not the point:-)
User avatar
mikeyww
Posts: 26979
Joined: 09 Sep 2014, 18:38

Re: How to play two sounds at once?

22 Mar 2024, 16:36

Code: Select all

#Requires AutoHotkey v1.1.33.11
s1 := "s1.wav"
s2 := "s2.wav"
playAudio(s1)
playAudio(s2)

playAudio(audioFile) {
 psScript := "(New-Object Media.SoundPlayer """ audioFile """).PlaySync()"
 Run % "powershell -NoProfile -Command & {" psScript "}",, Hide
}
Rohwedder
Posts: 7655
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: How to play two sounds at once?

23 Mar 2024, 02:03

Hallo,
just like via SoundPlay
try:

Code: Select all

SoundPlay, C:\Example-Sound_1.mp3
Sleep, 3000
SoundPlay("C:\Example-Sound_2.mp3")

q::SoundPlayEnd() ; Ends all running SoundPlay(File)

SoundPlay(File) {
    FileDelete, ~.ahk
    FileAppend,#NoTrayIcon`nFileDelete`,~.ahk`nSoundPlay`,%File%`,1,~.ahk,UTF-8
    Run, ~.ahk
} SoundPlayEnd() {
	DetectHiddenWindows On
	SetTitleMatchMode, 2
	While, WinExist("~.ahk")
		WinClose 
}
User avatar
jaka1
Posts: 17
Joined: 27 Nov 2022, 08:09

Re: How to play two sounds at once?

23 Mar 2024, 08:47

I very appreciate your answer mikeyww, your code looks good enough (very user friendly:-), but the double playing still fails to work. It surprisingly plays out the second file only, not the first. First, I thought that it was not work at all, because I supposed that I could use mp.3 too… I do not know, what I am doing wrong.

I am surprised that there is no address link to a file and with it came this question: How it is handled with finding the proper file?
Another issue is that I could not stop the audio by Reload, that surprised me.

PS: I have AutoHotkey v1 1.1.37.01c1
Last edited by jaka1 on 23 Mar 2024, 11:13, edited 2 times in total.
User avatar
jaka1
Posts: 17
Joined: 27 Nov 2022, 08:09

Re: How to play two sounds at once?

23 Mar 2024, 08:52

Thank you very much Rohwedder, it works! I almost did not believe it. Although I do not totally understand at all why there is a part:

Code: Select all

    FileDelete, ~.ahk
    FileAppend,#NoTrayIcon`nFileDelete`,~.ahk`nSoundPlay`,%File%`,1,~.ahk,UTF-8
    Run, ~.ahk
However, what is important: it works:-)
However, one small shortcoming I found in my testing: Cannot stop both files playing via hotkey „q“, only the second file, and only with Reload. What could it be?
But this is not a problem for me now. The desired function works:-)
Last edited by jaka1 on 23 Mar 2024, 11:14, edited 1 time in total.
Rohwedder
Posts: 7655
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: How to play two sounds at once?

23 Mar 2024, 09:58

Then:

Code: Select all

SoundPlayEnd() ; to delete possible ~.ahk
SoundPlay("C:\Example-Sound_1.mp3")
Sleep, 3000
SoundPlay("C:\Example-Sound_2.mp3")

q::SoundPlayEnd() ; Ends all running SoundPlay(File)

SoundPlay(File) {
	While, FileExist("~.ahk")
		Sleep, 10
    FileAppend,#NoTrayIcon`nFileDelete`,~.ahk`nSoundPlay`,%File%`,1,~.ahk,UTF-8
    Run, ~.ahk
} SoundPlayEnd() {
	DetectHiddenWindows On
	SetTitleMatchMode, 2
	While, WinExist("~.ahk")
		WinClose
	FileDelete,~.ahk
}
SoundPlay(File) writes and runs a script called ~.ahk which deletes itself and executes the SoundPlay
User avatar
mikeyww
Posts: 26979
Joined: 09 Sep 2014, 18:38

Re: How to play two sounds at once?

23 Mar 2024, 10:00

I agree that running multiple scripts could be a way to address this issue. The PowerShell approach might require WAV files, but I have not tested further. The posted script did work here when I used WAV files.
User avatar
jaka1
Posts: 17
Joined: 27 Nov 2022, 08:09

Re: How to play two sounds at once?

23 Mar 2024, 11:07

Answer to Rohwedder: The first track stops after the time of Sleep. :?:

Answer to mikeyww: Even if it was working only with .wav, it would be good:-)
Rohwedder
Posts: 7655
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: How to play two sounds at once?

23 Mar 2024, 12:09

Have you updated SoundPlay(File) and SoundPlayEnd()?
I started the same mp3 via loop, 9 times simultaneously to test it. Sounded terrible, but it worked.
The two superimposed sine tones of this FreqGenerator_1.1 sound better!
http://antic-r.ru/download/FreqGenerator_1.1.exe
Too bad Autohotkey can't do that.
User avatar
jaka1
Posts: 17
Joined: 27 Nov 2022, 08:09

Re: How to play two sounds at once?

23 Mar 2024, 12:32

I have no idea about the process or part involved in your code. As I wrote: I do not totally understand at all why there is a part… It is very high level for me:-)
Where to look for it? How to do it?
Rohwedder
Posts: 7655
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: How to play two sounds at once?

23 Mar 2024, 12:58

Modify the SoundPlay(File) function:

Code: Select all

SoundPlay(File) {
	While, FileExist("~.ahk")
		Sleep, 10
    FileAppend,#NoTrayIcon`nFileDelete`,~.ahk`nSoundPlay`,%File%`,1,~.ahk,UTF-8
	ExitApp ; Run, ~.ahk <<<
}
and look at the generated script ~.ahk with your editor:

Code: Select all

#NoTrayIcon
FileDelete,~.ahk
SoundPlay,C:\Example-Sound_1.mp3,1
Got it?
Rohwedder
Posts: 7655
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: How to play two sounds at once?

24 Mar 2024, 02:41

Instead of the above method with temporary subscripts ~.ahk
here another method with the static subscript SoundPlay.ahk:

Code: Select all

; SoundPlay.ahk
#NoTrayIcon
SoundPlay,% A_Args[1], 1

Code: Select all

; Main.ahk
SoundPlay(A_WorkingDir "\Example-Sound_1.mp3")
Sleep, 3000
SoundPlay(A_WorkingDir "\Example-Sound_2.mp3")

q::SoundPlayEnd() ; Ends all running SoundPlay.ahk

SoundPlay(File) { ; runs a new instance of SoundPlay.ahk
	Run, "%A_WorkingDir%\SoundPlay.ahk" "%File%"
; and passes the path of the sound file
} SoundPlayEnd() {
    DetectHiddenWindows On
    SetTitleMatchMode, 2 ; closes the hidden script windows
    While, WinExist("SoundPlay.ahk")
        WinClose ; of all instances of SoundPlay.ahk
}
User avatar
jaka1
Posts: 17
Joined: 27 Nov 2022, 08:09

Re: How to play two sounds at once?

24 Mar 2024, 05:40

Thank you Rohwedder, I understand it this way:
I will create ahk file "SoundPlay" in working directory with the code:

Code: Select all

#NoTrayIcon
SoundPlay,% A_Args[1], 1
And apply the code below to my ahk file.

Code: Select all

; Main.ahk
SoundPlay(A_WorkingDir "\Tea Time.mp3")
Sleep, 3000
SoundPlay(A_WorkingDir "\ENG.mp3")

q::SoundPlayEnd() ; Ends all running SoundPlay.ahk

SoundPlay(File) { ; runs a new instance of SoundPlay.ahk
	Run, "%A_WorkingDir%\SoundPlay.ahk" "%File%"
; and passes the path of the sound file
} SoundPlayEnd() {
    DetectHiddenWindows On
    SetTitleMatchMode, 2 ; closes the hidden script windows
    While, WinExist("SoundPlay.ahk")
        WinClose ; of all instances of SoundPlay.ahk
}
Am I right?
But again, the first track stops after the time of Sleep. Where am I doing wrong?

As to the first syntax that you sent me, it works:-) Only stopping two audios at one time do not work, as I would expect, because it states in the syntax "Ends all running SoundPlay(File)". It stops the second audio, the first audio can be stopped by Reload.
User avatar
mikeyww
Posts: 26979
Joined: 09 Sep 2014, 18:38

Re: How to play two sounds at once?

24 Mar 2024, 05:55

Code: Select all

; This script plays sounds concurrently
#Requires AutoHotkey v1.1.33.11
Global playFile := A_ScriptDir "\play.ahk"
GroupAdd playing, % playFile " ahk_class AutoHotkey"
OnExit("stop")

F3::stop() ; F3 = Stop play
F2::       ; F2 = Start play
play("Tea Time.mp3")
play("ENG.mp3")
Return

play(filePath) {  ; Play a sound file
 If FileExist(filePath) {
  If !FileExist(playFile)
   FileAppend,
    ( % LTrim
    #Requires AutoHotkey v1.1.33.11
    #SingleInstance Off
    #NoTrayIcon
    SoundPlay % A_Args[1], % WAIT := True
    ExitApp
    ), % playFile
  Run % playFile " """ filePath """"
 } Else MsgBox 48, Error, % "File not found.`n`n" filePath
}

stop(exitReason := "", exitCode := "") {
 DetectHiddenWindows On
 WinClose ahk_group playing ; Close all playing files (scripts)
}
Rohwedder
Posts: 7655
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: How to play two sounds at once?

24 Mar 2024, 10:18

Am I right?
But again, the first track stops after the time of Sleep. Where am I doing wrong?
No idea! Here with Unicode A_AhkVersion: 1.1.37.01 64bit I can even run nine .mp3 at the same time!
No matter if with temporary subscripts ~.ahk or the static subscript SoundPlay.ahk.

Just for testing: Comment out the #NoTrayIcon of SoundPlay.ahk which suppresses the icon.
If everything works correctly, 3 icons should be created after the Sleep, 3000. One for the main script and two for the 2 SoundPlay.ahk instances.
User avatar
jaka1
Posts: 17
Joined: 27 Nov 2022, 08:09

Re: How to play two sounds at once?

24 Mar 2024, 14:04

Thank you very much mikeyww, this works fine:-) However, I have no clue, how to add delay for the second audio, which is now desired function for me. And, secondly, it needs to be launched in the way the SoundPlay command works, e.g. implemented in Gui with a button… Could it be handled in this way?
Anyway, I am glad for this one, and I believe, it will be used sometime:-)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 87 guests