PoshCode Archive  Artifact [0984acf272]

Artifact 0984acf2726c77df0c1e4f9ada624a1e50db9b698acd1d720ef7d2f1654811ef:

  • File SpeakToMe.ps1 — part of check-in [c83c01d646] at 2018-06-10 13:35:34 on branch trunk — MSagent speech script with voice selection and interluded speaking (user: chriskenis size: 2533)

# encoding: ascii
# api: powershell
# title: SpeakToMe
# description: MSagent speech script with voice selection and interluded speaking
# version: 0.1
# type: script
# author: chriskenis
# license: CC0
# x-poshcode-id: 4055
# x-archived: 2013-03-30T05:07:09
# x-published: 2013-03-27T23:38:00
#
# https://github.com/chriskenis/POSH.git
#
<#
inspired by HelloKitty script and Mike Hays's obsession for Julie Andrews
added voice selection menu, should work for everyone anywhere
interluded speaking by using string array joined with newline
still need to figure out $voiceToUse code, it's working but I don't know why or how
https://github.com/chriskenis/POSH.git
#>

[CmdletBinding()]
param(
[string] $Name = [Environment]::UserName,
[string[]] $Greetings = @("This is your computer speaking","It is now $(Get-Date)","The end"),
[byte] $Speed = 1
)

process{
$message = @"
        .-. __ _ .-.
        |  `  / \  |
        /     '.()--\
       |         '._/
      _| O   _   O |_
      =\    '-'    /=
        '-._____.-'
        /`/\___/\`\
       /\/o     o\/\
      (_|         |_)
        |____,____|
        (____|____)
		
	Hello World
"@
Write-Host $message -foregroundcolor magenta
$voice = new-object -com SAPI.SpVoice
$voice.Voice =  $(SelectVoice $voice)
$voice.Rate = $Speed

}

begin{
# script variables
$nl = [Environment]::NewLine

Function SelectVoice($VoiceCom){
$voiceList = $VoiceCom.GetVoices()
$voiceDescList = @()
For ($i=0; $i -lt $voiceList.Count; $i++){
	$voiceDescList += $($voiceList.Item($i).GetDescription())
	write-verbose "$voiceDescList[$i]"
	}
Write-Host "$nl Voice Selection: $nl"
For ($i=0; $i -lt $voiceList.Count; $i++){Write-Host -ForegroundColor Green "$i --> $($voiceDescList[$i])"}
Write-Host -ForegroundColor Green "q --> quit script"
Do {
	$SelectIndex = Read-Host "Select voice by number or 'enter' (=default voice)"
	Switch -regex ($SelectIndex){
		"^q.*" 	{$SelectIndex="quit"; $kip = $true}
		"\d" 	{$SelectIndex = $SelectIndex -as [int];$kip = $false}
		"^\s*$" {$SelectIndex=0; $kip = $false}
		}
	}
Until (($SelectIndex -lt $voiceList.Count) -OR $SelectIndex -like "q*")
If ($kip) {exit}
$voiceMember = "Name=" + $voiceDescList[$SelectIndex]
write-verbose "$voiceMember"
$voiceToUse = $VoiceCom.GetVoices($voiceMember.Id).Item($SelectIndex)
write-verbose "Voice to use = $($voiceToUse.Id)"
return $voiceToUse
}

}

end{
[void]$voice.speak("Hi $Name, " + $nl + [string]::join($nl,$Greetings))
}