At the forefront of Artificial Intelligence
  Home Articles Reviews Interviews JDK Glossary Features Discussion Search

Text-to-Speech Using Windows Script

If you use a lot of Windows Scripting (WSH), then you might find this useful - using SAPI's Text-to-speech (TTS) engine within WSH files is very easy. Here is the function:

// Speak the given phrase, returns when speaking finishes
function wshSpeak(phrase) {
   var vt = WScript.CreateObject("Speech.VoiceText");
   vt.Register("", WScript.ScriptName);
   vt.Speak(phrase, 1);
   while ( vt.IsSpeaking ) WScript.Sleep(100);
}
This requires the vtxtauto.tlb (normally found in c:\windows\speech), if don't have it you can download the Speech SDK from Microsoft (a pretty hefty download if you only want this TTS ability, though).

Reusing the Code

If you put the code into a separate file (let's say "wshTTS.js"), you can use it in a Windows Script File (WSF) over again and across languages too. For example:
<job>
  <script language="JScript" src="wshTTS.js" />

  <script language="VBScript">
    wshSpeak "This is Sapi talking from a WSF file"
  </script>
</job>

MSAgent and TTS

As much as I dislike MSAgent, here is some code that demonstrates how to control Microsoft Agent and its TTS features:
Dim AgentControl
On Error Resume Next
Set AgentControl = CreateObject("Agent.Control.1")

If IsObject(AgentControl) Then
  AgentControl.Connected = True
  Dim genie
  On Error Resume Next

  ' You might need to change this line to suit your system
  ' The Agent name and directory may change.
  AgentControl.Characters.Load "genie", "C:\WINDOWS\Msagent\CHARS\GENIE.ACS"

  Set genie = AgentControl.Characters ("genie")
  genie.Get "state", "Showing"
  genie.Get "state", "Speaking"
  genie.MoveTo 20, 20
  genie.Show
  genie.Get "state", "Moving"
  genie.MoveTo 200, 200
  genie.Speak ("Hello!! I'm an annoying darkblue genie!")
  WScript.Sleep(1000)
  genie.Speak ("Bye!")
  genie.Hide
End If

Wscript.echo "Note: MSAgent stuff executes in a separate thread."

Conclusion

As you can see Windows Scripting makes it very easy to add TTS abilities to scripts, HTML Applications, and a variety of other related technologies.

Submitted: 15/07/2001

Article content copyright © James Matthews, 2001.
 Article Toolbar
Print
BibTeX entry

Search

Latest News
- Generation5 10-year Anniversary (03/09/2008)
- New Generation5 Design! (09/04/2007)
- Happy New Year 2007 (02/01/2007)
- Where has Generation5 Gone?! (04/11/2005)
- NeuroEvolving Robotic Operatives (NERO) (25/06/2005)

What's New?
- Back-propagation using the Generation5 JDK (07/04/2008)
- Hough Transforms (02/01/2008)
- Kohonen-based Image Analysis using the Generation5 JDK (11/12/2007)
- Modelling Bacterium using the JDK (19/03/2007)
- Modelling Bacterium using the JDK (19/03/2007)


All content copyright © 1998-2007, Generation5 unless otherwise noted.
- Privacy Policy - Legal - Terms of Use -