/// <summary> /// Raises an event indicating that the active engine has changed /// </summary> /// <param name="oldEngine">previous engine</param> /// <param name="newEngine">the newly activated engine</param> private void notifyEngineChanged(ITTSEngine oldEngine, ITTSEngine newEngine) { if (EvtEngineChanged != null) { EvtEngineChanged(oldEngine, newEngine); } }
/// <summary> /// Creates a TTS engine oft he specified Type, initializes it /// and sets it as the active engine. If it could not create it, /// it sets the null engine as the active engine /// </summary> /// <param name="engineType">The .NET type of the engine</param> /// <returns>true on success</returns> private bool createAndSetActiveEngine(Type engineType) { bool retVal; try { var engine = (ITTSEngine)Activator.CreateInstance(engineType); retVal = engine.Init(); ActiveEngine = (retVal) ? engine : null; } catch (Exception ex) { Log.Debug("Unable to instantiate TTS engine of type " + engineType.ToString() + ", assembly: " + engineType.Assembly.FullName + ". Exception: " + ex.ToString()); retVal = false; } if (!retVal) { ActiveEngine = _nullEngine; } Log.Debug("retVal=" + retVal); return(retVal); }
public string GetOrCreateFile( ITTSEngine engine, string tts, string ext, string parameter, Action <string> fileCreator) { // TODO: per-file lock? lock (this) { var newCacheFile = GetCacheFileNameNew(engine, tts, ext, parameter); if (File.Exists(newCacheFile)) { return(newCacheFile); } var oldCacheFile = GetCacheFileNameOld(engine, tts, ext, parameter); if (File.Exists(oldCacheFile)) { // Rename old cache file to new file name File.Move(oldCacheFile, newCacheFile); return(newCacheFile); } // Create the file if (!Directory.Exists(CacheDirectory)) { Directory.CreateDirectory(CacheDirectory); } fileCreator(newCacheFile); return(newCacheFile); } }
public override void Configuration(IConfiguration config) { // 初始化TTS引擎 IConfiguration tts = config.Children["TTSEngine"]; if (tts != null) { Type ttsType = Type.GetType(tts.Attributes["type"]); if (tts != null && ttsType != null) { try { int threadNumber = int.Parse(tts.Attributes["threadnumber"]); ITTSEngine ttsEngine = Activator.CreateInstance(ttsType, new object[] { workItem, threadNumber }) as AbstractTTSEngine; workItem.Services.Add <ITTSEngine>(ttsEngine); } catch (Exception ex) { logger.Error(String.Format("初始化 {0} TTS引擎失败", tts.Attributes["name"]), ex); throw ex; } } } // 初始化所有的通道对象 foreach (IConfiguration conf in config.Children["Channels"].Children) { int chnlid = int.Parse(conf.Attributes["id"]); if (chnlid >= 0 && chnlid < ChannelCount) { IChannel chnl = Channels[chnlid]; if (chnl != null) { logger.Info("配置第 " + chnl.ChannelID.ToString() + " 条通道 : "); chnl.ChannelAlias = conf.Attributes["alias"]; chnl.HitTimeout = int.Parse(conf.Attributes["HitTimeout"]); chnl.DetectPolarity = bool.Parse(conf.Attributes["DetectPolarity"]); // 注册脚本处理程序 if (conf.Children.Count > 0 && conf.Children["Scripts"].Children.Count > 0) { foreach (IConfiguration script in conf.Children["Scripts"].Children) { if (script.Name.ToLower() == "add") { if ((script.Attributes["EventHandler"] != null || script.Attributes["EventHandler"] != string.Empty) && (script.Attributes["filename"] != null || script.Attributes["filename"] != string.Empty)) { logger.Info("注册脚本处理程序,注册的事件名为 " + script.Attributes["EventHandler"] + " ,脚本文件为 \"" + script.Attributes["filename"] + "\""); chnl.RegisgerScript(script.Attributes["EventHandler"], script.Attributes["filename"]); } } } } chnl.Enable = bool.Parse(conf.Attributes["enable"]); // 启用或禁用通道 } } } logger.Info("完成板卡适配器下的所有通道资源的初始化工作"); Active = bool.Parse(config.Attributes["active"]); // 激活板卡适配器 }
private void ControllerOnTtsEngineChanged(bool fromView, string engine) { Controller.NotifyLogMessageAppend(fromView, $"TTSEngine Changed: fromView = {fromView}, engine = {engine}"); lock (this) { _ttsEngine?.Stop(); _ttsEngine = null; _ttsEngine = TTSEngineFactory.CreateEngine(engine); _ttsEngine.AttachToAct(this); _ttsEngine.PostAttachToAct(this); } }
/// <summary> /// Switch language to the specified one. /// </summary> /// <param name="ci">culture to switch to</param> /// <returns>true on success</returns> public bool SwitchLanguage(CultureInfo ci) { if (ActiveEngine != null) { ActiveEngine.Dispose(); ActiveEngine = null; } bool ret = SetActiveEngine(ci); return(ret); }
/// <summary> /// Sets the active spellchecker for the specified culture. If /// culture is null, the default culture is used /// </summary> /// <param name="ci">culture info</param> /// <returns>true on success</returns> public bool SetActiveEngine(CultureInfo ci = null) { bool retVal = true; Guid guid = Guid.Empty; Guid cultureNeutralGuid = Guid.Empty; if (ci == null) { ci = CultureInfo.DefaultThreadCurrentUICulture; } guid = _ttsEngines.GetPreferredOrDefaultByCulture(ci); cultureNeutralGuid = _ttsEngines.GetPreferredOrDefaultByCulture(null); if (!Equals(guid, Guid.Empty)) // found something for the specific culture { var type = _ttsEngines.Lookup(guid); if (ActiveEngine != null) { ActiveEngine.Dispose(); ActiveEngine = null; } retVal = createAndSetActiveEngine(type, ci); if (!retVal) { ActiveEngine = TTSEngines.NullTTSEngine; retVal = true; // TODO:: } } else { if (!Equals(cultureNeutralGuid, Guid.Empty)) { var type = _ttsEngines.Lookup(cultureNeutralGuid); retVal = createAndSetActiveEngine(type, ci); } else { retVal = false; } if (!retVal) { ActiveEngine = TTSEngines.NullTTSEngine; retVal = true; // TODO:: } } return(retVal); }
/// <summary> /// User clicked on the speaker icon. Mute or unmute /// the text to speech /// </summary> /// <param name="sender">event sender</param> /// <param name="e">event args</param> private void labelSpeaker_Click(object sender, EventArgs e) { ITTSEngine engine = TTSManager.Instance.ActiveEngine; if (engine.IsMuted()) { engine.UnMute(); } else { engine.Mute(); } }
private static string GetCacheFileNameNew( ITTSEngine engine, string tts, string ext, string parameter) { // 10 digits sha-1 hash in base36 var hash = Hash($"{engine.Name}.{tts}.{parameter}").Substring(0, 10); var cacheName = $"{hash}.{ext}"; var fileName = Path.Combine( CacheDirectory, cacheName); return(fileName); }
public override void DeInitPlugin() { Controller.TTSEngineChanged -= ControllerOnTtsEngineChanged; TtsInjector.Stop(); SoundPlayer.Stop(); UpdateChecker.Stop(); lock (this) { _ttsEngine?.Stop(); _ttsEngine = null; } if (_settingsLoaded) { Settings?.Save(); } StatusLabel.Text = "Exited. Bye~"; }
/// <summary> /// キャッシュファイル用の衝突しない名前を生成する /// </summary> /// <param name="ctrl"></param> /// <param name="ttsType"></param> /// <param name="tts"></param> /// <param name="parameter"></param> /// <returns> /// キャッシュファイル名</returns> private static string GetCacheFileNameOld( ITTSEngine engine, string tts, string ext, string parameter) { tts = tts.Replace(Environment.NewLine, "+"); var hashTTS = tts.GetHashCode().ToString("X4"); var hashParam = parameter.GetHashCode().ToString("X4"); var cacheName = $"{engine.Name}.{Truncate(tts, 50)}.{hashTTS}{hashParam}.{ext}"; // ファイル名に使用できない文字を除去する cacheName = string.Concat(cacheName.Where(c => !InvalidChars.Contains(c))); var fileName = Path.Combine( CacheDirectory, cacheName); return(fileName); }
/// <summary> /// Creates the TTS Engine for the specified culture. If it fails, /// it set the null TTS Engine as the active one. /// </summary> /// <param name="type">Type of the TTS Engine</param> /// <param name="ci">Culture</param> /// <returns>true on success</returns> private bool createAndSetActiveEngine(Type type, CultureInfo ci) { bool retVal; try { var ttsEngine = (ITTSEngine)Activator.CreateInstance(type); retVal = ttsEngine.Init(ci); if (retVal) { saveSettings(ttsEngine); ActiveEngine = ttsEngine; } } catch (Exception ex) { Log.Debug("Unable to load TTS Engine" + type + ", assembly: " + type.Assembly.FullName + ". Exception: " + ex); retVal = false; } return(retVal); }
public string GetOrCreateFile( ITTSEngine engine, string tts, string ext, string parameter, Action <string> fileCreator) { // TODO: per-file lock? lock (this) { var newCacheFile = GetCacheFileNameNew(engine, tts, ext, parameter); Logger.Debug($"Looking for cache file: {newCacheFile}"); if (File.Exists(newCacheFile)) { Logger.Debug("Cache hit."); return(newCacheFile); } var oldCacheFile = GetCacheFileNameOld(engine, tts, ext, parameter); if (File.Exists(oldCacheFile)) { // Rename old cache file to new file name Logger.Debug($"Old cache hit: {oldCacheFile}. Rename to new cache name."); File.Move(oldCacheFile, newCacheFile); return(newCacheFile); } // Create the file if (!Directory.Exists(CacheDirectory)) { Directory.CreateDirectory(CacheDirectory); } Logger.Debug("Cache missing, creating..."); fileCreator(newCacheFile); return(newCacheFile); } }
private void InitSapiVoices() { string[] voices = SapiTTS.GetAllVoices(); foreach (string voice in voices) { try { SapiTTS sapi = new SapiTTS(voice); sapi.VoiceName = voice; m_ttsCollection[voice] = sapi; if (m_defaultTts == null) { m_defaultTts = sapi; } } catch (Exception) { } } m_defaultTts = m_ttsCollection["RHVoice Aleksandr (Russian)"]; }
/// <summary> /// キャッシュファイル用の衝突しない名前を生成する /// </summary> /// <param name="ctrl"></param> /// <param name="ttsType"></param> /// <param name="tts"></param> /// <param name="parameter"></param> /// <returns> /// キャッシュファイル名</returns> public static string GetCacheFileName( this ITTSEngine engine, string tts, string ext, string parameter) { var hashTTS = tts.GetHashCode().ToString("X4"); var hashParam = parameter.GetHashCode().ToString("X4"); var cacheName = $"{engine.Name}.{tts}.{hashTTS}{hashParam}.{ext}"; // ファイル名に使用できない文字を除去する cacheName = string.Concat(cacheName.Where(c => !InvalidCars.Contains(c))); var fileName = Path.Combine( CacheDirectory, cacheName); if (!Directory.Exists(Path.GetDirectoryName(fileName))) { Directory.CreateDirectory(Path.GetDirectoryName(fileName)); } return(fileName); }
/// <summary> /// Creates a TTS engine oft he specified Type, initializes it /// and sets it as the active engine. If it could not create it, /// it sets the null engine as the active engine /// </summary> /// <param name="engineType">The .NET type of the engine</param> /// <returns>true on success</returns> private bool createAndSetActiveEngine(Type engineType) { bool retVal; try { var engine = (ITTSEngine)Activator.CreateInstance(engineType); retVal = engine.Init(); ActiveEngine = (retVal) ? engine : null; } catch (Exception ex) { Log.Debug("Unable to instantiate TTS engine of type " + engineType.ToString() + ", assembly: " + engineType.Assembly.FullName + ". Exception: " + ex.ToString()); retVal = false; } if (!retVal) { ActiveEngine = _nullEngine; } Log.Debug("retVal=" + retVal); return retVal; }
/// <summary> /// Saves settings for the specified tts engine /// </summary> /// <param name="spellChecker">TTS Engine object</param> private void saveSettings(ITTSEngine ttsEngine) { ttsEngine.Save(); }
/// <summary> /// The text to speech engine changed /// </summary> /// <param name="oldEngine">previous engine</param> /// <param name="newEngine">new engine</param> private void Instance_EvtEngineChanged(ITTSEngine oldEngine, ITTSEngine newEngine) { oldEngine.EvtPropertyChanged -= ActiveEngine_EvtPropertyChanged; newEngine.EvtPropertyChanged += ActiveEngine_EvtPropertyChanged; }
/// <summary> /// Initializes the singleton instance of the class /// </summary> private TTSManager() { _nullEngine = new Core.TTSEngines.NullEngine(); ActiveEngine = _nullEngine; ActiveEngine.Init(); }
/// <summary> /// Initializes the singleton instance of the class /// </summary> private TTSManager() { _nullEngine = new Core.TTSEngines.NullTTSEngine(); ActiveEngine = _nullEngine; ActiveEngine.Init(); }