Example #1
0
 /// <summary>
 /// Performs synchronous speech recognition: receive results after all audio
 /// has been sent and processed.
 /// </summary>
 /// <param name="request">
 /// The request object containing all of the parameters for the API call.
 /// </param>
 /// <param name="callSettings">
 /// If not null, applies overrides to this RPC call.
 /// </param>
 /// <returns>
 /// The RPC response.
 /// </returns>
 public override RecognizeResponse Recognize(
     RecognizeRequest request,
     gaxgrpc::CallSettings callSettings = null)
 {
     Modify_RecognizeRequest(ref request, ref callSettings);
     return(_callRecognize.Sync(request, callSettings));
 }
        public string Recognize(RecognitionConfig config, Stream audioStream)
        {
            byte[] audioBytes;
            using (MemoryStream buffer = new MemoryStream())
            {
                audioStream.CopyTo(buffer);
                audioBytes = buffer.ToArray();
            }

            RecognizeRequest request = new RecognizeRequest();

            request.Config = config;
            request.Audio  = new RecognitionAudio
            {
                Content = Google.Protobuf.ByteString.CopyFrom(audioBytes, 0, audioBytes.Length)
            };

            var response = _clientSTT.Recognize(request, this.GetMetadataSTT());

            var texts = new List <string>();

            foreach (var result in response.Results)
            {
                foreach (var alt in result.Alternatives)
                {
                    texts.Add(alt.Transcript);
                }
            }

            return(string.Join(" ", texts));
        }
        public async stt::Task RecognizeRequestObjectAsync()
        {
            moq::Mock <Speech.SpeechClient> mockGrpcClient = new moq::Mock <Speech.SpeechClient>(moq::MockBehavior.Strict);

            mockGrpcClient.Setup(x => x.CreateOperationsClient()).Returns(new moq::Mock <lro::Operations.OperationsClient>().Object);
            RecognizeRequest request = new RecognizeRequest
            {
                Config = new RecognitionConfig(),
                Audio  = new RecognitionAudio(),
            };
            RecognizeResponse expectedResponse = new RecognizeResponse
            {
                Results =
                {
                    new SpeechRecognitionResult(),
                },
                TotalBilledTime = new wkt::Duration(),
            };

            mockGrpcClient.Setup(x => x.RecognizeAsync(request, moq::It.IsAny <grpccore::CallOptions>())).Returns(new grpccore::AsyncUnaryCall <RecognizeResponse>(stt::Task.FromResult(expectedResponse), null, null, null, null));
            SpeechClient      client = new SpeechClientImpl(mockGrpcClient.Object, null);
            RecognizeResponse responseCallSettings = await client.RecognizeAsync(request, gaxgrpc::CallSettings.FromCancellationToken(st::CancellationToken.None));

            xunit::Assert.Same(expectedResponse, responseCallSettings);
            RecognizeResponse responseCancellationToken = await client.RecognizeAsync(request, st::CancellationToken.None);

            xunit::Assert.Same(expectedResponse, responseCancellationToken);
            mockGrpcClient.VerifyAll();
        }
Example #4
0
        private void OnRecognizeResponse(RESTConnector.Request req, RESTConnector.Response resp)
        {
            RecognizeRequest recognizeReq = req as RecognizeRequest;

            if (recognizeReq == null)
            {
                throw new WatsonException("Unexpected request type.");
            }

            SpeechResultList result = null;

            if (resp.Success)
            {
                result = ParseRecognizeResponse(resp.Data);
                if (result == null)
                {
                    Log.Error("SpeechToText", "Failed to parse json response: {0}",
                              resp.Data != null ? Encoding.UTF8.GetString(resp.Data) : "");
                }
                else
                {
                    Log.Status("SpeechToText", "Received Recognize Response, Elapsed Time: {0}, Results: {1}",
                               resp.ElapsedTime, result.Results.Length);
                }
            }
            else
            {
                Log.Error("SpeechToText", "Recognize Error: {0}", resp.Error);
            }

            if (recognizeReq.Callback != null)
            {
                recognizeReq.Callback(result);
            }
        }
