Пример #1
0
        private static void SendAudioHelper(string wavFileName)
        {
            log.Debug(" --> SendAudioHelper()");
            using (FileStream fileStream = new FileStream(wavFileName, FileMode.Open, FileAccess.Read))
            {
                // Note for wave files, we can just send data from the file right to the server.
                // In the case you are not an audio file in wave format, and instead you have just
                // raw data (for example audio coming over bluetooth), then before sending up any
                // audio data, you must first send up an SpeechAudioFormat descriptor to describe
                // the layout and format of your raw audio data via DataRecognitionClient's sendAudioFormat() method.
                int    bytesRead = 0;
                byte[] buffer    = new byte[1024];

                try
                {
                    do
                    {
                        // Get more Audio data to send into byte buffer.
                        bytesRead = fileStream.Read(buffer, 0, buffer.Length);

                        // Send of audio data to service.
                        dataClient.SendAudio(buffer, bytesRead);
                    }while (bytesRead > 0);
                }
                finally
                {
                    // We are done sending audio.  Final recognition results will arrive in OnResponseReceived event call.
                    dataClient.EndAudio();
                    log.Debug(" <-- SendAudioHelper()");
                }
            }
        }
Пример #2
0
        public void SendAudioHelper(Stream recordedStream)
        {
            // Note for wave files, we can just send data from the file right to the server.
            // In the case you are not an audio file in wave format, and instead you have just
            // raw data (for example audio coming over bluetooth), then before sending up any
            // audio data, you must first send up an SpeechAudioFormat descriptor to describe
            // the layout and format of your raw audio data via DataRecognitionClient's sendAudioFormat() method.
            var buffer = new byte[1024];

            try
            {
                int bytesRead;
                do
                {
                    // Get more Audio data to send into byte buffer.
                    bytesRead = recordedStream.Read(buffer, 0, buffer.Length);

                    // Send of audio data to service.
                    _dataClient.SendAudio(buffer, bytesRead);
                } while (bytesRead > 0);
            }
            catch (Exception ex)
            {
                WriteLine("Exception ------------ " + ex.Message);
            }
            finally
            {
                // We are done sending audio.  Final recognition results will arrive in OnResponseReceived event call.
                _dataClient.EndAudio();
            }
        }
Пример #3
0
        /// <summary>
        /// Function to convert from an audio file to text, through the Bing Speech API
        /// </summary>
        /// <param name="audioFileName"></param>
        public void StartAudioFileToText(string audioFileName)
        {
            using (FileStream fileStream = new FileStream(audioFileName, FileMode.Open, FileAccess.Read))
            {
                int    bytesRead = 0;
                byte[] buffer    = new byte[1024];

                try
                {
                    do
                    {
                        bytesRead = fileStream.Read(buffer, 0, buffer.Length);

                        _dataRecClient.SendAudio(buffer, bytesRead);
                    }while (bytesRead > 0);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine($"Exception caught: {ex.Message}");
                }
                finally
                {
                    _dataRecClient.EndAudio();
                }
            }
        }
Пример #4
0
        public void Stop()
        {
            _client?.EndAudio();

            _client?.Dispose();
            _client  = null;
            _started = false;
        }
Пример #5
0
        void RunSpeechToTextFromStream(Stream stream)
        {
            stream.Seek(0, SeekOrigin.Begin);

            DataRecognitionClient dataClient = SpeechRecognitionServiceFactory.CreateDataClientWithIntent(
                "en-US",
                "7bb47d20e78846c3aa0340cc2e148a85",
                "LofnLuisBot", // "yourLuisAppID",
                ""             // "yourLuisSubsrciptionID"
                );

            dataClient.AuthenticationUri = ""; // this.AuthenticationUri;

            // Event handlers for speech recognition results
            dataClient.OnResponseReceived        += DataClient_OnResponseReceived;
            dataClient.OnPartialResponseReceived += DataClient_OnPartialResponseReceived;
            dataClient.OnConversationError       += (sender, args) =>
            {
            };

            // Event handler for intent result
            dataClient.OnIntent += (sender, args) =>
            {
            };

            // Note for wave files, we can just send data from the file right to the server.
            // In the case you are not an audio file in wave format, and instead you have just
            // raw data (for example audio coming over bluetooth), then before sending up any
            // audio data, you must first send up an SpeechAudioFormat descriptor to describe
            // the layout and format of your raw audio data via DataRecognitionClient's sendAudioFormat() method.
            int bytesRead = 0;

            byte[] buffer = new byte[1024];

            try
            {
                do
                {
                    // Get more Audio data to send into byte buffer.
                    bytesRead = stream.Read(buffer, 0, buffer.Length);

                    // Send of audio data to service.
                    dataClient.SendAudio(buffer, bytesRead);
                }while (bytesRead > 0);
            }
            finally
            {
                // We are done sending audio.  Final recognition results will arrive in OnResponseReceived event call.
                dataClient.EndAudio();
            }
        }
Пример #6
0
        /// <summary>
        ///     Speech recognition with data (for example from a file or audio source).
        ///     The data is broken up into buffers and each buffer is sent to the Speech Recognition Service.
        ///     No modification is done to the buffers, so the user can apply their
        ///     own Silence Detection if desired.
        /// </summary>
        void DoDataRecognition(DataRecognitionClient dataClient)
        {
            // Choose between a two minute recitation of the wikipedia page for batman
            // or a a short utterance
            string filename = (m_recoMode == SpeechRecognitionMode.LongDictation) ? "C:\\dev\\audio\\v1.wav" :
                              "C:\\dev\\audio\\v1.wav";

            if (m_filename != null)
            {
                filename = m_filename;
            }
            int waitSeconds = (m_recoMode == SpeechRecognitionMode.LongDictation) ? 200 : 15;

            using (FileStream fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read))
            {
                // Note for wave files, we can just send data from the file right to the server.
                // In the case you are not an audio file in wave format, and instead you have just
                // raw data (for example audio coming over bluetooth), then before sending up any
                // audio data, you must first send up an SpeechAudioFormat descriptor to describe
                // the layout and format of your raw audio data via DataRecognitionClient's sendAudioFormat() method.

                int    bytesRead = 0;
                byte[] buffer    = new byte[1024];

                try
                {
                    do
                    {
                        // Get more Audio data to send into byte buffer.
                        bytesRead = fileStream.Read(buffer, 0, buffer.Length);

                        // Send of audio data to service.
                        dataClient.SendAudio(buffer, bytesRead);
                    } while (bytesRead > 0);
                }
                finally
                {
                    // We are done sending audio.  Final recognition results will arrive in OnResponseReceived event call.
                    dataClient.EndAudio();
                }

                // sleep until the final result in OnResponseReceived event call, or waitSeconds, whichever is smaller.
                bool isReceivedResponse = dataClient.WaitForFinalResponse(waitSeconds * 1000);
                if (!isReceivedResponse)
                {
                    Console.WriteLine("{0}: Timed out waiting for conversation response after {1} ms",
                                      DateTime.UtcNow, waitSeconds * 1000);
                }
            }
        }
Пример #7
0
        private static void SendAudioHelper(string wavFileName)
        {
            using (FileStream fileStream = new FileStream(wavFileName, FileMode.Open, FileAccess.Read))
            {
                int    bytesRead = 0;
                byte[] buffer    = new byte[1024];

                try
                {
                    do
                    {
                        bytesRead = fileStream.Read(buffer, 0, buffer.Length);
                        _dataClient.SendAudio(buffer, bytesRead);
                    }while (bytesRead > 0);
                }
                finally
                {
                    _dataClient.EndAudio();
                }
            }
        }
        /// <summary>
        ///     Speech recognition with data (for example from a file or audio source).  
        ///     The data is broken up into buffers and each buffer is sent to the Speech Recognition Service.
        ///     No modification is done to the buffers, so the user can apply their
        ///     own Silence Detection if desired.
        /// </summary>
        void DoDataRecognition(DataRecognitionClient dataClient)
        {
            // Choose between a two minute recitation of the wikipedia page for batman
            // or a a short utterance
            string filename = (m_recoMode == SpeechRecognitionMode.LongDictation) ? "C:\\dev\\audio\\v1.wav" :
                                                                                    "C:\\dev\\audio\\v1.wav";
            if (m_filename != null)
            {
                filename = m_filename;
            }
            int waitSeconds = (m_recoMode == SpeechRecognitionMode.LongDictation) ? 200 : 15;

            using (FileStream fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read))
            {
                // Note for wave files, we can just send data from the file right to the server.
                // In the case you are not an audio file in wave format, and instead you have just
                // raw data (for example audio coming over bluetooth), then before sending up any 
                // audio data, you must first send up an SpeechAudioFormat descriptor to describe 
                // the layout and format of your raw audio data via DataRecognitionClient's sendAudioFormat() method.

                int bytesRead = 0;
                byte[] buffer = new byte[1024];

                try
                {
                    do
                    {
                        // Get more Audio data to send into byte buffer.
                        bytesRead = fileStream.Read(buffer, 0, buffer.Length);

                        // Send of audio data to service. 
                        dataClient.SendAudio(buffer, bytesRead);
                    } while (bytesRead > 0);
                }
                finally
                {
                    // We are done sending audio.  Final recognition results will arrive in OnResponseReceived event call.
                    dataClient.EndAudio();
                }

                // sleep until the final result in OnResponseReceived event call, or waitSeconds, whichever is smaller.
                bool isReceivedResponse = dataClient.WaitForFinalResponse(waitSeconds * 1000);
                if (!isReceivedResponse)
                {
                    Console.WriteLine("{0}: Timed out waiting for conversation response after {1} ms",
                                      DateTime.UtcNow, waitSeconds * 1000);
                }
            }
        }