WARNING: This is old thread. It is continued here.
TTS() for AutoHotkey_L - recommended
See also:
- Text-To-Speech Examples for AutoHotkey_L by jballi
- Text-To-Speech Examples for AutoHotkey Basic by jballi
- Google TTS (on German AHK forum)
_____________________
Here is an old version for AHK Basic which is not maintained any more and requires COM.ahk by Sean
TTS(Voice, Task, Value="") { ; by Learning one. Thanks: jballi and Sean ; Tasks: ToggleSpeak, Speak, Pause, Stop, SetRate, SetVolume, SetVoice, GetVoices, GetStatus ; Reference: http://www.autohotkey.com/forum/topic45471.html if task = ToggleSpeak ; speak or stop speaking { Status := COM_Invoke(Voice,"Status.RunningState") if Status = 1 ; finished COM_Invoke(Voice,"Speak",Value,0x1) Else if Status = 0 ; paused { COM_Invoke(Voice,"Resume") COM_Invoke(Voice,"Speak","",0x1|0x2) ; stop COM_Invoke(Voice,"Speak",Value,0x1) ; value is text you want voice to speak } Else if Status = 2 ; reading COM_Invoke(Voice,"Speak","",0x1|0x2) ; stop } Else if task = Speak { Status := COM_Invoke(Voice,"Status.RunningState") if Status = 0 ; paused COM_Invoke(Voice,"Resume") COM_Invoke(Voice,"Speak","",0x1|0x2) COM_Invoke(Voice,"Speak",Value,0x1) } Else if task = Pause ; Pause toggle { Status := COM_Invoke(Voice,"Status.RunningState") if Status = 0 ; paused COM_Invoke(Voice,"Resume") else if Status = 2 ; reading COM_Invoke(Voice,"Pause") } Else if task = Stop { Status := COM_Invoke(Voice,"Status.RunningState") if Status = 0 ; paused COM_Invoke(Voice,"Resume") COM_Invoke(Voice,"Speak","",0x1|0x2) } Else if task = SetRate COM_Invoke(Voice,"Rate",Value) ; rate (reading speed): value from -10 to 10. 0 is default. Else if task = SetVolume COM_Invoke(Voice,"Volume",Value) ; volume (reading loudness): value from 0 to 100. 100 is default Else if task = SetVoice COM_Invoke(Voice,"Voice","+" . COM_Invoke(Voice,"GetVoices(" . """" . "Name=" . Value . """" . ").Item(0)")) Else if task = GetVoices { loop % COM_Invoke(Voice,"GetVoices.Count") { Name:=COM_Invoke(Voice,"GetVoices.Item(" . A_Index-1 . ")" . ".GetAttribute","Name") VoiceList := (VoiceList = "") ? Name : VoiceList "|" Name } Return VoiceList } Else if task = GetStatus { StatusNum := COM_Invoke(Voice,"Status.RunningState") if StatusNum = 0 ; paused Return "paused" Else if StatusNum = 1 ; finished Return "finished" Else if StatusNum = 2 ; reading Return "reading" } }Example 1 - for AHK Basic
;===Auto-execute=== OnExit, ExitSub COM_Init(), Voice:=COM_CreateObject("SAPI.SpVoice") Return ;===Hotkey=== 1::TTS(Voice, "ToggleSpeak", gst()) ; speak selected text or stop speaking. ;===Subroutine=== ExitSub: COM_Release(Voice), COM_Term() ExitApp ;===Functions=========================================================================== #Include Com.ahk ; by Sean TTS(Voice, Task, Value="") { ; by Learning one. Thanks: jballi and Sean ; Tasks: ToggleSpeak, Speak, Pause, Stop, SetRate, SetVolume, SetVoice, GetVoices, GetStatus ; Reference: http://www.autohotkey.com/forum/topic45471.html if task = ToggleSpeak ; speak or stop speaking { Status := COM_Invoke(Voice,"Status.RunningState") if Status = 1 ; finished COM_Invoke(Voice,"Speak",Value,0x1) Else if Status = 0 ; paused { COM_Invoke(Voice,"Resume") COM_Invoke(Voice,"Speak","",0x1|0x2) ; stop COM_Invoke(Voice,"Speak",Value,0x1) ; value is text you want voice to speak } Else if Status = 2 ; reading COM_Invoke(Voice,"Speak","",0x1|0x2) ; stop } Else if task = Speak { Status := COM_Invoke(Voice,"Status.RunningState") if Status = 0 ; paused COM_Invoke(Voice,"Resume") COM_Invoke(Voice,"Speak","",0x1|0x2) COM_Invoke(Voice,"Speak",Value,0x1) } Else if task = Pause ; Pause toggle { Status := COM_Invoke(Voice,"Status.RunningState") if Status = 0 ; paused COM_Invoke(Voice,"Resume") else if Status = 2 ; reading COM_Invoke(Voice,"Pause") } Else if task = Stop { Status := COM_Invoke(Voice,"Status.RunningState") if Status = 0 ; paused COM_Invoke(Voice,"Resume") COM_Invoke(Voice,"Speak","",0x1|0x2) } Else if task = SetRate COM_Invoke(Voice,"Rate",Value) ; rate (reading speed): value from -10 to 10. 0 is default. Else if task = SetVolume COM_Invoke(Voice,"Volume",Value) ; volume (reading loudness): value from 0 to 100. 100 is default Else if task = SetVoice COM_Invoke(Voice,"Voice","+" . COM_Invoke(Voice,"GetVoices(" . """" . "Name=" . Value . """" . ").Item(0)")) Else if task = GetVoices { loop % COM_Invoke(Voice,"GetVoices.Count") { Name:=COM_Invoke(Voice,"GetVoices.Item(" . A_Index-1 . ")" . ".GetAttribute","Name") VoiceList := (VoiceList = "") ? Name : VoiceList "|" Name } Return VoiceList } Else if task = GetStatus { StatusNum := COM_Invoke(Voice,"Status.RunningState") if StatusNum = 0 ; paused Return "paused" Else if StatusNum = 1 ; finished Return "finished" Else if StatusNum = 2 ; reading Return "reading" } } gst() { ; GetSelectedText by Learning one IsClipEmpty := (Clipboard = "") ? 1 : 0 if !IsClipEmpty { ClipboardBackup := ClipboardAll While !(Clipboard = "") { Clipboard = Sleep, 10 } } Send, ^c ClipWait, 0.1 ToReturn := Clipboard, Clipboard := ClipboardBackup if !IsClipEmpty ClipWait, 0.5, 1 Return ToReturn }Example 2 - for AHK Basic
;===Auto-execute=== OnExit, ExitSub COM_Init(), Voice:=COM_CreateObject("SAPI.SpVoice") TextToSpeak = ( This is easy text to speech by Learning one. Thanks to jballi and Sean. Each script is a plain text file containing commands to be executed by the program (AutoHotkey.exe). A script may also contain hotkeys and hotstrings, or even consist entirely of them. However, in the absence of hotkeys and hotstrings, a script will perform its commands sequentially from top to bottom the moment it is launched. ) Return ;===Hotkeys=== 1::TTS(Voice, "Speak", TextToSpeak) 2::TTS(Voice, "Pause") 3::TTS(Voice, "Stop") 4::TTS(Voice, "SetRate", 2) 5::TTS(Voice, "SetRate", 0) 6::TTS(Voice, "SetVolume", 50) 7::TTS(Voice, "SetVolume", 100) 8::TTS(Voice, "SetVoice", "Microsoft Mary") 9::TTS(Voice, "SetVoice", "Microsoft Anna") 0::MsgBox % TTS(Voice, "GetVoices") ;===Subroutine=== ExitSub: COM_Release(Voice), COM_Term() ExitApp ;===Functions=========================================================================== #Include Com.ahk ; by Sean TTS(Voice, Task, Value="") { ; by Learning one. Thanks: jballi and Sean ; Tasks: ToggleSpeak, Speak, Pause, Stop, SetRate, SetVolume, SetVoice, GetVoices, GetStatus ; Reference: http://www.autohotkey.com/forum/topic45471.html if task = ToggleSpeak ; speak or stop speaking { Status := COM_Invoke(Voice,"Status.RunningState") if Status = 1 ; finished COM_Invoke(Voice,"Speak",Value,0x1) Else if Status = 0 ; paused { COM_Invoke(Voice,"Resume") COM_Invoke(Voice,"Speak","",0x1|0x2) ; stop COM_Invoke(Voice,"Speak",Value,0x1) ; value is text you want voice to speak } Else if Status = 2 ; reading COM_Invoke(Voice,"Speak","",0x1|0x2) ; stop } Else if task = Speak { Status := COM_Invoke(Voice,"Status.RunningState") if Status = 0 ; paused COM_Invoke(Voice,"Resume") COM_Invoke(Voice,"Speak","",0x1|0x2) COM_Invoke(Voice,"Speak",Value,0x1) } Else if task = Pause ; Pause toggle { Status := COM_Invoke(Voice,"Status.RunningState") if Status = 0 ; paused COM_Invoke(Voice,"Resume") else if Status = 2 ; reading COM_Invoke(Voice,"Pause") } Else if task = Stop { Status := COM_Invoke(Voice,"Status.RunningState") if Status = 0 ; paused COM_Invoke(Voice,"Resume") COM_Invoke(Voice,"Speak","",0x1|0x2) } Else if task = SetRate COM_Invoke(Voice,"Rate",Value) ; rate (reading speed): value from -10 to 10. 0 is default. Else if task = SetVolume COM_Invoke(Voice,"Volume",Value) ; volume (reading loudness): value from 0 to 100. 100 is default Else if task = SetVoice COM_Invoke(Voice,"Voice","+" . COM_Invoke(Voice,"GetVoices(" . """" . "Name=" . Value . """" . ").Item(0)")) Else if task = GetVoices { loop % COM_Invoke(Voice,"GetVoices.Count") { Name:=COM_Invoke(Voice,"GetVoices.Item(" . A_Index-1 . ")" . ".GetAttribute","Name") VoiceList := (VoiceList = "") ? Name : VoiceList "|" Name } Return VoiceList } Else if task = GetStatus { StatusNum := COM_Invoke(Voice,"Status.RunningState") if StatusNum = 0 ; paused Return "paused" Else if StatusNum = 1 ; finished Return "finished" Else if StatusNum = 2 ; reading Return "reading" } } gst() { ; GetSelectedText by Learning one IsClipEmpty := (Clipboard = "") ? 1 : 0 if !IsClipEmpty { ClipboardBackup := ClipboardAll While !(Clipboard = "") { Clipboard = Sleep, 10 } } Send, ^c ClipWait, 0.1 ToReturn := Clipboard, Clipboard := ClipboardBackup if !IsClipEmpty ClipWait, 0.5, 1 Return ToReturn }