1
+ $null = [Reflection.Assembly ]::LoadWithPartialName(" System.Speech" )
2
+
3
+ # # Create the two main objects we need for speech recognition and synthesis
4
+ if (! $global :SpeechModuleListener ) {
5
+ # # For XP's sake, don't create them twice...
6
+ $global :SpeechModuleSpeaker = New-Object System.Speech.Synthesis.SpeechSynthesizer
7
+ $global :SpeechModuleListener = New-Object System.Speech.Recognition.SpeechRecognizer
8
+ }
9
+
10
+ $script :SpeechModuleMacros = @ {}
11
+ # # Add a way to turn it off
12
+ $script :SpeechModuleMacros.Add (" Stop Listening" , {$script :listen = $false ; Suspend-Listening })
13
+ $script :SpeechModuleComputerName = ${env: ComputerName}
14
+ <#
15
+ function Update-SpeechCommands {
16
+ #.#Synopsis
17
+ # Recreate the speech recognition grammar
18
+ #.#Description
19
+ # This parses out the speech module macros,
20
+ # and recreates the speech recognition grammar and semantic results,
21
+ # and then updates the SpeechRecognizer with the new grammar,
22
+ # and makes sure that the ObjectEvent is registered.
23
+ $choices = New-Object System.Speech.Recognition.Choices
24
+ foreach ($choice in $script:SpeechModuleMacros.GetEnumerator()) {
25
+ New-Object System.Speech.Recognition.SemanticResultValue $choice.Key, $choice.Value.ToString() |
26
+ ForEach-Object { $choices.Add($_.ToGrammarBuilder()) }
27
+ }
28
+
29
+ if ($VerbosePreference -ne "SilentlyContinue") {
30
+ $script:SpeechModuleMacros.Keys |
31
+ ForEach-Object { Write-Host"$Computer, $_" -Fore Cyan }
32
+ }
33
+
34
+ $builder = New-Object System.Speech.Recognition.GrammarBuilder"$Computer, "
35
+ $builder.Append((New-ObjectSystem.Speech.Recognition.SemanticResultKey"Commands", $choices.ToGrammarBuilder()))
36
+ $grammar = New-Object System.Speech.Recognition.Grammar $builder
37
+ $grammar.Name = "Power VoiceMacros"
38
+
39
+ ## Take note of the events, but only once (make sure to remove the old one)
40
+ Unregister-Event"SpeechModuleCommandRecognized" -ErrorAction SilentlyContinue
41
+ $null = Register-ObjectEvent $grammar SpeechRecognized `
42
+ -SourceIdentifier"SpeechModuleCommandRecognized" `
43
+ -Action {iex $event.SourceEventArgs.Result.Semantics.Item("Commands").Value}
44
+
45
+ $global:SpeechModuleListener.UnloadAllGrammars()
46
+ $global:SpeechModuleListener.LoadGrammarAsync($grammar)
47
+ }
48
+
49
+ function Add-SpeechCommands {
50
+ #.#Synopsis
51
+ # Add one or more commands to the speech-recognition macros, and update the recognition
52
+ #.#Parameter CommandText
53
+ # The string key for the command to remove
54
+ [CmdletBinding()]
55
+ Param([hashtable]$VoiceMacros,[string]$Computer=$Script:SpeechModuleComputerName)
56
+
57
+ ## Add the new macros
58
+ $script:SpeechModuleMacros += $VoiceMacros
59
+ ## Update the default if they change it, so they only have to do that once.
60
+ $script:SpeechModuleComputerName = $Computer
61
+ Update-SpeechCommands
62
+ }
63
+
64
+ function Remove-SpeechCommands {
65
+ #.#Synopsis
66
+ # Remove one or more command from the speech-recognition macros, and update the recognition
67
+ #.#Parameter CommandText
68
+ # The string key for the command to remove
69
+ Param([string[]]$CommandText)
70
+ foreach ($command in $CommandText) {
71
+ $script:SpeechModuleMacros.Remove($Command)
72
+ }
73
+ Update-SpeechCommands
74
+ }
75
+
76
+ function Clear-SpeechCommands {
77
+ #.#Synopsis
78
+ # Removes all commands from the speech-recognition macros, and update the recognition
79
+ #.#Parameter CommandText
80
+ # The string key for the command to remove
81
+ $script:SpeechModuleMacros = @{}
82
+ ## Default value: A way to turn it off
83
+ $script:SpeechModuleMacros.Add("Stop Listening", {Suspend-Listening})
84
+ Update-SpeechCommands
85
+ }
86
+
87
+ function Start-Listening {
88
+ #.#Synopsis
89
+ # Sets the SpeechRecognizer to Enabled
90
+ $global:SpeechModuleListener.Enabled = $true
91
+ Say "Speech Macros are $($Global:SpeechModuleListener.State)"
92
+ Write-Host "Speech Macros are $($Global:SpeechModuleListener.State)"
93
+ }
94
+
95
+ function Suspend-Listening {
96
+ #.#Synopsis
97
+ # Sets the SpeechRecognizer to Disabled
98
+ $global:SpeechModuleListener.Enabled = $false
99
+ Say "Speech Macros are disabled"
100
+ Write-Host "Speech Macros are disabled"
101
+ }
102
+
103
+ function Out-Speech {
104
+ #.#Synopsis
105
+ # Speaks the input object
106
+ #.#Description
107
+ # Uses the default SpeechSynthesizer settings to speak the string representation of the InputObject
108
+ #.#Parameter InputObject
109
+ # The object to speak
110
+ # NOTE: this should almost always be a pre-formatted string,
111
+ # most objects don't render to very speakable text.
112
+ Param([Parameter(ValueFromPipeline=$true)][Alias("IO")]$InputObject)
113
+ $null = $global:SpeechModuleSpeaker.SpeakAsync(($InputObject | Out-String))
114
+ }
115
+
116
+ function Remove-SpeechXP {
117
+ #.Synopis
118
+ # Dispose of the SpeechModuleListener and SpeechModuleSpeaker
119
+ $global:SpeechModuleListener.Dispose(); $global:SpeechModuleListener = $null
120
+ $global:SpeechModuleSpeaker.Dispose(); $global:SpeechModuleSpeaker = $null
121
+ }
122
+
123
+ Set-Alias asc Add-SpeechCommands
124
+ Set-Alias rsc Remove-SpeechCommands
125
+ Set-Alias csc Clear-SpeechCommands
126
+ Set-Alias say Out-Speech
127
+ Set-Alias listen Start-Listener
128
+ Export-ModuleMember -Function * -Alias * -VariableSpeechModuleListener, SpeechModuleSpeaker
129
+ #>
0 commit comments