Introduction

After some play on system's speech library "System.Speech" we can control some system application and locking through speech with minimize modulation or without any pre voice recording.

Concept behind the approach

Here we mainly concentrate on "SpeechRecognitionEngine" and "GrammarBuilder". Normally this engine would recognize the word only when we pronounce the word with correct modulation. So we have to pronounce the word with accurate modulation. So here we have avoid this by a simple logic that try to match the result value. So it avoid some voice modulation problem in a better way.

C# code behind the approach

using System.Speech.Recognition;

using System.Speech.Synthesis;

[DllImport("user32")]

public static extern void LockWorkStation();

private SpeechRecognitionEngine recognizer;

private void initialization()

{

// Create grammar that makes a sentence as required

GrammarBuilder grammarBuilder = new GrammarBuilder();

grammarBuilder.Append(new Choices("system"));

grammarBuilder.Append(new Choices("shutdown", "restart", "lock"));

// Create a instance of SpeechRecognitionEngine

recognizer = new SpeechRecognitionEngine();

recognizer.RequestRecognizerUpdate();

//Load grammar in SpeechRecognitionEngine

recognizer.LoadGrammar(new Grammar(grammarBuilder));

//Hook SpeechRecognized, for words detected with correct modulation

recognizer.SpeechRecognized += _recognizer_SpeechRecognized;

//Hook SpeechRecognized, for words detected with incorrect modulation

recognizer.SpeechRecognitionRejected += _recognizer_SpeechRecognitionRejected;

recognizer.SetInputToDefaultAudioDevice();

recognizer.RecognizeAsync(RecognizeMode.Multiple);

}

void _recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)

{

//Hook SpeechRecognized, for words detected with correct modulation

recognize(e.Result);

}

void _recognizer_SpeechRecognitionRejected(object sender, SpeechRecognitionRejectedEventArgs e)

{

//Hook SpeechRecognized, for words detected with incorrect modulation

recognize(e.Result );

}

private void recognize(RecognitionResult e)

{

// Get and match the detected result, for words detected with incorrect modulation

foreach (RecognizedPhrase r in e.Alternates)

{

if (r.Text == "system shutdown")

Process.Start("shutdown", "/s /t 0"); //System to shutdown

else if (r.Text == "system restart")

Process.Start("shutdown", "/r /t 0"); //System to restart

else if (r.Text == "system lock")

LockWorkStation(); //System to lock

}

}

Conclusion

In the above logic will generate some relevant word according to the word that it detected and the grammar. And it matches those word to perform out desire. So it could avoid some voice modulation problem in a better way.