private static WaveStream NormalizeStream(WaveStream stream) { WaveStream audioStream = stream; if (audioStream.WaveFormat.BitsPerSample != 16) { audioStream = new Wave32To16CaptureStream(stream); } if (audioStream.WaveFormat.Channels != 1) { audioStream = new MonoCaptureStream(audioStream); } if (audioStream.WaveFormat.BitsPerSample != 16000) { audioStream = new DownsampleCaptureStream(audioStream); } return(audioStream); }
public LocalSpeechRecognizer(WaveStream stream) { this.recognizer = new SpeechRecognitionEngine(); this.recognizer.LoadGrammar(new DictationGrammar()); this.resultId = Guid.NewGuid().ToString(); this.lockObj = new object(); this.recognizer.SpeechRecognized += (sndr, evt) => { string id = null; lock (this.lockObj) { id = this.resultId; this.resultId = Guid.NewGuid().ToString(); } this.SpeechRecognized?.Invoke(this, new RecognitionEventArgs(evt, id)); }; this.recognizer.SpeechHypothesized += (snder, evt) => { string id = null; lock (this.lockObj) { id = this.resultId; } this.SpeechPredicted?.Invoke(this, new RecognitionEventArgs(evt, id, DateTime.Now - this.startTime)); }; WaveStream audioStream = stream; if (audioStream.WaveFormat.BitsPerSample != 16) { audioStream = new Wave32To16CaptureStream(stream); } this.recognizer.SetInputToAudioStream(audioStream, new SpeechAudioFormatInfo(audioStream.WaveFormat.SampleRate, (AudioBitsPerSample)audioStream.WaveFormat.BitsPerSample, (AudioChannel)audioStream.WaveFormat.Channels)); }