Exemplo n.º 1
0
 public static async Task SpeakOutLoud(string data)
 {
     try
     {
         speechSynthesizer.CancelAll();
         await speechSynthesizer.SpeakTextAsync(data);
     }
     catch { }
 }
Exemplo n.º 2
0
 static public void Stop()
 {
     if (_speech != null)
     {
         _speech.CancelAll();
     }
 }
Exemplo n.º 3
0
        private async void ReadItem_Click(object sender, EventArgs e)
        {
            if (this.apiManager.currentArticle != null)
            {
                if (is_speech)
                {
                    is_speech = false;
                    synth.CancelAll();
                    InitAppBar(this.apiManager.currentArticle.is_liked, false);
                    return;
                }
                if (!is_speech)
                {
                    HtmlAgilityPack.HtmlDocument html = new HtmlAgilityPack.HtmlDocument();
                    html.LoadHtml(this.apiManager.currentArticle.body);

                    is_speech = true;
                    InitAppBar(this.apiManager.currentArticle.is_liked, false);

                    foreach (var node in html.DocumentNode.ChildNodes)
                    {
                        await synth.SpeakTextAsync(node.InnerText);
                    }
                }
                is_speech = false;
                InitAppBar(this.apiManager.currentArticle.is_liked, false);
            }
        }
Exemplo n.º 4
0
        public async Task Speak(string text, CancellationToken cancelToken)
        {
            using (var synth = new SpeechSynthesizer()) {
                cancelToken.Register(() => {
                    if (this.IsSpeaking)
                    {
                        synth.CancelAll();
                    }
                });
                //this.SetVoice(synth, options);

                this.IsSpeaking = true;
                try {
                    // stop cancel exception
                    await synth.SpeakTextAsync(text);
                }
                catch { }
                this.IsSpeaking = false;
            }
        }
Exemplo n.º 5
0
        public async void speak(string argsJSON)
        {
            if (lastCallbackId != null)
            {
                DispatchCommandResult(new PluginResult(PluginResult.Status.OK), lastCallbackId);
                lastCallbackId = null;
                synth.CancelAll();
            }

            var args    = JsonHelper.Deserialize <string[]>(argsJSON);
            var options = JsonHelper.Deserialize <Options>(args[0]);

            lastCallbackId = args[1];

            var locale = options.locale != null ? options.locale : "en-US";
            var rate   = options.rate != null ? options.rate : 1.0;

            var ssml =
                @"<?xml version=""1.0""?>
<speak version=""1.0""
    xmlns=""http://www.w3.org/2001/10/synthesis""
    xml:lang=""" + locale + @""">
    <prosody pitch=""x-high"" rate=""" + rate + @""">" + xmlEncode(options.text) + @"</prosody>
</speak>";

            try {
                await synth.SpeakSsmlAsync(ssml);

                lastCallbackId = null;
                DispatchCommandResult(new PluginResult(PluginResult.Status.OK));
            } catch (OperationCanceledException) {
                // do nothing
            } catch (Exception e) {
                Debug.WriteLine(e.Message);
                lastCallbackId = null;
                DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e.Message));
            }
        }
Exemplo n.º 6
0
 public async void stop(string argsJSON)
 {
     synth.CancelAll();
 }
Exemplo n.º 7
0
        /// <summary>
        /// Speak back text
        /// </summary>
        /// <param name="text">Text to speak</param>
        /// <param name="queue">If you want to chain together speak command or cancel current</param>
        /// <param name="crossLocale">Locale of voice</param>
        /// <param name="pitch">Pitch of voice</param>
        /// <param name="speakRate">Speak Rate of voice (All) (0.0 - 2.0f)</param>
        /// <param name="volume">Volume of voice (iOS/WP) (0.0-1.0)</param>
        public async void Speak(string text, bool queue = false, CrossLocale?crossLocale = null, float?pitch = null, float?speakRate = null, float?volume = null)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                return;
            }

            if (speechSynthesizer == null)
            {
                Init();
            }

#if !NETFX_CORE
            if (!queue)
            {
                speechSynthesizer.CancelAll();
            }
