ComObject("SAPI.SpVoice").Speak("Text") , Paramaters in AHKv2?, prevent from pause script while Speak? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Tobgun1
Posts: 123
Joined: 23 Feb 2023, 07:28

ComObject("SAPI.SpVoice").Speak("Text") , Paramaters in AHKv2?, prevent from pause script while Speak?

25 Apr 2023, 22:54

Hello, i am using Lots of

Code: Select all

ComObject("SAPI.SpVoice").Speak("Long Text Speaking")
commands

mostly i do them at the end of an action to start the timer and then give me a audio feedback.

for lots of AHKv1 Threaths i read about Parameters to allow the Voice Speak while script goes on with its actions

whatever i try i do not get different actions in the syncronazion of the voice

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force
InstallKeybdHook
InstallMouseHook
SendMode "Event"
SetKeyDelay(800, 100)
OnExit ExitFunc
; *********************************************************************************************************************************************
~*#::#
; *********************************************************************************************************************************************

F4:: {
    ComObject("SAPI.SpVoice").Speak("Long Text Speaking")
    MsgBox "i will show up after voice speak.", "Test"
}

F5:: {
    MsgBox "i will show up bevore voice speak.", "Test"
    ComObject("SAPI.SpVoice").Speak("Long Text Speaking")
}

F6:: {
    MsgBox "i will show up bevore voice speak.", "Test"
    ComObject("SAPI.SpVoice").Speak("Long Text Speaking",0x1|0x2)
    MsgBox "i will show up while voice speak.", "Test"
}

F7:: {
    ComObject("SAPI.SpVoice").Speak("Long Text Speaking") , 1
    MsgBox "i will show up while voice speak.", "Test"
}

; *********************************************************************************************************************************************
ExitFunc(ExitReason, ExitCode) {
    if ExitReason != "Close" and ExitReason != "ExitApp" {
        ; ----- Edit Here --------
        SoundBeep 1000
}}

*F10:: ExitApp
neogna2
Posts: 600
Joined: 15 Sep 2016, 15:44

Re: ComObject("SAPI.SpVoice").Speak("Text") , Paramaters in AHKv2?, prevent from pause script while Speak?  Topic is solved

26 Apr 2023, 04:40

edit: updated post
Tobgun1 wrote:
25 Apr 2023, 22:54
prevent from pause script while Speak? [...] whatever i try i do not get different actions in the syncronazion of the voice
It seems we cannot in one dot chained line get the COM object and call Speak with a flag
ComObject("SAPI.SpVoice").Speak("This does not work", 0x1)
whereas this without flag works
ComObject("SAPI.SpVoice").Speak("This works")

As a workaround you can split the action into two steps

Code: Select all

F6:: {
    MsgBox "i will show up before voice speak.", "Test"
    oVoice := ComObject("SAPI.SpVoice")
    oVoice.Speak("Long Text Speaking",0x1|0x2)
    MsgBox "i will show up while voice speak.", "Test"
}
or if you want a oneliner
oV := ComObject("SAPI.SpVoice"), oV.Speak("oneliner", 0x1)
teadrinker
Posts: 4398
Joined: 29 Mar 2015, 09:41
Contact:

Re: ComObject("SAPI.SpVoice").Speak("Text") , Paramaters in AHKv2?, prevent from pause script while Speak?

26 Apr 2023, 05:23

neogna2 wrote: It seems we cannot in one dot chained line get the COM object and call Speak with a flag
I think we cannot call Speak with the SVSFlagsAsync flag, as the object would be lost without saving to a variable.
Tobgun1
Posts: 123
Joined: 23 Feb 2023, 07:28

Re: ComObject("SAPI.SpVoice").Speak("Text") , Paramaters in AHKv2?, prevent from pause script while Speak?

26 Apr 2023, 05:42

Hey Thanks for your replys :)
neogna2 wrote:
26 Apr 2023, 04:40
edit: updated post
Tobgun1 wrote:
25 Apr 2023, 22:54
prevent from pause script while Speak? [...] whatever i try i do not get different actions in the syncronazion of the voice
It seems we cannot in one dot chained line get the COM object and call Speak with a flag
ComObject("SAPI.SpVoice").Speak("This does not work", 0x1)
whereas this without flag works
ComObject("SAPI.SpVoice").Speak("This works")

As a workaround you can split the action into two steps

Code: Select all

F6:: {
    MsgBox "i will show up before voice speak.", "Test"
    oVoice := ComObject("SAPI.SpVoice")
    oVoice.Speak("Long Text Speaking",0x1|0x2)
    MsgBox "i will show up while voice speak.", "Test"
}
or if you want a oneliner
oV := ComObject("SAPI.SpVoice"), oV.Speak("oneliner", 0x1)


