コード例 #1
0
        public SpeechkinController(WindowFabric windowFabric, SpeechRecognitionClient recognitionClient, TranslationApiClient translationApiClient)
        {
            _windowFabric         = windowFabric;
            _recognitionClient    = recognitionClient;
            _translationApiClient = translationApiClient;
            Model           = new MainWindowDataModel();
            Model.IsStarted = false;
            Model.FromLanguages.Add(new LanguageItem {
                Id = (int)TranslationLanguage.Auto, Text = "Auto"
            });
            Model.FromLanguages.Add(new LanguageItem {
                Id = (int)TranslationLanguage.Russian, Text = "Russian"
            });
            Model.FromLanguages.Add(new LanguageItem {
                Id = (int)TranslationLanguage.English, Text = "English"
            });
            Model.FromLanguages.Add(new LanguageItem {
                Id = (int)TranslationLanguage.German, Text = "German"
            });


            Model.ToLanguages.Add(new LanguageItem {
                Id = (int)TranslationLanguage.Russian, Text = "Russian"
            });
            Model.ToLanguages.Add(new LanguageItem {
                Id = (int)TranslationLanguage.English, Text = "English"
            });
            Model.ToLanguages.Add(new LanguageItem {
                Id = (int)TranslationLanguage.German, Text = "German"
            });

            Model.FromLanguageId = (int)TranslationLanguage.Auto;
            Model.ToLanguageId   = (int)TranslationLanguage.English;
        }
コード例 #2
0
        static void Main(string[] args)
        {
            var client = new SpeechRecognitionClient();

            client.Recognize += (s, e) => { Console.WriteLine(e.Recognized); };
            client.Connect(address, port);
            WaitConsoleReadLine();
        }
コード例 #3
0
        static void Main(string[] args)
        {
            Console.OutputEncoding = Encoding.UTF8;
            var recognition = new SpeechRecognitionClient();

            recognition.Recognize += Recognized;
            recognition.Connect(address, port);
            WaitConsoleReadLine();
        }
コード例 #4
0
    /// <summary>
    /// InitializeSpeechRecognitionService is used to authenticate the client app
    /// with the Speech API Cognitive Services. A subscription key is passed to
    /// obtain a token, which is then used in the header of every APi call.
    /// </summary>
    private void InitializeSpeechRecognitionService()
    {
        // If you see an API key below, it's a trial key and will either expire soon or get invalidated. Please get your own key.
        // Get your own trial key to Bing Speech or the new Speech Service at https://azure.microsoft.com/try/cognitive-services
        // Create an Azure Cognitive Services Account: https://docs.microsoft.com/azure/cognitive-services/cognitive-services-apis-create-account

        // DELETE THE NEXT THREE LINE ONCE YOU HAVE OBTAINED YOUR OWN SPEECH API KEY
        //Debug.Log("You forgot to initialize the sample with your own Speech API key. Visit https://azure.microsoft.com/try/cognitive-services to get started.");
        //Console.ReadLine();
        //return;
        // END DELETE
#if USENEWSPEECHSDK
        bool useClassicBingSpeechService = false;
        //string authenticationKey = @"INSERT-YOUR-NEW-SPEECH-API-KEY-HERE";
        string authenticationKey = @"895664ef53e44b6fac574c3ecd6f3b75";
#else
        bool useClassicBingSpeechService = true;
        //string authenticationKey = @"INSERT-YOUR-BING-SPEECH-API-KEY-HERE";
        string authenticationKey = @"4d5a1beefe364f8986d63a877ebd51d5";
#endif

        try
        {
            Debug.Log($"Instantiating Cognitive Services Speech Recognition Service client.");
            recoServiceClient = new SpeechRecognitionClient(useClassicBingSpeechService);

            // Make sure to match the region to the Azure region where you created the service.
            // Note the region is NOT used for the old Bing Speech service
            region = "westus";

            auth = new CogSvcSocketAuthentication();
            Task <string> authenticating = auth.Authenticate(authenticationKey, region, useClassicBingSpeechService);

            // Since the authentication process needs to run asynchronously, we run the code in a coroutine to
            // avoid blocking the main Unity thread.
            // Make sure you have successfully obtained a token before making any Speech Service calls.
            StartCoroutine(AuthenticateSpeechService(authenticating));

            // Register an event to capture recognition events
            Debug.Log($"Registering Speech Recognition event handler.");
            recoServiceClient.OnMessageReceived += RecoServiceClient_OnMessageReceived;
        }
        catch (Exception ex)
        {
            string msg = String.Format("Error: Initialization failed. See error details below:{0}{1}{2}{3}",
                                       Environment.NewLine, ex.ToString(), Environment.NewLine, ex.Message);
            Debug.LogError(msg);
            UpdateUICanvasLabel(msg, FontStyle.Normal);
        }
    }
