using System;
namespace ConsoleApplication1
{
class Program
{
static System.Threading.AutoResetEvent are = new
System.Threading.AutoResetEvent(false);
static void Main(string[] args)
{
System.Speech.Synthesis.SpeechSynthesizer tts = new
System.Speech.Synthesis.SpeechSynthesizer();
System.Speech.AudioFormat.SpeechAudioFormatInfo aFormat =
new
System.Speech.AudioFormat.SpeechAudioFormatInfo(System.Speech.AudioFormat.EncodingFormat.Pcm
, 8000, 16, 1, 16000, 2, new byte[] { });
tts.SetOutputToWaveFile(@"test.wav", aFormat);
tts.SelectVoiceByHints(System.Speech.Synthesis.VoiceGender.Female,
System.Speech.Synthesis.VoiceAge.Adult, 0);
tts.Speak("Linux");
tts.Dispose();
System.Speech.Recognition.SpeechRecognitionEngine recon = null;
foreach (System.Speech.Recognition.RecognizerInfo info in
System.Speech.Recognition.SpeechRecognitionEngine.InstalledRecognizers())
{
if (info.Name.Equals("Microsoft English Recognizer v5.1",
StringComparison.OrdinalIgnoreCase))
recon = (new
System.Speech.Recognition.SpeechRecognitionEngine(info));
}
if (recon == null) throw new ApplicationException();
recon.SpeechRecognized += new
EventHandler<System.Speech.Recognition.SpeechRecognizedEventArgs>(recon_SpeechRecognized);
recon.RecognizeCompleted += new
EventHandler<System.Speech.Recognition.RecognizeCompletedEventArgs>(recon_RecognizeCompleted);
recon.BabbleTimeout = TimeSpan.FromSeconds(5);
recon.InitialSilenceTimeout = TimeSpan.FromSeconds(5);
recon.EndSilenceTimeoutAmbiguous = TimeSpan.FromSeconds(5);
System.Speech.Recognition.GrammarBuilder gbLinux = new
System.Speech.Recognition.GrammarBuilder("Linux");
System.Speech.Recognition.GrammarBuilder gbMicrosoft = new
System.Speech.Recognition.GrammarBuilder("Microsoft");
System.Speech.Recognition.Choices choices = new
System.Speech.Recognition.Choices(gbLinux, gbMicrosoft);
System.Speech.Recognition.GrammarBuilder bothGB = new
System.Speech.Recognition.GrammarBuilder(choices);
System.Speech.Recognition.Grammar grammar = new
System.Speech.Recognition.Grammar(bothGB);
recon.LoadGrammar(grammar);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
byte[] raw = System.IO.File.ReadAllBytes(@"test.wav");
ms.Write(raw, 58, raw.Length-58);
ms.Position = 0;
recon.SetInputToAudioStream(ms, aFormat);
recon.RecognizeAsync(System.Speech.Recognition.RecognizeMode.Single);
are.WaitOne();
Console.ReadLine();
}
static void recon_RecognizeCompleted(object sender,
System.Speech.Recognition.RecognizeCompletedEventArgs e)
{
if (e.Result != null)
Console.WriteLine("\nRC {0}: " + e.Result.Text,
e.Result.Confidence);
are.Set();
}
static void recon_SpeechRecognized(object sender,
System.Speech.Recognition.SpeechRecognizedEventArgs e)
{
if (e.Result != null)
Console.Write("SR {0}: "+e.Result.Text, e.Result.Confidence);
}
}
}