3

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
10
  • 1
    As a side note I have tried this exact code on my other pc and it seemed to work fine.
    – Bob
    Apr 24, 2012 at 19:43
  • Any documentation on that method?
    – Tudor
    Apr 24, 2012 at 19:44
  • This is msdn.microsoft.com/en-us/library/ms586905.aspx right?
    – Mr Lister
    Apr 24, 2012 at 19:46
  • 1
    Can you copy the callstack of the exception? That might show that the error occurred inside the library. Honestly it doesn't look like this code is the issue.
    – Adam V
    Apr 24, 2012 at 22:01
  • 1
    I've seen this error reported once before. At least in that case the programmer tried to make a Polish machine speak English. Anything similar? He didn't post a stack trace either so impossible to diagnose. Apr 25, 2012 at 0:19

4 Answers 4

3

I resolved going into the voices key register and, after a backup of that key, I removed one after one all voices trying the code each time untill the error disappeared. The error was in a Loquendo voice. After that I tried to restore the backup (all voices) looking for the error again and... OS answered that a value cannot be written because it was in use by another application... That's the magic thing: all worked fine!

1

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.

6
  • I checked the registry value for the language and it seems to already be set to 409. Also the text to speech preview in windows 7 speech properties works, so maybe the library .dll is messed up?
    – Bob
    Apr 25, 2012 at 13:36
  • It is a framework assembly, you'd have to work really hard to mess that up. Check all of the voices listed, not just the first one. Use SysInternal's ProcMon tool to see the framework code reading the registry. There are possible complications on a 64-bit operating system. Apr 25, 2012 at 13:46
  • The only voice listed in the registry is MS Anna. How exactly do I see the framework code reading the registry using ProcMon?
    – Bob
    Apr 25, 2012 at 16:35
  • You were right I found that I actually had some Loquendo TTS software installed. After uninstalling, everything works just fine.
    – Bob
    Apr 26, 2012 at 8:16
  • @Chirs seems like i have the same problem here, what's strange is that the exception happens when targeting .NET 4. If i switch to .NET 3.5 then it works.
    – Zmaster
    Oct 14, 2012 at 13:17
1

I had the same problem. Hans Passant pointed us to the fact that this API parses string values from the registry to detect installed languages. I too had a Loquendo TTS language packs installed. Using PROCMON and trial and error, I managed to pinpoint the exact registry key that caused the error (Language). The API expects this string to hold only numeric characters for an integer conversion. Trying to parse and convert this string ("40c") triggers a FormatException:

[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Speech\Voices\Tokens\LQBernard\Attributes] "Language"="40c"

changed to:

"Language"="40" (removed the trailing 'c' character).

I repeated this process for both my installed French Loquendo TTS language packs (Bernard and Juliette) and that solved it for me.

3
  • Doesn't really answer the question. Oct 17, 2012 at 3:07
  • I beg to differ. Bob had a FormatException when trying to use the Speech Synthesis API. Hans Passant pointed us to the fact that this API parses string values from the registry to detect installed languages. Bob and I both had Loquendo TTS language packs installed. Using PROCMON and trial and error, I managed to pinpoint the exact registry key that caused the error (Language). Trying to parse and convert to an integer a string that has non-numeric characters triggers a FormatException. CQFD.
    – fstarnaud
    Oct 26, 2012 at 20:55
  • Ok, I'll concede that. But it wasn't clear how this answers the question. If you will edit your answer to include the information you included in the comment, then I'll be able to remove the downvote. Oct 26, 2012 at 22:55
0

This may not be the source of your problem, but it looks like in the API that you should just be able to call

new SpeechSynthesizer().Speak("Hello world.");

and avoid all the extra code... have you tried removing the period?

2
  • Removing the period doesn't seem to do anything. I have tried a variety of different combinations from complete sentences to simple phrases and it still throws an error.
    – Bob
    Apr 24, 2012 at 21:55
  • It looks like @therealmitchconnors is actually suggesting you remove the Prompt class altogether and Speak the string directly, to see if that's causing the problem.
    – Adam V
    Apr 24, 2012 at 22:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.