Example #5
0
        /// <summary>Snippet for RecognizeAsync</summary>
        public async Task RecognizeAsync_RequestObject()
        {
            // Snippet: RecognizeAsync(RecognizeRequest,CallSettings)
            // Additional: RecognizeAsync(RecognizeRequest,CancellationToken)
            // Create client
            SpeechClient speechClient = await SpeechClient.CreateAsync();

            // Initialize request argument(s)
            RecognizeRequest request = new RecognizeRequest
            {
                Config = new RecognitionConfig
                {
                    Encoding        = RecognitionConfig.Types.AudioEncoding.Flac,
                    SampleRateHertz = 44100,
                    LanguageCode    = "en-US",
                },
                Audio = new RecognitionAudio
                {
                    Uri = "gs://bucket_name/file_name.flac",
                },
            };
            // Make the request
            RecognizeResponse response = await speechClient.RecognizeAsync(request);

            // End snippet
        }
Example #6
0
        /// <summary>
        /// Adds additional details short audio file included in this recognition request
        /// </summary>
        /// <param name="localFilePath">Path to local audio file, e.g. /path/audio.wav</param>
        public static void SampleRecognize(string localFilePath)
        {
            SpeechClient speechClient = SpeechClient.Create();
            // string localFilePath = "resources/commercial_mono.wav"
            RecognizeRequest request = new RecognizeRequest
            {
                Config = new RecognitionConfig
                {
                    Metadata = new RecognitionMetadata
                    {
                        InteractionType     = RecognitionMetadata.Types.InteractionType.VoiceSearch,
                        RecordingDeviceType = RecognitionMetadata.Types.RecordingDeviceType.Smartphone,
                        RecordingDeviceName = "Pixel 3",
                    },
                    // The language of the supplied audio. Even though additional languages are
                    // provided by alternative_language_codes, a primary language is still required.
                    LanguageCode = "en-US",
                },
                Audio = new RecognitionAudio
                {
                    Content = ByteString.CopyFrom(File.ReadAllBytes(localFilePath)),
                },
            };
            RecognizeResponse response = speechClient.Recognize(request);

            foreach (var result in response.Results)
            {
                // First alternative is the most probable result
                SpeechRecognitionAlternative alternative = result.Alternatives[0];
                Console.WriteLine($"Transcript: {alternative.Transcript}");
            }
        }
        /// <summary>
        /// Print confidence level for individual words in a transcription of a short audio file
        /// </summary>
        /// <param name="localFilePath">Path to local audio file, e.g. /path/audio.wav</param>
        public static void SampleRecognize(string localFilePath)
        {
            SpeechClient speechClient = SpeechClient.Create();
            // string localFilePath = "resources/brooklyn_bridge.flac"
            RecognizeRequest request = new RecognizeRequest
            {
                Config = new RecognitionConfig
                {
                    // When enabled, the first result returned by the API will include a list
                    // of words and the confidence level for each of those words.
                    EnableWordConfidence = true,
                    // The language of the supplied audio
                    LanguageCode = "en-US",
                },
                Audio = new RecognitionAudio
                {
                    Content = ByteString.CopyFrom(File.ReadAllBytes(localFilePath)),
                },
            };
            RecognizeResponse response = speechClient.Recognize(request);
            // The first result includes confidence levels per word
            SpeechRecognitionResult result = response.Results[0];
            // First alternative is the most probable result
            SpeechRecognitionAlternative alternative = result.Alternatives[0];

            Console.WriteLine($"Transcript: {alternative.Transcript}");
            // Print the confidence level of each word
            foreach (var word in alternative.Words)
            {
                Console.WriteLine($"Word: {word.Word}");
                Console.WriteLine($"Confidence: {word.Confidence}");
            }
        }
