private Speaker() { if (synthesizer == null) { synthesizer = new SpeechSynthesizer(); synthesizer.Volume = 100; } if (InstalledVoices == null) { InstalledVoices = new List <InstalledVoice>(); } // インストール済みのSAPIなボイスを列挙 foreach (InstalledVoice voice in synthesizer.GetInstalledVoices()) { // メンバ変数へ列挙されたボイスを追加。 if (voice != null) { InstalledVoices.Add(voice); } } synthesizer.SpeakCompleted += new EventHandler <SpeakCompletedEventArgs>(OnSAPISpeakingEnd); synthesizer.SpeakProgress += new EventHandler <SpeakProgressEventArgs>(OnSAPISpeakingSentence); }
public static void LoadSettings() { var xml = SettingsManager.Options.VoicesData; InstalledVoiceEx[] savedVoices = null; if (!string.IsNullOrEmpty(xml)) { try { savedVoices = Serializer.DeserializeFromXmlString <InstalledVoiceEx[]>(xml); } catch (Exception) { } } if (savedVoices == null) { savedVoices = new InstalledVoiceEx[0]; } foreach (var voice in savedVoices) { // If voice is not missing information then... if (!string.IsNullOrEmpty(voice.SourceKeys)) { InstalledVoices.Add(voice); } } }
// Set voice. static InstalledVoiceEx SelectVoice(string name, string language, MessageGender gender) { // Get only enabled voices. var data = InstalledVoices.Where(x => x.Enabled).ToArray(); InstalledVoiceEx voice = null; var culture = Capturing.message.GetCultureInfo(language); // Order voices by putting matching gender with highest value first. OrderVoicesByGender(ref data, gender); var missing = ""; missing += "There are no voices enabled in \"{0}\" column with value \"{1}\". "; missing += "Set popularity value to 100 ( normal usage ) or 101 ( normal usage / favourite ) for at least one voice."; // Initial choice will be all enabled voices. InstalledVoiceEx[] choice = data; InstalledVoiceEx[] tmp; // If voice name was supplied then... if (!string.IsNullOrEmpty(name)) { // Select voices by name if exists ("IVONA 2 Amy"). tmp = data.Where(x => string.Equals(x.Name, name, StringComparison.InvariantCulture)).ToArray(); // If choice available then... if (tmp.Length > 0) { choice = tmp; } else { OnHelp(missing, "Name", name); } } // Filter by gender (more important than by culture). tmp = FilterVoicesByGender(choice, gender); // If choice available then... if (tmp.Length > 0) { choice = tmp; } else { OnHelp(missing, "Gender", gender); // Order by Male as default. OrderVoicesByGender(ref data, MessageGender.Male); } // If culture supplied. if (culture != null) { tmp = FilterVoicesByCulture(choice, culture); // If choice available then... if (tmp.Length > 0) { choice = tmp; } else { OnHelp(missing, "Culture", language); } } // If nothing to choose from then... if (choice.Length == 0) { return(null); } // Generate number for selecting voice. var number = MainHelper.GetNumber(0, choice.Length - 1, "name", name); voice = choice[number]; if (SelectedVoice != voice) { OnEvent(VoiceChanged, voice); } return(voice); }