comentsys.wordpress.com today features a Universal Windows Platform tutorial showing how to create an Speaking App which allows text files to be opened then read out in different Voices using text-to-speech – they can also be saved back as a text file or the spoken version as an audio file.
The Library class defines two const for file extensions for “.txt” and “.wav” then there’s a SpeechSynthesizer which is the core of the text-to-speak functionality. The OpenAsync method uses a FileOpenPicker to select a file with the “.txt” extension and returns a string using PickSingleFileAsync and ReadTextAsync to get the contents of the Text file. The SaveAsync takes in contexts for a Text file as a string and uses a FileSavePicker with a default extension as “.txt” but also there’s two file type choices – one is the “.txt” and the other is the “.wav” extension to get the StorageFile with PickSaveFileAsync, then if the extension is “.txt” then the file is saved with WriteTextAsync, it the extension is “.wav” then a SpeechSynthesisStream is used with a DataReader and LoadAsync to get the contents of the SpeechSynthesisStream as a IBuffer which is then written using WriteBufferAsync.
The Speak method takes in string text to read and a MediaElement to use for playback – if the this is Playing then Stop is called otherwise a SpeechSynthesisStream is obtained from SynthesizeTextToStreamAsync using the text and then the MediaElement is set to have AutoPlay to true and the SetSource set from the SpeechSynthesisStream. Then Voices is used to get a list of all the voices that the SpeechSynthesizer supports and Voice gets a Voice by an Id, New is used to reset the TextBox and MediaElement, Open reads in a Text file, Save writes to a Text File or Wave File and Play reads the Text contents using text-to-speech.
Behind the Scenes
Text-to-speech is an interesting technology to use as it enables additional options and functionality when thinking about text beyond what can be done visually by building in the functionality you can have dictation features within any application with just a few lines of code to turn text into speech. This example shows how easy it is to use the SpeechSynthesizer to translate simple text into speech, it also supports Speech Synthesis Markup Language (SSML) which allows more finer control over how the context is spoken if needed.