One of the new features that came out with .NET 3.5 and 4.0 is the addition of System.Speech library. This library is a collection of classes that enables you to speech recognition and speech synthesis (text-to-speech).
Speech synthesis artificially produces human speech and talk to users. Windows 7's Text-To-Speech (TTS) engine converts text in a specific language into speech. The speech synthesis engine is accessed directly in managed applications by using the classes in System.Speech.Synthesis or, alternatively, by the Speech API (SAPI) when used in unmanaged applications.
Here is a small sample that will use using System.Speech.Synthesis. Add reference to System.Speech.
Create WPF window as below
<Window x:Class="Text_to_Speech.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="400" Width="600" Loaded="Window_Loaded">
<Grid Width="auto" Height="auto">
<Label Content="Voice:" Height="28" HorizontalAlignment="Left" Margin="12,12,0,0" Name="label3" VerticalAlignment="Top" Width="45" /> <ComboBox Height="23" HorizontalAlignment="Left" Margin="54,12,0,0" Name="comboVoice" ItemsSource="{Binding}" VerticalAlignment="Top" Width="110" />
<Label Height="28" HorizontalAlignment="Left" Margin="182,12,0,0"
Name="label1" VerticalAlignment="Top" Width="54">Volume:</Label>
<Slider Height="23" HorizontalAlignment="Left" Margin="231,12,0,0" Name="sliderVolume" VerticalAlignment="Top" Width="110" Value="50" Maximum="100" ValueChanged="sliderVolume_ValueChanged" />
<Label Height="28" Margin="369,12,173,0" Name="label2" VerticalAlignment="Top">
Rate:</Label>
<Slider Height="23" HorizontalAlignment="Left" Margin="402,12,0,0" Maximum="5" Name="sliderRate" Value="3" VerticalAlignment="Top" Width="110" ValueChanged="sliderRate_ValueChanged" />
<Button Height="23" HorizontalAlignment="Right" Margin="0,0,12,8"
Name="ButtonSpeak" VerticalAlignment="Bottom" Width="101" Click="ButtonSpeak_Click">
Speak
</Button>
<RichTextBox Margin="54,106,0,37" Name="richTextBox1" />
<Button Height="23" HorizontalAlignment="Left" Margin="351,77,0,0"
Name="OpenTextFileButton" VerticalAlignment="Top" Width="110" Click="OpenTextFileButton_Click">
Open a Text File
</Button>
<TextBox Height="23" Margin="54,77,0,0" Name="FileNameTextBox" VerticalAlignment="Top"
HorizontalAlignment="Left" Width="299" />
<Label Content="(Open text file or type text in textbox)" HorizontalAlignment="Left" Margin="12,54,0,0" Name="label4" VerticalAlignment="Top" />
<Label Height="28" HorizontalAlignment="Left" Margin="266,330,0,0" Name="labelProgress" VerticalAlignment="Top" />
<Label Content="Text:" Height="28" HorizontalAlignment="Left" Margin="12,106,0,0" Name="label5" VerticalAlignment="Top" />
<Label Content="File:" Height="28" HorizontalAlignment="Left" Margin="12,75,0,0" Name="label6" VerticalAlignment="Top" />
<Label Height="28" HorizontalAlignment="Left" Margin="12,330,0,0" Name="labelState" VerticalAlignment="Top" />
</Grid>
</Window>
Now let's start with the code
- Add using directive
Imports System.Speech.Synthesis
-
Initialize speechsynthesizer object
'speech synthesizer
Private synthesizerAs SpeechSynthesizer
-
Add speechsynthesizer events on window load
Private Sub Window_Loaded(ByVal senderAs Object,ByVal e As RoutedEventArgs)
synthesizer = New SpeechSynthesizer()
'#Region "synthesizer eventes"
synthesizer.StateChanged += New EventHandler(Of StateChangedEventArgs)(synthesizer_StateChanged)
synthesizer.SpeakStarted += New EventHandler(Of SpeakStartedEventArgs)(synthesizer_SpeakStarted)
synthesizer.SpeakProgress += New EventHandler(Of SpeakProgressEventArgs)(synthesizer_SpeakProgress)
synthesizer.SpeakCompleted += New EventHandler(Of SpeakCompletedEventArgs)(synthesizer_SpeakCompleted)
'#End Region
End Sub
-
Add event handlers
Private Sub synthesizer_StateChanged(ByVal senderAs Object,ByVal e As StateChangedEventArgs)
'show the synthesizer's current state
labelState.Content = e.State.ToString()
End Sub
Private Sub synthesizer_SpeakStarted(ByVal senderAs Object,ByVal e As SpeakStartedEventArgs)
End Sub
Private Sub synthesizer_SpeakProgress(ByVal sender As Object, ByVal e As SpeakProgressEventArgs)
'show the synthesizer's current progress
labelProgress.Content = e.Text
End Sub
Private Sub synthesizer_SpeakCompleted(ByVal senderAs Object,ByVal e As SpeakCompletedEventArgs)
'reset when complete
ButtonSpeak.Content = "Speak"
labelProgress.Content = ""
End Sub
-
And finally the buttonSpeak_click
Private Sub ButtonSpeak_Click(ByVal senderAs Object,ByVal e As RoutedEventArgs)
If comboVoice.SelectedItemIsNot Nothing Then
synthesizer.SelectVoice(comboVoice.SelectedItem.ToString())
End If
synthesizer.Volume = Convert.ToInt32(sliderVolume.Value)
synthesizer.Rate = Convert.ToInt32(sliderRate.Value)
Select Case synthesizer.State
'if synthesizer is ready
Case SynthesizerState.Ready
synthesizer.SpeakAsync(ConvertRichTextBoxContentsToString())
ButtonSpeak.Content = "Pause"
Exit Select
'if synthesizer is paused
Case SynthesizerState.Paused
synthesizer.[Resume]()
ButtonSpeak.Content = "Pause"
Exit Select
'if synthesizer is speaking
Case SynthesizerState.Speaking
synthesizer.Pause()
ButtonSpeak.Content = "Resume"
Exit Select
End Select
End Sub
The resulting screen of the application will be as-