private void SpeakMessage(TextToSpeechMessage message)
        {
            var enabled = GlobalSettingsStore.Instance.ProfileSettingsStore.GetClientSettingBool(ProfileSettingsKeys.EnableTextToSpeech);

            if (!enabled)
            {
                return;
            }

            Task task = new Task(() =>
            {
                try
                {
                    using (var synth = new SpeechSynthesizer())
                        using (var stream = new MemoryStream())
                        {
                            synth.SelectVoiceByHints(VoiceGender.Female, VoiceAge.Adult, 0, new CultureInfo("en-GB", false));
                            synth.Rate = 1;

                            var vol = double.Parse(GlobalSettingsStore.Instance.ProfileSettingsStore.GetClientSetting(ProfileSettingsKeys.TextToSpeechVolume).RawValue, CultureInfo.InvariantCulture);

                            var intVol = (int)(vol * 100.0);

                            if (intVol > 100)
                            {
                                intVol = 100;
                            }

                            synth.Volume = intVol;
                            if (mixer.WaveFormat.Channels == 2)
                            {
                                synth.SetOutputToAudioStream(stream,
                                                             new SpeechAudioFormatInfo(mixer.WaveFormat.SampleRate, AudioBitsPerSample.Sixteen, AudioChannel.Stereo));
                            }
                            else
                            {
                                synth.SetOutputToAudioStream(stream,
                                                             new SpeechAudioFormatInfo(mixer.WaveFormat.SampleRate, AudioBitsPerSample.Sixteen, AudioChannel.Mono));
                            }
                            synth.Speak(message.Message);

                            //clear current message
                            buffer.ClearBuffer();
                            byte[] byteArr = stream.ToArray();
                            buffer.AddSamples(byteArr, 0, byteArr.Length);
                        }
                }
                catch (Exception ex)
                {
                    Logger.Error(ex, "Error with Text to Speech");
                }
            });

            task.Start();
        }
    public SpeechAnimator(ChannelSystem channelSystem, BoneSystem boneSystem)
    {
        this.channelSystem = channelSystem;
        this.boneSystem    = boneSystem;

        this.synth   = new SpeechSynthesizer();
        synth.Volume = 100;

        synth.SelectVoice(Voice);
        synth.VisemeReached += Synth_VisemeReached;

        visemeChannels = VisemeChannelMap
                         .Select(name => name == null ? null : channelSystem.ChannelsByName[name + "?value"])
                         .ToArray();

        headBone = boneSystem.BonesByName["head"];

        var audioDevice = new XAudio2(XAudio2Flags.DebugEngine, ProcessorSpecifier.AnyProcessor);

        masteringVoice = new MasteringVoice(audioDevice);

        WaveFormat monoWaveFormat = WaveFormat.CreateIeeeFloatWaveFormat(44100, 2);

        sourceVoice = new SourceVoice(audioDevice, monoWaveFormat, VoiceFlags.None);
        sourceVoice.SetVolume(1);
        sourceVoice.Start();

        phononStream = new PhononSourceVoiceStream(sourceVoice);
        synth.SetOutputToAudioStream(phononStream, new SpeechAudioFormatInfo(44100, AudioBitsPerSample.Sixteen, AudioChannel.Mono));
    }
        private void speak(string text)
        {
            SpeechSynthesizer tts = new SpeechSynthesizer();

            tts.SelectVoice((string)cmbTTS.SelectedItem);
            tts.Volume = sliderTTSVol.Value * 5;
            tts.Rate   = sliderTTSSpeed.Value - 10;
            MemoryStream ms = new MemoryStream();

            tts.SetOutputToAudioStream(ms, formatInfo);
            if (voiceStreams.Count == 0)
            {
                initVoiceStreams();
            }
            var toDiscordStream = safeGetVoiceStream();

            tts.SpeakAsync(text);

            tts.SpeakCompleted += async(a, b) => {
                activeStreams.Add(toDiscordStream);
                ms.Seek(0, SeekOrigin.Begin);
                ms.CopyTo(toDiscordStream);
                await toDiscordStream.FlushAsync();

                activeStreams.Remove(toDiscordStream);
            };
        }