#endif
            var localCode = string.Empty;

            //nothing fancy needed here
            if (pitch == null && speakRate == null && volume == null)
            {
                if (crossLocale.HasValue && !string.IsNullOrWhiteSpace(crossLocale.Value.Language))
                {
                    localCode = crossLocale.Value.Language;
#if NETFX_CORE
                    var voices = from voice in SpeechSynthesizer.AllVoices
                                 where (voice.Language == localCode &&
                                        voice.Gender.Equals(VoiceGender.Female))
                                 select voice;
                    speechSynthesizer.Voice = (voices.Any() ? voices.ElementAt(0) : SpeechSynthesizer.DefaultVoice);
#else
                    var voices = from voice in InstalledVoices.All
                                 where (voice.Language == localCode &&
                                        voice.Gender.Equals(VoiceGender.Female))
                                 select voice;
                    speechSynthesizer.SetVoice(voices.Any() ? voices.ElementAt(0) : InstalledVoices.Default);
#endif
                }
                else
                {
#if NETFX_CORE
                    speechSynthesizer.Voice = SpeechSynthesizer.DefaultVoice;
#else
                    speechSynthesizer.SetVoice(InstalledVoices.Default);
#endif
                }

#if NETFX_CORE
                try
                {
                    var stream = await speechSynthesizer.SynthesizeTextToStreamAsync(text);

                    element.SetSource(stream, stream.ContentType);
                    element.Play();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("Unable to playback stream: " + ex);
                }
#else
                speechSynthesizer.SpeakTextAsync(text);
#endif
                return;
            }

            if (crossLocale.HasValue && !string.IsNullOrWhiteSpace(crossLocale.Value.Language))
            {
                localCode = crossLocale.Value.Language;
#if NETFX_CORE
                var voices = from voice in SpeechSynthesizer.AllVoices
                             where (voice.Language == localCode &&
                                    voice.Gender.Equals(VoiceGender.Female))
                             select voice;
#else
                var voices = from voice in InstalledVoices.All
                             where (voice.Language == localCode &&
                                    voice.Gender.Equals(VoiceGender.Female))
                             select voice;
#endif
                if (!voices.Any())
                {
#if NETFX_CORE
                    localCode = SpeechSynthesizer.DefaultVoice.Language;
#else
                    localCode = InstalledVoices.Default.Language;
#endif
                }
            }
            else
            {
#if NETFX_CORE
                localCode = SpeechSynthesizer.DefaultVoice.Language;
#else
                localCode = InstalledVoices.Default.Language;
#endif
            }


            if (!volume.HasValue)
            {
                volume = 100.0f;
            }
            else if (volume.Value > 1.0f)
            {
                volume = 100.0f;
            }
            else if (volume.Value < 0.0f)
            {
                volume = 0.0f;
            }
            else
            {
                volume = volume.Value * 100.0f;
            }

            var pitchProsody = "default";
            //var test = "x-low", "low", "medium", "high", "x-high", or "default";
            if (!pitch.HasValue)
            {
                pitchProsody = "default";
            }
            else if (pitch.Value >= 1.6f)
            {
                pitchProsody = "x-high";
            }
            else if (pitch.Value >= 1.1f)
            {
                pitchProsody = "high";
            }
            else if (pitch.Value >= .9f)
            {
                pitchProsody = "medium";
            }
            else if (pitch.Value >= .4f)
            {
                pitchProsody = "low";
            }
            else
            {
                pitchProsody = "x-low";
            }


            string ssmlText = "<speak version=\"1.0\" ";
            ssmlText += "xmlns=\"http://www.w3.org/2001/10/synthesis\" xml:lang=\"" + localCode + "\">";
            ssmlText += "<prosody pitch=\"" + pitchProsody + "\" volume=\"" + volume.Value + "\" rate=\"" + speakRate.Value + "\" >" + text + "</prosody>";
            ssmlText += "</speak>";

#if NETFX_CORE
            try{
                var stream = await speechSynthesizer.SynthesizeSsmlToStreamAsync(ssmlText);

                element.SetSource(stream, stream.ContentType);
                element.Play();
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Unable to playback stream: " + ex);
            }
#else
            speechSynthesizer.SpeakSsmlAsync(ssmlText);
#endif
        }
 public void Stop()
 {
     synth.CancelAll();
 }
Exemplo n.º 9
0
 private void Appcanlbarread_Click(object sender, EventArgs e)
 {
     synth.CancelAll();
 }
