Text To Voice Conversion

Here is a little script that is fun to use, and could be useful in some applications.

I put this in my profile, so that I am greeted with the data whenever I start a Powershell session.

$today = [DateTime]::Now # Get Current time and date.
$Voice = new-object -com SAPI.SpVoice #Make a voice object using the com object.
$Voice.Speak( “Good Day Patrick!”, 1 )#1 causes function to continue w/o wating.
$day = $today.DayOfWeek #Determine the day.
$dayNumber = $today.Day #Determine the day number
$Voice.Speak( “Today is $day the $dayNumber”, 1 )

The only down side to this is the fact that there is no suffix on the $dayNumber value. A good way to work around that would be to create a switch statement.

Here is an example of how that could be implemented.

switch ($dayNumber)
{
19 {$properDay = “nineteenth” }
20 {$properDay = “twentieth” }
}

To implement this for the entire month, you would add the entire range of possible day numbers from 1 to 31.
Here is how the whole script would look.

$today = [DateTime]::Now # Get Current time and date.
$Voice = new-object -com SAPI.SpVoice #Make a voice object using the com object.
$Voice.Speak( “Good Day Patrick!”, 1 ) #The 1 parameter after the text causes the function to continue without wating.
$day = $today.DayOfWeek #Determine the day.$dayNumber = $today.Day #Determine the day number

switch ($dayNumber)

{
1 {$properDay = “first” }
2 {$properDay = “second” }
3 {$properDay = “third” }
4 {$properDay = “fourth” }
5 {$properDay = “fifth” }
6 {$properDay = “sixth” }
7 {$properDay = “seventh” }
8 {$properDay = “eigth” }
9 {$properDay = “ninth” }
10 {$properDay = “tenth” }
11 {$properDay = “eleventh” }
12 {$properDay = “tweflth” }
13 {$properDay = “thirteenth” }
14 {$properDay = “four-teenth” }
15 {$properDay = “fifteenth” }
16 {$properDay = “sixteenth” }
17 {$properDay = “seventeenth” }
18 {$properDay = “eighteenth” }
19 {$properDay = “nineteenth” }
20 {$properDay = “twentieth” }
21 {$properDay = “twenty-first” }
22 {$properDay = “twenty-second” }
23 {$properDay = “twenty-third” }
24 {$properDay = “twenty-fourth” }
25 {$properDay = “twenty-fifth” }
26 {$properDay = “twenty-sixth” }
27 {$properDay = “twenty-seventh” }
28 {$properDay = “twenty-eight” }
29 {$properDay = “twenty-ninth” }
30 {$properDay = “thirtieth” }
31 {$properDay = “thirty-first” }

}
$Voice.Speak( “Today is $day the $properDay”, 1 )

The voice utilized by this com object reminds me of the voice from the movie
“War Games”. The classic line is “Would you like to play a game?”

Advertisements

About Patrick
I am a Senior Systems Administrator for AT&T. I have been with AT&T for over 15 years. I spend most of my time working with Microsoft Powershell in an effort to find creative ways to manage the data on our file shares. I’ve found Powershell to be a useful, and interesting way to perform Sys. Admin functions.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: