From 38366f093a26bc84c01704e107df467ef807666d Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Mon, 6 Jan 2025 16:56:04 -0500 Subject: [PATCH] Update System.Speech to also recognize Speech_OneCore (#110123) --- .../Internal/ObjectToken/ObjectTokenCategory.cs | 7 ++++++- .../src/Internal/ObjectToken/SAPICategories.cs | 2 ++ .../src/Internal/Synthesis/VoiceSynthesis.cs | 17 +++++++++++------ 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/libraries/System.Speech/src/Internal/ObjectToken/ObjectTokenCategory.cs b/src/libraries/System.Speech/src/Internal/ObjectToken/ObjectTokenCategory.cs index 7c4fde5da614c..d478c44859120 100644 --- a/src/libraries/System.Speech/src/Internal/ObjectToken/ObjectTokenCategory.cs +++ b/src/libraries/System.Speech/src/Internal/ObjectToken/ObjectTokenCategory.cs @@ -21,7 +21,12 @@ protected ObjectTokenCategory(string keyId, RegistryDataKey key) internal static ObjectTokenCategory Create(string sCategoryId) { RegistryDataKey key = RegistryDataKey.Open(sCategoryId, true); - return new ObjectTokenCategory(sCategoryId, key); + if (key != null) + { + return new ObjectTokenCategory(sCategoryId, key); + } + + return null; } #endregion diff --git a/src/libraries/System.Speech/src/Internal/ObjectToken/SAPICategories.cs b/src/libraries/System.Speech/src/Internal/ObjectToken/SAPICategories.cs index afc092b996505..806c44bf6fa11 100644 --- a/src/libraries/System.Speech/src/Internal/ObjectToken/SAPICategories.cs +++ b/src/libraries/System.Speech/src/Internal/ObjectToken/SAPICategories.cs @@ -59,6 +59,7 @@ internal static int DefaultDeviceOut() #endregion private const string SpeechRegistryKey = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\"; + private const string SpeechOneCoreRegistryKey = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech_OneCore\"; internal const string CurrentUserVoices = @"HKEY_CURRENT_USER\SOFTWARE\Microsoft\Speech\Voices"; @@ -66,6 +67,7 @@ internal static int DefaultDeviceOut() internal const string Recognizers = SpeechRegistryKey + "Recognizers"; internal const string Voices = SpeechRegistryKey + "Voices"; + internal const string Voices_OneCore = SpeechOneCoreRegistryKey + "Voices"; internal const string AudioIn = SpeechRegistryKey + "AudioInput"; diff --git a/src/libraries/System.Speech/src/Internal/Synthesis/VoiceSynthesis.cs b/src/libraries/System.Speech/src/Internal/Synthesis/VoiceSynthesis.cs index 0016455f76f35..2accf7f19406a 100644 --- a/src/libraries/System.Speech/src/Internal/Synthesis/VoiceSynthesis.cs +++ b/src/libraries/System.Speech/src/Internal/Synthesis/VoiceSynthesis.cs @@ -1480,20 +1480,25 @@ private static List BuildInstalledVoices(VoiceSynthesis voiceSyn { List voices = new(); - using (ObjectTokenCategory category = ObjectTokenCategory.Create(SAPICategories.Voices)) + ReadOnlySpan categoryIds = [SAPICategories.Voices, SAPICategories.Voices_OneCore]; + foreach (string categoryId in categoryIds) { - if (category != null) + using (ObjectTokenCategory category = ObjectTokenCategory.Create(categoryId)) { - // Build a list with all the voicesInfo - foreach (ObjectToken voiceToken in category.FindMatchingTokens(null, null)) + if (category != null) { - if (voiceToken != null && voiceToken.Attributes != null) + // Build a list with all the voicesInfo + foreach (ObjectToken voiceToken in category.FindMatchingTokens(null, null)) { - voices.Add(new InstalledVoice(voiceSynthesizer, new VoiceInfo(voiceToken))); + if (voiceToken != null && voiceToken.Attributes != null) + { + voices.Add(new InstalledVoice(voiceSynthesizer, new VoiceInfo(voiceToken))); + } } } } } + return voices; }