Example #4
0
        public void SpeakMessage(AudioVideoFlow flow, string message)
        {
            try
            {
                SpeechSynthesizer synth = new SpeechSynthesizer();
                SpeechAudioFormatInfo formatInfo = new SpeechAudioFormatInfo(16000, AudioBitsPerSample.Sixteen, Microsoft.Speech.AudioFormat.AudioChannel.Mono);
                SpeechSynthesisConnector connector = new SpeechSynthesisConnector();

                synth.SetOutputToAudioStream(connector.Stream, formatInfo);

                connector.AttachFlow(flow);
                connector.Start();

                synth.SpeakCompleted += new EventHandler<SpeakCompletedEventArgs>(
                    (sender, args) =>
                    {
                        connector.Stop();
                        synth.Dispose();
                    });

                synth.SpeakAsync(message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed to play the message. {0}", ex);
            }

        }
Example #5
0
        private void speak(string text)
        {
            var delay = (1) * text.Split().Length;

            while (DateTime.Now - whenSpeakRan < TimeSpan.FromSeconds(delay))
            {
                Thread.Sleep(125);
            }

            SpeechSynthesizer tts = new SpeechSynthesizer();

            tts.SelectVoice(( string )cmbTTS.SelectedItem);
            tts.Volume = sliderTTSVol.Value * 5;
            tts.Rate   = sliderTTSSpeed.Value - 10;
            MemoryStream ms = new MemoryStream();

            tts.SetOutputToAudioStream(ms, formatInfo);
            if (voiceStream == null)
            {
                voiceStream = audioClient.CreatePCMStream(AudioApplication.Voice, 128 * 1024);
            }
            tts.SpeakAsync(text);
            tts.SpeakCompleted += (a, b) => {
                ms.Seek(0, SeekOrigin.Begin);
                ms.CopyTo(voiceStream);
                voiceStream.Flush();
            };
            whenSpeakRan = DateTime.Now;
        }
Example #6
0
 /// <summary>
 /// Convert XML to WAV bytes. WAV won't have the header, so you have to add it separately.
 /// </summary>
 static byte[] ConvertSsmlXmlToWav(string voiceId, string xml, WaveFormat format)
 {
     using (var ms = new MemoryStream())
     {
         using (var synthesizer = new SpeechSynthesizer())
         {
             //var format = new SpeechAudioFormatInfo(
             if (format != null)
             {
                 //var bps = format.BitsPerSample == 8 ? AudioBitsPerSample.Eight : AudioBitsPerSample.Sixteen;
                 var blockAlignment         = format.BitsPerSample / 8 * format.Channels;
                 var averagerBytesPerSecond = format.SampleRate * format.BitsPerSample / 8 * format.Channels;
                 var formatInfo             = new SpeechAudioFormatInfo(EncodingFormat.Pcm, format.SampleRate, format.BitsPerSample, format.Channels, averagerBytesPerSecond, blockAlignment, new byte[0]);
                 // Returns WAV data only.
                 synthesizer.SetOutputToAudioStream(ms, formatInfo);
             }
             try
             {
                 var voice = synthesizer.GetInstalledVoices().Cast <InstalledVoice>().FirstOrDefault(x => x.VoiceInfo.Id == voiceId);
                 synthesizer.SelectVoice(voice.VoiceInfo.Name);
                 synthesizer.SpeakSsml(xml);
                 return(ms.ToArray());
             }
             catch (Exception ex)
             {
                 ex.Data.Add("Voice", "voiceName");
                 OnEvent(Exception, ex);
             }
         }
     }
     return(null);
 }
Example #7
0
        private byte[] TextToSpeech()
        {
            try
            {
                using (var synth = new SpeechSynthesizer())
                    using (var stream = new MemoryStream())
                    {
                        synth.SelectVoiceByHints(VoiceGender.Female, VoiceAge.Adult, 0, new CultureInfo("en-GB", false));
                        synth.Rate = 1;

                        var intVol = (int)(volume * 100.0);

                        if (intVol > 100)
                        {
                            intVol = 100;
                        }

                        synth.Volume = intVol;

                        synth.SetOutputToAudioStream(stream,
                                                     new SpeechAudioFormatInfo(INPUT_SAMPLE_RATE, AudioBitsPerSample.Sixteen, AudioChannel.Mono));

                        synth.Speak(path);

                        return(stream.ToArray());
                    }
            }
            catch (Exception ex)
            {
                Logger.Error(ex, "Error with Text to Speech");
            }
            return(new byte[0]);
        }
Example #8
0
        public void Run()
        {
            // Create AudioVideoFlow
            AudioVideoFlowHelper audioVideoFlowHelper = new AudioVideoFlowHelper();

            _audioVideoFlow = audioVideoFlowHelper.CreateAudioVideoFlow(
                null,
                audioVideoFlow_StateChanged);

            // Create a speech synthesis connector and attach it to an AudioVideoFlow
            SpeechSynthesisConnector speechSynthesisConnector = new SpeechSynthesisConnector();

            speechSynthesisConnector.AttachFlow(_audioVideoFlow);

            // Create a speech synthesis and set connector to it
            SpeechSynthesizer     speechSynthesis = new SpeechSynthesizer();
            SpeechAudioFormatInfo audioformat     = new SpeechAudioFormatInfo(16000, AudioBitsPerSample.Sixteen, Microsoft.Speech.AudioFormat.AudioChannel.Mono);

            speechSynthesis.SetOutputToAudioStream(speechSynthesisConnector, audioformat);

            //Load readme file as the source
            Console.WriteLine();
            Console.Write("Please enter the source file => ");
            string filename = Console.ReadLine();

            string msg = "";

            try
            {
                StreamReader objReader = new StreamReader(filename);
                msg = objReader.ReadToEnd();
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine("\r\nFile doesn't exist.");
                ShutdownPlatform();
            }

            //Start connector
            speechSynthesisConnector.Start();
            Console.WriteLine("\r\nStreaming source file for 15 seconds.");

            //Start streaming from speech synthesis.
            speechSynthesis.SpeakAsync(new Prompt(msg));

            //Allow the connector to stream 15 seconds by waiting for 15 seconds
            Thread.Sleep(15000);

            //Stop the connector
            speechSynthesisConnector.Stop();
            Console.WriteLine("\r\nSpeech synthesis connector stopped.");

            //speech synthesis connector must be detached from the flow, otherwise if the connector is rooted, it will keep the flow in memory.
            speechSynthesisConnector.DetachFlow();

            // Shutdown the platform
            ShutdownPlatform();

            _waitForShutdownEventCompleted.WaitOne();
        }
        public void SynthesizeText(string text,
                                   List <AudioMediaBuffer> audioMediaBuffers,
                                   List <VideoMediaBuffer> videoMediaBuffers)
        {
            // stream for the output audio
            var audioStream = new MemoryStream();

            // set the synthesizer output to the stream, make sure the output format is matching the audio socket settings
            _synth.SetOutputToAudioStream(audioStream,
                                          new SpeechAudioFormatInfo(samplesPerSecond: 16000, bitsPerSample: AudioBitsPerSample.Sixteen,
                                                                    channel: AudioChannel.Mono));

            // observe the synthesizer to generate the visemes timeline
            VisemesTimeline timeline = new VisemesTimeline();

            _synth.VisemeReached += (sender, visemeReachedEventArgs) =>
            {
                timeline.Add(visemeReachedEventArgs.Viseme, visemeReachedEventArgs.Duration.Milliseconds);
            };

            // synthesize the text -> audio and visemes are generated
            _synth.Speak(text);

            // geneate the buffers and synchronize them with the current time
            long referenceTimeTick = DateTime.Now.Ticks;

            CreateAudioBuffers(audioStream, audioMediaBuffers, referenceTimeTick);
            CreateVideoBuffers(timeline, videoMediaBuffers, referenceTimeTick);
        }
Example #10
0
        public static string GetBase64Audio(string textInput)
        {
            var speechAudioFormatConfig = new SpeechAudioFormatInfo(samplesPerSecond: 8000, bitsPerSample: AudioBitsPerSample.Sixteen, channel: AudioChannel.Stereo);
            var waveFormat = new WaveFormat(speechAudioFormatConfig.SamplesPerSecond, speechAudioFormatConfig.BitsPerSample, speechAudioFormatConfig.ChannelCount);
            var prompt     = new PromptBuilder
            {
                Culture = CultureInfo.CreateSpecificCulture("en-US")
            };

            prompt.StartVoice(prompt.Culture);
            prompt.StartSentence();
            prompt.StartStyle(new PromptStyle()
            {
                Emphasis = PromptEmphasis.Reduced,
                Rate     = PromptRate.Slow
            });
            prompt.AppendText(textInput);
            prompt.EndStyle();
            prompt.EndSentence();
            prompt.EndVoice();

            var mp3Stream = new MemoryStream();

            byte[] audioOutputBytes;
            string audioOutputAsString = null;

            using (var synthWaveMemoryStream = new MemoryStream())
            {
                var resetEvent = new ManualResetEvent(false);
                ThreadPool.QueueUserWorkItem(arg =>
                {
                    try
                    {
                        var siteSpeechSynth = new SpeechSynthesizer();
                        siteSpeechSynth.SetOutputToAudioStream(synthWaveMemoryStream, speechAudioFormatConfig);
                        siteSpeechSynth.Speak(prompt);
                    }
                    finally
                    {
                        resetEvent.Set();
                    }
                });
                WaitHandle.WaitAll(new WaitHandle[] { resetEvent });
                var bitRate = (speechAudioFormatConfig.AverageBytesPerSecond * 8);

                synthWaveMemoryStream.Position = 0;

                using (var mp3FileWriter = new LameMP3FileWriter(outStream: mp3Stream, format: waveFormat, bitRate: bitRate))
                {
                    synthWaveMemoryStream.CopyTo(mp3FileWriter);
                }

                audioOutputBytes    = mp3Stream.ToArray();
                audioOutputAsString = $"data:audio/mp3;base64,{Convert.ToBase64String(audioOutputBytes)}";
            }

            return(audioOutputAsString);
        }
Example #11
0
        private static void SynthToCam(string text, CameraWindow cw)
        {
            var synthFormat = new System.Speech.AudioFormat.SpeechAudioFormatInfo(System.Speech.AudioFormat.EncodingFormat.Pcm, 11025, 16, 1, 22100, 2, null);

            using (var synthesizer = new SpeechSynthesizer())
            {
                using (var waveStream = new MemoryStream())
                {
                    //write some silence to the stream to allow camera to initialise properly
                    var silence = new byte[1 * 22050];
                    waveStream.Write(silence, 0, silence.Count());

                    var pbuilder = new PromptBuilder();
                    var pStyle   = new PromptStyle
                    {
                        Emphasis = PromptEmphasis.Strong,
                        Rate     = PromptRate.Slow,
                        Volume   = PromptVolume.ExtraLoud
                    };

                    pbuilder.StartStyle(pStyle);
                    pbuilder.StartParagraph();
                    pbuilder.StartVoice(VoiceGender.Male, VoiceAge.Adult, 2);
                    pbuilder.StartSentence();
                    pbuilder.AppendText(text);
                    pbuilder.EndSentence();
                    pbuilder.EndVoice();
                    pbuilder.EndParagraph();
                    pbuilder.EndStyle();

                    synthesizer.SetOutputToAudioStream(waveStream, synthFormat);
                    synthesizer.Speak(pbuilder);
                    synthesizer.SetOutputToNull();

                    //write some silence to the stream to allow camera to end properly
                    waveStream.Write(silence, 0, silence.Count());
                    waveStream.Seek(0, SeekOrigin.Begin);

                    var ds = new DirectStream(waveStream)
                    {
                        RecordingFormat = new WaveFormat(11025, 16, 1)
                    };
                    var talkTarget = TalkHelper.GetTalkTarget(cw.Camobject, ds);
                    ds.Start();
                    talkTarget.Start();
                    while (ds.IsRunning)
                    {
                        Thread.Sleep(100);
                    }
                    ds.Stop();
                    talkTarget.Stop();
                    talkTarget = null;
                    ds         = null;

                    waveStream.Close();
                }
            }
        }
Example #12
0
        public virtual void PrepareMessageInStream(string message)
        {
            Stream = new MemoryStream();

            _speechSynthesizer.SetOutputToAudioStream(Stream, _speechAudioFormatInfo);
            _speechSynthesizer.Speak(message);

            Stream.Position = 0;
        }
Example #13
0
        public static TimeSpan NarrateText(string text, Stream outputStream)
        {
            var narrator = new SpeechSynthesizer();

            narrator.SetOutputToAudioStream(outputStream, SpeechFormat);
            long before = outputStream.Position;

            narrator.Speak(text);
            return(TimeSpan.FromSeconds((double)(outputStream.Position - before) / SpeechFormat.AverageBytesPerSecond));
        }
Example #14
0
        private static void SynthToCam(string text, CameraWindow cw)
        {
            var synthFormat = new System.Speech.AudioFormat.SpeechAudioFormatInfo(System.Speech.AudioFormat.EncodingFormat.Pcm, 11025, 16, 1, 22100, 2, null);
            using (var synthesizer = new SpeechSynthesizer())
            {
                using (var waveStream = new MemoryStream())
                {

                    //write some silence to the stream to allow camera to initialise properly
                    var silence = new byte[1 * 22050];
                    waveStream.Write(silence, 0, silence.Length);

                    var pbuilder = new PromptBuilder();
                    var pStyle = new PromptStyle
                    {
                        Emphasis = PromptEmphasis.Strong,
                        Rate = PromptRate.Slow,
                        Volume = PromptVolume.ExtraLoud
                    };

                    pbuilder.StartStyle(pStyle);
                    pbuilder.StartParagraph();
                    pbuilder.StartVoice(VoiceGender.Male, VoiceAge.Adult, 2);
                    pbuilder.StartSentence();
                    pbuilder.AppendText(text);
                    pbuilder.EndSentence();
                    pbuilder.EndVoice();
                    pbuilder.EndParagraph();
                    pbuilder.EndStyle();

                    synthesizer.SetOutputToAudioStream(waveStream, synthFormat);
                    synthesizer.Speak(pbuilder);
                    synthesizer.SetOutputToNull();

                    //write some silence to the stream to allow camera to end properly
                    waveStream.Write(silence, 0, silence.Length);
                    waveStream.Seek(0, SeekOrigin.Begin);

                    var ds = new DirectStream(waveStream) { RecordingFormat = new WaveFormat(11025, 16, 1) };
                    var talkTarget = TalkHelper.GetTalkTarget(cw.Camobject, ds); 
                    ds.Start();
                    talkTarget.Start();
                    while (ds.IsRunning)
                    {
                        Thread.Sleep(100);
                    }
                    ds.Stop();
                    talkTarget.Stop();
                    talkTarget = null;
                    ds = null;
                }
            }


        }
Example #15
0
        private SpeechSynthesizer CreateSpeechSynthesizer()
        {
            var synthesizer = new SpeechSynthesizer();

            if (!string.IsNullOrEmpty(this.Configuration.Voice))
            {
                synthesizer.SelectVoice(this.Configuration.Voice);
            }

            if (this.Configuration.UseDefaultAudioPlaybackDevice)
            {
                // If specified, don't create an output stream and just set up the
                // synthesizer to play sound directly to the default audio device.
                synthesizer.SetOutputToDefaultAudioDevice();
            }
            else
            {
                // Create the format info from the configuration input format
                SpeechAudioFormatInfo formatInfo = new SpeechAudioFormatInfo(
                    (EncodingFormat)this.Configuration.OutputFormat.FormatTag,
                    (int)this.Configuration.OutputFormat.SamplesPerSec,
                    this.Configuration.OutputFormat.BitsPerSample,
                    this.Configuration.OutputFormat.Channels,
                    (int)this.Configuration.OutputFormat.AvgBytesPerSec,
                    this.Configuration.OutputFormat.BlockAlign,
                    (this.Configuration.OutputFormat is WaveFormatEx) ? ((WaveFormatEx)this.Configuration.OutputFormat).ExtraInfo : null);

                // Configure synthesizer to write to the output stream
                synthesizer.SetOutputToAudioStream(
                    new IOStream(
                        (buffer, offset, count) =>
                {
                    byte[] audioData = buffer;
                    if (buffer.Length != count)
                    {
                        audioData = new byte[count];
                        Array.Copy(buffer, offset, audioData, 0, count);
                    }

                    this.Out.Post(new AudioBuffer(audioData, this.Configuration.OutputFormat), this.pipeline.GetCurrentTime());
                }),
                    formatInfo);
            }

            // Register all handlers
            synthesizer.BookmarkReached += this.OnBookmarkReached;
            synthesizer.PhonemeReached  += this.OnPhonemeReached;
            synthesizer.SpeakCompleted  += this.OnSpeakCompleted;
            synthesizer.SpeakProgress   += this.OnSpeakProgress;
            synthesizer.SpeakStarted    += this.OnSpeakStarted;
            synthesizer.StateChanged    += this.OnStateChanged;
            synthesizer.VisemeReached   += this.OnVisemeReached;

            return(synthesizer);
        }
Example #16
0
 public void AddEndpoint(string ID, Stream outstream)
 {
     SpeechSynthesizer voice = new SpeechSynthesizer();
     if (outstream == null) voice.SetOutputToDefaultAudioDevice();
     else voice.SetOutputToAudioStream(outstream, new System.Speech.AudioFormat.SpeechAudioFormatInfo(
         16000, System.Speech.AudioFormat.AudioBitsPerSample.Sixteen, System.Speech.AudioFormat.AudioChannel.Mono));
     //if (chkIVONA.Checked) voice.SelectVoice("IVONA 2 Amy");
     //else voice.SelectVoice("Microsoft Anna")
     voices.Add(ID, voice);
     outStreams.Add(ID, outstream);
 }
        public MemoryStream SynthesizeText(string text)
        {
            var audioStream = new MemoryStream();

            // set the synthesizer output to the stream, make sure the output format is matching the audio socket settings
            _synth.SetOutputToAudioStream(audioStream,
                                          new SpeechAudioFormatInfo(samplesPerSecond: 16000, bitsPerSample: AudioBitsPerSample.Sixteen, channel: AudioChannel.Mono));

            _synth.Speak(text);

            return(audioStream);
        }
Example #18
0
        public MemoryStream BeginRead(string toRead)
        {
            MemoryStream Bufferers = new MemoryStream();

            Reader.SetOutputToAudioStream(Bufferers, new SpeechAudioFormatInfo(43500, AudioBitsPerSample.Sixteen, AudioChannel.Stereo));
            if (toRead != null && toRead.Trim(' ') != "")
            {   // await for a stream
                Reader.Speak(toRead);
            }

            return(Bufferers);
        }
Example #19
0
        private static void TextToSpeech(SpeechSynthesizer synth, string text)
        {
            var stream = new MemoryStream();
            var format = new SpeechAudioFormatInfo(16000, AudioBitsPerSample.Sixteen, AudioChannel.Mono);

            synth.SetOutputToAudioStream(stream, format);
            synth.SpeakSsml(text);

            var stdout = Console.OpenStandardOutput();

            stream.Position = 0;
            stream.CopyTo(stdout);
        }
Example #20
0
        private async void Discord_ChatMessage(object sender, MessageEventArgs e)
        {
            if (this.beam.IsConnected && e.User.Id != this.discord.CurrentUser.Id && e.Channel.Id == 187760090206437376)
            {
                var msg = $"[discord] {e.Message.User.Name}: {e.Message.Text}";
                await this.beam.SendMessage(msg);
            }

            if (this.readingMessages && e.Channel == this.readingChannel)
            {
                using (var synthesizer = new SpeechSynthesizer())
                    using (var mem = new MemoryStream())
                    {
                        var info = new SpeechAudioFormatInfo(48000, AudioBitsPerSample.Sixteen, AudioChannel.Stereo);
                        synthesizer.SetOutputToAudioStream(mem, info);

                        PromptBuilder builder = new PromptBuilder();
                        builder.Culture = CultureInfo.CreateSpecificCulture("en-US");
                        builder.StartVoice(builder.Culture);
                        builder.StartSentence();
                        builder.StartStyle(new PromptStyle()
                        {
                            Emphasis = PromptEmphasis.Reduced
                        });
                        builder.AppendText(e.Message.User.Name);
                        builder.AppendText(" says ");
                        builder.EndStyle();
                        builder.AppendText(e.Message.Text);
                        builder.EndSentence();
                        builder.EndVoice();
                        synthesizer.Speak(builder);
                        mem.Seek(0, SeekOrigin.Begin);

                        int count, block = 96000;
                        var buffer = new byte[block];
                        while ((count = mem.Read(buffer, 0, block)) > 0)
                        {
                            if (count < block)
                            {
                                for (int i = count; i < block; i++)
                                {
                                    buffer[i] = 0;
                                }
                            }

                            this.audio.Send(buffer, 0, block);
                        }
                    }
            }
        }
Example #21
0
        public async Task <bool> PlayMessage(string message)
        {
            if (Util.IsUiThread)
            {
                return(await Task.Run(() => this.PlayMessage( message )));
            }

            await this.m_playbackSemaphore.WaitAsync();

            Stream mStream = null;

            try
            {
                if (this.IsPlaying)
                {
                    return(false);
                }

                mStream = new MemoryStream(OneMeg);

                var name = this.VoiceName;
                using (var tts = new SpeechSynthesizer())
                {
                    if (!string.IsNullOrEmpty(name))
                    {
                        tts.SelectVoice(name);
                    }
                    tts.SetOutputToAudioStream(mStream,
                                               new SpeechAudioFormatInfo(44100, AudioBitsPerSample.Sixteen, AudioChannel.Stereo));
                    tts.Speak(message);
                }
                mStream.Flush();
                mStream.Seek(0, SeekOrigin.Begin);
                this.m_mixer.AddInputStream(new DisposingWaveProvider(mStream));
                this.m_waveOut.Play();
            }
            catch (Exception e)
            {
                SimpleLogger.Log(nameof(SimpleAudioPlayer), $"Exception {e.GetType().Name} has occurred: {e.Message}");
                mStream?.Dispose();
                return(false);
            }
            finally
            {
                this.m_playbackSemaphore.Release();
            }

            return(true);
        }
        private byte[] LocalTTS(string msg)
        {
            try
            {
                using (var synth = new SpeechSynthesizer())
                    using (var stream = new MemoryStream())
                    {
                        if (opts.Voice == null || opts.Voice.Length == 0)
                        {
                            if (opts.Culture == null)
                            {
                                synth.SelectVoiceByHints(this.SpeakerGender, VoiceAge.Adult);
                            }
                            else
                            {
                                synth.SelectVoiceByHints(this.SpeakerGender, VoiceAge.Adult, 0, new CultureInfo(opts.Culture, false));
                            }
                        }
                        else
                        {
                            synth.SelectVoice(opts.Voice);
                        }

                        synth.Rate = opts.speed;

                        var intVol = (int)(opts.Volume * 100.0);

                        if (intVol > 100)
                        {
                            intVol = 100;
                        }

                        synth.Volume = intVol;

                        synth.SetOutputToAudioStream(stream,
                                                     new SpeechAudioFormatInfo(INPUT_SAMPLE_RATE, AudioBitsPerSample.Sixteen, AudioChannel.Mono));

                        synth.Speak(msg);

                        return(stream.ToArray());
                    }
            }
            catch (Exception ex)
            {
                Logger.Error(ex, $"Error with Microsoft Text to Speech: {ex.Message}");
            }

            return(new byte[0]);
        }
Example #23
0
        public void AddSpeak(string text)
        {
            using (var stream = new MemoryStream())
            {
                _speaker.SetOutputToAudioStream(stream, new SpeechAudioFormatInfo(_config.SampleRate, AudioBitsPerSample.Sixteen, AudioChannel.Stereo));
                _speaker.Speak(text);
                _speaker.SetOutputToNull();

                _writer.Write(stream.GetBuffer(), 0, stream.GetBuffer().Length);
                AddEmpty(_config.WaitTime);
                _writer.Write(stream.GetBuffer(), 0, stream.GetBuffer().Length);
                AddEmpty(_config.WaitTime);
                stream.Dispose();
            }
        }
        private byte[] TextToSpeech()
        {
            try
            {
                using (var synth = new SpeechSynthesizer())
                    using (var stream = new MemoryStream())
                    {
                        bool isVoiceInstalled = false;
                        foreach (var voice in synth.GetInstalledVoices())
                        {
                            var info = voice.VoiceInfo;
                            if (this.SpeakerCulture == info.Culture.ToString())
                            {
                                isVoiceInstalled = true;
                                break;
                            }
                        }
                        if (!isVoiceInstalled)
                        {
                            this.SpeakerCulture = "en-GB";
                        }

                        synth.SelectVoiceByHints(this.SpeakerGender, VoiceAge.Adult, 0, new CultureInfo(this.SpeakerCulture, false));
                        synth.Rate = 1;

                        var intVol = (int)(volume * 100.0);

                        if (intVol > 100)
                        {
                            intVol = 100;
                        }

                        synth.Volume = intVol;

                        synth.SetOutputToAudioStream(stream,
                                                     new SpeechAudioFormatInfo(INPUT_SAMPLE_RATE, AudioBitsPerSample.Sixteen, AudioChannel.Mono));

                        synth.Speak(path);

                        return(stream.ToArray());
                    }
            }
            catch (Exception ex)
            {
                Logger.Error(ex, "Error with Text to Speech");
            }
            return(new byte[0]);
        }
Example #25
0
        public Sync(SyncOp pOp)
        {
            AudioStream = new MemoryStream();
            Synth       = new SpeechSynthesizer();

            ReloadLexicons();

            Synth.SetOutputToAudioStream(AudioStream, new SpeechAudioFormatInfo(44100, AudioBitsPerSample.Sixteen, AudioChannel.Stereo)
            {
            });
            _voices    = Synth.GetInstalledVoices();
            voiceArray = _voices?.Select(s => s.VoiceInfo.Name).ToArray();

            _op = pOp;
            _op(this, true);
        }
Example #26
0
        public static async Task Say(VoiceNextConnection vnc, string say)
        {
            await vnc.SendSpeakingAsync(true);             // send a speaking indicator

            using (MemoryStream stream = new MemoryStream())
            {
                var info = new SpeechAudioFormatInfo(48000, AudioBitsPerSample.Sixteen, AudioChannel.Stereo);
                using (SpeechSynthesizer synth = new SpeechSynthesizer())
                {
                    //synth.SetOutputToAudioStream(stream, info);
                    synth.SetOutputToAudioStream(stream, info);

                    //var t = synth.GetInstalledVoices();
                    //synth.SelectVoice(t.First().VoiceInfo.Name);
                    synth.Speak(say);
                    synth.SetOutputToNull();
                }

                //await vnc.SendAsync(stream.ToArray(), 20, info.BitsPerSample);

                stream.Seek(0, SeekOrigin.Begin);

                Console.WriteLine("Format: {0}", info.EncodingFormat);
                Console.WriteLine("BitRate: {0}", info.BitsPerSample);
                Console.WriteLine("Block Alignment: {0}", info.BlockAlign);
                Console.WriteLine("Samples per second: {0}", info.SamplesPerSecond);

                var buff = new byte[3840];
                var br   = 0;
                while ((br = stream.Read(buff, 0, buff.Length)) > 0)
                {
                    if (br < buff.Length)                     // not a full sample, mute the rest
                    {
                        for (var i = br; i < buff.Length; i++)
                        {
                            buff[i] = 0;
                        }
                    }


                    await vnc.SendAsync(buff, 20, info.BitsPerSample);
                }
            }

            await vnc.SendSpeakingAsync(false);             // we're not speaking anymore
        }
        public async Task Speak(string text, BufferedWaveProvider waveProvider, int rate)
        {
            var fmt = new System.Speech.AudioFormat.SpeechAudioFormatInfo(waveProvider.WaveFormat.SampleRate, (System.Speech.AudioFormat.AudioBitsPerSample)waveProvider.WaveFormat.BitsPerSample, (System.Speech.AudioFormat.AudioChannel)waveProvider.WaveFormat.Channels);

            var _synthesizer = new SpeechSynthesizer()
            {
                Volume = 100,  // 0...100
                Rate   = rate, // -10...10
            };

            _synthesizer.SelectVoice(Voice.VoiceInfo.Name);
            _synthesizer.SetOutputToAudioStream(new OutputStream(waveProvider), fmt);

            _synthesizer.SpeakAsync(text);

            await Task.FromResult(0);
        }
Example #28
0
        public byte[] GenerateToByteArray(string text)
        {
            byte[] result;

            using (MemoryStream streamDestination = new MemoryStream())
            {
                var speechSynthesizer = new SpeechSynthesizer();

                speechSynthesizer.SetOutputToAudioStream(streamDestination, _formatInfo);

                speechSynthesizer.Speak(text);

                result = streamDestination.ToArray();
            }

            return(result);
        }
Example #29
0
        private void SendTextToSpeech(object obj)
        {
            Connection        c      = (Connection)obj;
            ConnectionStream  stream = new ConnectionStream(c);
            SpeechSynthesizer s      = new SpeechSynthesizer();

            using (MemoryStream memStream = new MemoryStream()) {
                s.SetOutputToAudioStream(memStream, new SpeechAudioFormatInfo(EncodingFormat.ALaw,
                                                                              8000, 8, 1, 8000, 1, null));
                s.Speak(_textToSpeak);
                memStream.Seek(0, SeekOrigin.Begin);

                using (ConnectionWriter writer = new ConnectionWriter(stream)) {
                    writer.Reverse = true;
                    writer.Write(memStream);
                }
            }
        }
Example #30
0
 /// <summary>
 ///Starts speech connector and starts speaking the prompt message asynchronously.
 /// </summary>
 private void StartSpeakAsync()
 {
     //start Speech synthesizer
     if (AudioVideoCall.Flow.SpeechSynthesisConnector != null)
     {
         AudioVideoCall.Flow.SpeechSynthesisConnector.DetachFlow();
     }
     m_speechSynthesisConnector.AttachFlow(AudioVideoCall.Flow);
     m_speechSynthesizer.SetOutputToAudioStream(m_speechSynthesisConnector, m_audioformat);
     m_speechSynthesizer.SpeakCompleted += new EventHandler <SpeakCompletedEventArgs>(SpeechSynthesizer_SpeakCompleted);
     m_speechSynthesisConnector.Start();
     //If prompt is not set then speak Ssml pause.
     if (m_pbMainPrompt == null)
     {
         m_pbMainPrompt = new PromptBuilder();
         m_pbMainPrompt.AppendBreak();
     }
     m_speechSynthesizer.SpeakAsync(m_pbMainPrompt);
 }
        ReadAudio(string text, Message e)
        {
            var voiceChannel = e.Server.VoiceChannels.FirstOrDefault();

            if (e.User.VoiceChannel != null && e.User.VoiceChannel.Name.ToLower() != "afk")
            {
                voiceChannel = e.User.VoiceChannel;
            }
            var _vClient = await voiceChannel.JoinAudio();

            var channelCount        = 2;
            var OutFormat           = new WaveFormat(48000, 16, channelCount);
            SpeechSynthesizer synth = new SpeechSynthesizer();
            MemoryStream      ms    = new MemoryStream();

            synth.SetOutputToAudioStream(ms, new SpeechAudioFormatInfo(48000, AudioBitsPerSample.Sixteen, AudioChannel.Stereo));
            synth.Speak(text);
            byte[] bytes = ms.ToArray();
            var    test  = new RawSourceWaveStream(ms, OutFormat);

            test.Seek(0, SeekOrigin.Begin);
            using (var resampler = new MediaFoundationResampler(test, OutFormat))
            {
                resampler.ResamplerQuality = 60;
                int    blockSize = OutFormat.AverageBytesPerSecond / 50;
                byte[] buffer    = new byte[blockSize];
                int    byteCount;

                while ((byteCount = resampler.Read(buffer, 0, blockSize)) > 0)
                {
                    if (byteCount < blockSize)
                    {
                        // Incomplete Frame
                        for (int i = byteCount; i < blockSize; i++)
                        {
                            buffer[i] = 0;
                        }
                    }
                    _vClient.Send(buffer, 0, blockSize);
                }
            }
        }
Example #32
0
        public static void Speak(string text, string voice, int vol, int speed)
        {
            lock (speaklock) {
                if (voiceStream == null)
                {
                    voiceStream = audioClient.CreatePCMStream(AudioApplication.Voice, 128 * 1024);
                }
                SpeechSynthesizer tts = new SpeechSynthesizer();
                tts.SelectVoice(voice);
                tts.Volume = vol * 5;
                tts.Rate   = speed - 10;
                MemoryStream ms = new MemoryStream();
                tts.SetOutputToAudioStream(ms, formatInfo);

                tts.Speak(text);
                ms.Seek(0, SeekOrigin.Begin);
                ms.CopyTo(voiceStream);
                voiceStream.Flush();
            }
        }
Example #33
0
        public void StartWithWelcome(AudioVideoFlow flow, LyncServer server)
        {
            _flow = flow;
            _server = server;
           //attach speech synthasis to audio flow
            _speechSynthesisConnector.AttachFlow(_flow);

            _speechSynthesizer = new SpeechSynthesizer();
            _speechSynthesizer.SetOutputToAudioStream(_speechSynthesisConnector, audioformat);
            _speechSynthesizer.SelectVoice("Microsoft Hazel Desktop");  //slightly more english

            var toneController = new ToneController(); //this is for the DTMF tones
            toneController.AttachFlow(_flow);
           
            _speechSynthesisConnector.Start();

            _speechSynthesizer.Speak("Welcome to the UCMA IVR Demo!");
            SpeakMenuOptions();
            toneController.ToneReceived += toneController_ToneReceived;
        }
Example #34
0
        public C2Voice(int zone)
        {
            myVoice = new SpeechSynthesizer();
            rand    = new Random();
            InitializeVocabulary();
            myZone = zone;
            String host = "192.168.113." + (100 + myZone);

            myRtpServer = new RTPServer(host, 1234);
            myRtpServer.StartServer();
            //ConnectSocket(host, 1234);
            //myNetStream = new NetworkStream(myZoneSocket, true);
//            SpeechAudioFormatInfo audioFormat = new SpeechAudioFormatInfo(24000, AudioBitsPerSample.Sixteen, AudioChannel.Mono);
//            audioFormat.EncodingFormat = EncodingFormat.ALaw;
            myVoice.SetOutputToAudioStream(myRtpServer.AudioStream, new SpeechAudioFormatInfo(24000, AudioBitsPerSample.Sixteen, AudioChannel.Mono));

//            myVoice.SetOutputToAudioStream(myRtpServer.AudioStream, new SpeechAudioFormatType
//            SpeechAudioFormatType;
            myVoice.SetOutputToWaveStream(myRtpServer.AudioStream);
        }
Example #35
0
        private void SendTextToSpeech(object obj) {
            Connection c = (Connection)obj;
            ConnectionStream stream = new ConnectionStream(c);
            SpeechSynthesizer s = new SpeechSynthesizer();
            using (MemoryStream memStream = new MemoryStream()) {
                s.SetOutputToAudioStream(memStream, new SpeechAudioFormatInfo(EncodingFormat.ALaw,
                    8000, 8, 1, 8000, 1, null));
                s.Speak(_textToSpeak);
                memStream.Seek(0, SeekOrigin.Begin);

                using (ConnectionWriter writer = new ConnectionWriter(stream)) {
                    writer.Reverse = true;
                    writer.Write(memStream);
                }
            }
        }
Example #36
0
        private static byte[] GenerateVoiceAnnouncement(string announcement)
        {
            var synthesizer = new SpeechSynthesizer();
            var waveStream = new MemoryStream();
            var firstOrDefault = synthesizer.GetInstalledVoices()
                .FirstOrDefault(x => x.VoiceInfo.Name.ToUpper().Contains("DAVID"));
            if (firstOrDefault != null)
                synthesizer.SelectVoice(
                    firstOrDefault
                        .VoiceInfo.Name);
            synthesizer.SetOutputToAudioStream(waveStream,
                new SpeechAudioFormatInfo(EncodingFormat.Pcm,
                    44100, 16, 2, 176400, 2, null));
            synthesizer.Volume = 100;
            synthesizer.Rate = 1;
            synthesizer.Speak(announcement);
            synthesizer.SetOutputToNull();

            return waveStream.ToArray();
        }
Example #37
0
        protected override void OnLoad(EventArgs e)
        {
            Visible = false;
            ShowInTaskbar = false;
            base.OnLoad(e);
 
            /*
             *  Get all installed voices
             * 
             */
            var voices = speech.GetInstalledVoices();
            string voice = "";
 
            foreach (InstalledVoice v in voices)
            {
                if (v.Enabled)
                    //voice = v.VoiceInfo.Name;
                    Console.WriteLine(v.VoiceInfo.Name);
                    
            }

            queuetimer = new System.Timers.Timer(250);
            queuetimer.Elapsed += (object sender, ElapsedEventArgs ev) => 
            {
                TTSRequest r;
                if (Queue.TryDequeue(out r))
                {
                    Console.WriteLine("dequeing off of concurrent queue...");
                    if (r.Interrupt)
                    {
                        // stop current TTS
                            if (IsSpeaking)
                            {
                                //speech.StopSpeaking();
                            }
                            if (IsSounding)
                            {
                                //sound.Stop();
                                if(sound.PlaybackState == PlaybackState.Playing) {
                                    sound.Stop(); 
                                }
                            }
                        // clear queue
                        SpeechQueue.Clear();
                    }
                    if(!r.Reset) {
                        SpeechQueue.Enqueue(r);
                    }
                    RequestCount++;
                }
                
                var eventdata = new Hashtable();
                eventdata.Add("ProcessedRequests", RequestCount);
                eventdata.Add("QueuedRequests", SpeechQueue.Count);
                eventdata.Add("IsSpeaking", IsSounding);
                InstrumentationEvent blam = new InstrumentationEvent();
                blam.EventName = "status";
                blam.Data = eventdata;
                NotifyGui(blam.EventMessage());  
            };

            // when this timer fires, it will pull off of the speech queue and speak it
            // the long delay also adds a little pause between tts requests.
            speechtimer = new System.Timers.Timer(250);
            speechtimer.Elapsed += (object sender, ElapsedEventArgs ev) =>
            {
                if (IsSpeaking.Equals(false))
                {
                    if (SpeechQueue.Count > 0)
                    {
                        TTSRequest r = SpeechQueue.Dequeue();
                        Console.WriteLine("dequeuing off of speech queue");
                        IsSpeaking = true;
                        speechtimer.Enabled = false;

                        //speech.SpeakAsync(r.Text);

                        //using (speech = new SpeechSynthesizer()) {
                        speech = new SpeechSynthesizer();
                            speech.SpeakCompleted += speech_SpeakCompleted;
                            format = new SpeechAudioFormatInfo(EncodingFormat.ALaw, 8000, 8, 1, 1, 2, null);
                            //format = new SpeechAudioFormatInfo(11025, AudioBitsPerSample.Sixteen, AudioChannel.Mono);
                           // var si = speech.GetType().GetMethod("SetOutputStream", BindingFlags.Instance | BindingFlags.NonPublic);
                            stream = new MemoryStream();
                            //si.Invoke(speech, new object[] { stream, format, true, true });
                            //speech.SetOutputToWaveStream(stream);
                            speech.SetOutputToAudioStream(stream, format);
                            speech.SelectVoice(config.getVoice (r.Language, r.Voice));
                            int rate = (r.Speed * 2 - 10);
                            
                            Console.WriteLine(rate);
                            try
                            {
                                speech.Rate = rate;
                            }
                            catch (ArgumentOutOfRangeException ex)
                            {
                                speech.Rate = 0;
                            }
                            speech.SpeakAsync(r.Text);
                        //}

                        synthesis.WaitOne();
                        speech.SpeakCompleted -= speech_SpeakCompleted;
                        speech.SetOutputToNull();
                        speech.Dispose();
                        //IsSpeaking = false;
                        IsSounding = true;
                        stream.Position = 0;
                        //WaveFormat.CreateCustomFormat(WaveFormatEncoding.WmaVoice9, 11025, 1, 16000, 2, 16)
                        using(RawSourceWaveStream reader = new RawSourceWaveStream(stream, WaveFormat.CreateALawFormat(8000, 1))) {
                            WaveStream ws = WaveFormatConversionStream.CreatePcmStream(reader);

                            //var waveProvider = new MultiplexingWaveProvider(new IWaveProvider[] { ws }, 4);
                            //waveProvider.ConnectInputToOutput(0, 3);

                            sound = new WaveOutEvent();
                            // set output device *before* init
                            Console.WriteLine("Output Device: " + OutputDeviceId);
                            sound.DeviceNumber = OutputDeviceId;
                            sound.Init(ws);
                            //sound.Init(waveProvider);
                            sound.PlaybackStopped += output_PlaybackStopped;
                           // Console.WriteLine("playing here " + ws.Length);
                            sound.Play();
                        }
                        playback.WaitOne();
                        //IsSounding = false;
                        speechtimer.Enabled = true;
                    }
                }
            };

            queuetimer.Enabled = true;
            queuetimer.Start();
            speechtimer.Enabled = true;
            speechtimer.Start();

            InitHTTPServer();

        }