예제 #1
1
        private static byte[] StartSpeak(string word)
        {
            var ms = new MemoryStream();
            using (System.Speech.Synthesis.SpeechSynthesizer synhesizer = new System.Speech.Synthesis.SpeechSynthesizer())
            {
                foreach (var voice in synhesizer.GetInstalledVoices())
                {
                    Console.WriteLine("select(y/n): " + voice.VoiceInfo.Name);
                    var key = Console.ReadKey();
                    if (key.Key == ConsoleKey.Y)
                    {
                        synhesizer.SelectVoice(voice.VoiceInfo.Name);
                        synhesizer.SelectVoiceByHints(voice.VoiceInfo.Gender, voice.VoiceInfo.Age, 1, voice.VoiceInfo.Culture);
                        synhesizer.SetOutputToWaveStream(ms);
                        synhesizer.Speak(word);
                    }
                }
            }

            return ms.ToArray();
        }
예제 #2
0
 public byte[] SpeakContentStream(string content)
 {
     byte[] buffer = CacheHelper.Get(content) as byte[];
     if (buffer != null)
     {
         return(buffer);
     }
     else
     {
         using (System.Speech.Synthesis.SpeechSynthesizer speechSyn = new System.Speech.Synthesis.SpeechSynthesizer())
         {
             speechSyn.Volume = 100;
             speechSyn.Rate   = 0;
             using (var ms = new System.IO.MemoryStream())
             {
                 speechSyn.SetOutputToWaveStream(ms);
                 speechSyn.Speak(content);
                 speechSyn.SetOutputToNull();
                 buffer = ms.ToArray();
                 //设置平滑过期
                 CacheHelper.Set(content, buffer, TimeSpan.FromSeconds(30));
                 return(buffer);
             }
         }
     }
 }
        public System.IO.MemoryStream Speak(string phrase, string culture, string voice, int volume, int rate)
        {
            try
            {                                                   // paranoia here..
                System.Speech.Synthesis.PromptBuilder pb;

                if (culture.Equals("Default"))
                {
                    pb = new System.Speech.Synthesis.PromptBuilder();
                }
                else
                {
                    pb = new System.Speech.Synthesis.PromptBuilder(new System.Globalization.CultureInfo(culture));
                }

                if (voice.Equals("Female", StringComparison.InvariantCultureIgnoreCase))
                {
                    pb.StartVoice(System.Speech.Synthesis.VoiceGender.Female);
                }
                else if (voice.Equals("Male", StringComparison.InvariantCultureIgnoreCase))
                {
                    pb.StartVoice(System.Speech.Synthesis.VoiceGender.Male);
                }
                else if (!voice.Equals("Default", StringComparison.InvariantCultureIgnoreCase))
                {
                    pb.StartVoice(voice);
                }
                else
                {
                    pb.StartVoice(systemdefaultvoice);
                }

                synth.Volume = volume;
                synth.Rate   = rate;

                //System.Diagnostics.Debug.WriteLine((Environment.TickCount % 10000).ToString("00000") + " Speak " + phrase + ", Rate " + rate + " culture " + culture);

                System.IO.MemoryStream stream = new System.IO.MemoryStream();
                synth.SetOutputToWaveStream(stream);

                pb.AppendText(phrase);

                pb.EndVoice();

                synth.Speak(pb);

                //System.Diagnostics.Debug.WriteLine("Speech " + stream.Length);
                return(stream);
            }
            catch
            {
                return(null);
            }
        }
