Exemple #1
0
        private void buttonOK_Click(object sender, EventArgs e)
        {
            currentVoiceIndex = comboBoxVoice.SelectedIndex;
            if (currentVoiceIndex < 0)
            {
                currentVoiceIndex = 0;
            }

            if (tokens.Count <= 0)
            {
                return;
            }

            spVoice.SetVoice((ISpObjectToken)tokens.Item(currentVoiceIndex));
            spMemoryStream = new SpMemoryStream();
            SpAudioFormat spAudioFormat = new SpAudioFormat();

            spAudioFormat.Type        = waveType;
            spMemoryStream.Format     = spAudioFormat;
            spVoice.AudioOutputStream = spMemoryStream;

            spVoice.EndStream += new _ISpeechVoiceEvents_EndStreamEventHandler(SpeechDone);
            spVoice.Viseme    += new _ISpeechVoiceEvents_VisemeEventHandler(voice_Viseme);
            spVoice.Speak(textBoxSpeak.Text, SpeechVoiceSpeakFlags.SVSFlagsAsync);
        }
Exemple #2
0
        public static byte[] GetSound(string text)
        {
            const SpeechVoiceSpeakFlags speechFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
            var synth  = new SpVoice();
            var wave   = new SpMemoryStream();
            var voices = synth.GetVoices();

            try
            {
                // synth setup
                synth.Volume = 100;
                synth.Rate   = -3;
                foreach (SpObjectToken voice in voices)
                {
                    if (voice.GetAttribute("Name") == "Microsoft Matej")
                    {
                        synth.Voice = voice;
                        break;
                    }
                }

                wave.Format.Type        = SpeechAudioFormatType.SAFT22kHz16BitMono;
                synth.AudioOutputStream = wave;
                synth.Speak(text, speechFlags);
                synth.WaitUntilDone(Timeout.Infinite);

                var waveFormat = new WaveFormat(22050, 16, 1);
                using (var ms = new MemoryStream((byte[])wave.GetData()))
                    using (var reader = new RawSourceWaveStream(ms, waveFormat))
                        using (var outStream = new MemoryStream())
                            using (var writer = new WaveFileWriter(outStream, waveFormat))
                            {
                                reader.CopyTo(writer);
                                //return o.Mp3 ? ConvertToMp3(outStream) : outStream.GetBuffer();

                                //var bytes = ConvertToMp3(outStream);
                                File.WriteAllBytes("speak.wav", outStream.GetBuffer());

                                return(null);
                            }
            }
            finally
            {
                Marshal.ReleaseComObject(voices);
                Marshal.ReleaseComObject(wave);
                Marshal.ReleaseComObject(synth);
            }
        }
        private void buttonOK_Click(object sender, EventArgs e)
        {
            tokens = spVoice.GetVoices("", "");

            spVoice.SetVoice((ISpObjectToken)tokens.Item(0));
            spMemoryStream = new SpMemoryStream();
            SpAudioFormat spAudioFormat = new SpAudioFormat();

            spAudioFormat.Type        = waveType;
            spMemoryStream.Format     = spAudioFormat;
            spVoice.AudioOutputStream = spMemoryStream;

            spVoice.EndStream += new _ISpeechVoiceEvents_EndStreamEventHandler(SpeechDone);
            spVoice.Viseme    += new _ISpeechVoiceEvents_VisemeEventHandler(voice_Viseme);
            spVoice.Speak(textBoxSpeak.Text, SpeechVoiceSpeakFlags.SVSFlagsAsync);
        }
        private void OutputWaveStream(SpMemoryStream waveStream)
        {
            using var sourceStream = new MemoryStream((byte[])waveStream.GetData());
            using var output       = new WasapiOut(audioConfiguration.SelectedTTSDevice, AudioClientShareMode.Shared, true, 100);
            using var provider     = new RawSourceWaveStream(sourceStream, new WaveFormat(44100, BitResolution, 2));

            sourceStream.Seek(0, SeekOrigin.Begin);
            output.Volume = 1f;
            output.Init(provider);
            output.Play();

            while (output.PlaybackState == PlaybackState.Playing)
            {
                Thread.Sleep(10);
            }
        }
        private void SpeakToOutputDevice(string message, string language)
        {
            const SpeechVoiceSpeakFlags speechFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
            SpVoice             synth  = new SpVoice();
            SpMemoryStream      wave   = new SpMemoryStream();
            ISpeechObjectTokens voices = synth.GetVoices();

            try
            {
                synth.Rate   = 0;
                synth.Volume = 100;

                wave.Format.Type        = SpeechAudioFormatType.SAFT44kHz16BitStereo;
                synth.AudioOutputStream = wave;

                if (language != null)
                {
                    foreach (SpObjectToken voice in voices)
                    {
                        if (voice.GetAttribute("Name") == language)
                        {
                            synth.Voice = voice;
                        }
                    }
                }

                synth.Speak(message, speechFlags);
                synth.WaitUntilDone(Timeout.Infinite);

                OutputWaveStream(wave);
            }
            finally
            {
                Marshal.ReleaseComObject(voices);
                Marshal.ReleaseComObject(wave);
                Marshal.ReleaseComObject(synth);
            }
        }
        private void buttonOK_Click(object sender, EventArgs e)
        {
            currentVoiceIndex = comboBoxVoice.SelectedIndex;
            if (currentVoiceIndex < 0) currentVoiceIndex = 0;

            if (tokens.Count <= 0) return;

            spVoice.SetVoice((ISpObjectToken)tokens.Item(currentVoiceIndex));
            spMemoryStream = new SpMemoryStream();
            SpAudioFormat spAudioFormat = new SpAudioFormat();
            spAudioFormat.Type = waveType;
            spMemoryStream.Format = spAudioFormat;
            spVoice.AudioOutputStream = spMemoryStream;

            spVoice.EndStream += new _ISpeechVoiceEvents_EndStreamEventHandler(SpeechDone);
            spVoice.Viseme += new _ISpeechVoiceEvents_VisemeEventHandler(voice_Viseme);
            spVoice.Speak(textBoxSpeak.Text, SpeechVoiceSpeakFlags.SVSFlagsAsync);
        }
 private bool TryGetBytes(string textLine, out byte[] data)
 {
     try
     {
         var spFileStream = new SpMemoryStream();
         Voice.AudioOutputStream = spFileStream;
         Voice.Speak(textLine);
         Voice.WaitUntilDone(Timeout.Infinite);
         data = spFileStream.GetData() as byte[];
         return true;
     }
     catch (Exception ex)
     {
         Log.Error(string.Format("{0}: \r\n {1}", ex, textLine));
     }
     data = null;
     return false;
 }