Example #1
0
        static Task <bool> Initialize()
        {
            if (tcsInitialize != null && tts != null)
            {
                return(tcsInitialize.Task);
            }

            tcsInitialize = new TaskCompletionSource <bool>();
            tts           = new TtsClient();

            tts.StateChanged += (s, e) =>
            {
                if (e.Current == State.Ready)
                {
                    tcsInitialize?.TrySetResult(true);
                }
            };

            tts.UtteranceCompleted += (s, e) =>
            {
                tts?.Stop();
                tcsUtterances?.TrySetResult(true);
            };

            tts.Prepare();
            return(tcsInitialize.Task);
        }
Example #2
0
        /// <summary>
        /// 生成音频
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void butGenerateWav_Click(object sender, EventArgs e)
        {
            string txt = this.rtBoxResult.Text.Trim();

            if (string.IsNullOrEmpty(txt))
            {
                return;
            }
            string fistStr   = this.comBoxVoice.SelectedItem.ToString().Split(',').First();
            string voiceName = this.comBoxVoice.SelectedItem.ToString().Replace(fistStr + ",", " ").Trim();
            string _rate     = this.tBoxRate.Text.Trim();
            string _pitch    = this.tBoxPitch.Text.Trim();

            Task.Factory.StartNew(async() =>
            {
                TtsClient ttsClient = new TtsClient("3a1ec26757b94a84af648ac1f88cb95f");
                ttsClient.SynthesizeSuccessEvent += (s) =>  //合成成功
                {
                    SoundPlayer player = new SoundPlayer(new MemoryStream(s));
                    player.PlaySync();
                };
                ttsClient.SynthesizeErrorEvent += (s) =>    //合成失败
                {
                };
                await ttsClient.SyntheticAudio(new TtsInputOptions(txt, fistStr, voiceName), new Prosody()
                {
                    rate   = _rate,     //语速
                    volume = "+20.00%", //音量
                    pitch  = _pitch     //音调
                });
            });
        }
Example #3
0
 /// <summary>
 /// Dispose of TTS
 /// </summary>
 public void Dispose()
 {
     if (ttsInst != null)
     {
         ttsInst.Stop();
         ttsInst.Unprepare();
         ttsInst.StateChanged -= TtsStateChanged;
         ttsInst = null;
     }
 }
Example #4
0
 public TextToSpeech()
 {
     ttsInst = new TtsClient();
     ttsInst.Prepare();
     ttsInst.StateChanged += TtsStateChanged;
     try
     {
         list = (List <SupportedVoice>)ttsInst.GetSupportedVoices();
     }
     catch (Exception ex)
     {
         Console.Write(ex.Message);
     }
 }
Example #5
0
        public async void Speak(string text)
        {
            TtsClient speaker = new TtsClient();

            if (speaker.CurrentState == State.Created)
            {
                await Prepare(speaker);
            }

            if (speaker.CurrentState == State.Ready)
            {
                speaker.AddText(text, speaker.DefaultVoice.Language, (int)speaker.DefaultVoice.VoiceType, speaker.GetSpeedRange().Normal);
                speaker.Play();
            }
        }
Example #6
0
        public Task Prepare(TtsClient speaker)
        {
            TaskCompletionSource <bool> source = new TaskCompletionSource <bool>();

            speaker.StateChanged += (s, e) =>
            {
                if (e.Current == State.Ready)
                {
                    source.SetResult(true);
                }
            };

            if (speaker.CurrentState == State.Created)
            {
                speaker.Prepare();
            }

            return(source.Task);
        }
Example #7
0
 /// <summary>
 /// The service constructor.
 /// </summary>
 public TextToSpeechService()
 {
     _client = new TtsClient();
     _client.UtteranceCompleted += ClientOnUtteranceCompleted;
 }
        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel(IUnityContainer container)
        {
            ////if (IsInDesignMode)
            ////{
            ////    // Code runs in Blend --> create design time data.
            ////}
            ////else
            ////{
            ////    // Code runs "for real"
            ////}
            /// 初始化翻译类
            _translator = new Translator();
            #region 初始化语音识别客户端

            _ttsClient = new TtsClient();

            #endregion
            //初始化录音对象
            Recorder          = new Record(AppDomain.CurrentDomain.BaseDirectory + @"\RecordCache", SynchronizationContext.Current);
            Recorder.Success += Recorder_Success;
            #region 初始化定时器
            _timer          = new DispatcherTimer();
            _timer.Interval = TimeSpan.FromSeconds(1);
            _timer.Tick    += _timer_Tick;
            #endregion
            //初始化设置界面
            SettingView          = container.Resolve <SettingView>();
            _changeCaptureWindow = container.Resolve <ChangeCaptureView>();
            #region 初始化命令

            SpeakCommand       = new RelayCommand <string>(SpeakCommandExcute);
            StartRecordCommand = new RelayCommand <MouseButtonEventArgs>((e) =>
            {
                Recorder.Start();
                _timer.Start();
                TextVisibility  = Visibility.Collapsed;
                VolumVisibility = Visibility.Visible;
            });
            EndRecordCommand = new RelayCommand <MouseButtonEventArgs>((e) =>
            {
                _timer.Stop();
                if (_recordTime > 0.5)
                {
                    IsBusyVisibility = Visibility.Visible;
                }
                Recorder.Stop();

                TextVisibility  = Visibility.Visible;
                VolumVisibility = Visibility.Collapsed;
            });
            TranslateCommand = new RelayCommand(TranslateCommandExcute);

            InputKeyUpCommand = new RelayCommand <KeyEventArgs>((e) =>
            {
                if (e.Key == Key.Enter)
                {
                    //执行翻译
                    TranslateCommandExcute();
                }
            });
            ChangeDeviceCommand = new RelayCommand(() =>
            {
                _changeCaptureWindow.ShowChangeDeviceWindow(Recorder);
            });
            #endregion
        }