Search code examples
c#accessibilitysapi

Calling SAPI in C# for TTS but it is using a hidden menu instead of windows default


So for this let's just use the test program provided by Microsoft to keep it simple. Before running it you will need to go to manage NuGet package for project and add speech.synthesis. But after doing so, it will work.

using System;
using System.Speech.Synthesis;

namespace SampleSynthesis

{
    class Program
    {
        static void Main(string[] args)
    {

        // Initialize a new instance of the SpeechSynthesizer.  
        SpeechSynthesizer synth = new SpeechSynthesizer();

        // Configure the audio output.   
        synth.SetOutputToDefaultAudioDevice();

        // Speak a string.  
        synth.Speak("This example demonstrates a basic use of Speech Synthesizer");

        Console.WriteLine();
        Console.WriteLine("Press any key to exit...");
        Console.ReadKey();
    }
}

So here is the problem: it will talk with the default voice. But it doesn't use the normal default voice. If you go to control panel - change text to speech settings, those settings do not impact the voice used here.

But if you go here: %windir%\sysWOW64\speech\SpeechUX\SAPI.cpl

And change the settings there, it will impact the program.

I am making a program that is focused on being accessible to the visually impaired, and I would very much like for it to use the actual defaults, and not the defaults from this menu that's hard to find.

I tried to see if it was possible to read through the reg key then have it pick from the list after reading the key, but the one I found:

Computer\HKEY_CURRENT_USER\SOFTWARE\Microsoft\Speech\Voices

Is showing the default for the SAPI menu, and not the control panel menu.

I've read through all the documentation on the commands but I don't see a way to make it pay attention to the actual default for the user. Using a voice that they didn't select through the normal options seems strange, as changing the voice for my program would require them to go to this strange menu.

How would I get SAPI to pay attention to the control panel menu?

To clarify, it's also equally as important to get the actual text speed and volume that has been set up.


Solution

  • SAPI is a really, really old API, and Windows 8 (and beyond) have switched to the WinRT TTS APIs.

    In particular, the Windows Control Panel TTS settings set the defaults for the WinRT TTS APIs in the Windows.Media.SpeechSynthesis namespace.

    Luckily for you, however, changing your C# code to use the WinRT APIs is pretty straightforward; most of the work should be just changing the namespace and assembly reference.