Example #8
0
        /// <summary>
        /// Transcribe a short audio file with language detected from a list of possible languages
        /// </summary>
        /// <param name="localFilePath">Path to local audio file, e.g. /path/audio.wav</param>
        public static void SampleRecognize(string localFilePath)
        {
            SpeechClient speechClient = SpeechClient.Create();
            // string localFilePath = "resources/brooklyn_bridge.flac"
            RecognizeRequest request = new RecognizeRequest
            {
                Config = new RecognitionConfig
                {
                    // The language of the supplied audio. Even though additional languages are
                    // provided by alternative_language_codes, a primary language is still required.
                    LanguageCode             = "fr",
                    AlternativeLanguageCodes =
                    {
                        "es",
                        "en",
                    },
                },
                Audio = new RecognitionAudio
                {
                    Content = ByteString.CopyFrom(File.ReadAllBytes(localFilePath)),
                },
            };
            RecognizeResponse response = speechClient.Recognize(request);

            foreach (var result in response.Results)
            {
                // The languageCode which was detected as the most likely being spoken in the audio
                Console.WriteLine($"Detected language: {result.LanguageCode}");
                // First alternative is the most probable result
                SpeechRecognitionAlternative alternative = result.Alternatives[0];
                Console.WriteLine($"Transcript: {alternative.Transcript}");
            }
        }
Example #9
0
        /// <summary>
        /// Transcribe a short audio file with punctuation
        /// </summary>
        /// <param name="localFilePath">Path to local audio file, e.g. /path/audio.wav</param>
        public static void SampleRecognize(string localFilePath)
        {
            SpeechClient speechClient = SpeechClient.Create();
            // string localFilePath = "resources/commercial_mono.wav"
            RecognizeRequest request = new RecognizeRequest
            {
                Config = new RecognitionConfig
                {
                    // When enabled, trascription results may include punctuation (available for select languages).
                    EnableAutomaticPunctuation = true,
                    // The language of the supplied audio. Even though additional languages are
                    // provided by alternative_language_codes, a primary language is still required.
                    LanguageCode = "en-US",
                },
                Audio = new RecognitionAudio
                {
                    Content = ByteString.CopyFrom(File.ReadAllBytes(localFilePath)),
                },
            };
            RecognizeResponse response = speechClient.Recognize(request);

            foreach (var result in response.Results)
            {
                // First alternative is the most probable result
                SpeechRecognitionAlternative alternative = result.Alternatives[0];
                Console.WriteLine($"Transcript: {alternative.Transcript}");
            }
        }
        public void Recognize()
        {
            moq::Mock <Speech.SpeechClient> mockGrpcClient = new moq::Mock <Speech.SpeechClient>(moq::MockBehavior.Strict);

            mockGrpcClient.Setup(x => x.CreateOperationsClient()).Returns(new moq::Mock <lro::Operations.OperationsClient>().Object);
            RecognizeRequest request = new RecognizeRequest
            {
                Config = new RecognitionConfig(),
                Audio  = new RecognitionAudio(),
            };
            RecognizeResponse expectedResponse = new RecognizeResponse
            {
                Results =
                {
                    new SpeechRecognitionResult(),
                },
                TotalBilledTime = new wkt::Duration(),
            };

            mockGrpcClient.Setup(x => x.Recognize(request, moq::It.IsAny <grpccore::CallOptions>())).Returns(expectedResponse);
            SpeechClient      client   = new SpeechClientImpl(mockGrpcClient.Object, null);
            RecognizeResponse response = client.Recognize(request.Config, request.Audio);

            xunit::Assert.Same(expectedResponse, response);
            mockGrpcClient.VerifyAll();
        }
        /// <summary>
        /// Performs synchronous speech recognition on an audio file.
        /// </summary>
        /// <param name="sampleRateHertz">Sample rate in Hertz of the audio data sent in all `RecognitionAudio`
        /// messages. Valid values are: 8000-48000.</param>
        /// <param name="languageCode">The language of the supplied audio.</param>
        /// <param name="uriPath">Path to the audio file stored on GCS.</param>
        public static void SampleRecognize(int sampleRateHertz, string languageCode, string uriPath)
        {
            SpeechClient speechClient = SpeechClient.Create();
            // int sampleRateHertz = 44100
            // string languageCode = "en-US"
            // string uriPath = "gs://cloud-samples-data/speech/brooklyn_bridge.mp3"
            RecognizeRequest request = new RecognizeRequest
            {
                Config = new RecognitionConfig
                {
                    Encoding = RecognitionConfig.Types.AudioEncoding.Mp3,
                    // Sample rate in Hertz of the audio data sent in all `RecognitionAudio` messages. Valid values are:
                    // 8000-48000.
                    SampleRateHertz = 44100,
                    // The language of the supplied audio.
                    LanguageCode = "en-US",
                },
                Audio = new RecognitionAudio
                {
                    // Path to the audio file stored on GCS.
                    Uri = "gs://cloud-samples-data/speech/brooklyn_bridge.mp3",
                },
            };
            RecognizeResponse response = speechClient.Recognize(request);

            foreach (var result in response.Results)
            {
                string transcript = result.Alternatives[0].Transcript;
                Console.WriteLine($"Transcript: {transcript}");
            }
        }
