# 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))
}
