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

del_icio_us Save to del.icio.us
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


Search this Blog (and site)

Search this Site with PicoSearch


Subscribe to this Blog

 Subscribe!


Contact Me

Email: jimbo@radiks.net


Follow me on Twitter

http://twitter.com/lawlessGuy


Recent Posts

Mad Schemes : Learning Lisp via SICP

Auto Save Clipboard Images Redux

Extending SpiderMonkey JavaScript on Windows

Rhino JavaScript to EXE with launch4j

Compiling Rhino JavaScript to Java

Directory Traversal in Rhino JavaScript

Taking Shape

We've Moved!


Popular Posts

A Command-Line MP3 Player for Windows

Auto Save Images from the Clipboard

Java in a Windows EXE with launch4j

An Interview with Tom Zimmer: Forth System Developer

Setting Windows Console Text Colors in C


Random Posts

Thwarting HTTP Referer Trackbacks

The Protection Racket

An Interview with Game Developer James Hague

Invoking the Default Windows Screen-Saver

Obfuscated C

Obfuscated Perl

Book Review : Using Google App Engine

Mad Schemes : Learning Lisp via SICP

BBS Fun in the Eighties

CP/M Days


Full List of Posts

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


Blogroll

MicroISV on a Shoestring
DadHacker
The Bottom Feeder
Writin' That Code!
The Recursive ISV
The Thomsen Blog
Prototypically Speaking
The Reinvigorated Programmer