|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionI was always fascinated whenever I used Acrobat Reader's Read Out options. I found that Adobe Reader uses the Windows Speech engine. Almost all Windows OSs are shipped with the Speech engine. We can also use this engine programatically. There are many features available with the Speech engine, like speech recognition, text to speech, etc. With speech recognition, you can interact with your PC using voice commands rather than GUI commands. In this example, I have shown how to use the TTS feature of the Speech engine. BackgroundWindows XP is shipped with the Text-To-Speech engine. You can verify this by going to Control Panel ->Speech ->Text to speech. If this engine is not installed with your OS version, you can download it from Microsoft Speech SDK 5.1. If you want to use the TTS feature on a web browser, you can use an ActiveX provided by Microsoft by applying the new ActiveXObject (Sapi.SpVoice) in your JavaScript. A little about SAPIMicrosoft Speech API (SAPI) contains many interfaces and classes for managing speech. For TTS, the base class is
Methods:
Using the codeTo start with SAPI in your .NET application, you have to first add a reference to SAPI.dll from the path C:\Program Files\Common Files\Microsoft Shared\Speech if SAPI is not appearing in COM tab of Add Reference. Following is the code that generates audio based on the text entered. Note that I am assigning the Private Sub btnSpeak_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs)
Handles btnSpeak.Click
Me.Cursor = Cursors.WaitCursor
Dim oVoice As New SpeechLib.SpVoice
Dim cpFileStream As New SpeechLib.SpFileStream
oVoice.Voice = oVoice.GetVoices.Item(cmbVoices.SelectedIndex)
oVoice.Volume = trVolume.Value
oVoice.Speak(txtSpeach.Text,
SpeechLib.SpeechVoiceSpeakFlags.SVSFDefault)
oVoice = Nothing
Me.Cursor = Cursors.Arrow
End Sub
Find all available voices and then bind with Voice ComboBox by using the Private Sub Form1_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs)
Handles MyBase.Load
Dim x As New SpeechLib.SpVoice
Dim arrVoices As SpeechLib.ISpeechObjectTokens = x.GetVoices
Dim arrLst As New ArrayList
For i As Integer = 0 To arrVoices.Count - 1
arrLst.Add(arrVoices.Item(i).GetDescription)
Next
cmbVoices.DataSource = arrLst
End Sub
To save the audio output to a file, you must use If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim oVoice As New SpeechLib.SpVoice
Dim cpFileStream As New SpeechLib.SpFileStream
cpFileStream.Open(SaveFileDialog1.FileName,
SpeechLib.SpeechStreamFileMode.SSFMCreateForWrite, False)
oVoice.AudioOutputStream = cpFileStream
oVoice.Voice = oVoice.GetVoices.Item(cmbVoices.SelectedIndex)
oVoice.Volume = trVolume.Value
oVoice.Speak(txtSpeach.Text, SpeechLib.SpeechVoiceSpeakFlags.SVSFDefault)
oVoice = Nothing
cpFileStream.Close()
cpFileStream = Nothing
End If
ReferencesSince this example is on TTS, those who are interested in Speech recognition and grammar can refer to this article. For more details on Speech SDK, please refer to this article. History
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||