Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

Incorporating Text to Speech Capability in VB.Net

I wanted to start a thread that provides RELEVANT and ERROR-FREE vb.net code that accommodates text-to-speech algorithms.

The web is downright SPARCE with coding examples of this kind.

If you have ANY code related to TTS, paste here so we can ALL benefit from it - PREFERABLY VB.NET code!

All code that falls under this thread should first be handled as follows:

First, import the COM Object as a reference made to the Microsoft Speech Object Library, then add the following code above the Public CLASS Line of the form's code window:

Imports SpeechLib

Once completed, add the following code listed in CODE EXAMPLE 1 to create text-to-speech capability within a text box. In addition to possessing Visual Studio 2003, the user MUST have both a speech engine And a sound card installed On the PC for this code to work properly...

'CODE EXAMPLE 1: Connect the following code To a button's CLICK event

Private Sub Btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn1.Click
        Try
            Dim x As String = Me.TxtBox1.Text
            Dim voice As New SpVoice
            voice.Speak(x, SpeechVoiceSpeakFlags.SVSFlagsAsync)
        Catch ex As Exception
            MsgBox("Your PC cannot play the sound because:" & vbCrLf _
            & "(1) No speech recognition engine Is installed On this PC, Or" _
            & vbCrLf & "(2) This PC has no sound card", MsgBoxStyle.Critical + _
            MsgBoxStyle.OKOnly, "Critical Error")
        End Try
    End Sub
------------------------------------

'CODE EXAMPLE 2: Connect the following code to a button's CLICK event to save text box contents as a WAV File:

Private Sub Btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn1.Click
        Try
            If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
                Dim xVoice As New SpVoice
                Dim xStream As New SpFileStream
                Dim FileMode As SpeechStreamFileMode _
                = SpeechStreamFileMode.SSFMCreateForWrite

                xStream.Open(Me.SaveFileDialog1.FileName, FileMode, False)
                xVoice.AudioOutputStream = xStream
                xVoice.Speak(Me.TxtBox1.Text)
                xStream.Close()
            End If
            MsgBox("Sound file successfully saved" _
            , MsgBoxStyle.OKOnly, "File Saved")
        Catch ex As Exception
            MsgBox("Your PC cannot play the sound because:" & vbCrLf _
            & "(1) No speech recognition engine Is installed On this PC, Or" _
            & vbCrLf & "(2) This PC has no sound card", MsgBoxStyle.Critical + _
            MsgBoxStyle.OKOnly, "Critical Error")
        End Try
    End Sub

Brice Richard
Tuesday, June 21, 2005

*  Recent Topics

*  Fog Creek Home