Monday, November 11, 2013

WPF : Easy Text To Speech (speech synthesis from text)



There is two way to generate speech from text with WPF :

- Using the Windows System.Speech library that comes with .net framework 3.5
- Using a distant web service such as Google Text2Speech API


In this article, we will focus on the second way to do that, that means we will call Google REST web services to generate a MP3 file from a sample text.
We will use nAudio (http://naudio.codeplex.com) to play the resulting MP3 file into our application.

Ok, let's start !

The Google API URL to call is : 
http://translate.google.com/translate_tts?tl=[CULTURE]&q=[YOUR_TEXT]

For example : 
http://translate.google.com/translate_tts?tl=en&q=Hello+world


Culture can be : en, fr

Here is a list of the supported languages :
English, Afrikaans, Albanian, Catalan, Chinese (Mandarin), Croatian, Czech, Danish, Dutch, Finnish, Greek, Hungarian, Icelandic, Indonesian, Latvian, Macedonian, Norwegian, Polish, Portuguese, Romanian, Russian, Serbian, Slovak, Swahili, Swedish, Turkish, Vietnamese, Welsh, Haitian Creole, French, Italian and German...

Your text must be string with %20 or + instead of spaces (ex : &q=Hello%20world or &q=Hello+world)

Keep in mind that an URL shouldn't be more than 255 caracters otherwise it can be truncated by routers, proxies...

More info about that API :
http://techcrunch.com/2009/12/14/the-unofficial-google-text-to-speech-api

The c# code is quite simple, it just make an HTTP call to the Google URL and save or play the resulting audio stream.


Have fun with that :)

You can download the source code of this sample project (VS2012 / C#) :
WPFText2Speech.zip



1 comment: