03 Adding sound to your scripts

How can be this controlled in that simulator? Can HIDmacros be used with this application?
Post Reply
Baobob
Posts: 15
Joined: 12 Oct 2011, 02:59

03 Adding sound to your scripts

Post by Baobob » 30 Nov 2011, 05:00

Hello,

Welcome to the next installment of "scripting by dummies". In this episode we will be looking at a little script snippet the plays a wav file using SAPI.SpVoice and SAPI.SpFileStream objects. If you don't know what they are... don't worry that won't keep you from using them. I'm assuming at this point that you have read the previous three episodes and have a rough understanding of how these scripts work so let me push you right into the deep end on this one and pop the code right up there for all to see:

Code: Select all

Set oVoice = CreateObject("SAPI.SpVoice")
set oSpFileStream = CreateObject("SAPI.SpFileStream")

oSpFileStream.Open "Sounds\Speech_on.wav"
oVoice.SpeakStream oSpFileStream
oSpFileStream.Close
You might notice that again I didn't declare my object variables... I guess I'm just reckless that way. In any case the code should be fairly straight forward if you were paying attention last time. In the first two lines we are creating the objects and naming them wnat we want them to be called. My wife still wants me to use SmellySocks... maybe in the next one. The next three lines make use of those objects and boss them around a bit.

Line 3 tells the SAPI.SpFileStream that we named "oSpFileStream" to open "Sounds\Speech_on.wav". "Sounds\Speech_on.wav" could just as easily say "c:\mysounds\fsx\cockpit\buttonclick.wav", but it does have to be a wav file mp3's won't work.

Can anyone tell me what I left out?? Did you notice how the first object we created was SAPI.SpVoice I sure noticed when I first saw it.

Code: Select all

oVoice.Speak "I have finished playing the wave file. Will there be anything else?"
I told you I had a surprise. Add that to the end of you code once... as another line of course.

Here we can see two things. The first is that you can use the windows text to speech engine (wicked cool) and the second is that you can re-use objects. Bear in mind each object has it's own capabilities and limitations. They are not just "do anything magic boxes". What I mean is, a dog can dig in the garbage, and chase parked cars... be he can't fly... for that you need to create a "bird" object.

Incidentally if you only want the text to speech portion of the script, what lines of code do you think you might need? Well the "Boss it around part that does the talking is "oVoice.Speak" right? so we would need first create the oVoice object and what kind of object does the speaking? "SAPI.SpVoice" so the code looks like this.

Code: Select all

Set oVoice = CreateObject("SAPI.SpVoice")
oVoice.Speak "I have finished playing the wave file. Will there be anything else?"
No fancy frilly foo foo... just that... pasted right into the "Scripted" tab of the macro.

Till next time
Fly Well,
Bao

"Getting it up there is all well and good. Lets just get it back down in one piece shall we?"

Post Reply