Hello world to TTS (1)

In this post, I will show you how easily you can make your first TTS application with C#. This example will use the COM interop to call SAPI functionality because SAPI has implemented a number of automation objects that can be directly used in C# or any other .Net languages.

Suppose you have Windows Vista (or Windows 7 Beta ) and Vistual Studio installed. Here is a step by step guide:

1. Create the solution by clicking the menu: File -> New -> Project, select the "Visual C#" -> "Windows" -> "Console Application",

choose a project name "SAPIInterop" . A basic C# project will be created automatically

 2. Add the referrence to SAPI.dll by clicking "Project" -> "Add Referrence" -> "COM". In the list, find and select the "Microsoft Speech Object Library".  A "SpeechLib" referrence will  appear in the project referrences list.

 3. Now we can add some piece of code to make the first "hello world" TTS application.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SpeechLib;
using System.Diagnostics;

namespace SAPIInterop
{
    class Program
    {
        static void Speak(int voiceIdx)
        {
            SpVoice spVoice = new SpVoice();
            ISpeechObjectTokens tokens = spVoice.GetVoices(null, null);
            if (voiceIdx < 0 || voiceIdx >= tokens.Count)
            {
                Console.WriteLine("voice index out of range from [0-{0}]", tokens.Count);
                return;
            }

            spVoice.Voice = tokens.Item(voiceIdx);
            spVoice.Rate = 0;
            spVoice.Speak("Hello world", SpeechVoiceSpeakFlags.SVSFDefault);
        }

        static void ListVoice()
        {
            SpObjectTokenCategory tokenCat = new SpObjectTokenCategory();
            tokenCat.SetId(SpeechLib.SpeechStringConstants.SpeechCategoryVoices, false);
            ISpeechObjectTokens tokens = tokenCat.EnumerateTokens(null, null);
            foreach (SpObjectToken item in tokens)
            {
                Console.WriteLine(item.GetDescription(0));
            }
        }

        static void Main(string[] args)
        {
            // list voices installed on the system
            ListVoice();
            // speak with the second voices. If you have only one voice, change it to Speak(0)
            Speak(1);
        }
    }
}