Skip to content

Commit

Permalink
Update System.Speech to also recognize Speech_OneCore (dotnet#110123)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub authored Jan 6, 2025
1 parent 2979d1b commit 38366f0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,15 @@ 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";

#region internal Fields

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";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1480,20 +1480,25 @@ private static List<InstalledVoice> BuildInstalledVoices(VoiceSynthesis voiceSyn
{
List<InstalledVoice> voices = new();

using (ObjectTokenCategory category = ObjectTokenCategory.Create(SAPICategories.Voices))
ReadOnlySpan<string> 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;
}

Expand Down

0 comments on commit 38366f0

Please sign in to comment.