Example #12
0
 /// <summary>
 /// Performs synchronous speech recognition: receive results after all audio
 /// has been sent and processed.
 /// </summary>
 /// <param name="request">
 /// The request object containing all of the parameters for the API call.
 /// </param>
 /// <param name="callSettings">
 /// If not null, applies overrides to this RPC call.
 /// </param>
 /// <returns>
 /// A Task containing the RPC response.
 /// </returns>
 public override stt::Task <RecognizeResponse> RecognizeAsync(
     RecognizeRequest request,
     gaxgrpc::CallSettings callSettings = null)
 {
     Modify_RecognizeRequest(ref request, ref callSettings);
     return(_callRecognize.Async(request, callSettings));
 }
        public async Task RecognizeAsync2()
        {
            Mock <Speech.SpeechClient> mockGrpcClient = new Mock <Speech.SpeechClient>(MockBehavior.Strict);

            mockGrpcClient.Setup(x => x.CreateOperationsClient())
            .Returns(new Mock <Operations.OperationsClient>().Object);
            RecognizeRequest request = new RecognizeRequest
            {
                Config = new RecognitionConfig
                {
                    Encoding        = RecognitionConfig.Types.AudioEncoding.Flac,
                    SampleRateHertz = 44100,
                    LanguageCode    = "en-US",
                },
                Audio = new RecognitionAudio
                {
                    Uri = "gs://bucket_name/file_name.flac",
                },
            };
            RecognizeResponse expectedResponse = new RecognizeResponse();

            mockGrpcClient.Setup(x => x.RecognizeAsync(request, It.IsAny <CallOptions>()))
            .Returns(new Grpc.Core.AsyncUnaryCall <RecognizeResponse>(Task.FromResult(expectedResponse), null, null, null, null));
            SpeechClient      client   = new SpeechClientImpl(mockGrpcClient.Object, null);
            RecognizeResponse response = await client.RecognizeAsync(request);

            Assert.Same(expectedResponse, response);
            mockGrpcClient.VerifyAll();
        }
