Page 1 of 2
How to play two sounds at once?
Posted: 22 Mar 2024, 08:48
by jaka1
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
Re: How to play two sounds at once?
Posted: 22 Mar 2024, 08:57
by mikeyww
You could try Windows Media Player. I did not test it.
viewtopic.php?p=81003#p81003
Re: How to play two sounds at once?
Posted: 22 Mar 2024, 09:51
by jaka1
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.
Re: How to play two sounds at once?
Posted: 22 Mar 2024, 10:07
by V0RT3X
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.
Re: How to play two sounds at once?
Posted: 22 Mar 2024, 11:06
by jaka1
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:-)
Re: How to play two sounds at once?
Posted: 22 Mar 2024, 16:36
by mikeyww
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
}
Re: How to play two sounds at once?
Posted: 23 Mar 2024, 02:03
by Rohwedder
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
}
Re: How to play two sounds at once?
Posted: 23 Mar 2024, 08:47
by jaka1
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
Re: How to play two sounds at once?
Posted: 23 Mar 2024, 08:52
by jaka1
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:-)
Re: How to play two sounds at once?
Posted: 23 Mar 2024, 09:58
by Rohwedder
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
Re: How to play two sounds at once?
Posted: 23 Mar 2024, 10:00
by mikeyww
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.
Re: How to play two sounds at once?
Posted: 23 Mar 2024, 11:07
by jaka1
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:-)
Re: How to play two sounds at once?
Posted: 23 Mar 2024, 12:09
by Rohwedder
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.
Re: How to play two sounds at once?
Posted: 23 Mar 2024, 12:32
by jaka1
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?
Re: How to play two sounds at once?
Posted: 23 Mar 2024, 12:58
by Rohwedder
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?
Re: How to play two sounds at once?
Posted: 24 Mar 2024, 02:41
by Rohwedder
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
}
Re: How to play two sounds at once?
Posted: 24 Mar 2024, 05:40
by jaka1
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.
Re: How to play two sounds at once?
Posted: 24 Mar 2024, 05:55
by mikeyww
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)
}
Re: How to play two sounds at once?
Posted: 24 Mar 2024, 10:18
by Rohwedder
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.
Re: How to play two sounds at once?
Posted: 24 Mar 2024, 14:04
by jaka1
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:-)