Speech synthesis example

The last couple of posts have dealt with recognition. The other half of speech is synthesis. The System.Speech.Synthesis namespace contains a variety of classes for rendering text to speech. Here's some sample code that uses a PromptBuilder class to utilize a variety of voices.

Imports

System.Speech.Synthesis

Public

Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim pb As New PromptBuilder

'Sam

      pb.StartVoice(New VoiceInfo("Microsoft Sam"))

      pb.AppendText("I am Sam. Sam I am. Today's date is")

      pb.AppendTextWithFormat(DateTime.Today.Month.ToString + "/" _

                                                   + DateTime.Today.Day.ToString, _

                                                   PromptFormat.DateMonthDay)

      'Mike

      pb.StartVoice(New VoiceInfo("Microsoft Mike"))

      pb.AppendText("I'm Mike. Anything Sam can do I can do better.")

      'Mary

      pb.StartVoice(New VoiceInfo("Microsoft Mary"))

      pb.AppendText("I'm Mary. Anything Mike and Sam can do I can do too.")

      'pause for 1 second

      pb.AppendBreak(New System.TimeSpan(0, 0, 1))

      pb.EndVoice()

      'back to Mike

      pb.AppendText("But can you spell potato?")

      pb.AppendTextWithFormat("potato", PromptFormat.SpellOut)

      pb.AppendText("What do you think, Sam?")

      pb.EndVoice()

      'back to Sam

      pb.AppendText("That's the stuff dreams are made of.")

      pb.EndVoice()

      'say it all

      Dim synth As New SpeechSynthesizer

      synth.SpeakAsync(pb)

      End Sub

End Class

In this case, I've chosen the voices included in the SAPI SDK. But in general, any SAPI 5 compliant voice should work (although we recently learned that the current beta of the API doesn't work with Cepstral's voices, so if you have one of those, sorry, it doesn't work right now).