Search code examples
c#text-to-speechspeech-synthesis

Speech Synthesizer "Input string was not in a correct format"


Here is my code:

string _message = "Hello world.";
SpeechSynthesizer _synth = new SpeechSynthesizer();
Prompt _prompt = new Prompt(_message);
_synth.Speak(_prompt);

I can not for the life of me figure out what exactly is causing this error:

"Input string was not in a correct format."

The line that causes this error is when I call _synth.Speak(_prompt);
EDIT: I have tried this code on my desktop computer and it works fine so something is wrong with my install on my laptop. Still I'm not too sure how to fix this...

EDIT:

The stacktrace:

System.FormatException: Input string was not in a correct format.
   at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffe
r& number, NumberFormatInfo info, Boolean parseDecimal)
   at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo in
fo)
   at System.Speech.Internal.SapiAttributeParser.GetCultureInfoFromLanguageStrin
g(String valueString)
   at System.Speech.Synthesis.VoiceInfo..ctor(VoiceObjectToken token)
   at System.Speech.Internal.Synthesis.VoiceSynthesis.BuildInstalledVoices(Voice
Synthesis voiceSynthesizer)
   at System.Speech.Internal.Synthesis.VoiceSynthesis..ctor(WeakReference speech
Synthesizer)
   at System.Speech.Synthesis.SpeechSynthesizer.get_VoiceSynthesizer()
   at System.Speech.Synthesis.SpeechSynthesizer.Speak(Prompt prompt)
   at TTSTesting.Program.Speak(String _message) in C:\Users\ctanaka\Desktop\TTST
esting\TTSTesting\Program.cs:line 22

Solution

  • The registry of your machine is messed up, it contains invalid voice configuration data. The relevant key is HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens. Underneath, you'll find the installed voices. An English machine typically has MS-Anna there but there can be others if you purchased more.

    The messed up value is Attribute\Language, it isn't an hexadecimal number like it should be. Like "409", the hex value of the LCID for English.

    You might fix it by uninstalling voices you added, deleting bad voices in the registry or fixing the Language value. Reinstalling is tricky, this is part of the Windows setup on Vista and up. You'll need help from superuser.com if you can't get it fixed. Or your setup DVD.