.speak() method is giving unnecesary output

128 Views Asked by At

Recently, I found out about the spvoice interface. Therefore, I wanted to make a small program that makes Powershell say something I type into it. So, I made this:

$Voice = New-Object -ComObject Sapi.spvoice
for(;;){
Clear-Host
$UserInput = Read-Host
if($UserInput -eq "Voice.Rate"){
$VoiceRate = Read-Host "Set new voice rate"}
else{
$Voice.rate = $VoiceRate
$Voice.speak("$UserInput")}}

And although it did whatever I wanted it to, the .speak() method was returning the number 1. How can I prevent this from happening?

1

There are 1 best solutions below

0
Jeff Zeitlin On

I've done this, but not using sapi.spvoice. Instead, I used System.Speech, as follows:

Add-Type -AssemblyName System.Speech
$voice = New-Object -TypeName System.Speech.Synthesis.SpeechSynthesizer
$voice.SelectVoice("Microsoft Zira Desktop")

While ($true) {
    Clear-Host
    $UserInput = Read-Host
    $voice.Speak("$UserInput")
}

This example doesn't have the provisions for setting the rate, but you can change the rate and volume by setting $voice.Rate and $voice.Volume respectively. See Microsoft Docs on the Speech Synthesizer.