public GoogleSpeechRecognizer(ISpeechOutput output, ActivationWord[] activators, float activationConfidence, float commandConfidence, string captureDeviceName) { GoogleSpeechApiUtils.Authorize(); _output = output; Activators = activators; ActivationConfidence = activationConfidence; CommandConfidence = commandConfidence; CaptureDeviceIndex = Utils.GetCaptureDeviceIndex(captureDeviceName); _activator = new MicrosoftSpeechRecognizer(activationConfidence, captureDeviceName); _activator.Keywords = activators.Select(x => Utils.PrepareActivatorString(x.Word)).ToArray(); foreach (var activator in activators) { if (activator is ActivatorWord_StartGoogleRecognition _googleRecognition) { _googleRecognition.SetAction(async(args) => await ActivateInternalAsync(args.ActivatorContinue)); } } _activator.Activated += (o, e) => { new Thread(() => { var args = e as ActivatorEventArgs; bool found = false; foreach (var activator in activators) { if (Utils.CompareActivatorPhrases(activator.Word, args.Word)) { activator.Activate((ActivatorEventArgs)e); found = true; break; } } if (!found) { args.ActivatorContinue(); } }) { IsBackground = true, Priority = ThreadPriority.Highest } .Start(); }; }
public static async Task StreamingMicRecognizeAsync(int ms, ISpeechOutput output, Action finish, float minConfidence, int captureDeviceIndex) { var streamingCall = Client.StreamingRecognize(); await streamingCall.WriteAsync( new StreamingRecognizeRequest() { StreamingConfig = new StreamingRecognitionConfig() { Config = new RecognitionConfig() { Encoding = RecognitionConfig.Types.AudioEncoding.Linear16, SampleRateHertz = 16000, LanguageCode = "ru" }, InterimResults = false, } }); var normalSpeechDetected = false; Task printResponses = Task.Run(async() => { var outText = string.Empty; while (await streamingCall.ResponseStream.MoveNext()) { foreach (var result in streamingCall.ResponseStream.Current.Results) { var confidence = result.Alternatives.Max(x => x.Confidence); Debug.WriteLine("SpeechRecognition"); foreach (var alt in result.Alternatives) { Debug.WriteLine(alt.Transcript + " " + alt.Confidence); } if (confidence >= minConfidence) { var alternative = result.Alternatives.LastOrDefault(x => x.Confidence == confidence); output.IntermediateResult(alternative.Transcript); outText = alternative.Transcript; normalSpeechDetected = true; } } } if (normalSpeechDetected) { output.Result(outText); } }); object writeLock = new object(); bool writeMore = true; var waveIn = new NAudio.Wave.WaveInEvent(); waveIn.DeviceNumber = captureDeviceIndex; waveIn.WaveFormat = new NAudio.Wave.WaveFormat(16000, 1); waveIn.DataAvailable += (object sender, NAudio.Wave.WaveInEventArgs args) => { lock (writeLock) { if (!writeMore) { return; } streamingCall.WriteAsync( new StreamingRecognizeRequest() { AudioContent = Google.Protobuf.ByteString .CopyFrom(args.Buffer, 0, args.BytesRecorded) }).Wait(); } }; output.RecordStarted(); try { waveIn.StartRecording(); await Task.Delay(TimeSpan.FromMilliseconds(ms)); if (!normalSpeechDetected) { output.RecordCanceled(); } waveIn.StopRecording(); lock (writeLock) { writeMore = false; } await streamingCall.WriteCompleteAsync(); await printResponses; finish?.Invoke(); output.RecordFinished(); } catch { output.RecordCanceled(); } }