Speech synthesis in scripts and bat files

Speech synthesizer (text-to-speech, TTS) can be implemented using free component called Microsoft Speech Platform. It supports as many as 26 languages and can sinthesize speech from text or recognise speech to text.

Below are examples how to implement Speech synthesis in JScript or VBScript to make it speaking some messages to users. bat/cmd files can also have Speech synthesis feature just by starting a script like cscript speech.js Hello, world!.

Some software components have to be downloaded and installed.

  • Speech Platform itself available as SpeechPlatformRuntime.msi from Microsoft Speech Platform Runtime 11. There are different downloads for x86 and x64.
  • Languages available from Redistributable Language Packs 11. Only files with TTS in name are applicable for Speech synthesis for example MSSpeech_TTS_en-US_Helen.msi for English US. Files with SR are for Speech Recognition.

Just run downloaded msi files as administrator.

How to check Speech Platform is correctly installed

Following JScript code checks if Speech Platform is available:

  • The code uses SpVoice interface to access Speech Platform
  • The code create SpVoice object and enumarate voices available
  • script code can be ported to VBScript (see below)
var speech = WScript.CreateObject("SPEECH.SpVoice");
if ( speech == null )
    WScript.Echo("Speech not installed. Exit");
var voices = speech.GetVoices();
if ( voices.Count == 0 ) {
    WScript.Echo("Voices not installed. Exit.");
    WScript.Quit(0);
}

WScript.Echo("Voices available: " + voices.Count);
for ( var i = 0; i < voices.Count; ++i)
    WScript.Echo(voices.Item(i).GetDescription());
WScript.Echo("Current Voice: " + speech.Voice.GetDescription());

When there is only one voice installed:

Voices available: 1
Microsoft Server Speech Text to Speech Voice (en-US, Helen)
Current Voice: Microsoft Server Speech Text to Speech Voice (en-US, Helen)

When there are two or more voices output will be like this:

Voices available: 2
Microsoft Server Speech Text to Speech Voice (en-US, ZiraPro)
Microsoft Server Speech Text to Speech Voice (en-US, Helen)
Current Voice: Microsoft Server Speech Text to Speech Voice (en-US, ZiraPro)

How to implement Speech synthesising

With Speech Platform installed it requires a few code lines to make a script speak:

var speech = WScript.CreateObject("SPEECH.SpVoice");
speech.Speak("Hello!");

Following code will what time is it:

var speech = WScript.CreateObject("SPEECH.SpVoice");
var now = new Date();
speech.Speak(now.getHours() + ":" + now.getMinutes());

Scheduling a PC to run this code every hour will turn it into a speaking clock.

Following script accepts text through command line arguments and then tells it:

var speech = WScript.CreateObject("SPEECH.SpVoice");
var text = "";
for ( var i = 0; i < WScript.Arguments.length; ++i ) {
    if ( text == "" )
        text += " ";
    text += WScript.Arguments(i);
}
speech.Speak(text);

Command line arguments passed to the the script within bat file or another program/script will be spoken:

wscript.exe speech.js A message received

Of course scripts can be ported to VBScript:

CreateObject("Speech.SpVoice").Speak"Hello!"

Save this line to .vbs file and run it – that’s very simple! This file can automatically greet user every time the PC is started – just copy the script file to Startup folder!

Compatibility

Microsoft Speech Platform 11 works in Windows 7, Windows Server 2008, Windows Server 2008 R2, Windows Vista thus Speech Platform based scripts can be used on any of these versions of Windows.

The scripts will not work on Windows 8 or later.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *