Jim Lawless' Blog


Windows Text to Speech in WSH JavaScript

Originally published on: Mon, 30 Nov 2009 12:47:15 +0000

I have recently begun to explore the Windows Speech API (SAPI) available on Windows XP and Vista. My earliest experiment involves a very simple WSH script that instantiates a SpVoice object, invokes the Speak() method, and then waits for the speech synthesis to finish by using WaitUntilDone()

I coupled the invocations of the two above SAPI SpVoice methods in a JavaScript function named speak().

speech.js


// Simple speech API call
//
// License: MIT / X11
// Copyright (c) 2009 by James K. Lawless
// jimbo@radiks.net http://www.radiks.net/~jimbo
// http://www.mailsend-online.com
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.


   var voice=new ActiveXObject("SAPI.SpVoice");
   
   function speak(s) {
      voice.Speak(s,1);
      voice.WaitUntilDone(-1);
   }

   speak("I like peanut butter.");

To execute the above, type:


cscript speech.js

The next script I wrote uses the SAPI to synthesize speech for all arguments found on the command-line:

talker.js


// Speak the text supplied on the command line.
// cscript talker.js "phrase 1" "phrase 2" ... etc.
//
// License: MIT / X11
// Copyright (c) 2009 by James K. Lawless
// jimbo@radiks.net http://www.radiks.net/~jimbo
// http://www.mailsend-online.com
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

   var voice=new ActiveXObject("SAPI.SpVoice");
   var args=WScript.Arguments;
   var i;
   
   function speak(s) {
      voice.Speak(s,1);
      voice.WaitUntilDone(-1);
   }

   for(i=0;i<args.Count();i++) {
      speak(args(i));
   }

The examples below illustrate how to use this script either from the command-line or from batch files:


cscript talker.js "Tippecanoe and tyler too."
cscript talker.js "This" "is a" "test"

The Speech Control Panel applet provides a simple GUI interface to the SAPI speech synthesis system. The script below emulates that function of the applet. Since the WSH JavaScript API does not provide a GUI input dialog, a mixed-language WSF file is used. The VBScript portion of the script wraps the InputBox function with a function called prompt(). The prompt function then becomes available to the JavaScript portion of the code.

speechui.wsf


<job id="Text2SpeechUI">
<!--
 Prompt for text and then synthesize the equivalent speech.
 
 License: MIT / X11
 Copyright (c) 2009 by James K. Lawless
 jimbo@radiks.net http:www.radiks.net/~jimbo
 http:www.mailsend-online.com

 Permission is hereby granted, free of charge, to any person
 obtaining a copy of this software and associated documentation
 files (the "Software"), to deal in the Software without
 restriction, including without limitation the rights to use,
 copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the
 Software is furnished to do so, subject to the following
 conditions:

 The above copyright notice and this permission notice shall be
 included in all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
 -->
   <script language="VBScript">
      Function prompt(s)
        prompt = InputBox(s)
      End Function
   </script>
   <script language="JavaScript">
      var voice=new ActiveXObject("SAPI.SpVoice");
      var txt;
   
      function speak(s) {
         voice.Speak(s,1);
         voice.WaitUntilDone(-1);
      }

      for(;;) {
         txt=prompt("Enter a line of text. Click 'Cancel' to exit.");
         if(typeof(txt)=="undefined")
            break;
         if(txt!="")
            speak(txt);
      
      }
   </script>
</job>

You can invoke the above by issuing the following command-line:


wscript speechui.wsf

Enter text in the input area and click OK.

Click 'Cancel' to terminate the script.

Unless otherwise noted, all code and text entries are Copyright ©2009 by James K. Lawless



Views expressed in this blog are those of the author and do not necessary reflect those of the author's employer. Views expressed in the comments are those of the responding individual.

stumbleupon Save to StumbleUpon
digg Digg it
reddit Save to Reddit
facebook Share on Facebook
twitter Share on Twitter
aolfav More bookmarks


Previous post: A Simple Associative Array Library in C
Next post:Structuring my Thinking


About Jim ...


Click **here**
to try out MailWrench;
a command-line SMTP /
SMTPS (Google Gmail)
mailer for Windows.


Follow me on Twitter

http://twitter.com/lawlessGuy


Recent Posts

A JavaScript REPL for Android Devices

MailSend is Free

My Blog Engine

The October 10th Bug

A Review of Kevin Mitnick's Book Ghost in the Wires

Spellbound by Web Programming

Backlinks to my Blog Posts

Play MP3 Files with Python on Windows


Random Posts

Obfuscated Ruby

Pi Day Meets the HTML5 Canvas

Taking Shape

Book Review : Using Google App Engine

My Big Shareware Splash

Auto Save Clipboard Images Redux

Rhino JavaScript to EXE with launch4j

Extending SpiderMonkey JavaScript on Windows

MailSend is Free

A Simple Associative Array Library in C


Full List of Posts

http://www.mailsend-online.com/bloglist.htm


Recent Posts from my Other Blog

Remembering Dr. San Guinary

Why Some Web Sites will go Dark on Jan 18th

SNL Superhero Skit

More Ruby Games

My Ruby Game Challenge Entry

Steal this Bookmarklet

Nerd Toys

Learn New Jargon, You Must

Spot the Wiebe

Tech Magazine Glory Days

Book Review : Paull Allen - Idea Man

A 90's Experiment in Online Systems - The U.S. West CommunityLink Service