Esempio n. 1
0
        public async Task <byte[]> SynthesizeSpeechAsync(string message)
        {
            var request = new HttpRequestMessage(HttpMethod.Post, _speechEndpoint)
            {
                Content = new StringContent(GenerateSsml(message), Encoding.UTF8, "application/ssml+xml"),
            };

            var autToken = await AzureAuthenticationService.GetAccessToken(_subscriptionKey, _region);

            request.Headers.Add("Authorization", autToken);

            var responseMessage = await Client.SendAsync(request, HttpCompletionOption.ResponseContentRead, CancellationToken.None);

            responseMessage.EnsureSuccessStatusCode();
            var httpStream = await responseMessage.Content.ReadAsStreamAsync();

            // Convert stream to byte array
            using (var memoryStream = new MemoryStream())
            {
                await httpStream.CopyToAsync(memoryStream);

                return(memoryStream.ToArray());
            }
        }
Esempio n. 2
0
        private async Task ConnectAsync(SpeechClientOptions options)
        {
            if (s2smtClient != null && s2smtClient.IsConnected())
            {
                return;
            }

            if (options.GetType() != typeof(SpeechTranslateClientOptions))
            {
                throw new InvalidOperationException("Type of SpeechClientOptions is not supported.");
            }
            options.AuthHeaderValue = await AzureAuthenticationService.GetAccessToken(subscriptionKey);

            // Create the client
            s2smtClient = new SpeechClient((SpeechTranslateClientOptions)options, CancellationToken.None);
            TextMessageDecoder textDecoder = TextMessageDecoder.CreateTranslateDecoder();

            s2smtClient.OnBinaryData      += (c, a) => { AddSamplesToStream(a); };
            s2smtClient.OnEndOfBinaryData += (c, a) => { AddSamplesToStream(a); };
            s2smtClient.OnTextData        += (c, a) => { textDecoder.AppendData(a); lastReceivedPacketTick = DateTime.Now.Ticks; };
            s2smtClient.OnEndOfTextData   += (c, a) =>
            {
                textDecoder.AppendData(a);
                lastReceivedPacketTick = DateTime.Now.Ticks;
                textDecoder
                .Decode()
                .ContinueWith(t =>
                {
                    if (t.IsFaulted)
                    {
                        Trace.TraceError("Failed to decode incoming text message: {0}", t.Exception);
                    }
                    else
                    {
                        object msg = t.Result;
                        TranscriptUtterance utterance = null;
                        if (msg.GetType() == typeof(FinalResultMessage))
                        {
                            var final     = msg as FinalResultMessage;
                            long offset   = long.Parse(final.AudioTimeOffset);
                            long duration = long.Parse(final.AudioTimeSize);
                            TimeSpan currFileStartTime = TimeSpan.FromTicks(offset - currentFileStartTicks);
                            TimeSpan currFileEndime    = TimeSpan.FromTicks(currFileStartTime.Ticks + duration);
                            Trace.TraceInformation("Final recognition {0} ({1} - {2}): {3}", final.Id, currFileStartTime.ToString(), currFileEndime.ToString(), final.Recognition);
                            Trace.TraceInformation("Final translation {0}: {1}", final.Id, final.Translation);
                            utterance             = new TranscriptUtterance();
                            utterance.Recognition = final.Recognition;
                            utterance.Translation = final.Translation;
                        }
                        if (msg.GetType() == typeof(PartialResultMessage))
                        {
                            // Partial results are not used in this lab, leaving code as a reference
                            var partial = msg as PartialResultMessage;
                            Trace.TraceInformation("Partial recognition {0}: {1}", partial.Id, partial.Recognition);
                            Trace.TraceInformation("Partial translation {0}: {1}", partial.Id, partial.Translation);
                            utterance             = new TranscriptUtterance();
                            utterance.Recognition = partial.Recognition;
                            utterance.Translation = partial.Translation;
                        }

                        if (utterance != null)
                        {
                            Transcripts.Add(utterance);
                        }
                    }
                });
            };
            s2smtClient.Failed += (c, ex) =>
            {
                Trace.TraceError("SpeechTranslation client reported an error: {0}", ex);
            };
            s2smtClient.Disconnected += (c, ea) =>
            {
                Trace.TraceInformation("Connection has been lost.");
                Trace.TraceInformation($"Errors (if any): \n{string.Join("\n", s2smtClient.Errors)}");
            };

            await s2smtClient.Connect();
        }