Example #14
0
        public IActionResult Post([FromBody] RecognizeRequest request)
        {
            using (var connection = _rabbitConnection.CreateConnection())
                using (var channel = connection.CreateModel())
                {
                    CreateQueue(channel);
                    var body = Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(request));
                    channel.BasicPublish(exchange: "e.recognize",
                                         routingKey: "",
                                         basicProperties: null,
                                         body: body);
                }

            return(Ok());
        }
 /// <summary>Snippet for Recognize</summary>
 public void Recognize_RequestObject()
 {
     // Snippet: Recognize(RecognizeRequest, CallSettings)
     // Create client
     SpeechClient speechClient = SpeechClient.Create();
     // Initialize request argument(s)
     RecognizeRequest request = new RecognizeRequest
     {
         Config = new RecognitionConfig(),
         Audio  = new RecognitionAudio(),
     };
     // Make the request
     RecognizeResponse response = speechClient.Recognize(request);
     // End snippet
 }
        /// <summary>
        /// Performs synchronous speech recognition with speech adaptation.
        /// </summary>
        /// <param name="sampleRateHertz">Sample rate in Hertz of the audio data sent in all `RecognitionAudio`
        /// messages. Valid values are: 8000-48000.</param>
        /// <param name="languageCode">The language of the supplied audio.</param>
        /// <param name="phrase">Phrase "hints" help Speech-to-Text API recognize the specified phrases from
        /// your audio data.</param>
        /// <param name="boost">Positive value will increase the probability that a specific phrase will be
        /// recognized over other similar sounding phrases.</param>
        /// <param name="uriPath">Path to the audio file stored on GCS.</param>
        public static void SampleRecognize(int sampleRateHertz, string languageCode, string phrase, float boost, string uriPath)
        {
            SpeechClient speechClient = SpeechClient.Create();
            // int sampleRateHertz = 44100
            // string languageCode = "en-US"
            // string phrase = "Brooklyn Bridge"
            // float boost = 20f
            // string uriPath = "gs://cloud-samples-data/speech/brooklyn_bridge.mp3"
            RecognizeRequest request = new RecognizeRequest
            {
                Config = new RecognitionConfig
                {
                    Encoding = RecognitionConfig.Types.AudioEncoding.Mp3,
                    // Sample rate in Hertz of the audio data sent in all `RecognitionAudio` messages. Valid values are:
                    // 8000-48000.
                    SampleRateHertz = 44100,
                    // The language of the supplied audio.
                    LanguageCode   = "en-US",
                    SpeechContexts =
                    {
                        new SpeechContext
                        {
                            Phrases =
                            {
                                "Brooklyn Bridge",
                            },
                            // Positive value will increase the probability that a specific phrase will be recognized over other
                            // similar sounding phrases.
                            Boost = 20f,
                        },
                    },
                },
                Audio = new RecognitionAudio
                {
                    // Path to the audio file stored on GCS.
                    Uri = "gs://cloud-samples-data/speech/brooklyn_bridge.mp3",
                },
            };
            RecognizeResponse response = speechClient.Recognize(request);

            foreach (var result in response.Results)
            {
                // First alternative is the most probable result
                SpeechRecognitionAlternative alternative = result.Alternatives[0];
                Console.WriteLine($"Transcript: {alternative.Transcript}");
            }
        }
        /// <summary>
        /// Provides "hints" to the speech recognizer to favor specific classes of words in the results.
        ///</summary>
        /// <param name="uriPath">Path to the audio file stored on GCS.</param>
        public static object TranscribeContextClasses(
            string uriPath = "gs://cloud-samples-data/speech/brooklyn_bridge.mp3")
        {
            var           speechClient  = SpeechClient.Create();
            SpeechContext speechContext = new SpeechContext();

            // SpeechContext: to configure your speech_context see:
            // https://cloud.google.com/speech-to-text/docs/reference/rpc/google.cloud.speech.v1#speechcontext
            // Full list of supported phrases (class tokens) here:
            // https://cloud.google.com/speech-to-text/docs/class-tokens
            speechContext.Phrases.Add("$TIME");

            // RecognitionConfig: to configure your encoding and sample_rate_hertz, see:
            // https://cloud.google.com/speech-to-text/docs/reference/rpc/google.cloud.speech.v1#recognitionconfig
            RecognitionConfig recognitionConfig = new RecognitionConfig
            {
                Encoding        = RecognitionConfig.Types.AudioEncoding.Linear16,
                SampleRateHertz = 8000,
                LanguageCode    = "en-US"
            };

            recognitionConfig.SpeechContexts.Add(speechContext);

            // Set the path to your audio file
            RecognitionAudio audio = new RecognitionAudio
            {
                Uri = uriPath
            };

            // Build the request
            RecognizeRequest request = new RecognizeRequest
            {
                Config = recognitionConfig,
                Audio  = audio
            };

            // Perform the request
            var response = speechClient.Recognize(request);

            foreach (SpeechRecognitionResult result in response.Results)
            {
                // First alternative is the most probable result
                var alternative = result.Alternatives[0];
                Console.WriteLine($"Transcript: {alternative.Transcript}");
            }
            return(0);
        }
        /// <summary>Snippet for RecognizeAsync</summary>
        public async Task RecognizeAsync_RequestObject()
        {
            // Snippet: RecognizeAsync(RecognizeRequest, CallSettings)
            // Additional: RecognizeAsync(RecognizeRequest, CancellationToken)
            // Create client
            SpeechClient speechClient = await SpeechClient.CreateAsync();

            // Initialize request argument(s)
            RecognizeRequest request = new RecognizeRequest
            {
                Config = new RecognitionConfig(),
                Audio  = new RecognitionAudio(),
            };
            // Make the request
            RecognizeResponse response = await speechClient.RecognizeAsync(request);

            // End snippet
        }
