Exemplo n.º 1
0
        //static void Main(string[] args)
        //{
        //    SpeechRecognitionServiceExample recoExample = new SpeechRecognitionServiceExample();

        //    recoExample.m_recoMode = SpeechRecognitionMode.ShortPhrase;
        //    recoExample.m_isMicrophoneReco = true;
        //    bool isIntent = true;
        //    bool tryMultipleRecos = false;

        //    if (args.Length == 1)
        //    {
        //        recoExample.m_filename = args[0];
        //    }

        //    string primaryOrSecondaryKey = ConfigurationManager.AppSettings["primaryKey"];
        //    string luisAppID = ConfigurationManager.AppSettings["luisAppID"];
        //    string luisSubscriptionID = ConfigurationManager.AppSettings["luisSubscriptionID"];

        //    if (recoExample.m_isMicrophoneReco)
        //    {
        //        MicrophoneRecognitionClientWithIntent intentMicClient;

        //        if (!isIntent)
        //        {
        //            recoExample.m_micClient = SpeechRecognitionServiceFactory.CreateMicrophoneClient(recoExample.m_recoMode, "en-us",
        //                                                                                             primaryOrSecondaryKey);
        //        }
        //        else
        //        {
        //            Debug.Assert(recoExample.m_recoMode == SpeechRecognitionMode.ShortPhrase);
        //            intentMicClient =
        //                SpeechRecognitionServiceFactory.CreateMicrophoneClientWithIntent("en-us",
        //                                                                                 primaryOrSecondaryKey,
        //                                                                                 luisAppID,
        //                                                                                 luisSubscriptionID);
        //            intentMicClient.OnIntent += recoExample.OnIntentHandler;
        //            recoExample.m_micClient = intentMicClient;
        //        }
        //        // Event handlers for speech recognition results
        //        recoExample.m_micClient.OnResponseReceived += recoExample.OnResponseReceivedHandler;
        //        recoExample.m_micClient.OnPartialResponseReceived += recoExample.OnPartialResponseReceivedHandler;
        //        recoExample.m_micClient.OnConversationError += recoExample.OnConversationErrorHandler;
        //        recoExample.m_micClient.OnMicrophoneStatus += recoExample.OnMicrophoneStatus;

        //        try
        //        {
        //            recoExample.DoMicrophoneRecognition(recoExample.m_micClient);
        //        }
        //        finally
        //        {
        //            recoExample.m_micClient.Dispose();
        //        }
        //    }
        //    else
        //    {
        //        DataRecognitionClient dataClient;
        //        DataRecognitionClientWithIntent intentDataClient;

        //        if (!isIntent)
        //        {
        //            dataClient = SpeechRecognitionServiceFactory.CreateDataClient(recoExample.m_recoMode, "en-us",
        //                                                                          primaryOrSecondaryKey);
        //        }
        //        else
        //        {
        //            Debug.Assert(recoExample.m_recoMode == SpeechRecognitionMode.ShortPhrase);
        //            intentDataClient =
        //                SpeechRecognitionServiceFactory.CreateDataClientWithIntent("en-us",
        //                                                                           primaryOrSecondaryKey,
        //                                                                           luisAppID,
        //                                                                           luisSubscriptionID);
        //            intentDataClient.OnIntent += recoExample.OnIntentHandler;
        //            dataClient = intentDataClient;
        //        }
        //        // Event handlers for speech recognition results
        //        dataClient.OnResponseReceived += recoExample.OnResponseReceivedHandler;
        //        dataClient.OnPartialResponseReceived += recoExample.OnPartialResponseReceivedHandler;
        //        dataClient.OnConversationError += recoExample.OnConversationErrorHandler;

        //        try
        //        {
        //            recoExample.DoDataRecognition(dataClient);
        //            if (tryMultipleRecos)
        //            {
        //                recoExample.DoDataRecognition(dataClient);
        //            }
        //        }
        //        finally
        //        {
        //            dataClient.Dispose();
        //        }
        //    }

        //    Console.WriteLine("Press any key to exit");
        //    Console.ReadLine();
        //}

        public static void Listen(bool useItent)
        {
            SpeechRecognitionServiceExample recoExample = new SpeechRecognitionServiceExample();

            bool isIntent         = false;
            bool tryMultipleRecos = false;

            if (useItent == true)
            {
                isIntent = true;
                recoExample.m_recoMode         = SpeechRecognitionMode.ShortPhrase;
                recoExample.m_isMicrophoneReco = true;
            }
            else if (useItent == false)
            {
                isIntent = false;
                recoExample.m_recoMode         = SpeechRecognitionMode.LongDictation;
                recoExample.m_isMicrophoneReco = true;
            }

            //if (args.Length == 1)
            //{
            //    recoExample.m_filename = args[0];
            //}

            string primaryOrSecondaryKey = ConfigurationManager.AppSettings["primaryKey"];
            string luisAppID             = ConfigurationManager.AppSettings["luisAppID"];
            string luisSubscriptionID    = ConfigurationManager.AppSettings["luisSubscriptionID"];

            if (recoExample.m_isMicrophoneReco)
            {
                MicrophoneRecognitionClientWithIntent intentMicClient;

                if (!isIntent)
                {
                    recoExample.m_micClient = SpeechRecognitionServiceFactory.CreateMicrophoneClient(recoExample.m_recoMode, "en-us",
                                                                                                     primaryOrSecondaryKey);
                }
                else
                {
                    Debug.Assert(recoExample.m_recoMode == SpeechRecognitionMode.ShortPhrase);
                    intentMicClient =
                        SpeechRecognitionServiceFactory.CreateMicrophoneClientWithIntent("en-us",
                                                                                         primaryOrSecondaryKey,
                                                                                         luisAppID,
                                                                                         luisSubscriptionID);
                    intentMicClient.OnIntent += recoExample.OnIntentHandler;
                    recoExample.m_micClient   = intentMicClient;
                }
                // Event handlers for speech recognition results
                recoExample.m_micClient.OnResponseReceived        += recoExample.OnResponseReceivedHandler;
                recoExample.m_micClient.OnPartialResponseReceived += recoExample.OnPartialResponseReceivedHandler;
                recoExample.m_micClient.OnConversationError       += recoExample.OnConversationErrorHandler;
                recoExample.m_micClient.OnMicrophoneStatus        += recoExample.OnMicrophoneStatus;

                try
                {
                    recoExample.DoMicrophoneRecognition(recoExample.m_micClient);
                }
                finally
                {
                    recoExample.m_micClient.Dispose();
                }
            }
            else
            {
                DataRecognitionClient           dataClient;
                DataRecognitionClientWithIntent intentDataClient;

                if (!isIntent)
                {
                    dataClient = SpeechRecognitionServiceFactory.CreateDataClient(recoExample.m_recoMode, "en-us",
                                                                                  primaryOrSecondaryKey);
                }
                else
                {
                    Debug.Assert(recoExample.m_recoMode == SpeechRecognitionMode.ShortPhrase);
                    intentDataClient =
                        SpeechRecognitionServiceFactory.CreateDataClientWithIntent("en-us",
                                                                                   primaryOrSecondaryKey,
                                                                                   luisAppID,
                                                                                   luisSubscriptionID);
                    intentDataClient.OnIntent += recoExample.OnIntentHandler;
                    dataClient = intentDataClient;
                }
                // Event handlers for speech recognition results
                dataClient.OnResponseReceived        += recoExample.OnResponseReceivedHandler;
                dataClient.OnPartialResponseReceived += recoExample.OnPartialResponseReceivedHandler;
                dataClient.OnConversationError       += recoExample.OnConversationErrorHandler;

                try
                {
                    recoExample.DoDataRecognition(dataClient);
                    if (tryMultipleRecos)
                    {
                        recoExample.DoDataRecognition(dataClient);
                    }
                }
                finally
                {
                    dataClient.Dispose();
                }
            }

            Console.WriteLine("Press any key to exit");
            Console.ReadLine();
        }
        //static void Main(string[] args)
        //{
        //    SpeechRecognitionServiceExample recoExample = new SpeechRecognitionServiceExample();

        //    recoExample.m_recoMode = SpeechRecognitionMode.ShortPhrase;
        //    recoExample.m_isMicrophoneReco = true;
        //    bool isIntent = true;
        //    bool tryMultipleRecos = false;

        //    if (args.Length == 1)
        //    {
        //        recoExample.m_filename = args[0];
        //    }

        //    string primaryOrSecondaryKey = ConfigurationManager.AppSettings["primaryKey"];
        //    string luisAppID = ConfigurationManager.AppSettings["luisAppID"];
        //    string luisSubscriptionID = ConfigurationManager.AppSettings["luisSubscriptionID"];

        //    if (recoExample.m_isMicrophoneReco)
        //    {
        //        MicrophoneRecognitionClientWithIntent intentMicClient;

        //        if (!isIntent)
        //        {
        //            recoExample.m_micClient = SpeechRecognitionServiceFactory.CreateMicrophoneClient(recoExample.m_recoMode, "en-us",
        //                                                                                             primaryOrSecondaryKey);
        //        }
        //        else
        //        {
        //            Debug.Assert(recoExample.m_recoMode == SpeechRecognitionMode.ShortPhrase);
        //            intentMicClient =
        //                SpeechRecognitionServiceFactory.CreateMicrophoneClientWithIntent("en-us",
        //                                                                                 primaryOrSecondaryKey,
        //                                                                                 luisAppID,
        //                                                                                 luisSubscriptionID);
        //            intentMicClient.OnIntent += recoExample.OnIntentHandler;
        //            recoExample.m_micClient = intentMicClient;
        //        }
        //        // Event handlers for speech recognition results
        //        recoExample.m_micClient.OnResponseReceived += recoExample.OnResponseReceivedHandler;
        //        recoExample.m_micClient.OnPartialResponseReceived += recoExample.OnPartialResponseReceivedHandler;
        //        recoExample.m_micClient.OnConversationError += recoExample.OnConversationErrorHandler;
        //        recoExample.m_micClient.OnMicrophoneStatus += recoExample.OnMicrophoneStatus;

        //        try
        //        {
        //            recoExample.DoMicrophoneRecognition(recoExample.m_micClient);
        //        }
        //        finally
        //        {
        //            recoExample.m_micClient.Dispose();
        //        }
        //    }
        //    else
        //    {
        //        DataRecognitionClient dataClient;
        //        DataRecognitionClientWithIntent intentDataClient;

        //        if (!isIntent)
        //        {
        //            dataClient = SpeechRecognitionServiceFactory.CreateDataClient(recoExample.m_recoMode, "en-us",
        //                                                                          primaryOrSecondaryKey);
        //        }
        //        else
        //        {
        //            Debug.Assert(recoExample.m_recoMode == SpeechRecognitionMode.ShortPhrase);
        //            intentDataClient =
        //                SpeechRecognitionServiceFactory.CreateDataClientWithIntent("en-us",
        //                                                                           primaryOrSecondaryKey,
        //                                                                           luisAppID,
        //                                                                           luisSubscriptionID);
        //            intentDataClient.OnIntent += recoExample.OnIntentHandler;
        //            dataClient = intentDataClient;
        //        }
        //        // Event handlers for speech recognition results
        //        dataClient.OnResponseReceived += recoExample.OnResponseReceivedHandler;
        //        dataClient.OnPartialResponseReceived += recoExample.OnPartialResponseReceivedHandler;
        //        dataClient.OnConversationError += recoExample.OnConversationErrorHandler;

        //        try
        //        {
        //            recoExample.DoDataRecognition(dataClient);
        //            if (tryMultipleRecos)
        //            {
        //                recoExample.DoDataRecognition(dataClient);
        //            }
        //        }
        //        finally
        //        {
        //            dataClient.Dispose();
        //        }
        //    }

        //    Console.WriteLine("Press any key to exit");
        //    Console.ReadLine();
        //}

        public static void Listen(bool useItent)
        {
            SpeechRecognitionServiceExample recoExample = new SpeechRecognitionServiceExample();

            bool isIntent = false;
            bool tryMultipleRecos = false;

            if (useItent == true)
            {
                isIntent = true;
                recoExample.m_recoMode = SpeechRecognitionMode.ShortPhrase;
                recoExample.m_isMicrophoneReco = true;
            }
            else if (useItent == false)
            {
                isIntent = false;
                recoExample.m_recoMode = SpeechRecognitionMode.LongDictation;
                recoExample.m_isMicrophoneReco = true;
            }

            //if (args.Length == 1)
            //{
            //    recoExample.m_filename = args[0];
            //}

            string primaryOrSecondaryKey = ConfigurationManager.AppSettings["primaryKey"];
            string luisAppID = ConfigurationManager.AppSettings["luisAppID"];
            string luisSubscriptionID = ConfigurationManager.AppSettings["luisSubscriptionID"];

            if (recoExample.m_isMicrophoneReco)
            {
                MicrophoneRecognitionClientWithIntent intentMicClient;

                if (!isIntent)
                {
                    recoExample.m_micClient = SpeechRecognitionServiceFactory.CreateMicrophoneClient(recoExample.m_recoMode, "en-us",
                                                                                                     primaryOrSecondaryKey);
                }
                else
                {
                    Debug.Assert(recoExample.m_recoMode == SpeechRecognitionMode.ShortPhrase);
                    intentMicClient =
                        SpeechRecognitionServiceFactory.CreateMicrophoneClientWithIntent("en-us",
                                                                                         primaryOrSecondaryKey,
                                                                                         luisAppID,
                                                                                         luisSubscriptionID);
                    intentMicClient.OnIntent += recoExample.OnIntentHandler;
                    recoExample.m_micClient = intentMicClient;
                }
                // Event handlers for speech recognition results
                recoExample.m_micClient.OnResponseReceived += recoExample.OnResponseReceivedHandler;
                recoExample.m_micClient.OnPartialResponseReceived += recoExample.OnPartialResponseReceivedHandler;
                recoExample.m_micClient.OnConversationError += recoExample.OnConversationErrorHandler;
                recoExample.m_micClient.OnMicrophoneStatus += recoExample.OnMicrophoneStatus;

                try
                {
                    recoExample.DoMicrophoneRecognition(recoExample.m_micClient);
                }
                finally
                {
                    recoExample.m_micClient.Dispose();
                }
            }
            else
            {
                DataRecognitionClient dataClient;
                DataRecognitionClientWithIntent intentDataClient;

                if (!isIntent)
                {
                    dataClient = SpeechRecognitionServiceFactory.CreateDataClient(recoExample.m_recoMode, "en-us",
                                                                                  primaryOrSecondaryKey);
                }
                else
                {
                    Debug.Assert(recoExample.m_recoMode == SpeechRecognitionMode.ShortPhrase);
                    intentDataClient =
                        SpeechRecognitionServiceFactory.CreateDataClientWithIntent("en-us",
                                                                                   primaryOrSecondaryKey,
                                                                                   luisAppID,
                                                                                   luisSubscriptionID);
                    intentDataClient.OnIntent += recoExample.OnIntentHandler;
                    dataClient = intentDataClient;
                }
                // Event handlers for speech recognition results
                dataClient.OnResponseReceived += recoExample.OnResponseReceivedHandler;
                dataClient.OnPartialResponseReceived += recoExample.OnPartialResponseReceivedHandler;
                dataClient.OnConversationError += recoExample.OnConversationErrorHandler;

                try
                {
                    recoExample.DoDataRecognition(dataClient);
                    if (tryMultipleRecos)
                    {
                        recoExample.DoDataRecognition(dataClient);
                    }
                }
                finally
                {
                    dataClient.Dispose();
                }
            }

            Console.WriteLine("Press any key to exit");
            Console.ReadLine();
        }