Text to Speech In C#

Text to Speech is very usefull in Web Application as well as in windows application.Microsoft provide Speech Object Library  that is use for this purpose.Here i have mention steps to implement text to speech in C#.

1). Add Speech Object Library reference to your Application using right click on Project & click on Add Reference.

2). After Adding Referece of Speech Object Library Drag one textbox control on your WinForm & give the name txttexttospeech.

3). After that put this belowed code on your page load event of the form.

 

   1: SpVoice voice = new SpVoice();
   2: voice.Voice = voice.GetVoices("Name=LH Michael", "Language=409").Item(0);
   3: voice.Speak(txttexttospeech.Text, SpeechVoiceSpeakFlags.SVSFDefault);
Advertisements

16 thoughts on “Text to Speech In C#

  1. Jay

    Text to Speech is indeed a very cool thing and it’s so easy to integrate in .NET. I never figured out how those language files are synthesized… But as long as it’s easy to code, it’s great!

  2. Madhukar

    hi.. can you tell me the procedure and namespaces to implement text to speech in webapplication. And thats a good work that you have done in windows application.

    Thanks

  3. I have done the c# text to speech project but its giving 1 error + 89 warnings. they are. how to correct this during set up it says to exclude sapi.dll . i’m in urgent plz help me ..
    Error 90 ‘sapi.dll’ should be excluded because its source file ‘C:\Program Files\Common Files\Microsoft Shared\Speech\sapi.dll’ is under Windows System File Protection. C:\Documents and Settings\Administrator.SERVER\Desktop\siren withou talk\Siren\Setup1\Setup1.vdproj Setup1

    Warning 1 At least one of the arguments for ‘SpObjectTokenCategory.GetStringValue’ cannot be marshaled by the runtime marshaler. Such arguments will therefore be passed as a pointer and may require unsafe code to manipulate. Siren
    Warning 2 At least one of the arguments for ‘SpObjectTokenCategory.EnumKeys’ cannot be marshaled by the runtime marshaler. Such arguments will therefore be passed as a pointer and may require unsafe code to manipulate. Siren
    Warning 3 At least one of the arguments for ‘SpObjectTokenCategory.EnumValues’ cannot be marshaled by the runtime marshaler. Such arguments will therefore be passed as a pointer and may require unsafe code to manipulate. Siren
    Warning 4 At least one of the arguments for ‘SpObjectTokenCategory.GetId’ cannot be marshaled by the runtime marshaler. Such arguments will therefore be passed as a pointer and may require unsafe code to manipulate. Siren
    Warning 5 At least one of the arguments for ‘SpObjectTokenCategory.GetDefaultTokenId’ cannot be marshaled by the runtime marshaler. Such arguments will therefore be passed as a pointer and may require unsafe code to manipulate. Siren
    Warning 6 At least one of the arguments for ‘ISpObjectTokenCategory.GetStringValue’ cannot be marshaled by the runtime marshaler. Such arguments will therefore be passed as a pointer and may require unsafe code to manipulate. Siren
    Warning 7 At least one of the arguments for ‘ISpObjectTokenCategory.EnumKeys’ cannot be marshaled by the runtime marshaler. Such arguments will therefore be passed as a pointer and may require unsafe code to manipulate. Siren
    Warning 8 At least one of the arguments for ‘ISpObjectTokenCategory.EnumValues’ cannot be marshaled by the runtime marshaler. Such arguments will therefore be passed as a pointer and may require unsafe code to manipulate. Siren

    follows……

  4. Morgan

    for Visual Studeio 2010 (Pro).. and probably earlier versions, you also need to add:

    using System.Speech.Synthesis;

    with the other using statements at beginning of program

  5. Hi dhaval,

    I want create C# apps for Speech text exact opposite to your apps. i have tried following. but it not given exact out put when my wave file speech is “I am table” it give out put text as “A table” please try to solve and help me

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using SpeechLib;

    namespace Speech_To_Text
    {
    public partial class Form1 : Form
    {
    private SpeechLib.ISpeechRecoContext wavRecoContext = null;
    private SpeechLib.SpFileStream InputWAV = null;
    private SpeechLib.ISpeechRecoGrammar Grammar = null;

    private String _WAVFile = null;
    private string strData = “No recording yet”;
    private String _lastRecognized = “”;
    public Form1()
    {
    InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
    OpenFileDialog dialog = new OpenFileDialog();
    dialog.Title = “Select a Speech file”;
    dialog.Filter = “Wave Files (.WAV)|*.wav”; // Filter files by extension
    dialog.InitialDirectory = Environment.CurrentDirectory + “\\Content”;
    dialog.ShowDialog();
    _WAVFile = dialog.FileName;
    if (String.IsNullOrEmpty(_WAVFile)) return;
    wavRecoContext = new SpeechLib.SpInProcRecoContext();
    ((SpInProcRecoContext)wavRecoContext).Recognition +=
    new _ISpeechRecoContextEvents_RecognitionEventHandler(wavRecoContext_Recognition);
    ((SpInProcRecoContext)wavRecoContext).EndStream += new _ISpeechRecoContextEvents_EndStreamEventHandler(wavRecoContext_EndStream);
    Grammar = wavRecoContext.CreateGrammar(2);

    Grammar.DictationLoad(“”, SpeechLoadOption.SLOStatic);
    InputWAV = new SpFileStreamClass();
    InputWAV.Open(@_WAVFile, SpeechStreamFileMode.SSFMOpenForRead, false);
    wavRecoContext.Recognizer.AudioInputStream = InputWAV;
    Grammar.DictationSetState(SpeechRuleState.SGDSActive);
    //InputWAV.Close();

    }
    private void wavRecoContext_Recognition(int StreamNumber, object StreamPosition, SpeechRecognitionType RecognitionType, ISpeechRecoResult Result)
    {

    strData = Result.PhraseInfo.GetText(0, -1, true);
    //parseSpeechResult(strData);// — Call a function to parse it if wanted
    _lastRecognized = textBox1.Text;

    textBox1.Text =textBox1.Text+ strData;
    }

    private void wavRecoContext_EndStream(int StreamNumber, object StreamPosition, bool f)
    {
    Grammar.DictationSetState(SpeechRuleState.SGDSInactive);
    Console.Out.WriteLine(“Finished Stream”);
    }

    private void button2_Click(object sender, EventArgs e)
    {
    textBox1.Text = “”;
    }

    }

    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s