Ejemplo n.º 1
0
        public BotSpeechClient(SynthesisProperties properties)
        {
            bingSpeech = new BingSpeech();
            bingSpeech.OnAudioAvailable += Cortana_OnAudioAvailable;
            bingSpeech.OnError          += Cortana_OnError;
            Properties = properties;

            Validate(properties);
        }
Ejemplo n.º 2
0
 private void Validate(SynthesisProperties properties)
 {
     if (!string.IsNullOrEmpty(properties.VoiceName))
     {
         if (!properties.VoiceName.Contains(properties.Locale))
         {
             throw new Exception("Voicename has different Locale than the speech client");
         }
     }
 }
        /// <summary>
        /// Sends the specified text to be spoken to the TTS service and saves the response audio to a file.
        /// </summary>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>A Task</returns>
        internal Task Speak(Activity activity, CancellationToken cancellationToken, SynthesisProperties inputOptions)
        {
            var activityConverter = new ActivityConverter(inputOptions);

            client.DefaultRequestHeaders.Clear();
            foreach (var header in inputOptions.GetHeaders())
            {
                client.DefaultRequestHeaders.TryAddWithoutValidation(header.Key, header.Value);
            }

            var genderValue = inputOptions.Gender.ToString();

            var request = new HttpRequestMessage(HttpMethod.Post, new Uri(inputOptions.SpeechUri))
            {
                Content = new StringContent(activityConverter.ToSsml(activity))
            };

            var httpTask = client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken);

            Console.WriteLine("Response status code: [{0}]", httpTask.Result.StatusCode);

            var saveTask = httpTask.ContinueWith(
                async(responseMessage, token) =>
            {
                try
                {
                    if (responseMessage.IsCompleted && responseMessage.Result != null && responseMessage.Result.IsSuccessStatusCode)
                    {
                        var httpStream = await responseMessage.Result.Content.ReadAsStreamAsync().ConfigureAwait(false);
                        AudioAvailable(new DataEventArgs <Stream>(httpStream));
                    }
                    else
                    {
                        Error(new DataEventArgs <Exception>(new Exception(String.Format("Service returned {0}", responseMessage.Result.StatusCode))));
                    }
                }
                catch (Exception e)
                {
                    Error(new DataEventArgs <Exception>(e.GetBaseException()));
                }
                finally
                {
                    responseMessage.Dispose();
                    request.Dispose();
                }
            },
                TaskContinuationOptions.AttachedToParent,
                cancellationToken);

            return(saveTask);
        }
 public ActivityConverter(SynthesisProperties properties)
 {
     this.properties = properties;
 }