Exemplo n.º 10
0
        public async void buscar_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                speechSynth.CancelAll();
                SpeechRecognizerUI speechRecognizer = new SpeechRecognizerUI();

                var spanishRecognizer = InstalledSpeechRecognizers.All.Where(sr => sr.Language == "es-ES").First();
                if (speechRecognizer != null)
                {
                    speechRecognizer.Recognizer.SetRecognizer(spanishRecognizer);
                }
                else
                {
                    MessageBox.Show("Recognizer with the language not found");
                }


                speechRecognizer.Recognizer.Settings.InitialSilenceTimeout = TimeSpan.FromSeconds(4.0);
                speechRecognizer.Recognizer.Settings.BabbleTimeout         = TimeSpan.FromSeconds(3.0);
                speechRecognizer.Settings.ExampleText = "308,201e,120... etc";
                speechRecognizer.Settings.ListenText  = "Diga el bus que desea buscar";

                await speechSynth.SpeakTextAsync("Después del tono, indique el recorrido que desea buscar");


                var result = await speechRecognizer.RecognizeWithUIAsync();


                if (result.RecognitionResult != null)
                {
                    string result2 = Convert.ToString(result.RecognitionResult.Text);
                    temp = result2.Replace(".", string.Empty);
                    string num2 = obj2.conversornum(temp);
                    //microbuscada = result.RecognitionResult.Text;
                    microbuscada = Convert.ToString(num2);

                    temp = microbuscada.Replace(" ", string.Empty).Replace(".", string.Empty);
                    conversor();

                    y1 = xy[1] - 500;
                    y2 = xy[1] + 500;
                    x1 = xy[0] - 500;
                    x2 = xy[0] + 500;

                    await speechSynth.SpeakTextAsync("Por favor espere mientras se envía la solicitud");


                    XDocument objXML = XDocument.Load("Data.xml");
                    this.people = (
                        from obj in objXML.Descendants("Paradero")
                        where obj.Element("Servicio").Value == temp && (float)obj.Element("Cory") >= y1 && (float)obj.Element("Cory") <= y2 && (float)obj.Element("Corx") >= x1 && (float)obj.Element("Corx") <= x2
                        select new Persona
                    {
                        Servicio = obj.Element("Servicio").Value,
                        NumParadero = obj.Element("NumParadero").Value,
                        Comuna = obj.Element("Comuna").Value,
                        Eje = obj.Element("Eje").Value,
                        Desde = obj.Element("Desde").Value,
                        Hacia = obj.Element("Hacia").Value,
                        Corx = obj.Element("Corx").Value,
                        Cory = obj.Element("Cory").Value
                    }).ToArray();

                    if (people.Length == 0)
                    {
                        await speechSynth.SpeakTextAsync("No se han encontrado paraderos cercanos a su posición. si desea volver a buscar, presione de nuevo en su pantalla");

                        isolated["coorypa"] = 0;
                        isolated["coorxpa"] = 0;
                        if (people == null)
                        {
                            MessageBox.Show("hola");
                        }
                    }
                    else
                    {
                        await speechSynth.SpeakTextAsync("Se han encontrado " + people.Length + " Paraderos cercanos a su posición. si desea buscar el más cercano presione en la esquina inferior izquierda de su pantalla.");
                    }
                }
                else
                {
                    await speechSynth.SpeakTextAsync(" Ahora puede volver a buscar apretando en su pantalla ");
                }
            }
            catch (System.Threading.Tasks.TaskCanceledException) { }
            catch
            {
                error2();
            }
        }
