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