public async void GetTranscript(string uri, Action <string> callback)
        {
            if (client == null)
            {
                return;
            }
            var context = new SpeechContext()
            {
                Phrases = { File.ReadLines(CloudUtility.SwearList) }
            };
            var speechOperation = await client.LongRunningRecognizeAsync(new RecognitionConfig()
            {
                Encoding = RecognitionConfig.Types.AudioEncoding.Flac,

                LanguageCode          = "en-US",
                EnableWordTimeOffsets = true,
                SpeechContexts        = { context }
            }, RecognitionAudio.FromFile(uri));

            speechOperation = await speechOperation.PollUntilCompletedAsync();

            var    response = speechOperation.Result;
            string builder  = "";

            foreach (var result in response.Results)
            {
                foreach (var alternative in result.Alternatives)
                {
                    builder += alternative.Transcript;
                }
                builder += Environment.NewLine;
            }
            callback(builder);
        }
        static object RecognizeWithContext(string filePath, IEnumerable <string> phrases)
        {
            var speech  = SpeechClient.Create();
            var context = new SpeechContext();

            context.Phrases.AddRange(phrases);
            var config = new RecognitionConfig()
            {
                Encoding        = RecognitionConfig.Types.AudioEncoding.Linear16,
                SampleRateHertz = 16000,
                LanguageCode    = "en"
            };

            config.SpeechContexts.Add(context);
            var audio = IsStorageUri(filePath) ?
                        RecognitionAudio.FromStorageUri(filePath) :
                        RecognitionAudio.FromFile(filePath);
            var response = speech.Recognize(config, audio);

            foreach (var result in response.Results)
            {
                foreach (var alternative in result.Alternatives)
                {
                    Console.WriteLine(alternative.Transcript);
                }
            }
            return(0);
        }
        /// <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);
        }
Example #4
0
        public GoogleSpeech()
        {
            var credential = GoogleCredential.FromFile("C:/GoogleCreds.json").CreateScoped(SpeechClient.DefaultScopes);
            var channel    = new Grpc.Core.Channel(SpeechClient.DefaultEndpoint.ToString(), credential.ToChannelCredentials());

            client = SpeechClient.Create(channel);

            context = new SpeechContext();

            config = new RecognitionConfig
            {
                Encoding        = AudioEncoding.Linear16,
                SampleRateHertz = 16000,
                LanguageCode    = LanguageCodes.English.UnitedStates,
                Model           = "command_and_search"
            };
            config.Metadata = new RecognitionMetadata();
            config.Metadata.InteractionType = RecognitionMetadata.Types.InteractionType.VoiceCommand;
            config.SpeechContexts.Add(context);
        }
        private static StreamingRecognizeRequest GetStreamingRecognizeRequest(SpeechContext speechContext)
        {
            var streamingRecognizeRequest = new StreamingRecognizeRequest
            {
                StreamingConfig = new StreamingRecognitionConfig
                {
                    Config = new RecognitionConfig
                    {
                        Encoding =
                            RecognitionConfig.Types.AudioEncoding.Linear16,
                        SampleRateHertz = 16000,
                        LanguageCode    = "ru",
                        MaxAlternatives = 0,
                        SpeechContexts  = { speechContext }
                    },
                    InterimResults = true,
                }
            };

            return(streamingRecognizeRequest);
        }
