Computational Methods for Research in Speech Science

How to use Text-to-Speech within a Visual Basic .NET application

This page provides a tutorial for the use of the Windows Text-to-Speech voices from within a Visual Basic application.

An excellent free version of Visual Basic .NET called Visual Basic 2008 Express Edition can be downloaded from the MSDN web site.

Full documentation for the .NET System.Speech component.

1. A "Hello World!" application

It is very easy just to get an application to say "Hello". Create a Console Application and use Project|Add Reference to add a reference to the System.Speech component:

Then add these lines to the module source:

    Imports System.Speech.Synthesis
    Module Module1
    
        Sub Main()
            Dim synth As New SpeechSynthesizer
            synth.Speak("Hello world from the default voice.")
        End Sub
    
    End Module

2. Enumerate available voices

An elaboration of hello world in which each available voice says hello:

    Imports System.Speech.Synthesis
    Module Module1
    
        Sub Main()
            Dim synth As New SpeechSynthesizer
            Dim voices = synth.GetInstalledVoices()
            For Each v As InstalledVoice In voices
                System.Console.WriteLine(v.VoiceInfo.Name)
                synth.SelectVoice(v.VoiceInfo.Name)
                synth.Speak("Hello from " & v.VoiceInfo.Name)
            Next
        End Sub
    
    End Module

3. Using SSML to modify how words are spoken

The SpeakSSML() function allows you to mark-up the text to speak using Speech Synthesis Markup Language. Here is a simple example:

    Imports System.Speech.Synthesis
    Module Module1
    
        Sub Main()
            Dim synth As New SpeechSynthesizer
            Dim txt As String
            txt = "<speak version='1.0' xml:lang='en-GB'>"
            txt &= "One potato, two potato, three potato, four. "
            txt &= "<prosody rate='-50%'>"
            txt &= "One potato, two potato, three potato, four. "
            txt &= "</prosody></speak>"
            synth.SpeakSsml(txt)
    
        End Sub
    
    End Module

Note that this code expects that you have a British English Voice (en-GB), if you only have American English voices, use en-US.

4. Building a text prompt step-by-step

A PromptBuilder object can be used to build a complicated prompt piece by piece without needing to use SSML markup. For example:

    Imports System.Speech.Synthesis
    Imports System.Globalization
    Module Module1
    
        Sub Main()
            Dim synth As New SpeechSynthesizer
    
            Dim myPrompt = New PromptBuilder(New CultureInfo("en-US"))
    
            Dim mainStyle = New PromptStyle(PromptRate.Medium)
            Dim slowStyle = New PromptStyle(PromptRate.Slow)
    
            myPrompt.StartStyle(slowStyle)
            myPrompt.AppendText("Better")
            myPrompt.AppendTextWithPronunciation("Reading", "riːdɪŋ")
            myPrompt.EndStyle()
            myPrompt.StartStyle(mainStyle)
            myPrompt.AppendText("is a book published by the University of")
            myPrompt.AppendTextWithPronunciation("Reading.", "rɛdɪŋ")
            myPrompt.AppendBreak()
    
            myPrompt.AppendText("The acronym")
            myPrompt.AppendTextWithHint("BBC", System.Speech.Synthesis.SayAs.SpellOut)
            myPrompt.AppendText("stands for the British Broadcasting Corporation.")
            myPrompt.AppendBreak()
    
            myPrompt.EndStyle()
    
            synth.Speak(myPrompt)
        End Sub
    
    End Module

 

Also see:

 

Back to Computational Methods Home