예제 #4
0
        public override void Speak(string text, bool gender, int ipitch)
        {
            var Synth = new System.Speech.Synthesis.SpeechSynthesizer();

            Synth.SelectVoiceByHints((gender) ? System.Speech.Synthesis.VoiceGender.Female : System.Speech.Synthesis.VoiceGender.Male);
            if (text == "")
            {
                return;
            }
            var voci   = Synth.GetInstalledVoices();
            var stream = new System.IO.MemoryStream();
            var pitch  = Math.Max(0.1f, ipitch / 100f + 1f); //below 0.1 is just stupid, so just clamp there.

            if (pitch < 1f)
            {
                Synth.Rate = 10 - (int)(pitch * 10);
            }
            else
            {
                Synth.Rate = (int)(10 / pitch) - 10;
            }
            Synth.SetOutputToWaveStream(stream);
            Synth.SpeakAsync(text);

            EventHandler <System.Speech.Synthesis.SpeakCompletedEventArgs> OnComplete = null;

            OnComplete = (obj, evt) =>
            {
                GameThread.NextUpdate((u) =>
                {
                    stream.Seek(0, System.IO.SeekOrigin.Begin);
                    var sfx    = SoundEffect.FromStream(stream);
                    var inst   = sfx.CreateInstance();
                    inst.Pitch = pitch - 1f;
                    inst.Play();

                    GameThreadInterval interval = null;
                    interval = GameThread.SetInterval(() =>
                    {
                        if (inst.State == SoundState.Stopped)
                        {
                            sfx.Dispose(); //just catch and dispose these when appropriate
                            interval.Clear();
                        }
                    }, 1000);
                    Synth.Dispose();
                });
                Synth.SpeakCompleted -= OnComplete;
            };

            Synth.SpeakCompleted += OnComplete;
        }
        public System.IO.MemoryStream Speak(string phrase, string culture, string voice, int volume, int rate)
        {
            try
            {                                                   // paranoia here..
                System.Speech.Synthesis.PromptBuilder pb;

                if (culture.Equals("Default"))
                {
                    pb = new System.Speech.Synthesis.PromptBuilder();
                }
                else
                {
                    try
                    {
                        pb = new System.Speech.Synthesis.PromptBuilder(new System.Globalization.CultureInfo(culture)); // may except if crap culture for machine
                    }
                    catch
                    {
                        pb = new System.Speech.Synthesis.PromptBuilder();
                    }
                }


                if (voice.Equals("Female", StringComparison.InvariantCultureIgnoreCase))
                {
                    pb.StartVoice(System.Speech.Synthesis.VoiceGender.Female);
                }
                else if (voice.Equals("Male", StringComparison.InvariantCultureIgnoreCase))
                {
                    pb.StartVoice(System.Speech.Synthesis.VoiceGender.Male);
                }
                else if (!voice.Equals("Default", StringComparison.InvariantCultureIgnoreCase))
                {
                    pb.StartVoice(voice);
                }
                else
                {
                    pb.StartVoice(systemdefaultvoice);
                }

                synth.Volume = volume;
                synth.Rate   = rate;

                //System.Diagnostics.Debug.WriteLine((Environment.TickCount % 10000).ToString("00000") + " Speak " + phrase + ", Rate " + rate + " culture " + culture);

                System.IO.MemoryStream stream = new System.IO.MemoryStream();
                synth.SetOutputToWaveStream(stream);

                string[] ssmlstart = new string[] { "<say-as ", "<emphasis", "<phoneme", "<sub", "<prosody", "<break", "<voice", "<lexicon" };
                string[] ssmlend   = new string[] { "</say-as>", "</emphasis>", "</phoneme>", "</sub>", "</prosody>", "/>", "</voice>", "/>" };

                phrase = phrase.Trim();

                while (phrase.Length > 0)
                {
                    int ssmlindex;
                    int foundpos = phrase.IndexOf(ssmlstart, out ssmlindex); // find one of the ssml phrases
                    if (foundpos == -1)                                      // no more, task on rest as normal text
                    {
                        pb.AppendText(phrase);
                        break;
                    }
                    else
                    {
                        if (foundpos > 0)
                        {
                            pb.AppendText(phrase.Substring(0, foundpos));       // tack on front
                            phrase = phrase.Substring(foundpos);
                        }

                        int indexofend = phrase.IndexOf(ssmlend[ssmlindex]);

                        if (indexofend == -1) // allowed as a shortcut to drop the last one
                        {
                            indexofend = phrase.Length;
                            phrase    += ssmlend[ssmlindex];
                        }

                        indexofend += ssmlend[ssmlindex].Length; // move to end of it

                        string ssmlcmd = phrase.Substring(0, indexofend).Replace('\'', '"');
                        //System.Diagnostics.Debug.WriteLine("SSML " + ssmlcmd);

                        try
                        {
                            pb.AppendSsmlMarkup(ssmlcmd);
                        }
                        catch       // bad markup
                        {
                            pb.AppendText("Bad SSML Markup when added, contact developers");
                        }

                        phrase = phrase.Substring(indexofend).Trim();
                    }
                }

                pb.EndVoice();

                try
                {
                    synth.Speak(pb);
                }
                catch
                {
                    synth.Speak("Bad SSML Markup in phrase, your chosen voice may not support all options. See voice configuration menu to disable SSML");
                }

                //System.Diagnostics.Debug.WriteLine("Speech " + stream.Length);
                return(stream);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Exception " + ex.ToString());
                return(null);
            }
        }