コード例 #5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BingSpeechRecognizer"/> class.
        /// </summary>
        /// <param name="pipeline">The pipeline to add the component to.</param>
        /// <param name="configuration">The component configuration.</param>
        public BingSpeechRecognizer(Pipeline pipeline, BingSpeechRecognizerConfiguration configuration)
            : base(pipeline)
        {
            this.configuration = configuration ?? new BingSpeechRecognizerConfiguration();
            this.intentData    = pipeline.CreateEmitter <IntentData>(this, "IntentData");

            // create emitters for all possible Bing speech recognition events
            this.partialSpeechResponseEvent = pipeline.CreateEmitter <PartialSpeechResponseEventArgs>(this, nameof(PartialSpeechResponseEventArgs));
            this.speechErrorEvent           = pipeline.CreateEmitter <SpeechErrorEventArgs>(this, nameof(SpeechErrorEventArgs));
            this.speechResponseEvent        = pipeline.CreateEmitter <SpeechResponseEventArgs>(this, nameof(SpeechResponseEventArgs));

            // Create the Cognitive Services DataRecognitionClient
            this.speechRecognitionClient = this.CreateSpeechRecognitionClient();
        }
コード例 #6
0
        /// <summary>
        /// Dispose method.
        /// </summary>
        public void Dispose()
        {
            this.cancellationTokenSource.Cancel();
            this.cancellationTokenSource.Dispose();

            if (this.speechRecognitionClient != null)
            {
                // Event handlers for speech recognition results
                this.speechRecognitionClient.OnResponseReceived        -= this.OnResponseReceivedHandler;
                this.speechRecognitionClient.OnPartialResponseReceived -= this.OnPartialResponseReceivedHandler;
                this.speechRecognitionClient.OnConversationError       -= this.OnConversationErrorHandler;

                this.speechRecognitionClient.Dispose();
                this.speechRecognitionClient = null;
            }
        }
コード例 #7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BingSpeechRecognizer"/> class.
        /// </summary>
        /// <param name="pipeline">The pipeline to add the component to.</param>
        /// <param name="configuration">The component configuration.</param>
        public BingSpeechRecognizer(Pipeline pipeline, BingSpeechRecognizerConfiguration configuration)
            : base(pipeline)
        {
            this.Configuration             = configuration ?? new BingSpeechRecognizerConfiguration();
            this.PartialRecognitionResults = pipeline.CreateEmitter <IStreamingSpeechRecognitionResult>(this, nameof(this.PartialRecognitionResults));

            // create emitters for all possible Bing speech recognition events
            this.PartialSpeechResponseEvent = pipeline.CreateEmitter <PartialSpeechResponseEventArgs>(this, nameof(PartialSpeechResponseEventArgs));
            this.SpeechErrorEvent           = pipeline.CreateEmitter <SpeechErrorEventArgs>(this, nameof(SpeechErrorEventArgs));
            this.SpeechResponseEvent        = pipeline.CreateEmitter <SpeechResponseEventArgs>(this, nameof(SpeechResponseEventArgs));

            // create table of last stream originating times
            this.lastPostedOriginatingTimes = new Dictionary <int, DateTime>();

            // Create the Cognitive Services DataRecognitionClient
            this.speechRecognitionClient = this.CreateSpeechRecognitionClient();
        }
コード例 #8
0
        static void Main(string[] args)
        {
            try
            {
                Task.Run(async() =>
                {
                    // If you see an API key below, it's a trial key and will either expire soon or get invalidated. Please get your own key.
                    // Get your own trial key to Bing Speech or the new Speech Service at https://azure.microsoft.com/try/cognitive-services
                    // Create an Azure Cognitive Services Account: https://docs.microsoft.com/azure/cognitive-services/cognitive-services-apis-create-account

                    // DELETE THE NEXT THREE LINE ONCE YOU HAVE OBTAINED YOUR OWN SPEECH API KEY
                    //Console.WriteLine("You forgot to initialize the sample with your own Speech API key. Visit https://azure.microsoft.com/try/cognitive-services to get started.");
                    //Console.ReadLine();
                    //return;
                    // END DELETE
#if USENEWSPEECHSDK
                    bool useClassicBingSpeechService = false;
                    //string authenticationKey = @"INSERT-YOUR-NEW-SPEECH-API-KEY-HERE";
                    string authenticationKey = @"895664ef53e44b6fac574c3ecd6f3b75";
#else
                    bool useClassicBingSpeechService = true;
                    //string authenticationKey = @"INSERT-YOUR-BING-SPEECH-API-KEY-HERE";
                    string authenticationKey = @"4d5a1beefe364f8986d63a877ebd51d5";
#endif

                    var recoServiceClient = new SpeechRecognitionClient(useClassicBingSpeechService);
                    // Replace this with your own file. Add it to the project and mark it as "Content" and "Copy if newer".
                    string audioFilePath = @"Thisisatest.wav";

                    // Make sure to match the region to the Azure region where you created the service.
                    // Note the region is NOT used for the old Bing Speech service
                    string region = "westus";

                    // Register an event to capture recognition events
                    recoServiceClient.OnMessageReceived += RecoServiceClient_OnMessageReceived;

                    await recoServiceClient.CreateSpeechRecognitionJob(audioFilePath, authenticationKey, region);
                }).Wait();
            }
            catch (Exception ex)
            {
                Console.WriteLine("An exception occurred in the main program:" + Environment.NewLine + ex.Message);
            }
        }
