forked from Ixiko/AHK-libs-and-classes-collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathText-to-Speech.ahk
133 lines (132 loc) · 4.26 KB
/
Text-to-Speech.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
TTS(oVoice, command, param1="", param2="") {
; by Learning one. For AHK_L. Thanks: jballi, Sean, Frankie.
; AHK forum location: www.autohotkey.com/forum/topic57773.html
; Read more: msdn.microsoft.com/en-us/library/ms723602(v=VS.85).aspx, www.autohotkey.com/forum/topic45471.html, www.autohotkey.com/forum/topic83162.html
; https://autohotkey.com/board/topic/53429-function-easy-text-to-speech/#entry372022
static CommandList := "ToggleSpeak,Speak,SpeakWait,Pause,Stop,SetRate,SetVolume,SetPitch,SetVoice,GetVoices,GetStatus,GetCount,SpeakToFile"
if command not in %CommandList%
{
MsgBox, 16, TTS() error, "%command%" is not valid command.
return
}
if command = ToggleSpeak ; speak or stop speaking
{
Status := oVoice.Status.RunningState
if Status = 1 ; finished
oVoice.Speak(param1,0x1) ; speak asynchronously
Else if Status = 0 ; paused
{
oVoice.Resume
oVoice.Speak("",0x1|0x2) ; stop
oVoice.Speak(param1,0x1) ; speak asynchronously
}
Else if Status = 2 ; reading
oVoice.Speak("",0x1|0x2) ; stop
}
Else if command = Speak ; speak asynchronously
{
Status := oVoice.Status.RunningState
if Status = 0 ; paused
oVoice.Resume
oVoice.Speak("",0x1|0x2) ; stop
oVoice.Speak(param1,0x1) ; speak asynchronously
}
Else if command = SpeakWait ; speak synchronously
{
Status := oVoice.Status.RunningState
if Status = 0 ; paused
oVoice.Resume
oVoice.Speak("",0x1|0x2) ; stop
oVoice.Speak(param1,0x0) ; speak synchronously
}
Else if command = Pause ; Pause toggle
{
Status := oVoice.Status.RunningState
if Status = 0 ; paused
oVoice.Resume
else if Status = 2 ; reading
oVoice.Pause
}
Else if command = Stop
{
Status := oVoice.Status.RunningState
if Status = 0 ; paused
oVoice.Resume
oVoice.Speak("",0x1|0x2) ; stop
}
Else if command = SetRate
oVoice.Rate := param1 ; rate (reading speed): param1 from -10 to 10. 0 is default.
Else if command = SetVolume
oVoice.Volume := param1 ; volume (reading loudness): param1 from 0 to 100. 100 is default
Else if command = SetPitch ; http://msdn.microsoft.com/en-us/library/ms717077(v=vs.85).aspx
oVoice.Speak("<pitch absmiddle = '" param1 "'/>",0x20) ; pitch : param1 from -10 to 10. 0 is default.
Else if command = SetVoice
{
Loop, % oVoice.GetVoices.Count
{
Name := oVoice.GetVoices.Item(A_Index-1).GetAttribute("Name") ; 0 based
If (Name = param1)
{
DoesVoiceExist := 1
break
}
}
if !DoesVoiceExist
{
MsgBox,64,, Voice "%param1%" does not exist.
return
}
While !(oVoice.Status.RunningState = 1)
Sleep, 20
oVoice.Voice := oVoice.GetVoices("Name=" param1).Item(0) ; set voice to param1
}
Else if command = GetVoices
{
param1 := (param1 = "") ? "`n" : param1 ; param1 as delimiter
Loop, % oVoice.GetVoices.Count
{
Name := oVoice.GetVoices.Item(A_Index-1).GetAttribute("Name") ; 0 based
VoiceList .= Name param1
}
Return RTrim(VoiceList,param1)
}
Else if command = GetStatus
{
Status := oVoice.Status.RunningState
if Status = 0 ; paused
Return "paused"
Else if Status = 1 ; finished
Return "finished"
Else if Status = 2 ; reading
Return "reading"
}
Else if command = GetCount
return oVoice.GetVoices.Count
Else if command = SpeakToFile ; param1 = TextToSpeak, param2 = OutputFilePath
{
oldAOS := oVoice.AudioOutputStream
oldAAOFCONS := oVoice.AllowAudioOutputFormatChangesOnNextSet
oVoice.AllowAudioOutputFormatChangesOnNextSet := 1
SpStream := ComObjCreate("SAPI.SpFileStream")
FileDelete, % param2 ; OutputFilePath
SpStream.Open(param2, 3)
oVoice.AudioOutputStream := SpStream
TTS(oVoice, "SpeakWait", param1)
SpStream.Close()
oVoice.AudioOutputStream := oldAOS
oVoice.AllowAudioOutputFormatChangesOnNextSet := oldAAOFCONS
}
}
TTS_CreateVoice(VoiceName="", VoiceRate="", VoiceVolume="", VoicePitch="") { ; by Learning one. For AHK_L.
oVoice := ComObjCreate("SAPI.SpVoice")
if !(VoiceName = "")
TTS(oVoice, "SetVoice", VoiceName)
if VoiceRate between -10 and 10
oVoice.Rate := VoiceRate ; rate (reading speed): from -10 to 10. 0 is default.
if VoiceVolume between 0 and 100
oVoice.Volume := VoiceVolume ; volume (reading loudness): from 0 to 100. 100 is default
if VoicePitch between -10 and 10
TTS(oVoice, "SetPitch", VoicePitch) ; pitch: from -10 to 10. 0 is default.
return oVoice
}