private static byte[] CreateNewMemoryStream(TTSData ttsData)
        {
            using (var memstream = new MemoryStream()) {
                using (var synthesizer = new SpeechSynthesizer()) {
                    synthesizer.SetOutputToWaveStream(memstream);
                    synthesizer.Volume = ttsData.Volume;
                    synthesizer.Rate   = ttsData.Rate;

                    synthesizer.Speak(ttsData.Text);
                }

                memstream.Seek(0, SeekOrigin.Begin);
                return(memstream.ToArray());
            }
        }
Example #2
0
        public static void Speak(string tts, int volume, int rate)
        {
            var ttsData = new TTSData
            {
                Text   = tts,
                Volume = volume,
                Rate   = rate
            };
            var disposable = new Disposable
            {
                MemoryStream   = GetMemoryStream(ttsData),
                DirectSoundOut = (Common.Constants.DefaultAudioDevice == Guid.Empty) ? new DirectSoundOut(Latency) : new DirectSoundOut(Common.Constants.DefaultAudioDevice, Latency)
            };

            disposable.WaveFileReader = new WaveFileReader(disposable.MemoryStream);
            disposable.WaveChannel    = new WaveChannel32(disposable.WaveFileReader);
            disposable.DirectSoundOut.Init(disposable.WaveChannel);
            disposable.DirectSoundOut.PlaybackStopped += disposable.DisposeDirectSound;
            disposable.DirectSoundOut.Play();
        }
Example #3
0
        private static MemoryStream GetMemoryStream(TTSData ttsData)
        {
            var ttsBytes = Cached.GetOrAdd(ttsData, t => CreateNewMemoryStream(ttsData));

            return(new MemoryStream(ttsBytes));
        }