Currently I attempted Speech to text with System.Speech and Microsoft.Speech
what I can do is only with Microsoft.Speech because System.Speech doesn't work if default language is not English.
First, I tried to use this code to get text from my voice.
public void Recognize()
{
using ( SpeechSynthesizer Synthesizer = new SpeechSynthesizer() )
{
// Text To Speech
Synthesizer.SetOutputToDefaultAudioDevice();
//Synthesizer.Speak( "Test" );
using ( SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine() )
{
recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>( OnSpeechRecognized );
recognizer.LoadGrammarCompleted += new EventHandler<LoadGrammarCompletedEventArgs>( OnLoadGrammarCompleted );
recognizer.SetInputToDefaultAudioDevice();
recognizer.LoadGrammar( new Grammar( "Resources/Grammar.xml" )
{
Enabled = true
} );
Console.WriteLine( "Recognize()" );
RecognitionResult result = recognizer.Recognize( TimeSpan.FromSeconds( 3 ) );
if ( result != null )
{
Console.WriteLine( "Result => " + result.Text );
}
else
{
Console.WriteLine( "Text is not on list" );
}
}
}
}
and I noticed that if the sentence( or word ) in my speech doesn't contained in List in Grammar Items, it'll not applied on result.
so I searched about this problem and finally found this code.
public Grammar GrammarWithDictation()
{
GrammarBuilder builder = new GrammarBuilder();
builder.Append( "begin" );
builder.AppendDictation( "spelling" );
builder.Append( "end" );
Grammar grammarWithDictation = new Grammar( builder );
grammarWithDictation.Name = "Grammar with Dictation";
return grammarWithDictation;
}
So I applied it on my code like this.
public void Recognize()
{
using ( SpeechSynthesizer Synthesizer = new SpeechSynthesizer() )
{
// Text To Speech
Synthesizer.SetOutputToDefaultAudioDevice();
//Synthesizer.Speak( "Test" );
using ( SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine() )
{
recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>( OnSpeechRecognized );
recognizer.LoadGrammarCompleted += new EventHandler<LoadGrammarCompletedEventArgs>( OnLoadGrammarCompleted );
recognizer.SetInputToDefaultAudioDevice();
//recognizer.LoadGrammar( new Grammar( "Resources/Grammar.xml" )
//{
// Enabled = true
//} );
recognizer.LoadGrammarAsync( GrammarWithDictation() );
Console.WriteLine( "Recognize()" );
RecognitionResult result = recognizer.Recognize( TimeSpan.FromSeconds( 3 ) );
if ( result != null )
{
Console.WriteLine( "Result => " + result.Text );
}
else
{
Console.WriteLine( "No Results." );
}
}
}
}
and it doesn't work.
So this is the main point of this long article.
How can I get whole sentence in my speech with Microsoft.Speech?
is it possible? or do I need to use other things to do this?