Example #19
0
        /// <summary>
        /// Performs synchronous speech recognition with static context classes.
        /// </summary>
        /// <param name="sampleRateHertz">Sample rate in Hertz of the audio data sent in all `RecognitionAudio`
        /// messages. Valid values are: 8000-48000.</param>
        /// <param name="languageCode">The language of the supplied audio.</param>
        /// <param name="phrase">Phrase "hints" help Speech-to-Text API recognize the specified phrases from
        /// your audio data. In this sample we are using a static class phrase ($TIME). Classes represent
        /// groups of words that represent common concepts that occur in natural language. We recommend
        /// checking out the docs page for more info on static classes.</param>
        /// <param name="uriPath">Path to the audio file stored on GCS.</param>
        public static void SampleRecognize(int sampleRateHertz, string languageCode, string phrase, string uriPath)
        {
            SpeechClient speechClient = SpeechClient.Create();
            // int sampleRateHertz = 24000
            // string languageCode = "en-US"
            // string phrase = "$TIME"
            // string uriPath = "gs://cloud-samples-data/speech/time.mp3"
            RecognizeRequest request = new RecognizeRequest
            {
                Config = new RecognitionConfig
                {
                    Encoding = RecognitionConfig.Types.AudioEncoding.Mp3,
                    // Sample rate in Hertz of the audio data sent in all `RecognitionAudio` messages. Valid values are:
                    // 8000-48000.
                    SampleRateHertz = 24000,
                    // The language of the supplied audio.
                    LanguageCode   = "en-US",
                    SpeechContexts =
                    {
                        new SpeechContext
                        {
                            Phrases =
                            {
                                "$TIME",
                            },
                        },
                    },
                },
                Audio = new RecognitionAudio
                {
                    // Path to the audio file stored on GCS.
                    Uri = "gs://cloud-samples-data/speech/time.mp3",
                },
            };
            RecognizeResponse response = speechClient.Recognize(request);

            foreach (var result in response.Results)
            {
                // First alternative is the most probable result
                SpeechRecognitionAlternative alternative = result.Alternatives[0];
                Console.WriteLine($"Transcript: {alternative.Transcript}");
            }
        }
