Last active
November 8, 2016 01:30
-
-
Save SadProcessor/2a314c21cb77409955dc9568a6a3072a to your computer and use it in GitHub Desktop.
PoSh Text-to-Speech
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Out-Loud(){ | |
Param( | |
#Message | |
[Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)] | |
[ValidateNotNull()] | |
[String[]]$Speech, | |
#Voice | |
[ValidateSet('David', 'Zira', 'Hazel')] | |
[String]$Voice = 'Zira', | |
#Volume | |
[ValidateRange(0,100)] | |
[int]$Volume=100, | |
#Rate | |
[ValidateRange(-10,10)] | |
[int]$Rate=-1, | |
#Don't wait for promt | |
[switch]$Async, | |
#Print text to screen | |
[switch]$Print | |
) | |
Begin{ | |
#add speech type and create speech object | |
Add-Type -AssemblyName System.speech | |
$SpeechSynth = New-Object System.Speech.Synthesis.SpeechSynthesizer | |
#adjust voice settings | |
$SpeechSynth.SelectVoice("Microsoft $voice Desktop") | |
$SpeechSynth.volume=$Volume | |
$SpeechSynth.Rate=$Rate | |
} | |
Process{ | |
# Print text to screen if -Print | |
if($Print){$Speech} | |
# Don't wait for prompt if -Async | |
if($Async){$SpeechSynth.SpeakAsync($Speech) | Out-Null} | |
# or just say it... | |
else{$SpeechSynth.Speak($Speech)} | |
} | |
End{} | |
} | |
#create alias for cmdlet | |
set-alias Say Out-Loud | |
# 'Hello World' | Out-Loud | |
# Say 'Hello World' -Voice David -Volume 100 -Rate -3 -Async |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment