Text to Speech

Friday, May 29, 2009

Step 1: Add a reference with com object Mocrosoft speech Object Library
St2p 2:Import Sppechlib

Code :

Dim voice As New SpVoice
___________________________________________________________
Read from the File

OpenFileDialog.Filter = "Text Files (*.txt)|*.txt"
If OpenFileDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim fs As New System.IO.FileStream(Me.OpenFileDialog.FileName, IO.FileMode.Open, IO.FileAccess.Read)
Dim fr As New System.IO.StreamReader(fs)
Me.txttext.Text = fr.ReadToEnd
End If

___________________________________________________________

Some of the Property to be used

voice.Pause() ' For Pause
voice.resume() ' For Resume
voice.Rate = Val(txtrate.Text) 'speed
voice.Volume = Val(txtvolume.Text) ' volume

voice.Speak(Me.txttext.Text, SpeechVoiceSpeakFlags.SVSFlagsAsync) ' For Speek

________________________________________________________________

Save to a Wav File:
Dim cpFileStream As New SpeechLib.SpFileStream
Dim filename As String = "C:\rajiv.wav"
Try
SaveFileDialog.Filter = ".Wav File|*.wav"
If SaveFileDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
filename = SaveFileDialog.FileName
If File.Exists(filename) Then Kill(filename)
Dim oVoice As New SpeechLib.SpVoice
cpFileStream.Open(filename, SpeechLib.SpeechStreamFileMode.SSFMCreateForWrite, False)
oVoice.AudioOutputStream = cpFileStream
oVoice.Volume = Val(txtvolume.Text)
oVoice.Rate = Val(txtrate.Text)
oVoice.Speak(txttext.Text, SpeechLib.SpeechVoiceSpeakFlags.SVSFDefault)
oVoice = Nothing
cpFileStream.Close()
cpFileStream = Nothing
MsgBox("File Saved Successfully!!!!", vbInformation)
End If
_____________________________________________________________

0 comments

Post a Comment