Mention the .link files as a supported file type (FS#9927)
[kugel-rb/myfork.git] / tools / sapi_voice.vbs
blob4f411de5e06e5c3c8e23eddd6a80b85bf1ea150a
1 '***************************************************************************
2 ' __________ __ ___.
3 ' Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 ' Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 ' Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 ' Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 ' \/ \/ \/ \/ \/
8 ' $Id$
10 ' Copyright (C) 2007 Steve Bavin, Jens Arnold, Mesar Hameed
12 ' All files in this archive are subject to the GNU General Public License.
13 ' See the file COPYING in the source tree root for full license agreement.
15 ' This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 ' KIND, either express or implied.
18 '***************************************************************************
20 Option Explicit
22 Const SSFMCreateForWrite = 3
24 ' Audio formats for SAPI5 filestream object
25 Const SPSF_8kHz16BitMono = 6
26 Const SPSF_11kHz16BitMono = 10
27 Const SPSF_12kHz16BitMono = 14
28 Const SPSF_16kHz16BitMono = 18
29 Const SPSF_22kHz16BitMono = 22
30 Const SPSF_24kHz16BitMono = 26
31 Const SPSF_32kHz16BitMono = 30
32 Const SPSF_44kHz16BitMono = 34
33 Const SPSF_48kHz16BitMono = 38
35 Const STDIN = 0
36 Const STDOUT = 1
37 Const STDERR = 2
39 Dim oShell, oArgs, oEnv
40 Dim oFSO, oStdIn, oStdOut
41 Dim bVerbose, bSAPI4, bList
42 Dim sLanguage, sVoice, sSpeed, sName, sVendor
44 Dim oSpVoice, oSpFS ' SAPI5 voice and filestream
45 Dim oTTS, nMode ' SAPI4 TTS object, mode selector
46 Dim oVoice ' for traversing the list of voices
47 Dim nLangID, sSelectString
49 Dim aLine, aData ' used in command reading
51 On Error Resume Next
53 Set oFSO = CreateObject("Scripting.FileSystemObject")
54 Set oStdIn = oFSO.GetStandardStream(STDIN, true)
55 Set oStdOut = oFSO.GetStandardStream(STDOUT, true)
57 Set oShell = CreateObject("WScript.Shell")
58 Set oEnv = oShell.Environment("Process")
59 bVerbose = (oEnv("V") <> "")
61 Set oArgs = WScript.Arguments.Named
62 bSAPI4 = oArgs.Exists("sapi4")
63 bList = oArgs.Exists("listvoices")
64 sLanguage = oArgs.Item("language")
65 sVoice = oArgs.Item("voice")
66 sSpeed = oArgs.Item("speed")
69 If bSAPI4 Then
70 ' Create SAPI4 ActiveVoice object
71 Set oTTS = WScript.CreateObject("ActiveVoice.ActiveVoice", "TTS_")
72 If Err.Number <> 0 Then
73 Err.Clear
74 Set oTTS = WScript.CreateObject("ActiveVoice.ActiveVoice.1", "TTS_")
75 If Err.Number <> 0 Then
76 WScript.StdErr.WriteLine "Error - could not get ActiveVoice" _
77 & " object. SAPI 4 not installed?"
78 WScript.Quit 1
79 End If
80 End If
81 oTTS.Initialized = 1
83 If bList Then
84 ' Just list available voices for the selected language
85 For Each nLangID in LangIDs(sLanguage)
86 For nMode = 1 To oTTS.CountEngines
87 If oTTS.LanguageID(nMode) = nLangID Then
88 WScript.StdErr.Write oTTS.ModeName(nMode) & ","
89 End If
90 Next
91 Next
92 WScript.StdErr.WriteLine
93 WScript.Quit 0
94 End If
96 ' Select matching voice
97 For Each nLangID in LangIDs(sLanguage)
98 sSelectString = "LanguageID=" & nLangID
99 If sVoice <> "" Then
100 sSelectString = sSelectString & ";Speaker=" & sVoice _
101 & ";ModeName=" & sVoice
102 End If
103 nMode = oTTS.Find(sSelectString)
104 If oTTS.LanguageID(nMode) = nLangID And (sVoice = "" Or _
105 oTTS.Speaker(nMode) = sVoice Or oTTS.ModeName(nMode) = sVoice) Then
106 sName = oTTS.ModeName(nMode)
107 If bVerbose Then
108 WScript.StdErr.WriteLine "Using " & sName & " for " & sSelectString
109 End If
110 Exit For
111 Else
112 sSelectString = ""
113 End If
114 Next
115 If sSelectString = "" Then
116 WScript.StdErr.WriteLine "Error - found no matching voice for " _
117 & sLanguage & ", " & sVoice
118 WScript.Quit 1
119 End If
120 oTTS.Select nMode
122 ' Speed selection
123 If sSpeed <> "" Then oTTS.Speed = sSpeed
125 ' Get vendor infomration
126 sVendor = oTTS.MfgName(nMode)
128 Else ' SAPI5
129 ' Create SAPI5 object
130 Set oSpVoice = CreateObject("SAPI.SpVoice")
131 If Err.Number <> 0 Then
132 WScript.StdErr.WriteLine "Error - could not get SpVoice object." _
133 & " SAPI 5 not installed?"
134 WScript.Quit 1
135 End If
137 If bList Then
138 ' Just list available voices for the selected language
139 For Each nLangID in LangIDs(sLanguage)
140 sSelectString = "Language=" & Hex(nLangID)
141 For Each oVoice in oSpVoice.GetVoices(sSelectString)
142 WScript.StdErr.Write oVoice.GetAttribute("Name") & ","
143 Next
144 Next
145 WScript.StdErr.WriteLine
146 WScript.Quit 0
147 End If
149 ' Select matching voice
150 For Each nLangID in LangIDs(sLanguage)
151 sSelectString = "Language=" & Hex(nLangID)
152 If sVoice <> "" Then
153 sSelectString = sSelectString & ";Name=" & sVoice
154 End If
155 Set oSpVoice.Voice = oSpVoice.GetVoices(sSelectString).Item(0)
156 If Err.Number = 0 Then
157 sName = oSpVoice.Voice.GetAttribute("Name")
158 If bVerbose Then
159 WScript.StdErr.WriteLine "Using " & sName & " for " & sSelectString
160 End If
161 Exit For
162 Else
163 sSelectString = ""
164 Err.Clear
165 End If
166 Next
167 If sSelectString = "" Then
168 WScript.StdErr.WriteLine "Error - found no matching voice for " _
169 & sLanguage & ", " & sVoice
170 WScript.Quit 1
171 End If
173 ' Speed selection
174 If sSpeed <> "" Then oSpVoice.Rate = sSpeed
176 ' Get vendor information, protect from missing attribute
177 sVendor = oSpVoice.Voice.GetAttribute("Vendor")
178 If Err.Number <> 0 Then
179 Err.Clear
180 sVendor = "(unknown)"
181 ' Some L&H engines don't set the vendor attribute - check the name
182 If Len(sName) > 3 And Left(sName, 3) = "LH " Then
183 sVendor = "L&H"
184 End If
185 End If
187 ' Filestream object for output
188 Set oSpFS = CreateObject("SAPI.SpFileStream")
189 oSpFS.Format.Type = AudioFormat(sVendor)
190 End If
193 aLine = Split(oStdIn.ReadLine, vbTab, 2)
194 If Err.Number <> 0 Then
195 WScript.StdErr.WriteLine "Error " & Err.Number & ": " & Err.Description
196 WScript.Quit 1
197 End If
198 Select Case aLine(0) ' command
199 Case "QUERY"
200 Select Case aLine(1)
201 Case "VENDOR"
202 oStdOut.WriteLine sVendor
203 End Select
204 Case "SPEAK"
205 aData = Split(aLine(1), vbTab, 2)
206 If bVerbose Then WScript.StdErr.WriteLine "Saying " & aData(1) _
207 & " in " & aData(0)
208 If bSAPI4 Then
209 oTTS.FileName = aData(0)
210 oTTS.Speak aData(1)
211 While oTTS.Speaking
212 WScript.Sleep 1
213 Wend
214 oTTS.FileName = ""
215 Else
216 oSpFS.Open aData(0), SSFMCreateForWrite, false
217 Set oSpVoice.AudioOutputStream = oSpFS
218 oSpVoice.Speak aData(1)
219 oSpFS.Close
220 End If
221 Case "EXEC"
222 If bVerbose Then WScript.StdErr.WriteLine "> " & aLine(1)
223 oShell.Run aLine(1), 0, true
224 If Err.Number <> 0 Then
225 If Not bVerbose Then
226 WScript.StdErr.Write "> " & aLine(1) & ": "
227 End If
228 If Err.Number = &H80070002 Then ' Actually file not found
229 WScript.StdErr.WriteLine "command not found"
230 Else
231 WScript.StdErr.WriteLine "error " & Err.Number & ":" _
232 & Err.Description
233 End If
234 WScript.Quit 2
235 End If
236 Case "SYNC"
237 If bVerbose Then WScript.StdErr.WriteLine "Syncing"
238 oStdOut.WriteLine aLine(1) ' Just echo what was passed
239 Case "QUIT"
240 If bVerbose Then WScript.StdErr.WriteLine "Quitting"
241 WScript.Quit 0
242 End Select
243 Loop
245 ' Subroutines
246 ' -----------
248 ' SAPI5 output format selection based on engine
249 Function AudioFormat(ByRef sVendor)
250 Select Case sVendor
251 Case "Microsoft"
252 AudioFormat = SPSF_22kHz16BitMono
253 Case "AT&T Labs"
254 AudioFormat = SPSF_32kHz16BitMono
255 Case "Loquendo"
256 AudioFormat = SPSF_16kHz16BitMono
257 Case "ScanSoft, Inc"
258 AudioFormat = SPSF_22kHz16BitMono
259 Case "Voiceware"
260 AudioFormat = SPSF_16kHz16BitMono
261 Case Else
262 AudioFormat = SPSF_22kHz16BitMono
263 WScript.StdErr.WriteLine "Warning - unknown vendor """ & sVendor _
264 & """ - using default wave format"
265 End Select
266 End Function
268 ' Language mapping rockbox->windows
269 Function LangIDs(ByRef sLanguage)
270 Dim aIDs
272 Select Case sLanguage
273 Case "afrikaans"
274 LangIDs = Array(&h436)
275 Case "bulgarian"
276 LangIDs = Array(&h402)
277 Case "catala"
278 LangIDs = Array(&h403)
279 Case "chinese-simp"
280 LangIDs = Array(&h804) ' PRC
281 Case "chinese-trad"
282 LangIDs = Array(&h404) ' Taiwan. Perhaps also Hong Kong, Singapore, Macau?
283 Case "czech"
284 LangIDs = Array(&h405)
285 Case "dansk"
286 LangIDs = Array(&h406)
287 Case "deutsch"
288 LangIDs = Array(&h407, &hc07, &h1007, &h1407)
289 ' Standard, Austrian, Luxembourg, Liechtenstein (Swiss -> wallisertitsch)
290 Case "eesti"
291 LangIDs = Array(&h425)
292 Case "english"
293 LangIDs = Array( &h809, &h409, &hc09, &h1009, &h1409, &h1809, _
294 &h1c09, &h2009, &h2409, &h2809, &h2c09, &h3009, _
295 &h3409)
296 ' British, American, Australian, Canadian, New Zealand, Ireland,
297 ' South Africa, Jamaika, Caribbean, Belize, Trinidad, Zimbabwe,
298 ' Philippines
299 Case "espanol"
300 LangIDs = Array( &h40a, &hc0a, &h80a, &h100a, &h140a, &h180a, _
301 &h1c0a, &h200a, &h240a, &h280a, &h2c0a, &h300a, _
302 &h340a, &h380a, &h3c0a, &h400a, &h440a, &h480a, _
303 &h4c0a, &h500a)
304 ' trad. sort., mordern sort., Mexican, Guatemala, Costa Rica,
305 ' Panama, Dominican Republic, Venezuela, Colombia, Peru, Argentina,
306 ' Ecuador, Chile, Uruguay, Paraguay, Bolivia, El Salvador,
307 ' Honduras, Nicaragua, Puerto Rico
308 Case "esperanto"
309 WScript.StdErr.WriteLine "Error: no esperanto support in Windows"
310 WScript.Quit 1
311 Case "finnish"
312 LangIDs = Array(&h40b)
313 Case "francais"
314 LangIDs = Array(&h40c, &h80c, &hc0c, &h100c, &h140c, &h180c)
315 ' Standard, Belgian, Canadian, Swiss, Luxembourg, Monaco
316 Case "galego"
317 LangIDs = Array(&h456)
318 Case "greek"
319 LangIDs = Array(&h408)
320 Case "hebrew"
321 LangIDs = Array(&h40d)
322 Case "hindi"
323 LangIDs = Array(&h439)
324 Case "islenska"
325 LangIDs = Array(&h40f)
326 Case "italiano"
327 LangIDs = Array(&h410, &h810) ' Standard, Swiss
328 Case "japanese"
329 LangIDs = Array(&h411)
330 Case "korean"
331 LangIDs = Array(&h412)
332 Case "magyar"
333 LangIDs = Array(&h40e)
334 Case "nederlands"
335 LangIDs = Array(&h413, &h813) ' Standard, Belgian
336 Case "norsk"
337 LangIDs = Array(&h414) ' Bokmal
338 Case "norsk-nynorsk"
339 LangIDs = Array(&h814)
340 Case "polski"
341 LangIDs = Array(&h415)
342 Case "portugues"
343 LangIDs = Array(&h816)
344 Case "portugues-brasileiro"
345 LangIDs = Array(&h416)
346 Case "romaneste"
347 LangIDs = Array(&h418)
348 Case "russian"
349 LangIDs = Array(&h419)
350 Case "slovenscina"
351 LangIDs = Array(&h424)
352 Case "srpski"
353 LangIDs = Array(&hc1a) ' Cyrillic
354 Case "svenska"
355 LangIDs = Array(&h41d, &h81d) ' Standard, Finland
356 Case "tagalog"
357 LangIDs = Array(&h464) ' Filipino, might not be 100% correct
358 Case "thai"
359 LangIDs = Array(&h41e)
360 Case "turkce"
361 LangIDs = Array(&h41f)
362 Case "wallisertitsch"
363 LangIDs = Array(&h807) ' Swiss German
364 End Select
365 End Function