Exemplo n.º 11
0
        /// <summary>
        /// Speak back text
        /// </summary>
        /// <param name="text">Text to speak</param>
        /// <param name="crossLocale">Locale of voice</param>
        /// <param name="pitch">Pitch of voice</param>
        /// <param name="speakRate">Speak Rate of voice (All) (0.0 - 2.0f)</param>
        /// <param name="volume">Volume of voice (iOS/WP) (0.0-1.0)</param>
        /// <param name="cancelToken">Canelation token to stop speak</param>
        /// <exception cref="ArgumentNullException">Thrown if text is null</exception>
        /// <exception cref="ArgumentException">Thrown if text length is greater than maximum allowed</exception>
        public async Task Speak(string text, CrossLocale?crossLocale = null, float?pitch = null, float?speakRate = null, float?volume = null, CancellationToken?cancelToken = null)
        {
            if (text == null)
            {
                throw new ArgumentNullException(nameof(text), "Text can not be null");
            }

            try
            {
                await semaphore.WaitAsync(cancelToken ?? CancellationToken.None);

                var localCode = string.Empty;

                //nothing fancy needed here
                if (pitch == null && speakRate == null && volume == null)
                {
                    if (crossLocale.HasValue && !string.IsNullOrWhiteSpace(crossLocale.Value.Language))
                    {
                        localCode = crossLocale.Value.Language;
#if NETFX_CORE
                        var voices = from voice in SpeechSynthesizer.AllVoices
                                     where (voice.Language == localCode &&
                                            voice.Gender.Equals(VoiceGender.Female))
                                     select voice;
                        speechSynthesizer.Voice = (voices.Any() ? voices.ElementAt(0) : SpeechSynthesizer.DefaultVoice);
#else
                        var voices = from voice in InstalledVoices.All
                                     where (voice.Language == localCode &&
                                            voice.Gender.Equals(VoiceGender.Female))
                                     select voice;
                        speechSynthesizer.SetVoice(voices.Any() ? voices.ElementAt(0) : InstalledVoices.Default);
#endif
                    }
                    else
                    {
#if NETFX_CORE
                        speechSynthesizer.Voice = SpeechSynthesizer.DefaultVoice;
#else
                        speechSynthesizer.SetVoice(InstalledVoices.Default);
#endif
                    }
                }


                if (crossLocale.HasValue && !string.IsNullOrWhiteSpace(crossLocale.Value.Language))
                {
                    localCode = crossLocale.Value.Language;
#if NETFX_CORE
                    var voices = from voice in SpeechSynthesizer.AllVoices
                                 where (voice.Language == localCode &&
                                        voice.Gender.Equals(VoiceGender.Female))
                                 select voice;
#else
                    var voices = from voice in InstalledVoices.All
                                 where (voice.Language == localCode &&
                                        voice.Gender.Equals(VoiceGender.Female))
                                 select voice;
#endif
                    if (!voices.Any())
                    {
#if NETFX_CORE
                        localCode = SpeechSynthesizer.DefaultVoice.Language;
#else
                        localCode = InstalledVoices.Default.Language;
#endif
                    }
                }
                else
                {
#if NETFX_CORE
                    localCode = SpeechSynthesizer.DefaultVoice.Language;
#else
                    localCode = InstalledVoices.Default.Language;
#endif
                }


                if (!volume.HasValue)
                {
                    volume = 100.0f;
                }
                else if (volume.Value > 1.0f)
                {
                    volume = 100.0f;
                }
                else if (volume.Value < 0.0f)
                {
                    volume = 0.0f;
                }
                else
                {
                    volume = volume.Value * 100.0f;
                }

                var pitchProsody = "default";
                //var test = "x-low", "low", "medium", "high", "x-high", or "default";
                if (!pitch.HasValue)
                {
                    pitchProsody = "default";
                }
                else if (pitch.Value >= 1.6f)
                {
                    pitchProsody = "x-high";
                }
                else if (pitch.Value >= 1.1f)
                {
                    pitchProsody = "high";
                }
                else if (pitch.Value >= .9f)
                {
                    pitchProsody = "medium";
                }
                else if (pitch.Value >= .4f)
                {
                    pitchProsody = "low";
                }
                else
                {
                    pitchProsody = "x-low";
                }


                string ssmlText = "<speak version=\"1.0\" ";
                ssmlText += "xmlns=\"http://www.w3.org/2001/10/synthesis\" xml:lang=\"" + localCode + "\">";
                ssmlText += "<prosody pitch=\"" + pitchProsody + "\" volume=\"" + volume.Value + "\" rate=\"" +
                            speakRate ?? 1F + "\" >" + text + "</prosody>";
                ssmlText += "</speak>";

#if NETFX_CORE
                var tcs     = new TaskCompletionSource <object>();
                var handler = new TypedEventHandler <MediaPlayer, object>((sender, args) => tcs.TrySetResult(null));

                try
                {
                    var player = BackgroundMediaPlayer.Current;
                    var stream = await speechSynthesizer.SynthesizeTextToStreamAsync(text);

                    player.MediaEnded += handler;
                    player.SetStreamSource(stream);
                    player.Play();

                    cancelToken?.Register(() =>
                    {
                        player.PlaybackRate = 0;
                        tcs.TrySetResult(null);
                    });

                    await tcs.Task;
                    player.MediaEnded -= handler;
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("Unable to playback stream: " + ex);
                }
#else
                //cancelToken?.Register(() => speechSynthesizer.CancelAll());
                //await speechSynthesizer.SpeakTextAsync(text);

                cancelToken?.Register(() => speechSynthesizer.CancelAll());

                await speechSynthesizer.SpeakSsmlAsync(ssmlText);
#endif
            }
            finally
            {
                semaphore.Release();
            }
        }