this is a fast edit i done now because i have to go to work,
i struggle with the following things
example script underneath
while F4 and F5 work now as i wanted it
F6 and F7 dont, if i call Timers, if i go now back to the regular speak it seems that it blocks the script until voice has spoken
if you close this test script with F10 the Functionable Message Box appears AFTER the speak
sorry for my bad english and fast explanation, this was just a fast try to throw in my thoughts b4 i have to go :D :thumbup:

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force
InstallKeybdHook
InstallMouseHook
SendMode "Event"
SetKeyDelay(800, 100)
OnExit ExitFunc
; *********************************************************************************************************************************************
~*#::#
; *********************************************************************************************************************************************

F4:: {
    oV := ComObject("SAPI.SpVoice"), oV.Speak("Long Text Speaking while message box", 0x1)
    MsgBox "i will show up while voice speak.", "Test"
}
F5:: {
    MsgBox "i will show up bevore voice speak.", "Test"
    oV := ComObject("SAPI.SpVoice"), oV.Speak("Long Text Speaking", 0x1)
    MsgBox "i will show up while voice speak.", "Test"
}
F6:: {
    Static on := False
    if on := !on {
    oVoice := ComObject("SAPI.SpVoice")
    oVoice.Speak("Long Text Speaking On",0x1|0x2)
    SetTimer(Function1, 8000)
    } else {
    oVoice := ComObject("SAPI.SpVoice")
    oVoice.Speak("Long Text Speaking Off",0x1|0x2)
    SetTimer(Function1, 0)
}}
F7:: {
    Static on := False
    if on := !on {
    SetTimer(Function1, 8000)
    oV := ComObject("SAPI.SpVoice"), oV.Speak("Long Text Speaking On", 0x1)
    } else {
    oV := ComObject("SAPI.SpVoice"), oV.Speak("Long Text Speaking Off", 0x1)
    SetTimer(Function1, 0)
}}

Function1() {
    MsgBox "When will i show up? With voice speak?", "Test"
}
; *********************************************************************************************************************************************

Function_Msgbox(Seconds) { ; MsgBox for the Closing of the Script
    Loop Seconds {
        Result := MsgBox("GoodBye: " ("for now") "`nScript is closing in: " Seconds--, "Script Closing", "T1")
        if !(Result = "Timeout"){
            Return Result ; Stops earlier if User pushes "OK"
        }
    }
    Return Result
}
ExitFunc(ExitReason, ExitCode) {
    if ExitReason != "Close" and ExitReason != "ExitApp" { ; if the Exit Reason is "Close" or "ExitApp"
     ComObject("SAPI.SpVoice").Speak("Script ShutDown")
     Function_Msgbox(3)   
}}

*F10:: ExitApp
teadrinker
Posts: 4398
Joined: 29 Mar 2015, 09:41
Contact:

Re: ComObject("SAPI.SpVoice").Speak("Text") , Paramaters in AHKv2?, prevent from pause script while Speak?

26 Apr 2023, 08:12

Tobgun1 wrote: F6 and F7 dont
oVoice and oV are local variables in the functions of the above hotkeys. When the function code terminates, these variables are lost along with the objects they contain.
Tobgun1
Posts: 123
Joined: 23 Feb 2023, 07:28

Re: ComObject("SAPI.SpVoice").Speak("Text") , Paramaters in AHKv2?, prevent from pause script while Speak?

26 Apr 2023, 09:52

teadrinker wrote:
26 Apr 2023, 08:12
Tobgun1 wrote: F6 and F7 dont
oVoice and oV are local variables in the functions of the above hotkeys. When the function code terminates, these variables are lost along with the objects they contain.
Ok cool. Thanks for the informations.
So that's the same thing as AHK cant multi threath ?
No workaround I guess ?
So when it calls the function it will always abort my message ?
Or may I add some kind if sleep between?

So i could let it talk while timer is loading but not interrupting the voice message ? Not sure if I made that a clear english sentence 😂🙈
teadrinker
Posts: 4398
Joined: 29 Mar 2015, 09:41
Contact:

Re: ComObject("SAPI.SpVoice").Speak("Text") , Paramaters in AHKv2?, prevent from pause script while Speak?

26 Apr 2023, 12:04

Tobgun1 wrote: So that's the same thing as AHK cant multi threath ?
No, that's this thing:
teadrinker wrote: When the function code terminates, these variables are lost along with the objects they contain.
 
Tobgun1 wrote: No workaround I guess ?
You obviously have to use a global or static variable for the SAPI.SpVoice object when the SVSFlagsAsync flag is set. I would suggest creating this object only once at the beginning, saving it to a variable in the global scope, and calling the Speak() method when needed:

