Click here to Skip to main content
15,860,861 members
Articles / Programming Languages / C#
Article

Text to Speech using Windows SAPI

Rate me:
Please Sign up or sign in to vote.
4.88/5 (24 votes)
25 Jun 2007CPOL2 min read 161.5K   11K   106   20
An article on how to use WindowsText-To-Speach (TTS)

Screenshot - TTS.jpg

Introduction

I 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.

Background

Windows 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 SAPI

Microsoft Speech API (SAPI) contains many interfaces and classes for managing speech. For TTS, the base class is SpVoice. The following are some important properties:

  • Voice Object of type SpObjectToken, which is inherited from ISpeechObjectTokens
  • Volume Integer specifies intensity of voice
  • AudioOutputStream specifies the stream for audio output; if you want to save it on file, use SpFileStream of SAPI
  • SynchronousSpeakTimeout specifies the milliseconds after which the voice's synchronous Speak and SpeakStream calls will time out

Methods:

  • GetVoices() returns all available voices; I have use this to populate the voice-type comboBox
  • Speak() returns the audio on the output stream (speaker/ file)
  • Pause() pauses the audio output
  • Resume() resumes the audio output
  • WaitUntilDone() blocks application execution while a voice is speaking asynchronously

Using the code

To 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 Voice property a value based on the voice type selected from the combo box. At form_load, I have filled the combo box with all available voices. See the next code section.

C#
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 GetVoices() method on the SpVoice class object. Note the list of available voices. We can use the getDescription() method to find out the voice name, e.g. LH Michael.

C#
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 SpFileStream and set AudioOutPutStream equal to your stream object of type SpFileStream.

C#
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

References

Since 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

  • 25 June, 2007 -- Original article posted

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
I have done Master Degree in Computers and MCAD and working on Microsoft technologie since last 4 yrs. Currently working with TCS (India).

Comments and Discussions

 
QuestionSpanish voice Pin
Member 126903714-Sep-16 10:23
Member 126903714-Sep-16 10:23 
QuestionNot working in windows Service Pin
prithvirajn8-Oct-14 2:52
prithvirajn8-Oct-14 2:52 
GeneralMy vote of 1 Pin
jjborella13-Oct-13 14:50
jjborella13-Oct-13 14:50 
GeneralMy vote of 1 Pin
jjborella13-Oct-13 14:50
jjborella13-Oct-13 14:50 
GeneralHow to make the file size smaller? Pin
danilovirus00728-Aug-12 17:10
danilovirus00728-Aug-12 17:10 
GeneralRe: How to make the file size smaller? Pin
iProgramIt8-Jun-15 13:48
professionaliProgramIt8-Jun-15 13:48 
General:) Pin
Zoe-Jojo26-Apr-11 18:30
Zoe-Jojo26-Apr-11 18:30 
GeneralNew Idea Pin
rjswaroop200421-Feb-10 17:54
rjswaroop200421-Feb-10 17:54 
Questionmultilanguage support how? Pin
29181195-Mar-09 1:57
29181195-Mar-09 1:57 
GeneralQuestion Pin
M4nglio26-Feb-09 9:15
M4nglio26-Feb-09 9:15 
QuestionCan we create this kind of tool with different language? Pin
Reaksmey Rin11-Sep-08 18:01
Reaksmey Rin11-Sep-08 18:01 
Generalgreats Pin
lousado5-Jun-08 11:46
lousado5-Jun-08 11:46 
GeneralLove it, works great! Pin
DracX5-Jul-07 10:54
DracX5-Jul-07 10:54 
GeneralGreat Job Pin
PurnaS26-Jun-07 18:22
PurnaS26-Jun-07 18:22 
GeneralDifferent Pin
RRoyycce26-Jun-07 12:08
RRoyycce26-Jun-07 12:08 
GeneralKewl stuff Pin
ChipLeader26-Jun-07 4:41
ChipLeader26-Jun-07 4:41 
GeneralMore voices Pin
Amatista25-Jun-07 22:58
Amatista25-Jun-07 22:58 
GeneralRe: More voices Pin
Saifi Hasan26-Jun-07 3:23
Saifi Hasan26-Jun-07 3:23 
GeneralRe: More voices Pin
Amatista26-Jun-07 7:53
Amatista26-Jun-07 7:53 
GeneralNice Start Pin
MCSDvikasmisra25-Jun-07 20:05
MCSDvikasmisra25-Jun-07 20:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.