<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Huddled Masses &#187; Speech recognition</title>
	<atom:link href="http://joelbennett.net/tag/speech-recognition/feed/" rel="self" type="application/rss+xml" />
	<link>http://joelbennett.net</link>
	<description>The internet home of Joel "Jaykul" Bennett...</description>
	<lastBuildDate>Sat, 28 Jan 2012 21:37:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<cloud domain='joelbennett.net' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>Control your PC with your voice &#8230; and PowerShell</title>
		<link>http://joelbennett.net/control-your-pc-with-your-voice-and-powershell/</link>
		<comments>http://joelbennett.net/control-your-pc-with-your-voice-and-powershell/#comments</comments>
		<pubDate>Thu, 25 Jun 2009 06:57:54 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Home automation]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Remote Control]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Speech recognition]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1188</guid>
		<description><![CDATA[Have you ever wanted to be able to ask your computer questions and have it answer you out loud? Have you ever wondered if your computer could be more like the ones running the Star Trek Enterprise, responding to voice queries and commands? Have you played with ZWave or X10 home automation and thought that [...]]]></description>
			<content:encoded><![CDATA[	<ul>
		<li>Have you ever wanted to be able to ask your computer questions and have it answer you out loud? </li>
		<li>Have you ever wondered if your computer could be more like the ones running the Star Trek Enterprise, responding to voice queries and commands?</li>
	</ul>
	<ul>
		<li>Have you played with <a href="http://www.z-wave.com/">ZWave</a> or X10 home automation and thought that voice control of your devices would be an obvious next step?</li>
	</ul>

	<p>Well, ok &#8230; I&#8217;m not going to show you how to turn on lights or work with home automation &#8212; but that&#8217;s the main thing that keeps me thinking about this voice-recognition stuff. What geek doesn&#8217;t want to walk into the living room and say &#8220;Computer: Lights On&#8221; and have it work?</p>

	<p>Instead, as a first step to all of that, let me show you how to use PowerShell to do simple voice command recognition scripts &#8230; which can fire off any PowerShell script you care to write! <span id="more-1188"></span> The code which follows is really a module, although you can just dot-source it as a script, and it really requires PowerShell 2.0 for the events, although it would be trivial to refactor it to work on PowerShell 1.0 using Oisin&#8217;s <a href="http://pseventing.codeplex.com/">PSEventing library</a>.</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #660033; font-weight: bold;">$null</span> <span style="color: #66cc66;">=</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Reflection.<span style="color: #003366;">Assembly</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">LoadWithPartialName</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;System.Speech&quot;</span><span style="color: #333;">&#41;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## Create the two main objects we need for speech recognition and synthesis</span><br />
<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #66cc66;">!</span><span style="color: #660033; font-weight: bold;">$Global</span>:SpeechModuleListener<span style="color: #333;">&#41;</span><span style="color: #333;">&#123;</span> <span style="color: #666666; font-style: italic;">## For XP's sake, don't create them twice...</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Global</span>:SpeechModuleSpeaker <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">new-<span style="font-style: normal;">object</span></span> System.<span style="color: #003366;">Speech</span>.<span style="color: #003366;">Synthesis</span>.<span style="color: #003366;">SpeechSynthesizer</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Global</span>:SpeechModuleListener <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">new-<span style="font-style: normal;">object</span></span> System.<span style="color: #003366;">Speech</span>.<span style="color: #003366;">Recognition</span>.<span style="color: #003366;">SpeechRecognizer</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #660033; font-weight: bold;">$Script</span>:SpeechModuleMacros <span style="color: #66cc66;">=</span> @<span style="color: #333;">&#123;</span><span style="color: #333;">&#125;</span><br />
<span style="color: #666666; font-style: italic;">## Add a way to turn it off</span><br />
<span style="color: #660033; font-weight: bold;">$Script</span>:SpeechModuleMacros.<span style="color: #003366;">Add</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;Stop Listening&quot;</span>, <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$script</span>:listen <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$false</span>; <span style="color: #0066cc; font-style: italic;">Suspend-<span style="font-style: normal;">Listening</span></span> <span style="color: #333;">&#125;</span><span style="color: #333;">&#41;</span><br />
<span style="color: #660033; font-weight: bold;">$Script</span>:SpeechModuleComputerName <span style="color: #66cc66;">=</span> $<span style="color: #333;">&#123;</span>Env:ComputerName<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Update-<span style="font-style: normal;">SpeechCommands</span></span> <span style="color: #333;">&#123;</span><br />
<span style="color: #666666; font-style: italic;">#.Synopsis </span><br />
<span style="color: #666666; font-style: italic;"># &nbsp;Recreate the speech recognition grammar</span><br />
<span style="color: #666666; font-style: italic;">#.Description</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp;This parses out the speech module macros, </span><br />
<span style="color: #666666; font-style: italic;"># &nbsp;and recreates the speech recognition grammar and semantic results, </span><br />
<span style="color: #666666; font-style: italic;"># &nbsp;and then updates the SpeechRecognizer with the new grammar, </span><br />
<span style="color: #666666; font-style: italic;"># &nbsp;and makes sure that the ObjectEvent is registered.</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$choices</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">new-<span style="font-style: normal;">object</span></span> System.<span style="color: #003366;">Speech</span>.<span style="color: #003366;">Recognition</span>.<span style="color: #003366;">Choices</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">foreach</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$choice</span> <span style="color: #666699; font-weight: bold;">in</span> <span style="color: #660033; font-weight: bold;">$Script</span>:SpeechModuleMacros.<span style="color: #003366;">GetEnumerator</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> System.<span style="color: #003366;">Speech</span>.<span style="color: #003366;">Recognition</span>.<span style="color: #003366;">SemanticResultValue</span> <span style="color: #660033; font-weight: bold;">$choice</span>.<span style="color: #003366;">Key</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$choice</span>.<span style="color: #003366;">Value</span>.<span style="color: #003366;">ToString</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">|</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ForEach<span style="color: #66cc66;">-</span>Object<span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$choices</span>.<span style="color: #003366;">Add</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">ToGrammarBuilder</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$VerbosePreference</span> <span style="color: #000066;">-ne</span> <span style="color: #009900;">&quot;SilentlyContinue&quot;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$Script</span>:SpeechModuleMacros.<span style="color: #003366;">Keys</span> <span style="color: #66cc66;">|</span> <br />
&nbsp; &nbsp; &nbsp; ForEach<span style="color: #66cc66;">-</span>Object <span style="color: #333;">&#123;</span> <span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;$Computer, $_&quot;</span> <span style="color: #000066;">-Fore</span> Cyan <span style="color: #333;">&#125;</span> <span style="color: #333;">&#125;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$builder</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> System.<span style="color: #003366;">Speech</span>.<span style="color: #003366;">Recognition</span>.<span style="color: #003366;">GrammarBuilder</span> <span style="color: #009900;">&quot;$Computer, &quot;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$builder</span>.<span style="color: #003366;">Append</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> System.<span style="color: #003366;">Speech</span>.<span style="color: #003366;">Recognition</span>.<span style="color: #003366;">SemanticResultKey</span> <span style="color: #009900;">&quot;Commands&quot;</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$choices</span>.<span style="color: #003366;">ToGrammarBuilder</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$grammar</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">new-<span style="font-style: normal;">object</span></span> System.<span style="color: #003366;">Speech</span>.<span style="color: #003366;">Recognition</span>.<span style="color: #003366;">Grammar</span> <span style="color: #660033; font-weight: bold;">$builder</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$grammar</span>.<span style="color: #003366;">Name</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Power VoiceMacros&quot;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## Take note of the events, but only once (make sure to remove the old one)</span><br />
&nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Unregister-<span style="font-style: normal;">Event</span></span> <span style="color: #009900;">&quot;SpeechModuleCommandRecognized&quot;</span> <span style="color: #000066;">-ErrorAction</span> SilentlyContinue<br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$null</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Register-<span style="font-style: normal;">ObjectEvent</span></span> <span style="color: #660033; font-weight: bold;">$grammar</span> SpeechRecognized `<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">-SourceIdentifier</span> <span style="color: #009900;">&quot;SpeechModuleCommandRecognized&quot;</span> `<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">-Action</span> <span style="color: #333;">&#123;</span> <span style="color: #660033;">iex</span> <span style="color: #660033; font-weight: bold;">$event</span>.<span style="color: #003366;">SourceEventArgs</span>.<span style="color: #003366;">Result</span>.<span style="color: #003366;">Semantics</span>.<span style="color: #003366;">Item</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;Commands&quot;</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">Value</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Global</span>:SpeechModuleListener.<span style="color: #003366;">UnloadAllGrammars</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Global</span>:SpeechModuleListener.<span style="color: #003366;">LoadGrammarAsync</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$grammar</span> <span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Add-<span style="font-style: normal;">SpeechCommands</span></span> <span style="color: #333;">&#123;</span><br />
<span style="color: #666666; font-style: italic;">#.Synopsis</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp;Add one or more commands to the speech-recognition macros, and update the recognition</span><br />
<span style="color: #666666; font-style: italic;">#.Parameter CommandText</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp;The string key for the command to remove</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#91;</span>CmdletBinding<span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">Param</span><span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">hashtable</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$VoiceMacros</span>,<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Computer</span><span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$Script</span>:SpeechModuleComputerName<span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## Add the new macros</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Script</span>:SpeechModuleMacros <span style="color: #66cc66;">+=</span> <span style="color: #660033; font-weight: bold;">$VoiceMacros</span> <br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## Update the default if they change it, so they only have to do that once.</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Script</span>:SpeechModuleComputerName <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$Computer</span> <br />
&nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Update-<span style="font-style: normal;">SpeechCommands</span></span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Remove-<span style="font-style: normal;">SpeechCommands</span></span> <span style="color: #333;">&#123;</span><br />
<span style="color: #666666; font-style: italic;">#.Synopsis</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp;Remove one or more command from the speech-recognition macros, and update the recognition</span><br />
<span style="color: #666666; font-style: italic;">#.Parameter CommandText</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp;The string key for the command to remove</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">Param</span><span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#91;</span><span style="color: #333;">&#93;</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$CommandText</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">foreach</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$command</span> <span style="color: #666699; font-weight: bold;">in</span> <span style="color: #660033; font-weight: bold;">$CommandText</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$Script</span>:SpeechModuleMacros.<span style="color: #003366;">Remove</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Command</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Update-<span style="font-style: normal;">SpeechCommands</span></span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Clear-<span style="font-style: normal;">SpeechCommands</span></span> <span style="color: #333;">&#123;</span><br />
<span style="color: #666666; font-style: italic;">#.Synopsis</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp;Removes all commands from the speech-recognition macros, and update the recognition</span><br />
<span style="color: #666666; font-style: italic;">#.Parameter CommandText</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp;The string key for the command to remove</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Script</span>:SpeechModuleMacros <span style="color: #66cc66;">=</span> @<span style="color: #333;">&#123;</span><span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## Default value: A way to turn it off</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Script</span>:SpeechModuleMacros.<span style="color: #003366;">Add</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;Stop Listening&quot;</span>, <span style="color: #333;">&#123;</span> <span style="color: #0066cc; font-style: italic;">Suspend-<span style="font-style: normal;">Listening</span></span> <span style="color: #333;">&#125;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Update-<span style="font-style: normal;">SpeechCommands</span></span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Start-<span style="font-style: normal;">Listening</span></span> <span style="color: #333;">&#123;</span><br />
<span style="color: #666666; font-style: italic;">#.Synopsis</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp;Sets the SpeechRecognizer to Enabled</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Global</span>:SpeechModuleListener.<span style="color: #003366;">Enabled</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$true</span><br />
&nbsp; &nbsp;Say <span style="color: #009900;">&quot;Speech Macros are $($Global:SpeechModuleListener.State)&quot;</span><br />
&nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;Speech Macros are $($Global:SpeechModuleListener.State)&quot;</span><br />
<span style="color: #333;">&#125;</span><br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Suspend-<span style="font-style: normal;">Listening</span></span> <span style="color: #333;">&#123;</span><br />
<span style="color: #666666; font-style: italic;">#.Synopsis</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp;Sets the SpeechRecognizer to Disabled</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Global</span>:SpeechModuleListener.<span style="color: #003366;">Enabled</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$false</span><br />
&nbsp; &nbsp;Say <span style="color: #009900;">&quot;Speech Macros are disabled&quot;</span><br />
&nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;Speech Macros are disabled&quot;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Out-<span style="font-style: normal;">Speech</span></span> <span style="color: #333;">&#123;</span><br />
<span style="color: #666666; font-style: italic;">#.Synopsis</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp;Speaks the input object</span><br />
<span style="color: #666666; font-style: italic;">#.Description</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp;Uses the default SpeechSynthesizer settings to speak the string representation of the InputObject</span><br />
<span style="color: #666666; font-style: italic;">#.Parameter InputObject</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp;The object to speak </span><br />
<span style="color: #666666; font-style: italic;"># &nbsp;NOTE: this should almost always be a pre-formatted string,</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp; &nbsp; &nbsp; &nbsp;most objects don't render to very speakable text.</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">Param</span><span style="color: #333;">&#40;</span> <span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>ValueFromPipeline<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$true</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><span style="color: #333;">&#91;</span>Alias<span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;IO&quot;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><span style="color: #660033; font-weight: bold;">$InputObject</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$null</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$Global</span>:SpeechModuleSpeaker.<span style="color: #003366;">SpeakAsync</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$InputObject</span><span style="color: #66cc66;">|</span><span style="color: #0066cc; font-style: italic;">Out-<span style="font-style: normal;">String</span></span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Remove-<span style="font-style: normal;">SpeechXP</span></span> <span style="color: #333;">&#123;</span><br />
<span style="color: #666666; font-style: italic;">#.Synopis</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp;Dispose of the SpeechModuleListener and SpeechModuleSpeaker</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Global</span>:SpeechModuleListener.<span style="color: #003366;">Dispose</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span>; <span style="color: #660033; font-weight: bold;">$Global</span>:SpeechModuleListener <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$null</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Global</span>:SpeechModuleSpeaker.<span style="color: #003366;">Dispose</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span>; <span style="color: #660033; font-weight: bold;">$Global</span>:SpeechModuleSpeaker <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$null</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #0066cc; font-style: italic;">set-<span style="font-style: normal;">alias</span></span> asc <span style="color: #0066cc; font-style: italic;">Add-<span style="font-style: normal;">SpeechCommands</span></span><br />
<span style="color: #0066cc; font-style: italic;">set-<span style="font-style: normal;">alias</span></span> rsc <span style="color: #0066cc; font-style: italic;">Remove-<span style="font-style: normal;">SpeechCommands</span></span><br />
<span style="color: #0066cc; font-style: italic;">set-<span style="font-style: normal;">alias</span></span> csc <span style="color: #0066cc; font-style: italic;">Clear-<span style="font-style: normal;">SpeechCommands</span></span><br />
<span style="color: #0066cc; font-style: italic;">set-<span style="font-style: normal;">alias</span></span> say <span style="color: #0066cc; font-style: italic;">Out-<span style="font-style: normal;">Speech</span></span><br />
<span style="color: #0066cc; font-style: italic;">set-<span style="font-style: normal;">alias</span></span> listen <span style="color: #0066cc; font-style: italic;">Start-<span style="font-style: normal;">Listener</span></span><br />
<span style="color: #0066cc; font-style: italic;">Export-<span style="font-style: normal;">ModuleMember</span></span> <span style="color: #000066;">-Function</span> <span style="color: #66cc66;">*</span> <span style="color: #000066;">-Alias</span> <span style="color: #66cc66;">*</span> <span style="color: #000066;">-Variable</span> SpeechModuleListener, SpeechModuleSpeaker</div>

	<p>There&#8217;s basically just one function you need to worry about here: <code>New-VoiceCommands</code>.  You pass it a hashtable which maps strings to scriptblocks, and if you use the <code>-Listen</code> switch that&#8217;s all there is to it.  You can also call <code>Start-Listening</code> manually, and of course, I&#8217;ve provided the <code>Say</code> function to make it easier to have the computer speak&#8230;</p>

	<p>Once the computer is &#8220;listening&#8221; ... you just say it&#8217;s name, followed by one of your commands. I like that because it ensures that I don&#8217;t run the scripts by accident, but you can remove the <code>&#34;${Env:ComputerName}, &#34;</code> string from the beginning of the GrammarBuilder if you think it&#8217;s not necessary, or you can hard code it to something other than your computer&#8217;s name, like say &#8220;Hal, please, I beg you &#8230; &#8220; or &#8220;Computer, please &#8220; or whatever.  <img src='http://joelbennett.net/wordpress/wp-includes/' alt=':-)' class='wp-smiley' /> </p>

	<p>You can do a lot of things with this &#8230; anything, really &#8230; but to give you an example that you can easily understand, I&#8217;m going to do something very simple, and have my computer just answer a few basic questions by talking back to me, and then add a few commands to have it start an app or a web page.</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">Add-<span style="font-style: normal;">SpeechCommands</span></span> @<span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&quot;What time is it?&quot;</span> <span style="color: #66cc66;">=</span> <span style="color: #333;">&#123;</span> Say <span style="color: #009900;">&quot;It is $(Get-Date -f &quot;</span><span style="color: #660033;">h</span>:mm tt<span style="color: #009900;">&quot;)&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&quot;What day is it?&quot;</span> &nbsp;<span style="color: #66cc66;">=</span> <span style="color: #333;">&#123;</span> Say $<span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Date</span></span> <span style="color: #000066;">-f</span> <span style="color: #009900;">&quot;dddd, MMMM dd&quot;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&quot;What's running?&quot;</span> &nbsp;<span style="color: #66cc66;">=</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$proc</span> <span style="color: #66cc66;">=</span> <span style="color: #660033;">ps</span> <span style="color: #66cc66;">|</span> <span style="color: #660033;">sort</span> ws <span style="color: #000066;">-desc</span><br />
&nbsp; &nbsp; &nbsp; Say $<span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;$($proc.Count) processes, including $($proc[0].name), which is using &quot;</span> <span style="color: #66cc66;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&quot;$([int]($proc[0].ws/1mb)) megabytes of memory&quot;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-Computer</span> <span style="color: #009900;">&quot;Laptop&quot;</span> <span style="color: #000066;">-Verbose</span> <br />
<br />
<span style="color: #0066cc; font-style: italic;">Add-<span style="font-style: normal;">SpeechCommands</span></span> @<span style="color: #333;">&#123;</span> <span style="color: #009900;">&quot;Run Notepad&quot;</span> <span style="color: #66cc66;">=</span> <span style="color: #333;">&#123;</span> <span style="color: #66cc66;">&amp;</span>amp; <span style="color: #009900;">&quot;C:\Programs\DevTools\Notepad++\notepad++.exe&quot;</span> <span style="color: #333;">&#125;</span> <span style="color: #333;">&#125;</span><br />
<span style="color: #0066cc; font-style: italic;">Add-<span style="font-style: normal;">SpeechCommands</span></span> @<span style="color: #333;">&#123;</span> <span style="color: #009900;">&quot;Check Gee Mail&quot;</span> <span style="color: #66cc66;">=</span> <span style="color: #333;">&#123;</span> <span style="color: #0066cc; font-style: italic;">Start-<span style="font-style: normal;">Process</span></span> <span style="color: #009900;">&quot;https://mail.google.com&quot;</span> <span style="color: #333;">&#125;</span> <span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>You see how easy that is? You can use &#8220;Say&#8221; to speak any text (although sometext will get better results than others), and you can invoke any other powershell commands, including HttpRest commands to fetch web data, or <span class="caps">WASP</span> commands for windows automation, or PowerBoots commands to display output in large text, or cmdlets to control X10 or ZWave devices &#8230; you know, anything  <img src='http://joelbennett.net/wordpress/wp-includes/' alt=';-)' class='wp-smiley' /> </p>

<div class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/d6e29bc6-dd76-464f-8040-7c8d8e90b08f/" title="Reblog this post [with Zemanta]"><img class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=d6e29bc6-dd76-464f-8040-7c8d8e90b08f" alt="Reblog this post [with Zemanta]" /></a><span class="zem-script more-related pretty-attribution"><script type="text/javascript" src="http://static.zemanta.com/readside/loader.js" defer="defer"></script></span></div>]]></content:encoded>
			<wfw:commentRss>http://joelbennett.net/control-your-pc-with-your-voice-and-powershell/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

