Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public async Task<object> StreamingMicRecognizeAsync(int seconds)
- {
- streamingCall = SpeechClient.Create().StreamingRecognize();
- await streamingCall.WriteAsync(
- new StreamingRecognizeRequest()
- {
- StreamingConfig = new StreamingRecognitionConfig()
- {
- Config = new RecognitionConfig()
- {
- Encoding = RecognitionConfig.Types.AudioEncoding.Linear16,
- SampleRateHertz = 16000,
- LanguageCode = "en-US"
- },
- InterimResults = true,
- SingleUtterance = false
- }
- }
- );
- Task prinResp = Task.Run(async () =>
- {
- while (isActive && await streamingCall.ResponseStream.MoveNext(default(CancellationToken)))
- {
- foreach (var result in streamingCall.ResponseStream.Current.Results)
- {
- MessageBox.Show(result.Alternatives[0].Transcript.ToString());
- }
- }
- });
- // Read from the microphone and stream to API.
- object writeLock = new object();
- bool writeMore = true;
- waveIn = new NAudio.Wave.WaveInEvent();
- waveIn.DeviceNumber = 0;
- waveIn.WaveFormat = new NAudio.Wave.WaveFormat(16000, 1);
- waveIn.DataAvailable +=
- (object sender, NAudio.Wave.WaveInEventArgs args) =>
- {
- lock (writeLock)
- {
- if (!writeMore || !isActive) return;
- try
- {
- streamingCall.WriteAsync(
- new StreamingRecognizeRequest()
- {
- AudioContent = Google.Protobuf.ByteString.CopyFrom(args.Buffer, 0, args.BytesRecorded)
- }).Wait();
- }
- catch (Exception e) { my.message(e.Message); }
- }
- };
- waveIn.StartRecording();
- await Task.Delay(TimeSpan.FromSeconds(180*60));
- waveIn.StopRecording();
- lock (writeLock) writeMore = false;
- await streamingCall.WriteCompleteAsync();
- await prinResp;
- return 0;
- }
Add Comment
Please, Sign In to add comment