How do I use a lexicon with SpeechSynthesizer?

前端 未结 3 1093
逝去的感伤
逝去的感伤 2020-12-31 06:15

I\'m performing some text-to-speech and I\'d like to specify some special pronunciations in a lexicon file. I have ran MSDN\'s AddLexicon example verbatim, and it speaks the

3条回答
  •  旧巷少年郎
    2020-12-31 06:28

    I've been looking into this a little recently on Windows 10.

    There are two things I've discovered with System.Speech.Synthesis.

    Any Voice you use, must be matched against the language in the Lexicon file. Inside the lexicon you have the language:

    I find that I can name my Lexicon as "blue.en-US.pls" and make a copy with "blue.en-GB.pls". Inside it will have xml:lang="en-GB"

    In the code you'd use:

    string langFile = Path.Combine(_appPath, $"blue.{synth.Voice.Culture.IetfLanguageTag}.pls");
    synth.AddLexicon(new Uri(langFile), "application/pls+xml");

    The other thing I discovered is, it doesn't work with "Microsoft Zira Desktop - English (United States)" at all. I don't know why. This appears to be the default voice on Windows 10.

    Access and change your default voice here: %windir%\system32\speech\SpeechUX\SAPI.cpl

    Otherwise you should be able to set it via code:

    var voices = synth.GetInstalledVoices();
    // US: David, Zira. UK: Hazel.
    var voice = voices.First(v => v.VoiceInfo.Name.Contains("David"));
    synth.SelectVoice(voice.VoiceInfo.Name);

    I have David (United States) and Hazel (United Kingdom), and it works fine with either of those. This appears to be directly related to whether the voice token in the registry has the SpLexicon key value. The Microsoft Zira Desktop voice does not have this registry value. While Microsoft David Desktop voice has the following: Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-US_DAVID_11.0\Attributes\SpLexicon = {0655E396-25D0-11D3-9C26-00C04F8EF87C}

提交回复
热议问题