Example #6
0
        public string GoogleSpeechRecognition(byte[] filedata, List <string> KeyWordList)
        {
            try
            {
                var speech = SpeechClient.Create();

                var Speechcontext = new SpeechContext();
                foreach (var Key in KeyWordList)
                {
                    Speechcontext.Phrases.Add(Key);
                }

                var response = speech.Recognize(new RecognitionConfig()
                {
                    Encoding        = RecognitionConfig.Types.AudioEncoding.Linear16,
                    SampleRateHertz = 16000,
                    LanguageCode    = "ko",
                    Model           = "command_and_search",
                    SpeechContexts  = { Speechcontext }
                }, RecognitionAudio.FromBytes(filedata));

                string resultstring = "";
                foreach (var result in response.Results)
                {
                    foreach (var alternative in result.Alternatives)
                    {
                        resultstring = resultstring + " " + alternative.Transcript;
                    }
                }
                if (resultstring.Length > 1)
                {
                    resultstring = resultstring.Substring(1);
                }
                return(resultstring);
            }
            catch
            {
                return("");
            }
        }
        public static IContainer Bootstrap()
        {
            // Injection: Controller Registration
            var builder = new ContainerBuilder();

            // Injection: Context(s)
            var speechContext = new SpeechContext();

            builder.RegisterInstance(speechContext)
            .As <ISpeechContext>()
            .SingleInstance();

            // Injection: Config
            var riffConfigurableSettings = new RiffConfigurableSettings();

            builder.RegisterInstance(riffConfigurableSettings)
            .As <IRiffConfigurableSettings>()
            .SingleInstance();

            builder.RegisterType <MicrophoneContext>()
            .As <IMicrophoneContext>()
            .SingleInstance();

            builder.RegisterType <SpeechHandlerChain>()
            .As <ISpeechHandlerChain>()
            .SingleInstance();

            // Injection: RecognitionEngineProvider
            builder.RegisterType <GoogleRecognitionEngineProvider>()
            .As <IRecognitionEngineProvider>()
            .InstancePerDependency();

            // Injection: WebRequest
            var webRequest = new WebRequest();

            builder.RegisterInstance(webRequest)
            .As <WebRequest>()
            .SingleInstance();

            // Injection: System operations
            builder.RegisterType <RiffSystemOperations>()
            .As <RiffSystemOperations>()
            .SingleInstance();

            // Injection: Applications
            RegisterApplications(builder, riffConfigurableSettings, speechContext);

            // Injection: RiffApplication components
            builder.RegisterType <Greetings>()
            .As <Greetings>()
            .WithParameter(new TypedParameter(typeof(ISpeechContext), speechContext))
            .SingleInstance();

            builder.RegisterType <Email>()
            .As <Email>()
            .SingleInstance();

            builder.RegisterType <Clock>()
            .As <Clock>()
            .WithParameter(new TypedParameter(typeof(ISpeechContext), speechContext))
            .SingleInstance();

            builder.RegisterType <Weather>()
            .As <Weather>()
            .WithParameter(new TypedParameter(typeof(ISpeechContext), speechContext))
            .WithParameter(new TypedParameter(typeof(WebRequest), webRequest))
            .SingleInstance();

            builder.RegisterType <GoogleSearch>()
            .As <GoogleSearch>()
            .WithParameter(new TypedParameter(typeof(ISpeechContext), speechContext))
            .WithParameter(new TypedParameter(typeof(WebRequest), webRequest))
            .SingleInstance();

            builder.RegisterType <BatteryStatus>()
            .As <BatteryStatus>()
            .WithParameter(new TypedParameter(typeof(ISpeechContext), speechContext))
            .InstancePerDependency();

            // Injection: Build container / integrate with MVC
            m_appContainer = builder.Build();
            return(m_appContainer);
        }
        private async Task <int> StreamingMicRecognizeAsync()
        {
            try
            {
                _writeMore = true;
                timer      = new Stopwatch();
                timer.Start();
                if (WaveIn.DeviceCount < 1)
                {
                    throw new ApplicationException("No microphone!");
                }

                _speechClient = SpeechClient.Create();
                var stream = _speechClient.StreamingRecognize();
                streams.Add(stream);
                var speechContext = new SpeechContext();
                speechContext.Phrases.AddRange(new[]
                                               { "int", "for", "true", "false", "public", "private", "bool", "static", "void", "переменная" }
                                               /*.Concat(_variableProvider.GetVariables().Select(v => v.Name))*/);
                // Write the initial request with the config.
                StreamingRecognizeRequest recognizeRequest = GetStreamingRecognizeRequest(speechContext);
                await stream.WriteAsync(recognizeRequest);

                // Print responses as they arrive.

                Task printResponses = Task.Run(async() =>
                {
                    while (await stream.ResponseStream.MoveNext(default(CancellationToken)))
                    {
                        foreach (StreamingRecognitionResult streamingRecognitionResult in stream
                                 .ResponseStream
                                 .Current.Results)
                        {
                            if (streamingRecognitionResult.IsFinal)
                            {
                                var transcript = streamingRecognitionResult.Alternatives[0].Transcript;
                                OnSpeechRecognized?.Invoke(this, new SpeechRecognizerEventArgs(transcript));
                                if (timer.Elapsed.TotalSeconds >= threshold)
                                {
                                    Restart();
                                }
                            }
                        }
                    }
                });
                // Read from the microphone and stream to API.
                ActivateMicrophone();
                Console.WriteLine("Speak now.");
                //await Task.Delay(TimeSpan.FromSeconds(seconds));
                // Stop recording and shut down.
                //StopRecognition();
                await printResponses;
                //await printResponses;
                return(0);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
            }

            return(-1);
        }