-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextToSpeech.ahk
180 lines (159 loc) · 4.88 KB
/
textToSpeech.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#SingleInstance force
s := new TTS()
return
;!Escape::s.Speak(gst()) ; select some text and press F1 to test Speak()
!Escape::s.ToggleSpeak(gst()) ; select some text and press F2 to test ToggleSpeak()
+!Escape::s.NextVoice() ; select some text and press F3 to switch to the next voice
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
}
; Class TTS by evilC
; Based on code 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
Class TTS {
VoiceList := [] ; An indexed array of the available voice names
VoiceAssoc := {} ; An Associative array of voice names, key = voice name, value = voice index (VoiceList lookup)
VoiceCount := 0 ; The number of voices available
VoiceNumber := 0 ; The number of the current voice
VoiceName := "" ; The name of the current voice
__New(){
this.oVoice := ComObjCreate("SAPI.SpVoice")
this._GetVoices()
this.SetVoice(this.VoiceList.1)
this.oVoice.Rate := 3
}
; speak or stop speaking
ToggleSpeak(text){
Status := this.oVoice.Status.RunningState
if Status = 1 ; finished
this.oVoice.Speak(text,0x1) ; speak asynchronously
Else if Status = 0 ; paused
{
this.oVoice.Resume
this.oVoice.Speak("",0x1|0x2) ; stop
this.oVoice.Speak(text,0x1) ; speak asynchronously
}
Else if Status = 2 ; reading
this.oVoice.Speak("",0x1|0x2) ; stop
}
; speak asynchronously
Speak(text){
Status := this.oVoice.Status.RunningState
if Status = 0 ; paused
this.oVoice.Resume
this.oVoice.Speak("",0x1|0x2) ; stop
this.oVoice.Speak(text,0x1) ; speak asynchronously
}
; speak synchronously
SpeakWait(text){
Status := this.oVoice.Status.RunningState
if Status = 0 ; paused
this.oVoice.Resume
this.oVoice.Speak("",0x1|0x2) ; stop
this.oVoice.Speak(text,0x0) ; speak synchronously
}
; Pause toggle
Pause(){
Status := this.oVoice.Status.RunningState
if Status = 0 ; paused
this.oVoice.Resume
else if Status = 2 ; reading
this.oVoice.Pause
}
Stop(){
Status := this.oVoice.Status.RunningState
if Status = 0 ; paused
this.oVoice.Resume
this.oVoice.Speak("",0x1|0x2) ; stop
}
; rate (reading speed): rate from -10 to 10. 0 is default.
SetRate(rate){
this.oVoice.Rate := rate
}
; volume (reading loudness): vol from 0 to 100. 100 is default
SetVolume(vol){
this.oVoice.Volume := vol
}
; pitch : From -10 to 10. 0 is default.
; http://msdn.microsoft.com/en-us/library/ms717077(v=vs.85).aspx
SetPitch(pitch){
this.oVoice.Speak("<pitch absmiddle = '" pitch "'/>",0x20)
}
; Set voice by name
SetVoice(VoiceName){
if (!ObjHasKey(this.VoiceAssoc, VoiceName))
return 0
While !(this.oVoice.Status.RunningState = 1)
Sleep, 20
this.oVoice.Voice := this.oVoice.GetVoices("Name=" VoiceName).Item(0) ; set voice to param1
this.VoiceName := VoiceName
this.VoiceNumber := this.VoiceAssoc[VoiceName]
return 1
}
; Set voice by index
SetVoiceByIndex(VoiceIndex){
return this.SetVoice(this.VoiceList[VoiceIndex])
}
; Use the next voice. Loops around at end
NextVoice(){
v := this.VoiceNumber + 1
if (v > this.VoiceCount)
v := 1
return this.SetVoiceByIndex(v)
}
; Returns an array of voice names
GetVoices(){
return this.VoiceList
}
GetStatus(){
Status := this.oVoice.Status.RunningState
if Status = 0 ; paused
Return "paused"
Else if Status = 1 ; finished
Return "finished"
Else if Status = 2 ; reading
Return "reading"
}
GetCount(){
return this.VoiceCount
}
SpeakToFile(param1, param2){
oldAOS := this.oVoice.AudioOutputStream
oldAAOFCONS := this.oVoice.AllowAudioOutputFormatChangesOnNextSet
this.oVoice.AllowAudioOutputFormatChangesOnNextSet := 1
SpStream := ComObjCreate("SAPI.SpFileStream")
FileDelete, % param2 ; OutputFilePath
SpStream.Open(param2, 3)
this.oVoice.AudioOutputStream := SpStream
this.TTS("SpeakWait", param1)
SpStream.Close()
this.oVoice.AudioOutputStream := oldAOS
this.oVoice.AllowAudioOutputFormatChangesOnNextSet := oldAAOFCONS
}
; ====== Private funcs, not intended to be called by user =======
_GetVoices(){
this.VoiceList := []
this.VoiceAssoc := {}
this.VoiceCount := this.oVoice.GetVoices.Count
Loop, % this.VoiceCount
{
Name := this.oVoice.GetVoices.Item(A_Index-1).GetAttribute("Name") ; 0 based
this.VoiceList.push(Name)
this.VoiceAssoc[Name] := A_Index
}
}
}