Example #20
0
        /// <summary>
        /// This function POSTs the given audio clip the recognize function and convert speech into text. This function should be used
        /// only on AudioClips under 4MB once they have been converted into WAV format. Use the StartListening() for continuous
        /// recognition of text.
        /// </summary>
        /// <param name="clip">The AudioClip object.</param>
        /// <param name="callback">A callback to invoke with the results.</param>
        /// <returns></returns>
        public bool Recognize(AudioClip clip, OnRecognize callback)
        {
            if (clip == null)
            {
                throw new ArgumentNullException("clip");
            }
            if (callback == null)
            {
                throw new ArgumentNullException("callback");
            }

            RESTConnector connector = RESTConnector.GetConnector(SERVICE_ID, "/v1/recognize");

            if (connector == null)
            {
                return(false);
            }

            RecognizeRequest req = new RecognizeRequest();

            req.Clip     = clip;
            req.Callback = callback;

            req.Headers["Content-Type"] = "audio/wav";
            req.Send = WaveFile.CreateWAV(clip);
            if (req.Send.Length > MAX_RECOGNIZE_CLIP_SIZE)
            {
                Log.Error("SpeechToText", "AudioClip is too large for Recognize().");
                return(false);
            }
            req.Parameters["model"]            = m_RecognizeModel;
            req.Parameters["continuous"]       = "false";
            req.Parameters["max_alternatives"] = m_MaxAlternatives.ToString();
            req.Parameters["timestamps"]       = m_Timestamps ? "true" : "false";
            req.Parameters["word_confidence"]  = m_WordConfidence ? "true" : "false";
            req.OnResponse = OnRecognizeResponse;

            return(connector.Send(req));
        }
        public void Recognize()
        {
            Mock <Speech.SpeechClient> mockGrpcClient = new Mock <Speech.SpeechClient>(MockBehavior.Strict);

            mockGrpcClient.Setup(x => x.CreateOperationsClient())
            .Returns(new Mock <Operations.OperationsClient>().Object);
            RecognizeRequest expectedRequest = new RecognizeRequest
            {
                Config = new RecognitionConfig
                {
                    Encoding        = RecognitionConfig.Types.AudioEncoding.Flac,
                    SampleRateHertz = 44100,
                    LanguageCode    = "en-US",
                },
                Audio = new RecognitionAudio
                {
                    Uri = "gs://bucket_name/file_name.flac",
                },
            };
            RecognizeResponse expectedResponse = new RecognizeResponse();

            mockGrpcClient.Setup(x => x.Recognize(expectedRequest, It.IsAny <CallOptions>()))
            .Returns(expectedResponse);
            SpeechClient      client = new SpeechClientImpl(mockGrpcClient.Object, null);
            RecognitionConfig config = new RecognitionConfig
            {
                Encoding        = RecognitionConfig.Types.AudioEncoding.Flac,
                SampleRateHertz = 44100,
                LanguageCode    = "en-US",
            };
            RecognitionAudio audio = new RecognitionAudio
            {
                Uri = "gs://bucket_name/file_name.flac",
            };
            RecognizeResponse response = client.Recognize(config, audio);

            Assert.Same(expectedResponse, response);
            mockGrpcClient.VerifyAll();
        }
Example #22
0
        /// <summary>
        /// Performs synchronous speech recognition: receive results after all audiohas been sent and processed.
        /// Documentation https://developers.google.com/speech/v1/reference/speech/recognize
        /// Generation Note: This does not always build corectly.  Google needs to standardise things I need to figuer out which ones are wrong.
        /// </summary>
        /// <param name="service">Authenticated Speech service.</param>
        /// <param name="body">A valid Speech v1 body.</param>
        /// <returns>RecognizeResponseResponse</returns>
        public static RecognizeResponse Recognize(SpeechService service, RecognizeRequest body)
        {
            try
            {
                // Initial validation.
                if (service == null)
                {
                    throw new ArgumentNullException("service");
                }
                if (body == null)
                {
                    throw new ArgumentNullException("body");
                }

                // Make the request.
                return(service.Speech.Recognize(body).Execute());
            }
            catch (Exception ex)
            {
                throw new Exception("Request Speech.Recognize failed.", ex);
            }
        }
Example #23
0
        private void Recognize()
        {
            SpeechClientBuilder builder = new SpeechClientBuilder();

            builder.CredentialsPath = GOOGLE_API_CREDS_PATH;

            SpeechClient     client  = builder.Build();
            RecognizeRequest request = new RecognizeRequest()
            {
                Audio  = RecognitionAudio.FromFile(TEMP_AUDIO_PATH),
                Config = new RecognitionConfig()
                {
                    Encoding              = RecognitionConfig.Types.AudioEncoding.EncodingUnspecified,
                    LanguageCode          = "ru-RU",
                    EnableWordTimeOffsets = false
                }
            };
            RecognizeResponse response = client.Recognize(request);

            Result.Text = string.Join("\n", response.Results.Select(
                                          result => result.Alternatives[0].Transcript
                                          ));
        }
