ata-creative-zvm: make sure return value is defined
[kugel-rb.git] / tools / sapi_voice.vbs
blob4593b791c454c567edb4605d0f2d5a6885a19c3a
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 information
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 "arabic"
274 LangIDs = Array( &h401, &h801, &hc01, &h1001, &h1401, &h1801, _
275 &h1c01, &h2001, &h2401, &h2801, &h2c01, &h3001, _
276 &h3401, &h3801, &h3c01, &h4001)
277 ' Saudi Arabia, Iraq, Egypt, Libya, Algeria, Morocco, Tunisia,
278 ' Oman, Yemen, Syria, Jordan, Lebanon, Kuwait, U.A.E., Bahrain,
279 ' Qatar
280 Case "afrikaans"
281 LangIDs = Array(&h436)
282 Case "bulgarian"
283 LangIDs = Array(&h402)
284 Case "catala"
285 LangIDs = Array(&h403)
286 Case "chinese-simp"
287 LangIDs = Array(&h804) ' PRC
288 Case "chinese-trad"
289 LangIDs = Array(&h404) ' Taiwan. Perhaps also Hong Kong, Singapore, Macau?
290 Case "czech"
291 LangIDs = Array(&h405)
292 Case "dansk"
293 LangIDs = Array(&h406)
294 Case "deutsch"
295 LangIDs = Array(&h407, &hc07, &h1007, &h1407)
296 ' Standard, Austrian, Luxembourg, Liechtenstein (Swiss -> wallisertitsch)
297 Case "eesti"
298 LangIDs = Array(&h425)
299 Case "english"
300 LangIDs = Array( &h809, &h409, &hc09, &h1009, &h1409, &h1809, _
301 &h1c09, &h2009, &h2409, &h2809, &h2c09, &h3009, _
302 &h3409)
303 ' British, American, Australian, Canadian, New Zealand, Ireland,
304 ' South Africa, Jamaika, Caribbean, Belize, Trinidad, Zimbabwe,
305 ' Philippines
306 Case "espanol"
307 LangIDs = Array( &h40a, &hc0a, &h80a, &h100a, &h140a, &h180a, _
308 &h1c0a, &h200a, &h240a, &h280a, &h2c0a, &h300a, _
309 &h340a, &h380a, &h3c0a, &h400a, &h440a, &h480a, _
310 &h4c0a, &h500a)
311 ' trad. sort., mordern sort., Mexican, Guatemala, Costa Rica,
312 ' Panama, Dominican Republic, Venezuela, Colombia, Peru, Argentina,
313 ' Ecuador, Chile, Uruguay, Paraguay, Bolivia, El Salvador,
314 ' Honduras, Nicaragua, Puerto Rico
315 Case "esperanto"
316 WScript.StdErr.WriteLine "Error: no esperanto support in Windows"
317 WScript.Quit 1
318 Case "finnish"
319 LangIDs = Array(&h40b)
320 Case "francais"
321 LangIDs = Array(&h40c, &hc0c, &h100c, &h140c, &h180c)
322 ' Standard, Canadian, Swiss, Luxembourg, Monaco (Belgian -> walon)
323 Case "galego"
324 LangIDs = Array(&h456)
325 Case "greek"
326 LangIDs = Array(&h408)
327 Case "hebrew"
328 LangIDs = Array(&h40d)
329 Case "hindi"
330 LangIDs = Array(&h439)
331 Case "islenska"
332 LangIDs = Array(&h40f)
333 Case "italiano"
334 LangIDs = Array(&h410, &h810) ' Standard, Swiss
335 Case "japanese"
336 LangIDs = Array(&h411)
337 Case "korean"
338 LangIDs = Array(&h412)
339 Case "lietuviu"
340 LangIDs = Array(&h427)
341 Case "magyar"
342 LangIDs = Array(&h40e)
343 Case "nederlands"
344 LangIDs = Array(&h413, &h813) ' Standard, Belgian
345 Case "norsk"
346 LangIDs = Array(&h414) ' Bokmal
347 Case "norsk-nynorsk"
348 LangIDs = Array(&h814)
349 Case "polski"
350 LangIDs = Array(&h415)
351 Case "portugues"
352 LangIDs = Array(&h816)
353 Case "portugues-brasileiro"
354 LangIDs = Array(&h416)
355 Case "romaneste"
356 LangIDs = Array(&h418)
357 Case "russian"
358 LangIDs = Array(&h419)
359 Case "slovenscina"
360 LangIDs = Array(&h424)
361 Case "srpski"
362 LangIDs = Array(&hc1a) ' Cyrillic
363 Case "svenska"
364 LangIDs = Array(&h41d, &h81d) ' Standard, Finland
365 Case "tagalog"
366 LangIDs = Array(&h464) ' Filipino, might not be 100% correct
367 Case "thai"
368 LangIDs = Array(&h41e)
369 Case "turkce"
370 LangIDs = Array(&h41f)
371 Case "wallisertitsch"
372 LangIDs = Array(&h807) ' Swiss German
373 Case "walon"
374 LangIDs = Array(&h80c) ' Belgian French
375 End Select
376 End Function