While I was completing the last post it occurred to me that I wasn’t really doing it the new PowerShell way, i.e. using .Net classes. The SAPI is still supported as it is used for the Windows Text to Speech functions, but is an old style API. I modified the WinForms example below to use the System.Speech.Synthesis.SpeechSynthesizer class.

The API and class use similar methods and so I only had to change the Speak button.

SAPI –
$Voice = New-Object -com SAPI.SpVoice
$buttonSpeak = {
     $Voice.Voice = $Voice.GetVoices().Item($comboVoice.SelectedIndex)
     $Voice.Rate = $comboRate.SelectedItem
     $Voice.Volume = $comboVol.SelectedItem
     $Text2Speak = $textSpeak.Text
     [void] $Voice.Speak($Text2Speak)
     }

System.Speech.Synthesis.SpeechSynthesizer –
$Voice = New-Object System.Speech.Synthesis.SpeechSynthesizer
$buttonSpeak = {
     $TheVoice = $comboVoice.SelectedItem
     $Voice.SelectVoice($TheVoice)
     $Voice.Rate = $comboRate.SelectedItem
     $Voice.Volume = $comboVol.SelectedItem
     $Text2Speak = $textSpeak.Text
     [void] $Voice.Speak($Text2Speak)
     }

Text2Speech.NetDemo

Text2Speech.NetDemo.ps1

Advertisements