imxtools/sbtools: "fix" address handling in ELF
[maemo-rb.git] / tools / sapi_voice.vbs
blobc6c2c69372656e1477dd9e874089c62a77522fb6
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 bMSSP
43 Dim sLanguage, sVoice, sSpeed, sName, sVendor
45 Dim oSpVoice, oSpFS ' SAPI5 voice and filestream
46 Dim oTTS, nMode ' SAPI4 TTS object, mode selector
47 Dim oVoice ' for traversing the list of voices
48 Dim nLangID, sSelectString
50 Dim aLine, aData ' used in command reading
52 On Error Resume Next
54 Set oFSO = CreateObject("Scripting.FileSystemObject")
55 Set oStdIn = oFSO.GetStandardStream(STDIN, true)
56 Set oStdOut = oFSO.GetStandardStream(STDOUT, true)
58 Set oShell = CreateObject("WScript.Shell")
59 Set oEnv = oShell.Environment("Process")
60 bVerbose = (oEnv("V") <> "")
62 Set oArgs = WScript.Arguments.Named
63 bSAPI4 = oArgs.Exists("sapi4")
64 bMSSP = oArgs.Exists("mssp")
65 bList = oArgs.Exists("listvoices")
66 sLanguage = oArgs.Item("language")
67 sVoice = oArgs.Item("voice")
68 sSpeed = oArgs.Item("speed")
71 If bSAPI4 Then
72 ' Create SAPI4 ActiveVoice object
73 Set oTTS = WScript.CreateObject("ActiveVoice.ActiveVoice", "TTS_")
74 If Err.Number <> 0 Then
75 Err.Clear
76 Set oTTS = WScript.CreateObject("ActiveVoice.ActiveVoice.1", "TTS_")
77 If Err.Number <> 0 Then
78 WScript.StdErr.WriteLine "Error - could not get ActiveVoice" _
79 & " object. SAPI 4 not installed?"
80 WScript.Quit 1
81 End If
82 End If
83 oTTS.Initialized = 1
85 If bList Then
86 ' Just list available voices for the selected language
87 For Each nLangID in LangIDs(sLanguage)
88 For nMode = 1 To oTTS.CountEngines
89 If oTTS.LanguageID(nMode) = nLangID Then
90 WScript.StdErr.Write oTTS.ModeName(nMode) & ","
91 End If
92 Next
93 Next
94 WScript.StdErr.WriteLine
95 WScript.Quit 0
96 End If
98 ' Select matching voice
99 For Each nLangID in LangIDs(sLanguage)
100 sSelectString = "LanguageID=" & nLangID
101 If sVoice <> "" Then
102 sSelectString = sSelectString & ";Speaker=" & sVoice _
103 & ";ModeName=" & sVoice
104 End If
105 nMode = oTTS.Find(sSelectString)
106 If oTTS.LanguageID(nMode) = nLangID And (sVoice = "" Or _
107 oTTS.Speaker(nMode) = sVoice Or oTTS.ModeName(nMode) = sVoice) Then
108 sName = oTTS.ModeName(nMode)
109 If bVerbose Then
110 WScript.StdErr.WriteLine "Using " & sName & " for " & sSelectString
111 End If
112 Exit For
113 Else
114 sSelectString = ""
115 End If
116 Next
117 If sSelectString = "" Then
118 WScript.StdErr.WriteLine "Error - found no matching voice for " _
119 & sLanguage & ", " & sVoice
120 WScript.Quit 1
121 End If
122 oTTS.Select nMode
124 ' Speed selection
125 If sSpeed <> "" Then oTTS.Speed = sSpeed
127 ' Get vendor information
128 sVendor = oTTS.MfgName(nMode)
130 Else ' SAPI5
131 ' Create SAPI5 object
132 If bMSSP Then
133 Set oSpVoice = CreateObject("speech.SpVoice")
134 Else
135 Set oSpVoice = CreateObject("SAPI.SpVoice")
136 End If
137 If Err.Number <> 0 Then
138 WScript.StdErr.WriteLine "Error " & Err.Number _
139 & " - could not get SpVoice object." _
140 & " SAPI 5 not installed?"
141 WScript.Quit 1
142 End If
144 If bList Then
145 ' Just list available voices for the selected language
146 For Each nLangID in LangIDs(sLanguage)
147 sSelectString = "Language=" & Hex(nLangID)
148 For Each oVoice in oSpVoice.GetVoices(sSelectString)
149 WScript.StdErr.Write oVoice.GetAttribute("Name") & ";"
150 Next
151 Next
152 WScript.StdErr.WriteLine
153 WScript.Quit 0
154 End If
156 ' Select matching voice
157 For Each nLangID in LangIDs(sLanguage)
158 sSelectString = "Language=" & Hex(nLangID)
159 If sVoice <> "" Then
160 sSelectString = sSelectString & ";Name=" & sVoice
161 End If
162 Set oSpVoice.Voice = oSpVoice.GetVoices(sSelectString).Item(0)
163 If Err.Number = 0 Then
164 sName = oSpVoice.Voice.GetAttribute("Name")
165 If bVerbose Then
166 WScript.StdErr.WriteLine "Using " & sName & " for " & sSelectString
167 End If
168 Exit For
169 Else
170 sSelectString = ""
171 Err.Clear
172 End If
173 Next
174 If sSelectString = "" Then
175 WScript.StdErr.WriteLine "Error - found no matching voice for " _
176 & sLanguage & ", " & sVoice
177 WScript.Quit 1
178 End If
180 ' Speed selection
181 If sSpeed <> "" Then oSpVoice.Rate = sSpeed
183 ' Get vendor information, protect from missing attribute
184 sVendor = oSpVoice.Voice.GetAttribute("Vendor")
185 If Err.Number <> 0 Then
186 Err.Clear
187 sVendor = "(unknown)"
188 ' Some L&H engines don't set the vendor attribute - check the name
189 If Len(sName) > 3 And Left(sName, 3) = "LH " Then
190 sVendor = "L&H"
191 End If
192 End If
194 ' Filestream object for output
195 Set oSpFS = CreateObject("SAPI.SpFileStream")
196 oSpFS.Format.Type = AudioFormat(sVendor)
197 End If
200 aLine = Split(oStdIn.ReadLine, vbTab, 2)
201 If Err.Number <> 0 Then
202 WScript.StdErr.WriteLine "Error " & Err.Number & ": " & Err.Description
203 WScript.Quit 1
204 End If
205 Select Case aLine(0) ' command
206 Case "QUERY"
207 Select Case aLine(1)
208 Case "VENDOR"
209 oStdOut.WriteLine sVendor
210 End Select
211 Case "SPEAK"
212 aData = Split(aLine(1), vbTab, 2)
213 If bVerbose Then WScript.StdErr.WriteLine "Saying " & aData(1) _
214 & " in " & aData(0)
215 If bSAPI4 Then
216 oTTS.FileName = aData(0)
217 oTTS.Speak aData(1)
218 While oTTS.Speaking
219 WScript.Sleep 1
220 Wend
221 oTTS.FileName = ""
222 Else
223 oSpFS.Open aData(0), SSFMCreateForWrite, false
224 Set oSpVoice.AudioOutputStream = oSpFS
225 oSpVoice.Speak aData(1)
226 oSpFS.Close
227 End If
228 Case "EXEC"
229 If bVerbose Then WScript.StdErr.WriteLine "> " & aLine(1)
230 oShell.Run aLine(1), 0, true
231 If Err.Number <> 0 Then
232 If Not bVerbose Then
233 WScript.StdErr.Write "> " & aLine(1) & ": "
234 End If
235 If Err.Number = &H80070002 Then ' Actually file not found
236 WScript.StdErr.WriteLine "command not found"
237 Else
238 WScript.StdErr.WriteLine "error " & Err.Number & ":" _
239 & Err.Description
240 End If
241 WScript.Quit 2
242 End If
243 Case "SYNC"
244 If bVerbose Then WScript.StdErr.WriteLine "Syncing"
245 oStdOut.WriteLine aLine(1) ' Just echo what was passed
246 Case "QUIT"
247 If bVerbose Then WScript.StdErr.WriteLine "Quitting"
248 WScript.Quit 0
249 End Select
250 Loop
252 ' Subroutines
253 ' -----------
255 ' SAPI5 output format selection based on engine
256 Function AudioFormat(ByRef sVendor)
257 Select Case sVendor
258 Case "Microsoft"
259 AudioFormat = SPSF_22kHz16BitMono
260 Case "AT&T Labs"
261 AudioFormat = SPSF_32kHz16BitMono
262 Case "Loquendo"
263 AudioFormat = SPSF_16kHz16BitMono
264 Case "ScanSoft, Inc"
265 AudioFormat = SPSF_22kHz16BitMono
266 Case "Voiceware"
267 AudioFormat = SPSF_16kHz16BitMono
268 Case Else
269 AudioFormat = SPSF_22kHz16BitMono
270 WScript.StdErr.WriteLine "Warning - unknown vendor """ & sVendor _
271 & """ - using default wave format"
272 End Select
273 End Function
275 ' Language mapping rockbox->windows
276 Function LangIDs(ByRef sLanguage)
277 Dim aIDs
279 Select Case sLanguage
280 Case "afrikaans"
281 LangIDs = Array(&h436)
282 Case "arabic"
283 LangIDs = Array( &h401, &h801, &hc01, &h1001, &h1401, &h1801, _
284 &h1c01, &h2001, &h2401, &h2801, &h2c01, &h3001, _
285 &h3401, &h3801, &h3c01, &h4001)
286 ' Saudi Arabia, Iraq, Egypt, Libya, Algeria, Morocco, Tunisia,
287 ' Oman, Yemen, Syria, Jordan, Lebanon, Kuwait, U.A.E., Bahrain,
288 ' Qatar
289 Case "basque"
290 LangIDs = Array(&h42d)
291 Case "bulgarian"
292 LangIDs = Array(&h402)
293 Case "catala"
294 LangIDs = Array(&h403)
295 Case "chinese-simp"
296 LangIDs = Array(&h804) ' PRC
297 Case "chinese-trad"
298 LangIDs = Array(&h404) ' Taiwan. Perhaps also Hong Kong, Singapore, Macau?
299 Case "czech"
300 LangIDs = Array(&h405)
301 Case "dansk"
302 LangIDs = Array(&h406)
303 Case "deutsch"
304 LangIDs = Array(&h407, &hc07, &h1007, &h1407)
305 ' Standard, Austrian, Luxembourg, Liechtenstein (Swiss -> wallisertitsch)
306 Case "eesti"
307 LangIDs = Array(&h425)
308 Case "english-us"
309 LangIDs = Array( &h409, &h809, &hc09, &h1009, &h1409, &h1809, _
310 &h1c09, &h2009, &h2409, &h2809, &h2c09, &h3009, _
311 &h3409)
312 ' American, British, Australian, Canadian, New Zealand, Ireland,
313 ' South Africa, Jamaika, Caribbean, Belize, Trinidad, Zimbabwe,
314 ' Philippines
315 Case "english"
316 LangIDs = Array( &h809, &h409, &hc09, &h1009, &h1409, &h1809, _
317 &h1c09, &h2009, &h2409, &h2809, &h2c09, &h3009, _
318 &h3409)
319 ' British, American, Australian, Canadian, New Zealand, Ireland,
320 ' South Africa, Jamaika, Caribbean, Belize, Trinidad, Zimbabwe,
321 ' Philippines
322 Case "espanol"
323 LangIDs = Array( &h40a, &hc0a, &h80a, &h100a, &h140a, &h180a, _
324 &h1c0a, &h200a, &h240a, &h280a, &h2c0a, &h300a, _
325 &h340a, &h380a, &h3c0a, &h400a, &h440a, &h480a, _
326 &h4c0a, &h500a)
327 ' trad. sort., mordern sort., Mexican, Guatemala, Costa Rica,
328 ' Panama, Dominican Republic, Venezuela, Colombia, Peru, Argentina,
329 ' Ecuador, Chile, Uruguay, Paraguay, Bolivia, El Salvador,
330 ' Honduras, Nicaragua, Puerto Rico
331 Case "esperanto"
332 WScript.StdErr.WriteLine "Error: no esperanto support in Windows"
333 WScript.Quit 1
334 Case "finnish"
335 LangIDs = Array(&h40b)
336 Case "francais"
337 LangIDs = Array(&h40c, &hc0c, &h100c, &h140c, &h180c)
338 ' Standard, Canadian, Swiss, Luxembourg, Monaco (Belgian -> walon)
339 Case "galego"
340 LangIDs = Array(&h456)
341 Case "greek"
342 LangIDs = Array(&h408)
343 Case "hebrew"
344 LangIDs = Array(&h40d)
345 Case "hindi"
346 LangIDs = Array(&h439)
347 Case "hrvatski"
348 LangIDs = Array(&h41a, &h101a) ' Croatia, Bosnia and Herzegovina
349 Case "islenska"
350 LangIDs = Array(&h40f)
351 Case "italiano"
352 LangIDs = Array(&h410, &h810) ' Standard, Swiss
353 Case "japanese"
354 LangIDs = Array(&h411)
355 Case "korean"
356 LangIDs = Array(&h412)
357 Case "latviesu"
358 LangIDs = Array(&h426)
359 Case "lietuviu"
360 LangIDs = Array(&h427)
361 Case "magyar"
362 LangIDs = Array(&h40e)
363 Case "nederlands"
364 LangIDs = Array(&h413, &h813) ' Standard, Belgian
365 Case "norsk-nynorsk"
366 LangIDs = Array(&h814)
367 Case "norsk"
368 LangIDs = Array(&h414) ' Bokmal
369 Case "polski"
370 LangIDs = Array(&h415)
371 Case "portugues-brasileiro"
372 LangIDs = Array(&h416)
373 Case "portugues"
374 LangIDs = Array(&h816)
375 Case "romaneste"
376 LangIDs = Array(&h418)
377 Case "russian"
378 LangIDs = Array(&h419)
379 Case "slovak"
380 LangIDs = Array(&h41B)
381 Case "slovenscina"
382 LangIDs = Array(&h424)
383 Case "srpski"
384 LangIDs = Array(&hc1a) ' Cyrillic
385 Case "svenska"
386 LangIDs = Array(&h41d, &h81d) ' Standard, Finland
387 Case "tagalog"
388 LangIDs = Array(&h464) ' Filipino, might not be 100% correct
389 Case "thai"
390 LangIDs = Array(&h41e)
391 Case "turkce"
392 LangIDs = Array(&h41f)
393 Case "ukrainian"
394 LangIDs = Array(&h422)
395 Case "wallisertitsch"
396 LangIDs = Array(&h807) ' Swiss German
397 Case "walon"
398 LangIDs = Array(&h80c) ' Belgian French
399 End Select
400 End Function