Skip to content

Instantly share code, notes, and snippets.

@altrive
Last active December 24, 2015 01:19
Show Gist options
  • Select an option

  • Save altrive/6722853 to your computer and use it in GitHub Desktop.

Select an option

Save altrive/6722853 to your computer and use it in GitHub Desktop.
Speech API test
#Requires -Version 3.0
function Main
{
[Threading.Thread]::CurrentThread.CurrentCulture = "ja-jp"
[Threading.Thread]::CurrentThread.CurrentUICulture = "en-us"
$sw = [Diagnostics.Stopwatch]::StartNew()
Write-Verbose ($LocalizedData.OperationStarted) -Verbose
Write-Verbose ($LocalizedData.CurrentCulture -f [Threading.Thread]::CurrentThread.CurrentCulture.EnglishName, [Threading.Thread]::CurrentThread.CurrentUICulture.EnglishName) -Verbose
Write-Verbose ($LocalizedData.CurrentVoice -f $script:voice.Voice.Name) -Verbose
Write-Verbose ($LocalizedData.OperationCompleted -f $sw.ElapsedMilliseconds) -Verbose
#TODO:Need to dispose voice object after speech operation completed.
#Remove proxy function after operation complete
Remove-Item -Path Function:\Write-Verbose -ErrorAction Ignore -Force
$script:voice = $null
}
data LocalizedData
{
ConvertFrom-StringData @'
OperationStarted = "Operation started."
CurrentCulture = "Current culture is {0}, and UI culture is {1} ."
CurrentVoice = "Current speech voice is {0}."
OperationCompleted = "Operation completed. Elapesed time is {0} milliseconds."
'@
}
#Import-LocalizedData LocalizedData -FileName Speech.psd1 -ErrorAction Ignore
function Write-Verbose
{
[CmdletBinding(HelpUri = 'http://go.microsoft.com/fwlink/?LinkID=113429', RemotingCapability = 'None')]
param (
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
[Alias('Msg')]
[AllowEmptyString()]
[string]
${Message})
begin
{
#region Out-Voice region
function Out-Voice
{
param (
[string] $Message
)
if ([String]::IsNullOrEmpty($message)){
return
}
if (!(Test-Path Variable:\script:voice) -or $script:voice -eq $null)
{
Add-Type -AssemblyName System.Speech
$script:voice = New-Object System.Speech.Synthesis.SpeechSynthesizer
$voices = $voice.GetInstalledVoices().VoiceInfo
$selectedVoiceName = $voices | where Culture -eq ([Threading.Thread]::CurrentThread.CurrentUICulture) | select -ExpandProperty Name -First 1
if ($selectedVoiceName -ne $null){
$voice.SelectVoice($selectedVoiceName)
}
}
<#
$global:prompt = $voice.GetCurrentlySpokenPrompt()
if ($prompt -ne $null){
$voice.SpeakAsyncCancel($prompt)
}
#>
$voice.SpeakAsync($Message) > $null
}
#endregion
try {
Out-Voice $Message
$outBuffer = $null
if ($PSBoundParameters.TryGetValue('OutBuffer', [ref] $outBuffer))
{
$PSBoundParameters['OutBuffer'] = 1
}
$wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Write-Verbose', [System.Management.Automation.CommandTypes]::Cmdlet)
$scriptCmd = { & $wrappedCmd @PSBoundParameters }
$steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
$steppablePipeline.Begin($PSCmdlet)
} catch {
throw
}
}
process
{
try {
$steppablePipeline.Process($_)
} catch {
throw
}
}
end
{
try {
$steppablePipeline.End()
} catch {
throw
}
}
<#
.ForwardHelpTargetName Write-Verbose
.ForwardHelpCategory Cmdlet
#>
}
. Main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment