private void SendWavAudio(string wavFileName, DictationType dictationType) { Result = new List <RecognitionResult>(); var speechClient = CreateSpeechClient(dictationType); 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. speechClient.SendAudio(buffer, bytesRead); }while (bytesRead > 0); } finally { // We are done sending audio. Final recognition results will arrive in OnResponseReceived event call. speechClient.EndAudio(); } } }
private DataRecognitionClient CreateSpeechClient(DictationType dictationType) { var key = ConfigurationManager.AppSettings["MicrosoftSpeechApiKey"]; var speechClient = SpeechRecognitionServiceFactory.CreateDataClient( //SpeechRecognitionMode.ShortPhrase, SpeechRecognitionMode.LongDictation, "pt-BR", key ); speechClient.AuthenticationUri = ""; if (dictationType == DictationType.Short) { speechClient.OnResponseReceived += this.OnShortResponseReceived; } else { speechClient.OnResponseReceived += this.OnLongResponseReceived; } speechClient.OnConversationError += this.OnConversationError; speechClient.OnPartialResponseReceived += SpeechClient_OnPartialResponseReceived; return(speechClient); }
public void ConvertSpeechToText(string fileNameAndPath, DictationType dictationType) { waitHandle = new AutoResetEvent(false); SendWavAudio(fileNameAndPath, dictationType); waitHandle.WaitOne(); }