Buliding Text to Speech Applications using SAPI 5.1 and C#


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();//


Similar Articles