Daniel Palme

Daniel Palme

.NET consultant from Germany.

Blog > VSTO - Speech recognition in Outlook Addin with .NET

VSTO - Speech recognition in Outlook Addin with .NET

In this article I will show how you could create an Outlook Addin that enables you to enter the recipients of an email by speech.
First we setup the speech recognition engine with the contacts from the address book. Then we listen to the relevant events to start and stop speech recognition automatically.

Setting up the speech recognition engine

We use the System.Speech.dll introduced with .NET 3.0. The most important class is the SpeechRecognitionEngine, this class is responsible to capture voice and to recognize terms in a Grammar. In our use case the grammar is very simple, it consists only of terms containing the names from the address book:

SrgsOneOf srgsOneOf = new SrgsOneOf();

for (int i = 1; i <= contacts.Items.Count; i++)
{
  var contact = contacts.Items[i] as ContactItem;

  if (contact != null)
  {
    srgsOneOf.Add(new SrgsItem(contact.FirstName + " " + contact.LastName));
    srgsOneOf.Add(new SrgsItem(contact.LastNameAndFirstName.Replace(",", "")));
  }
}

SrgsRule rule = new SrgsRule("contacts");
rule.Add(srgsOneOf);

SrgsDocument doc = new SrgsDocument();
doc.Rules.Add(rule);
doc.Root = rule;

Grammar grammar = new Grammar(doc);

The Grammar is a hierarchical structure, it contains a SrgsDocument. The SrgsDocument contains a single grammar rule (SrgsRule). The SrgsRule consists of a SrgsOneOf, which finally contains the names of the contacts wrapped into SrgsItems. These SrgsItems are the terms that are recognized by the SpeechRecognitionEngine.

To get the SpeechRecognitionEngine ready, we have to perform the following actions:

this.speachRecognizerEngine.SetInputToDefaultAudioDevice();
this.speachRecognizerEngine.UnloadAllGrammars();
this.speachRecognizerEngine.LoadGrammar(grammar);

this.speachRecognizerEngine.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(this.RegonizedNewRecipient);

After the grammar got loaded, an event handler is registered. This handler is executed every time a term from the grammer is recognized. In this case we could add the name to the list of recipients:

void RegonizedNewRecipient(object sender, SpeechRecognizedEventArgs e)
{
  this.mail.Recipients.Add(e.Result.Text);
}

Listening to the relevant Outlook events

The speech recognition should automatically start when a new mail window is opened:
this.inspectors = application.Inspectors;
application.Inspectors.NewInspector += new InspectorsEvents_NewInspectorEventHandler(this.NewMailWindowOpening);

The first line of the above code is quite important. If you don't keep a reference to the object you add an event handler to, the event handler would would stop firing after some time.

In the event handler we have to register another handler that gets executed when the window is finally ready. In the corresponding method we could start speech recognition and add another handler that executes when the window is closed or looses focus:

this.inspector.Deactivate += new InspectorEvents_10_DeactivateEventHandler(this.NewMailWindowDeactivated);
this.speachRecognizerEngine.RecognizeAsync(RecognizeMode.Multiple);

To stop speech recognition call:

this.speachRecognizerEngine.RecognizeAsyncCancel();

The above code snippets contain only the most relevant code. In the attached file you will find the full source code.

Downloads

Speech2Recipient.zip


Subscribe to RSS Feed

Tags: .NET, C#, VSTO
 

Related posts

 

New comment

:

:

:

:

 

Comments

No comments available.