コード例 #9
0
        /// <summary>
        /// Creates a new recognition client.
        /// </summary>
        /// <returns>A new speech recognition client object.</returns>
        private SpeechRecognitionClient CreateSpeechRecognitionClient()
        {
            // Create cloud-based speech recognition client
            var client = new SpeechRecognitionClient(
                this.Configuration.RecognitionMode,
                this.Configuration.Language,
                this.Configuration.SubscriptionKey);

            // Event handlers for speech recognition results
            client.OnResponseReceived        += this.OnResponseReceivedHandler;
            client.OnPartialResponseReceived += this.OnPartialResponseReceivedHandler;
            client.OnConversationError       += this.OnConversationErrorHandler;

            // Set the audio format (16-bit, 1-channel PCM samples). Currently
            // only 16kHz and 8kHz sampling rates are supported.
            client.SetAudioFormat(this.Configuration.InputFormat);

            return(client);
        }
コード例 #10
0
        /// <summary>
        /// InitializeSpeechRecognitionService is used to authenticate the client app
        /// with the Speech API Cognitive Services. A subscription key is passed to
        /// obtain a token, which is then used in the header of every APi call.
        /// </summary>
        private void InitializeSpeechRecognitionService()
        {
            bool   useClassicBingSpeechService = useNewCognitiveAPI == false;
            string authenticationKey           = SpeechServiceAPIKey;

            Debug.Log("Auth key: " + authenticationKey);

            try
            {
                Debug.Log($"Instantiating Cognitive Services Speech Recognition Service client.");
                recoServiceClient = new SpeechRecognitionClient(useClassicBingSpeechService);

                // Make sure to match the region to the Azure region where you created the service.
                // Note the region is NOT used for the old Bing Speech service
                region = "northeurope";

                auth = new CogSvcSocketAuthentication();
                Task <string> authenticating = auth.Authenticate(authenticationKey, region, useClassicBingSpeechService);

                // Since the authentication process needs to run asynchronously, we run the code in a coroutine to
                // avoid blocking the main Unity thread.
                // Make sure you have successfully obtained a token before making any Speech Service calls.
                StartCoroutine(AuthenticateSpeechService(authenticating));

                // Register an event to capture recognition events
                Debug.Log($"Registering Speech Recognition event handler.");
                recoServiceClient.OnMessageReceived += (r) => {
                    Debug.Log("Message recevied" + r);
                    Debug.Log(MessageReceived == null);
                    MessageReceived?.Invoke(r);
                };
            }
            catch (Exception ex)
            {
                string msg = String.Format("Error: Initialization failed. See error details below:{0}{1}{2}{3}",
                                           Environment.NewLine, ex.ToString(), Environment.NewLine, ex.Message);
                Debug.LogError(msg);

                OnError?.Invoke(msg);
            }
        }
コード例 #11
0
        private async void btnStart_Click(object sender, RoutedEventArgs e)
        {
            // If you see an API key below, it's a trial key and will either expire soon or get invalidated. Please get your own key.
            // Get your own trial key to Bing Speech or the new Speech Service at https://azure.microsoft.com/try/cognitive-services
            // Create an Azure Cognitive Services Account: https://docs.microsoft.com/azure/cognitive-services/cognitive-services-apis-create-account

            // DELETE THE NEXT THREE LINE ONCE YOU HAVE OBTAINED YOUR OWN SPEECH API KEY
            //Console.WriteLine("You forgot to initialize the sample with your own Speech API key. Visit https://azure.microsoft.com/try/cognitive-services to get started.");
            //Console.ReadLine();
            //return;
            // END DELETE
            //#if USENEWSPEECHSDK
            //                    bool useClassicBingSpeechService = false;
            //                    //string authenticationKey = @"INSERT-YOUR-NEW-SPEECH-API-KEY-HERE";
            //                    string authenticationKey = @"f69d77d425e946e69a954c53db135f77";
            //#else
            //            bool useClassicBingSpeechService = true;
            //            //string authenticationKey = @"INSERT-YOUR-BING-SPEECH-API-KEY-HERE";
            //            string authenticationKey = @"4d5a1beefe364f8986d63a877ebd51d5";
            //#endif
            bool   useClassicBingSpeechService = false;
            string authenticationKey           = txtSubscriptionKey.Text;

            var recoServiceClient = new SpeechRecognitionClient(useClassicBingSpeechService);
            // Replace this with your own file. Add it to the project and mark it as "Content" and "Copy if newer".
            string audioFilePath = txtFilename.Text;

            // Make sure to match the region to the Azure region where you created the service.
            // Note the region is NOT used for the old Bing Speech service
            string region = txtRegion.Text;

            // Register an event to capture recognition events
            recoServiceClient.OnMessageReceived += RecoServiceClient_OnMessageReceived;

            recoServiceClient.CreateSpeechRecognitionJob(audioFilePath, authenticationKey, region);

            lblResult.Text = "Speech recognition job started... uploading audio file. Please wait for first result...";
        }