SIGN UP MEMBER LOGIN:    
ARTICLE

Buliding Text to Speech Applications using SAPI 5.1 and C#

Posted by G Gnana Arun Ganesh Articles | Speech in C# January 03, 2002
In this article,The Speech application programming interface (SAPI) considerably decreases the code necessary for an application to use speech recognition and text-to-speech, making speech technology more handy and robust for broad range of applications
Reader Level:

SAPI 5 Overview:

The Speech application programming interface (SAPI) considerably decreases the code necessary for an application to use speech recognition and text-to-speech, making speech technology more handy and robust for broad range of applications.

The SAPI API provides a high-level interface between an application and speech engines. SAPI implements all the low-level details needed to control and deal with the real-time operations of various speech engines.

The two fundamental types of SAPI engines are text-to-speech (TTS) systems and speech recognizers.

SAPI 5.1 supports OLE automation. That means languages other than C/C++ may now use SAPI for application development. The languages themselves require supporting OLE automation. Common languages which may be used comprises Visual Basic, C#, and JScript.

Now let us see how to use SAPI 5.1 in C#.

With the help of TlbImp.exe tool we can generate SpeechLib.dll file.

The Program:

namespace TTS
{
using System;
using System.WinForms;
using System.Threading;
using SpeechLib;
public class Tts: System.WinForms.Form
{
private System.WinForms.Button button1;
private System.WinForms.CheckBox checkBox1;
private System.WinForms.Button SPEAK;
private System.WinForms.TextBox textBox1;
public Tts()
{
InitializeComponent();
}
public override void Dispose()
{
}
private void InitializeComponent()
{
this.checkBox1 = new System.WinForms.CheckBox();
this.SPEAK = new System.WinForms.Button();
this.button1 = new System.WinForms.Button();
this.textBox1 = new System.WinForms.TextBox();
checkBox1.Location =
new System.Drawing.Point(88, 280);
checkBox1.Text = "Save To Wave.";
checkBox1.Size =
new System.Drawing.Size(112, 24);
checkBox1.TabIndex = 2;
checkBox1.BackColor = System.Drawing.Color.DodgerBlue;
SPEAK.Location =
new System.Drawing.Point(96, 96);
SPEAK.BackColor = System.Drawing.Color.DodgerBlue;
SPEAK.Size = new System.Drawing.Size(96, 24);
SPEAK.TabIndex = 1;
SPEAK.Text = "SPEAK";
SPEAK.Click +=
new System.EventHandler(SPEAK_Click);
button1.Location =
new System.Drawing.Point(88, 72);
button1.BackColor = System.Drawing.Color.DodgerBlue;
button1.Size =
new System.Drawing.Size(112, 24);
button1.TabIndex = 3;
button1.Text = "EXIT";
button1.Click +=
new System.EventHandler(button1_Click);
textBox1.Location =
new System.Drawing.Point(40, 120);
textBox1.Text = " ";
textBox1.Multiline =
true;
textBox1.ForeColor = (System.Drawing.Color)System.Drawing.Color.FromARGB
(
byte)192, (byte)0, (byte)0);
textBox1.TabIndex = 0;
textBox1.Size =
new System.Drawing.Size(216, 160);
textBox1.BackColor = (System.Drawing.Color)System.Drawing.Color.FromARGB
(
byte)192, (byte)192, (byte)255);
this.Text = "TEXT TO SPEECH APP _ ARUNGG";
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = (System.Drawing.Color)System.Drawing.Color.FromARGB((byte)
28, (
byte)128, (byte)255);
this.ClientSize = new System.Drawing.Size(312, 349);
this.Controls.Add(button1);
this.Controls.Add(checkBox1);
this.Controls.Add(SPEAK);
this.Controls.Add(textBox1);
}
static void Main()
{
Application.Run(
new Tts());
}
private void SPEAK_Click(object sender, System.EventArgs e)
{
try
{
SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
SpVoice speech =
new SpVoice();
if (checkBox1.Checked)
{
SaveFileDialog sfd =
new SaveFileDialog();
sfd.Filter = "All files (*.*)|*.*|wav files (*.wav)|*.wav";
sfd.Title = "Save to a wave file";
sfd.FilterIndex = 2;
sfd.RestoreDirectory =
true;
if (sfd.ShowDialog()== DialogResult.OK)
{
SpeechStreamFileMode SpFileMode = SpeechStreamFileMode.SSFMCreateForWrite;
SpFileStream SpFileStream =
new SpFileStream();
pFileStream.Open(sfd.FileName, SpFileMode,
false);
speech.AudioOutputStream = SpFileStream;
speech.Speak(textBox1.Text, SpFlags);
speech.WaitUntilDone(Timeout.Infinite);
SpFileStream.Close();
}
}
else
{
speech.Speak(textBox1.Text, SpFlags);
}
}
catch
{
MessageBox.Show("Speak error");
}
}
private void button1_Click(object sender, System.EventArgs e)
{
this.Close();
}
}
}

Output:



Explanation:

  • This TTS application demonstrates how to create a SpVoice object and how to use it to speak text and save  it to a .wav file.
  • Declare the SpVoice object. //SpVoice speech = new SpVoice();//
  • Create a wave stream //SpFileStream SpFileStream = new SpFileStream();//
  • Create a new .wav file for writing. //SpFileStream.Open(sfd.FileName,SpFileMode, false);//
  • Set the .wav file stream as the output for the Voice object.//speech.AudioOutputStream = pFileStream;//
  • Call the Speak method now will send the output to the .wav file. //speech.Speak textBox1.Text,SpFlags);//
  • Close the file. //SpFileStream.Close();//

Login to add your contents and source code to this article
share this article :
post comment
 

i want the whole code

Posted by sally aly May 31, 2010


The SAPI 5.3 works with xp yes

Posted by Bechir Bejaoui Mar 18, 2009

hi can SAPI work with win xp? thanks

Posted by mary vandad Mar 16, 2009

good job, just digging into speech and ran accross your article, thanks mark woodlief avalanche enterprises www.avalancheenterprises.com

Posted by Mark Woodlief Dec 12, 2007

I want a female voice in the text to speech using C# .What modifications are required in the above code because we have a female bot in our project.

Posted by Arian Joe Feb 04, 2007
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor