MVVM and Speech using the Kinect-Pt. II

In the last post I talked about what I recently done with Speech recognition and tying it in with MVVM’s concepts of Commands.

In this post, I want to walkthrough, step by step of how I set things up.  To get everything installed I just followed the directions for setting up the Kinect SDK, which also included the direction on setting up the Speech API.  Google that and you’ll be well on your way.

After getting it setup, I recommend you give the Kinect SDK samples a try to make sure everything installed correctly.  From there I took a look at what the Kinect speech sample was doing and modified it to work with the default audio source instead of the Kinect.  Mostly, because my Kinect needs to pull double duty between my hacking and actually letting me play on the Xbox.  Not sure how I can convince the wife we need a second one just yet.

Note that some of the code examples use some extensions methods in a little library of mine.  So you might not be able to directly copy/paste. Hit up the continue reading link for the rest…

Speech Command

Nothing groundbreaking here.  This is basically a note for note reimplementation of the DelegateCommand found in Prism and other MVVM frameworks.  The main purpose here is to give somewhere for the Phrase attached property to live.  As I look back, I could have very easily done this entire thing with one class, but I didn’t want to take time to setup an entire MVVM framework just for my little proof of concept so I still needed something that implemented ICommand and it just made sense to put it there.

The attached property does one bit of magic.  I provide a callback for when the value changes and in there we do this:

public static void PhraseChanged(DependencyObject d,
DependencyPropertyChangedEventArgs e)
if (DesignerProperties.GetIsInDesignMode(d))

if (!d.Is<ICommandSource>())
throw new
InvalidCastException("Can only use objects that implement the ICommandSource interface");

SpeechFactory.AddPhrase(e.NewValue.ToString(), d.As<ICommandSource>());
So we basically pass the phrase and the object that has the attached property set on it.  We do a quick check to make sure it implements the ICommandSource interface.  Objects that do this provide a Command property and a CommandParameter that we will use to execute the command later on.

Speech Factory

This is where 95% of the work is done.  Basically, it’s a static class that does all the instantiation and setup of the speech recognition engine.  It also maintains the list of phrases the developer has entered in the Xaml.

The first thing we do is setup all the recognition engine code:

private static bool _commandMode;
private static Timer _timer = new Timer(3000);
private static SpeechRecognitionEngine _engine;
private static Dictionary<string, ICommandSource> _phrases =
new Dictionary<string, ICommandSource>();
private static string _commandWord = "computer";

static SpeechFactory()

_engine = new SpeechRecognitionEngine();
_engine.SpeechRecognized += SpeechRecognized;
_engine.SpeechHypothesized += SpeechHypothesized;
_engine.SpeechRecognitionRejected += SpeechRecognitionRejected;

_engine.LoadGrammar(new Grammar(new GrammarBuilder(new Choices(_commandWord))));

_timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed);

We setup all the recognition events and kick it off.  Whenever a new phrase is added we reload the grammar and choice lists the engine uses to determine what has been spoken.

public static void AddPhrase(string phrase, ICommandSource source)

_phrases.Add(phrase.Trim().ToLower(), source);

// TODO: There are culture considerations to take into effect
var choices = _phrases.Keys.Union(new string[]{_commandWord}).ToArray();
var gb = new GrammarBuilder(new Choices(choices));

_engine.LoadGrammar(new Grammar(gb));
One final little thing I do is the concept of a command word.  So it

One Reply to “MVVM and Speech using the Kinect-Pt. II”

  1. Hi I was wondering if you had an example of this in full that I could have a look at. I have been rooming the internet for hours trying to figure out how to get the sound detection working on my WPF application but the problem is. All the examples use a console and I’m having trouble mapping them onto my application… would be greatful.

    Kind Regards


Leave a Reply

Your email address will not be published. Required fields are marked *