Tuesday, July 15, 2014

PowerShell and Fun with SAPI (Speech API)

Having Fun with PowerShell is Ok

Every now and then I have to remind myself that programming is fun!  And with that in mind, I want to share a fun function with you that makes your computer speak.  

So here's the function...

# Author:  Bryan Cafferky      Date:  2014-07-10
#
# Purpose:  Speaks the input...
#

function ufn_say_it([string] $speakit)
{
#  Fun using SAPI - the text to speech thing....   

     $speaker = new-object -com SAPI.SpVoice

     $speaker.Speak($speakit, 1) | out-null

}


Sw we can see the function creates an instance ot the COM object SAPI.SpVoice.  So much power is such a little statement.

Let's call this function to test it out.  Just add this line after the function code...

 ufn_say_it 'Good day to you!'

Ok, how about you decide what she should say.  Comment out  ufn_say_it 'Good day to you!' by placing # at the beginning of that line and add...

$speak = Read-Host "Enter what you want me to say: "
ufn_say_it $speak


Note:  Sometimes its easier and more readable if you put function parameters into variables.

Now just for the heck of it, try this...

$speak = dir
ufn_say_it $speak


I hope you found this interesting and please see my other posts.

Thanks,

Bryan

No comments:

Post a Comment