Code: Select all

#Requires AutoHotkey v2.0

oVoice := ComObject("SAPI.SpVoice")

F4:: {
    oVoice.Speak("Long Text Speaking while message box", 0x1)
    MsgBox "i will show up while voice speak.", "Test"
}

F5:: {
    MsgBox "i will show up bevore voice speak.", "Test"
    oVoice.Speak("Long Text Speaking", 0x1)
    MsgBox "i will show up while voice speak.", "Test"
}

F6:: {
    Static on := False
    if on := !on {
        oVoice.Speak("Long Text Speaking On", 0x1 | 0x2)
        SetTimer(Function1, 8000)
    } else {
        oVoice.Speak("Long Text Speaking Off", 0x1 | 0x2)
        SetTimer(Function1, 0)
    }
}

F7:: {
    Static on := False
    if on := !on {
        SetTimer(Function1, 8000)
        oVoice.Speak("Long Text Speaking On", 0x1)
    } else {
        oVoice.Speak("Long Text Speaking Off", 0x1)
        SetTimer(Function1, 0)
    }
}

Function1() {
    MsgBox "When will i show up? With voice speak?", "Test"
}
; *********************************************************************************************************************************************

Function_Msgbox(Seconds) { ; MsgBox for the Closing of the Script
    Loop Seconds {
        Result := MsgBox("GoodBye: " ("for now") "`nScript is closing in: " Seconds--, "Script Closing", "T1")
        if !(Result = "Timeout") {
            Return Result ; Stops earlier if User pushes "OK"
        }
    }
    Return Result
}

ExitFunc(ExitReason, ExitCode) {
    if ExitReason != "Close" and ExitReason != "ExitApp" { ; if the Exit Reason is "Close" or "ExitApp"
        oVoice.Speak("Script ShutDown")
        Function_Msgbox(3)
    }
}

*F10:: ExitApp
Tobgun1
Posts: 123
Joined: 23 Feb 2023, 07:28

Re: ComObject("SAPI.SpVoice").Speak("Text") , Paramaters in AHKv2?, prevent from pause script while Speak?

28 Apr 2023, 05:50

teadrinker wrote:
26 Apr 2023, 12:04
Tobgun1 wrote: So that's the same thing as AHK cant multi threath ?
No, that's this thing:
teadrinker wrote: When the function code terminates, these variables are lost along with the objects they contain.
 
Tobgun1 wrote: No workaround I guess ?
You obviously have to use a global or static variable for the SAPI.SpVoice object when the SVSFlagsAsync flag is set. I would suggest creating this object only once at the beginning, saving it to a variable in the global scope, and calling the Speak() method when needed:

Code: Select all

#Requires AutoHotkey v2.0

oVoice := ComObject("SAPI.SpVoice")

F4:: {
    oVoice.Speak("Long Text Speaking while message box", 0x1)
    MsgBox "i will show up while voice speak.", "Test"
}

F5:: {
    MsgBox "i will show up bevore voice speak.", "Test"
    oVoice.Speak("Long Text Speaking", 0x1)
    MsgBox "i will show up while voice speak.", "Test"
}

F6:: {
    Static on := False
    if on := !on {
        oVoice.Speak("Long Text Speaking On", 0x1 | 0x2)
        SetTimer(Function1, 8000)
    } else {
        oVoice.Speak("Long Text Speaking Off", 0x1 | 0x2)
        SetTimer(Function1, 0)
    }
}

F7:: {
    Static on := False
    if on := !on {
        SetTimer(Function1, 8000)
        oVoice.Speak("Long Text Speaking On", 0x1)
    } else {
        oVoice.Speak("Long Text Speaking Off", 0x1)
        SetTimer(Function1, 0)
    }
}

Function1() {
    MsgBox "When will i show up? With voice speak?", "Test"
}
; *********************************************************************************************************************************************

Function_Msgbox(Seconds) { ; MsgBox for the Closing of the Script
    Loop Seconds {
        Result := MsgBox("GoodBye: " ("for now") "`nScript is closing in: " Seconds--, "Script Closing", "T1")
        if !(Result = "Timeout") {
            Return Result ; Stops earlier if User pushes "OK"
        }
    }
    Return Result
}

ExitFunc(ExitReason, ExitCode) {
    if ExitReason != "Close" and ExitReason != "ExitApp" { ; if the Exit Reason is "Close" or "ExitApp"
        oVoice.Speak("Script ShutDown")
        Function_Msgbox(3)
    }
}

*F10:: ExitApp

Cool 😁👍
Thanks for your answers and solutions!

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Draken and 32 guests