Binary File, Array Scripting Secrets - Adding Voice to Your Scripts
(Page 4 of 4 )
Adding Voice to Your Scripts
It’s nice when your script can pass information back to the user, but it’s sometimes inconvenient to display a message box for simple notifications. And what if you want to add some accessibility to your scripts for the sight-impaired? Why not teach your scripts how to speak?
That’s right! Adding voice to your scripts can be both useful and fun. Not only can you teach your scripts to speak, but you can also teach them to read through the use of Microsoft’s Speech API (SAPI).
strText = "Hi, I am a talking script."
Set objVoice = CreateObject("SAPI.SpVoice")
objVoice.Speak strText
As you can see, making a script talk is extremely easy. You simply connect to the SAPI Voice object and use the Speak method. The text will immediately be read back by the Text-To-Speech Engine.
Const ForReading = 1
Set objVoice = CreateObject("SAPI.SpVoice")
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFile = objFso.OpenTextFile("C:speechraven.txt", ForReading)
Do Until objFile.AtEndOfStream
objVoice.Speak objFile.ReadLine
Loop
With slightly more effort, your script can read the contents of a text file. Here I’m using the FileSystemObject to open and read a simple text file line by line while passing that text to the Speech API.
You can find the files used in this example here: speech.zip
Set objVoice = CreateObject("SAPI.SpVoice")
Set objStream = CreateObject("SAPI.SpFileStream")
objStream.Open "C:speechraven.wav"
objVoice.SpeakStream objStream
Not only can you render text as speech, but the Speech API can also play back audio wave files. SAPI provides the SpFileStream object for opening and streaming the audio file. This time the SpeakStream method is used to play back the stream.
Admittedly, playing wav files in your scripts doesn’t seem all that useful to a system admin. After all, it’s probably not necessary for your scripts to serenade the end user. However, considering the fact that most system sounds are in wave format, this takes on a more practical use.
Set objVoice = CreateObject("SAPI.SpVoice")
Set objFile = CreateObject("SAPI.SpFileStream.1")
objFile.Open "C:WindowsMediaTada.wav"
objVoice.Speakstream objFile
And that, ladies and gentlemen wraps up the third issue of Nilpo’s scripting secrets. Go out and have a little fun with these techniques. Try writing a login script that will welcome the user by name! Until next time, keep coding!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |