Ejemplo n.º 1
0
        public async Task DecodeAudioAsync(
            Stream stream,
            string locale, PhraseMode mode,
            Func <RecognitionStep, Task> partialResult,
            Func <RecognitionFinalResult, Task> finalResult)
        {
            var serviceUrl = (mode == PhraseMode.LongDictation ? LongDictationUrl : ShortPhraseUrl);

            string subscriptionKey = configuration.GetSection("AudioToTextService.Core.AudioDecoder")["subscriptionKey"];

            // create the preferences object
            var preferences = new Preferences(locale, serviceUrl, new CognitiveServicesAuthorizationProvider(subscriptionKey));

            // Create a a speech client
            using (var speechClient = new SpeechClient(preferences))
            {
                speechClient.SubscribeToPartialResult((args) =>
                {
                    return(partialResult(ToStep(args)));
                });

                speechClient.SubscribeToRecognitionResult((args) =>
                {
                    return(finalResult(ToFinalResult(args)));
                });

                // create an audio content and pass it a stream.
                var deviceMetadata      = new DeviceMetadata(DeviceType.Near, DeviceFamily.Desktop, NetworkType.Ethernet, OsName.Windows, "1607", "Dell", "T3600");
                var applicationMetadata = new ApplicationMetadata("SampleApp", "1.0.0");
                var requestMetadata     = new RequestMetadata(Guid.NewGuid(), deviceMetadata, applicationMetadata, "SampleAppService");

                await speechClient.RecognizeAsync(new SpeechInput(stream, requestMetadata), this.cts.Token).ConfigureAwait(false);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 通过语音数据识别语音内容
        /// </summary>
        /// <param name="audioBytes">语音数据</param>
        /// <param name="locale">语种</param>
        /// <returns></returns>
        public async Task RunDiscernAsBytes(byte[] audioBytes, string locale)
        {
            Uri serviceUrl = ShortUrl;

            if (GetWavPlayTime(audioBytes) > 15)
            {
                serviceUrl = LongUrl;
            }
            var preferences = new Preferences(locale, serviceUrl, new AsrCheckKeyProvider(apiKey)); //参数配置类

            using (var speechClient = new SpeechClient(preferences))                                //创建语音客户端
            {
                speechClient.SubscribeToPartialResult(this.OnPartialResult);
                speechClient.SubscribeToRecognitionResult(this.OnRecognitionResult);
                using (Stream stream = new MemoryStream(audioBytes))
                {
                    var deviceMetadata      = new DeviceMetadata(DeviceType.Near, DeviceFamily.Desktop, NetworkType.Ethernet, OsName.Windows, "1607", "Dell", "T3600");
                    var applicationMetadata = new ApplicationMetadata("SampleApp", "1.0.0");
                    var requestMetadata     = new RequestMetadata(Guid.NewGuid(), deviceMetadata, applicationMetadata, "SampleAppService");
                    try
                    {
                        await speechClient.RecognizeAsync(new SpeechInput(stream, requestMetadata), this.cts.Token).ConfigureAwait(false);
                    }
                    catch (Exception ex)
                    {
                    }
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Sends a speech recognition request to the speech service
        /// </summary>
        /// <param name="audioFile">The audio file.</param>
        /// <param name="locale">The locale.</param>
        /// <param name="serviceUrl">The service URL.</param>
        /// <param name="subscriptionKey">The subscription key.</param>
        /// <returns>
        /// A task
        /// </returns>
        public async Task Run(string audioFile, string locale, Uri serviceUrl, string subscriptionKey, string resultFile)
        {
            // create the preferences object
            var preferences = new Preferences(locale, serviceUrl, new CognitiveServicesAuthorizationProvider(subscriptionKey));

            ResultFile = resultFile;
            File.CreateText(ResultFile);

            // Create a a speech client
            using (var speechClient = new SpeechClient(preferences))
            {
                speechClient.SubscribeToPartialResult(this.OnPartialResult);
                speechClient.SubscribeToRecognitionResult(this.OnRecognitionResult);

                // create an audio content and pass it a stream.
                using (var audio = new FileStream(audioFile, FileMode.Open, FileAccess.Read))
                {
                    var deviceMetadata      = new DeviceMetadata(DeviceType.Near, DeviceFamily.Desktop, NetworkType.Ethernet, OsName.Windows, "1607", "Dell", "T3600");
                    var applicationMetadata = new ApplicationMetadata("SampleApp", "1.0.0");
                    var requestMetadata     = new RequestMetadata(Guid.NewGuid(), deviceMetadata, applicationMetadata, "SampleAppService");

                    await speechClient.RecognizeAsync(new SpeechInput(audio, requestMetadata), this.cts.Token).ConfigureAwait(false);
                }
            }
        }
Ejemplo n.º 4
0
 private Task <Tuple <string, string> > transcribeAudioSegement(int index, string fileName, string audioFile, TimeSpan totalTime, CancellationToken token, string user)
 {
     return(Task.Factory.StartNew <Tuple <string, string> >(() =>
     {
         //var preferences = new Preferences("en-US", new Uri("wss://speech.platform.bing.com/api/service/recognition/continuous"), new CognitiveServicesAuthorizationProvider("68ecbfed77384b0badae81995a5b256b"));
         var preferences = new Preferences("en-US", new Uri("wss://5ba5d066af03405ba71e84ba3bc4d185.api.cris.ai/ws/cris/speech/recognize/continuous"), new CognitiveServicesAuthorizationProvider("36677b4f10da4d2a946af66da757ef0b"));
         DateTime lastReportTime = DateTime.Now;
         DateTime lastDetectionTime = DateTime.Now;
         int runonLength = 0;
         StringBuilder text = new StringBuilder();
         using (var speechClient = new SpeechClient(preferences))
         {
             speechClient.SubscribeToPartialResult((args) =>
             {
                 return Task.Factory.StartNew(() =>
                 {
                     token.ThrowIfCancellationRequested();
                     if (DateTime.Now - lastReportTime >= mReportInterval)
                     {
                         var percent = (int)(args.MediaTime * 0.00001 / totalTime.TotalSeconds);
                         percentages[index] = percent;
                         reportProgress(fileName, Math.Min(99, percentages.Sum()), args.DisplayText.Substring(0, Math.Min(args.DisplayText.Length, 50)) + "...", user).Wait();
                         lastReportTime = DateTime.Now;
                     }
                 });
             });
             speechClient.SubscribeToRecognitionResult((args) =>
             {
                 return Task.Factory.StartNew(() =>
                 {
                     if (args.Phrases.Count > 0)
                     {
                         string bestText = args.Phrases[args.Phrases.Count - 1].DisplayText;
                         runonLength += bestText.Length;
                         if ((DateTime.Now - lastDetectionTime >= TimeSpan.FromSeconds(5) || runonLength >= 1800) && runonLength >= 250)
                         {
                             text.Append("\r\n\r\n    ");
                             runonLength = 0;
                         }
                         text.Append(Regex.Replace(bestText, "(?<=[\\.,?])(?![$ ])", " "));
                         lastDetectionTime = DateTime.Now;
                     }
                 });
             });
             using (var audio = new FileStream(audioFile, FileMode.Open, FileAccess.Read))
             {
                 var deviceMetadata = new DeviceMetadata(DeviceType.Near, DeviceFamily.Desktop, NetworkType.Ethernet, OsName.Windows, "1607", "Dell", "T3600");
                 var applicationMetadata = new ApplicationMetadata("TranscriptionApp", "1.0.0");
                 var requestMetadata = new RequestMetadata(Guid.NewGuid(), deviceMetadata, applicationMetadata, "TranscriptionService");
                 speechClient.RecognizeAsync(new SpeechInput(audio, requestMetadata), mTokensource.Token).Wait();
                 return new Tuple <string, string>(audioFile, text.ToString());
             }
         }
     }));
 }
Ejemplo n.º 5
0
        public static async Task <string> ReconhecerFala(string contentUrl)
        {
            RecognitionStatus status       = RecognitionStatus.None;
            string            stringResult = string.Empty;
            WebClient         wc           = new WebClient();
            OgaToWavConverter converter    = new OgaToWavConverter();


            var ogaData = await wc.DownloadDataTaskAsync(contentUrl);

            var wavData = converter.Convert(ogaData);

            var preferences = new Preferences("pt-BR",
                                              new Uri(@"wss://speech.platform.bing.com/api/service/recognition"),
                                              new CognitiveServicesAuthorizationProvider(CognitiveServicesAuthorizationProvider.API_KEY));

            using (var speechClient = new SpeechClient(preferences))
            {
                speechClient.SubscribeToPartialResult(
                    result =>
                {
                    stringResult = result.DisplayText;
                    return(Task.FromResult(true));
                });

                speechClient.SubscribeToRecognitionResult(
                    result =>
                {
                    status = result.RecognitionStatus;
                    return(Task.FromResult(true));
                });

                var deviceMetadata      = new DeviceMetadata(DeviceType.Near, DeviceFamily.Unknown, NetworkType.Unknown, OsName.Windows, "10", "IBM", "ThinkCenter");
                var applicationMetadata = new ApplicationMetadata("WorkshopAtentoBot", "1.0");
                var requestMetada       = new RequestMetadata(Guid.NewGuid(), deviceMetadata, applicationMetadata, "ReconhecimentoFalaService");
                await speechClient.RecognizeAsync(new SpeechInput(new MemoryStream(wavData), requestMetada), CancellationToken.None).ConfigureAwait(false);
            }

            while (status == RecognitionStatus.None)
            {
                await Task.Delay(200);
            }

            if (status == RecognitionStatus.Success)
            {
                return(stringResult);
            }
            else
            {
                return($"Ocorreu um erro no reconhecimento de fala. Status = {status}");
            }
        }
Ejemplo n.º 6
0
        private void StartSpeechRecognition()
        {
            _recognitionCts = new CancellationTokenSource();

            string      speechSubscription = Service.Instance.Configuration.SpeechSubscription;
            Preferences preferences        = new Preferences("en-US", LongDictationUrl, new CognitiveServicesAuthorizationProvider(speechSubscription), enableAudioBuffering: false);

            _recognitionStream = new SpeechRecognitionPcmStream(16000);

            var deviceMetadata      = new DeviceMetadata(DeviceType.Far, DeviceFamily.Unknown, NetworkType.Unknown, OsName.Windows, "1607", "Dell", "T3600");
            var applicationMetadata = new ApplicationMetadata("HueBot", "1.0.0");
            var requestMetadata     = new RequestMetadata(Guid.NewGuid(), deviceMetadata, applicationMetadata, "HueBot");

            Task.Run(async() =>
            {
                do
                {
                    try
                    {
                        //create a speech client
                        using (_speechClient = new SpeechClient(preferences))
                        {
                            _speechClient.SubscribeToRecognitionResult(this.OnRecognitionResult);

                            await _speechClient.RecognizeAsync(new SpeechInput(_recognitionStream, requestMetadata), _recognitionCts.Token);
                        }
                        Log.Info(new CallerInfo(), LogContext.Media, $"[{this.Id}]: Speech recognize completed.");
                    }
                    catch (Exception exception)
                    {
                        Log.Error(new CallerInfo(), LogContext.Media, $"[{this.Id}]: Speech recognize threw exception {exception.ToString()}");
                    }

                    if (_recognitionCts.IsCancellationRequested)
                    {
                        Log.Info(new CallerInfo(), LogContext.Media, $"[{this.Id}]:Speech recognition cancelled because it was cancelled or max exception count was hit");
                        break;
                    }

                    Stream oldStream = _recognitionStream;
                    Log.Info(new CallerInfo(), LogContext.Media, $"[{this.Id}]: Restart speech recognition as the call is still alive. Speech recognition could have been completed because of babble/silence timeout");

                    _recognitionStream = new SpeechRecognitionPcmStream(16000);
                    oldStream.Dispose();
                } while (true);

                _speechRecoginitionFinished.Set();
            }
                     ).ForgetAndLogException(string.Format("Failed to start the SpeechRecognition Task for Id: {0}", Id));
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Takes a passed in file that is assumed to be mono and PCM encoded,
        /// and sends it to the speech client.
        /// </summary>
        /// <param name="audioFile">The full path and file name of a PCM encoded MONO channel file.</param>
        /// <returns></returns>
        public async Task ExecuteRecogniseAsync(string audioFile)
        {
            FileInfo      inputFileInfo = new FileInfo(audioFile);
            WaveStream    audioStream   = null;
            Mp3FileReader mp3Stream     = null;
            string        tempAudioFile = audioFile;

            switch (inputFileInfo.Extension.ToLower())
            {
            case ".mp3":
                mp3Stream   = new Mp3FileReader(audioFile);
                audioStream = WaveFormatConversionStream.CreatePcmStream(mp3Stream);
                // Have to send to a physical file, as the NAudio stream doesn't interact with the
                // Speech client nicely.
                tempAudioFile = System.IO.Path.GetTempFileName();
                WaveFileWriter.CreateWaveFile(tempAudioFile, audioStream);
                break;

            default:
                // Try and send the file to the speechClient, in the hope it is a PCM encoded file.
                break;
            }

            //using (var inputReader = new AudioFileReader(audioFile))
            //{
            //    var mono = inputReader.ToMono(0.5f, 0.5f);

            //}
            // Create a a speech client
            using (var speechClient = new SpeechClient(this._preferences))
            {
                speechClient.SubscribeToRecognitionResult(this.OnRecognitionResultAsync);

                var deviceMetadata      = new DeviceMetadata(DeviceType.Near, DeviceFamily.Desktop, NetworkType.Ethernet, OsName.Windows, "1607", "HyperV", "HyperV");
                var applicationMetadata = new ApplicationMetadata("SSISSpeachToText", "1.0.0.0");
                var requestMetadata     = new RequestMetadata(Guid.NewGuid(), deviceMetadata, applicationMetadata, "SQL Server Integration Services");
                using (var audio = new FileStream(tempAudioFile, FileMode.Open, FileAccess.Read))
                    await speechClient.RecognizeAsync(new SpeechInput(audio, requestMetadata), this.cts.Token).ConfigureAwait(false);
            }

            if (audioStream != null)
            {
                audioStream.Dispose();
            }
            if (mp3Stream != null)
            {
                mp3Stream.Dispose();
            }
        }
Ejemplo n.º 8
0
        public async Task <List <string> > DoRequest(Stream audioFile)
        {
            CancellationTokenSource cts = new CancellationTokenSource();

            var preferences = new Preferences(_locale, _serviceUrl, new CognitiveServicesAuthorizationProvider(_subscriptionKey));

            using (var speechClient = new SpeechClient(preferences))
            {
                speechClient.SubscribeToRecognitionResult(this.OnRecognitionResult);

                var deviceMetadata      = new DeviceMetadata(DeviceType.Near, DeviceFamily.Desktop, NetworkType.Ethernet, OsName.Windows, "1607", "Dell", "T3600");
                var applicationMetadata = new ApplicationMetadata("SampleApp", "1.0.0");
                var requestMetadata     = new RequestMetadata(Guid.NewGuid(), deviceMetadata, applicationMetadata, "SampleAppService");

                await speechClient.RecognizeAsync(new SpeechInput(audioFile, requestMetadata), cts.Token).ConfigureAwait(false);
            }

            return(_lines);
        }
        public async Task <string> RecognizeFile(Stream audio, string stsUrl, string authKey, Uri longDictationUrl)
        {
            var cts = new CancellationTokenSource();

            // create the preferences object
            var preferences = new Preferences("en-US", longDictationUrl,
                                              new CognitiveServicesAuthorizationProvider(stsUrl, authKey));

            List <RecognitionResult> results = new List <RecognitionResult>();

            using (var speechClient = new SpeechClient(preferences))
            {
                speechClient.SubscribeToRecognitionResult(async(rr) =>
                {
                    await Task.Run(() =>
                    {
                        results.Add(rr);
                        Console.WriteLine("--------------------------------------------");
                        Console.WriteLine($"RecognitionStatus = ${rr.RecognitionStatus}");
                        foreach (var p in rr.Phrases)
                        {
                            Console.WriteLine($"  {p.Confidence} {p.DisplayText}");
                        }
                    });
                });

                var deviceMetadata = new DeviceMetadata(DeviceType.Near,
                                                        DeviceFamily.Desktop,
                                                        NetworkType.Unknown,
                                                        OsName.Unknown, "1607", "Azure", "Function");
                var applicationMetadata = new ApplicationMetadata("ResilientLongVtt", "1.0.0");
                var requestMetadata     = new RequestMetadata(Guid.NewGuid(), deviceMetadata, applicationMetadata, "SampleAppService");

                await speechClient.RecognizeAsync(new SpeechInput(audio, requestMetadata), cts.Token);

                Console.Write($"TOTAL RESULTS : {results.Count}");
            }

            return("rien");
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Sends a speech recognition request to the speech service
        /// </summary>
        /// <param name="audioFile">The audio file.</param>
        /// <param name="locale">The locale.</param>
        /// <returns>
        /// A task
        /// </returns>
        public async Task <string> TranslateToText(string audioFile, string locale = "en-GB")
        {
            var preferences = new Preferences(locale, _serviceUrl, new CognitiveServicesAuthorizationProvider(_subscriptionKey));

            // Create a a speech client
            using (var speechClient = new SpeechClient(preferences))
            {
                speechClient.SubscribeToPartialResult(this.OnPartialResult);
                speechClient.SubscribeToRecognitionResult(this.OnRecognitionResult);

                try
                {
                    // create an audio content and pass it a stream.
                    using (var downloadStream = new WebClient())
                        using (var audio = new MemoryStream(downloadStream.DownloadData(audioFile)))
                        {
                            var deviceMetadata = new DeviceMetadata(DeviceType.Near, DeviceFamily.Desktop,
                                                                    NetworkType.Ethernet, OsName.Windows, "1607", "Dell", "T3600");
                            var applicationMetadata = new ApplicationMetadata("SampleApp", "1.0.0");
                            var requestMetadata     = new RequestMetadata(Guid.NewGuid(), deviceMetadata, applicationMetadata,
                                                                          "SampleAppService");

                            await speechClient.RecognizeAsync(new SpeechInput(audio, requestMetadata), this.cts.Token)
                            .ConfigureAwait(false);
                        }
                }
                catch (Exception e)
                {
                    if (e is PlatformNotSupportedException)
                    {
                        return(await TranslateToTextFallback(audioFile).ConfigureAwait(false)); //fallback for when websockets are not supported
                    }
                }
            }

            return(string.Empty);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Sends a speech recognition request to the speech service
        /// </summary>
        /// <param name="audioFile">The audio file.</param>
        /// <param name="locale">The locale.</param>
        /// <param name="serviceUrl">The service URL.</param>
        /// <param name="subscriptionKey">The subscription key.</param>
        /// <returns>
        /// A task
        /// </returns>
        ///

        public async Task Run(string audioFile, string locale, Uri serviceUrl, string subscriptionKey, string episode)
        {
            // create the preferences object
            var preferences = new Preferences(locale, serviceUrl, new CognitiveServicesAuthorizationProvider(subscriptionKey));


            string simpleFilename = Path.GetFileNameWithoutExtension(audioFile);

            if (int.TryParse(episode, out int outepisode))
            {
                episodenumber = outepisode;
            }
            else
            {
                return;
            }

            htmlOutputFilePath = Path.GetDirectoryName(audioFile) + @"\" + episodenumber.ToString() + ".html";
            opmlOutputFilePath = Path.GetDirectoryName(audioFile) + @"\" + episodenumber.ToString() + ".opml";

            // Create a a speech client
            using (var speechClient = new SpeechClient(preferences))
            {
                //speechClient.SubscribeToPartialResult(this.OnPartialResult);
                speechClient.SubscribeToRecognitionResult(this.OnRecognitionResult);

                // create an audio content and pass it a stream.
                using (var audio = new FileStream(audioFile, FileMode.Open, FileAccess.Read))
                {
                    var deviceMetadata      = new DeviceMetadata(DeviceType.Near, DeviceFamily.Desktop, NetworkType.Ethernet, OsName.Windows, "1607", "Dell", "T3600");
                    var applicationMetadata = new ApplicationMetadata("NA Transcribe", "1.0.0");
                    var requestMetadata     = new RequestMetadata(Guid.NewGuid(), deviceMetadata, applicationMetadata, "NATranscriptService");

                    await speechClient.RecognizeAsync(new SpeechInput(audio, requestMetadata), this.cts.Token).ConfigureAwait(false);
                }
            }
        }
    /// <summary>
    /// Sends a speech recognition request to the speech service
    /// </summary>
    /// <param name="audioFile">The audio file.</param>
    /// <param name="locale">The locale.</param>
    /// <param name="serviceUrl">The service URL.</param>
    /// <param name="subscriptionKey">The subscription key.</param>
    /// <returns>
    /// A task
    /// </returns>
    public async Task Run()
    {
        // create the preferences object
        var preferences = new Preferences("en-US", LongDictationUrl, new CognitiveServicesAuthorizationProvider("c26f94bbc00e4c98a0ee0cde5833506a"));

        // Create a a speech client
        using (var speechClient = new SpeechClient(preferences))
        {
            //speechClient.SubscribeToPartialResult(this.OnPartialResult);
            speechClient.SubscribeToRecognitionResult(this.OnRecognitionResult);

            // create an audio content and pass it a stream.
            using (var audio = new FileStream(filename, FileMode.Open, FileAccess.Read))
            {
                var deviceMetadata      = new DeviceMetadata(DeviceType.Near, DeviceFamily.Desktop, NetworkType.Ethernet, OsName.Windows, "1607", "Dell", "T3600");
                var applicationMetadata = new ApplicationMetadata("SampleApp", "1.0.0");
                var requestMetadata     = new RequestMetadata(Guid.NewGuid(), deviceMetadata, applicationMetadata, "SampleAppService");

                await speechClient.RecognizeAsync(new SpeechInput(audio, requestMetadata), this.cts.Token).ConfigureAwait(false);
            }
        }
        // Create a client.
        ITextAnalyticsAPI client = new TextAnalyticsAPI();

        client.AzureRegion     = AzureRegions.Westcentralus;
        client.SubscriptionKey = "922da2349b3f4d5f8d30cf175347ce7b";
        SentimentBatchResult result3 = client.Sentiment(
            new MultiLanguageBatchInput(
                new List <MultiLanguageInput>()
        {
            new MultiLanguageInput("en", "0", Msg),
        }));


        // Printing sentiment results
        var           document = result3.Documents[result3.Documents.Count() - 1];
        StringBuilder sb       = new StringBuilder();

        sb.AppendFormat("Sentiment Score: {0:0.00}", document.Score);

        //Save to DB
        string sql = "update [CaseConversation] set [ConversationTranscript] = '" + Msg + "',";

        sql += "[SentimentScore] = " + document.Score + " where id = " + sqlInt + ";";
        using (SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["DBConnectionString"]))
        {
            // 1. declare command object with parameter
            conn.Open();
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = sql;
            // 2. define parameters used in command object
            //SqlParameter param = new SqlParameter();
            //param.ParameterName = "@qkid";
            //param.Value = "''";

            // 3. add new parameter to command object
            //cmd.Parameters.Add(param);
            int sqlInt2 = cmd.ExecuteNonQuery();

            cmd.Dispose();
            conn.Close();
        }
        Msg += "<br /> <br />" + sb.ToString();
    }
Ejemplo n.º 13
0
        private async Task OnBinaryMessageReceived(WebSocketHandler handler, byte[] bytes, string conversationId, string watermark)
        {
            string replyMessage = null;

            // Convert speech to text
            try
            {
                using (var speechClient = new SpeechClient(this.speechPreference))
                {
                    speechClient.SubscribeToRecognitionResult(this.OnRecognitionResult);

                    // create an audio content and pass it a stream.
                    using (MemoryStream audioStream = new MemoryStream(bytes))
                    {
                        var deviceMetadata      = new DeviceMetadata(DeviceType.Near, DeviceFamily.Desktop, NetworkType.Wifi, OsName.Windows, "1607", "Dell", "T3600");
                        var applicationMetadata = new ApplicationMetadata("SampleApp", "1.0.0");
                        var requestMetadata     = new RequestMetadata(Guid.NewGuid(), deviceMetadata, applicationMetadata, "SampleAppService");

                        await speechClient.RecognizeAsync(new SpeechInput(PCMToWAV(audioStream), requestMetadata), this.cts.Token).ConfigureAwait(false);
                    }
                }
            }
            catch (Exception e)
            {
                //throw new DemoBotServiceException($"Convert text to speech failed: {e.Message}");
            }

            // await handler.SendMessage($"You said: {this.speechText}");

            if (!string.IsNullOrEmpty(speechText))
            {
                // Send text message to Bot Service
                await BotClientHelper.SendBotMessageAsync(this.directLineClient, conversationId, FromUserId, speechText);

                BotMessage botResponse = await BotClientHelper.ReceiveBotMessagesAsync(this.directLineClient, conversationId, watermark);

                replyMessage = botResponse.Text;
            }
            else
            {
                replyMessage = "Sorry, I don't understand.";
            }

            // Convert text to speech
            byte[] totalBytes;
            if (replyMessage.Contains("Music.Play"))
            {
                totalBytes = ((MemoryStream)SampleMusic.GetStream()).ToArray();
                await handler.SendBinary(totalBytes);
            }
            else
            {
                try
                {
                    var    authorizationProvider = new CognitiveServicesAuthorizationProvider(CognitiveSubscriptionKey);
                    string accessToken           = await authorizationProvider.GetAuthorizationTokenAsync();

                    var cortana = new Synthesize();
                    totalBytes = await cortana.Speak(CancellationToken.None, new Synthesize.InputOptions()
                    {
                        RequestUri = new Uri(Constants.SpeechSynthesisUrl),
                        Text       = replyMessage,
                        VoiceType  = Gender.Female,
                        Locale     = "en-US",
                        VoiceName  = "Microsoft Server Speech Text to Speech Voice (en-US, ZiraRUS)",

                        // Service can return audio in different output format.
                        OutputFormat       = AudioOutputFormat.Riff16Khz16BitMonoPcm,
                        AuthorizationToken = "Bearer " + accessToken,
                    });

                    // convert the audio format and send back to client
                    WaveFormat   target    = new WaveFormat(8000, 16, 2);
                    MemoryStream outStream = new MemoryStream();
                    using (WaveFormatConversionStream conversionStream = new WaveFormatConversionStream(target, new WaveFileReader(new MemoryStream(totalBytes))))
                    {
                        WaveFileWriter.WriteWavFileToStream(outStream, conversionStream);
                        outStream.Position = 0;
                    }

                    handler.SendBinary(outStream.ToArray()).Wait();
                    outStream.Dispose();
                }
                catch (Exception e)
                {
                    throw new DemoBotServiceException($"Convert text to speech failed: {e.Message}");
                }
            }
        }