Example #24
0
        private static async Task RecognizeRequest(
            SpeechRecognition.SpeechRecognitionClient client, string model, string client_id, string domain_id, string api_key)
        {
            using var call = client.RecognizeSpeech();

            var responseTask = Task.Run(async() => {
                await foreach (var result in call.ResponseStream.ReadAllAsync())
                {
                    var words = from w in result.Text.Words select w.Text;
                    Console.ForegroundColor = result.IsFinal ? ConsoleColor.DarkGreen : ConsoleColor.Gray;
                    Console.WriteLine(string.Join(" ", words));
                    Console.ResetColor();
                }
            });

            var recorder = new WaveInEvent();

            recorder.WaveFormat         = new WaveFormat(16000, 1);
            recorder.BufferMilliseconds = 200;

            try
            {
                var config = new RecognizeRequest
                {
                    Config = new RecognitionConfig
                    {
                        Model = new Model {
                            Id = model
                        },
                        Auth = new Auth {
                            ClientId = client_id, DomainId = domain_id, ApiKey = api_key
                        }
                    }
                };
                await call.RequestStream.WriteAsync(config);

                recorder.DataAvailable += async(object sender, WaveInEventArgs e) =>
                {
                    await call.RequestStream.WriteAsync(new RecognizeRequest
                    {
                        Sound = new Sound {
                            Samples = Google.Protobuf.ByteString.CopyFrom(e.Buffer)
                        }
                    });
                };
                recorder.StartRecording();
                Console.WriteLine("Started  recording. Press ESC to stop...");

                while (!(Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Escape))
                {
                    Thread.Sleep(100);
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                Console.WriteLine("Stopping recording...");
                recorder.StopRecording();
                await call.RequestStream.WriteAsync(new RecognizeRequest { Finish = new Finish() });
            }

            await call.RequestStream.CompleteAsync();

            await responseTask;
        }
Example #25
0
 partial void Modify_RecognizeRequest(ref RecognizeRequest request, ref gaxgrpc::CallSettings settings);
Example #26
0
 /// <summary>
 /// Performs synchronous speech recognition: receive results after all audio
 /// has been sent and processed.
 /// </summary>
 /// <param name="request">The request object containing all of the parameters for the API call.</param>
 /// <param name="cancellationToken">A <see cref="st::CancellationToken"/> to use for this RPC.</param>
 /// <returns>A Task containing the RPC response.</returns>
 public virtual stt::Task <RecognizeResponse> RecognizeAsync(RecognizeRequest request, st::CancellationToken cancellationToken) =>
 RecognizeAsync(request, gaxgrpc::CallSettings.FromCancellationToken(cancellationToken));
Example #27
0
 /// <summary>
 /// Performs synchronous speech recognition: receive results after all audio
 /// has been sent and processed.
 /// </summary>
 /// <param name="request">The request object containing all of the parameters for the API call.</param>
 /// <param name="callSettings">If not null, applies overrides to this RPC call.</param>
 /// <returns>A Task containing the RPC response.</returns>
 public virtual stt::Task <RecognizeResponse> RecognizeAsync(RecognizeRequest request, gaxgrpc::CallSettings callSettings = null) =>
 throw new sys::NotImplementedException();
Example #28
0
 /// <summary>
 /// Performs synchronous speech recognition: receive results after all audio
 /// has been sent and processed.
 /// </summary>
 /// <param name="request">The request object containing all of the parameters for the API call.</param>
 /// <param name="callSettings">If not null, applies overrides to this RPC call.</param>
 /// <returns>The RPC response.</returns>
 public virtual RecognizeResponse Recognize(RecognizeRequest request, gaxgrpc::CallSettings callSettings = null) =>
 throw new sys::NotImplementedException();
 /// <summary>
 /// Performs synchronous speech recognition: receive results after all audio
 /// has been sent and processed.
 /// </summary>
 /// <param name="request">
 /// The request object containing all of the parameters for the API call.
 /// </param>
 /// <param name="callSettings">
 /// If not null, applies overrides to this RPC call.
 /// </param>
 /// <returns>
 /// The RPC response.
 /// </returns>
 public virtual RecognizeResponse Recognize(
     RecognizeRequest request,
     CallSettings callSettings = null)
 {
     throw new NotImplementedException();
 }