Return to Snippet

Revision: 68855
at March 7, 2015 01:10 by warnerBro19


Initial Code
using System;
using System.Threading;
using Ozeki.Media.MediaHandlers;
using Ozeki.Media.MediaHandlers.Speech;
 
namespace Microsoft_Speech_Platform
{
    class Program
    {
        static Speaker _speaker;
        static Microphone _microphone;
        static MediaConnector _connector;
        static TextToSpeech _tts;
        static SpeechToText _stt;
 
        static void Main(string[] args)
        {
            _microphone = Microphone.GetDefaultDevice();
            _speaker = Speaker.GetDefaultDevice();
            _connector = new MediaConnector();
 
            SetupTextToSpeech();
 
            SetupSpeechToText();
 
            while (true) Thread.Sleep(10);
        }
 
        static void SetupTextToSpeech()
        {
            _tts = new TextToSpeech();
            _tts.AddTTSEngine(new MSSpeechPlatformTTS());
 
            var voices = _tts.GetAvailableVoices();
            foreach (var voice in voices)
            {
                if (voice.Language.Equals("en-GB"))
                    _tts.ChangeLanguage(voice.Language, voice.Name);
            }
 
            _speaker.Start();
            _connector.Connect(_tts, _speaker);
            _tts.AddAndStartText("Hello World!");
        }
 
 
        static void SetupSpeechToText()
        {
            string[] words = {"Hello", "Welcome"};
            _stt = SpeechToText.CreateInstance(words);
            _stt.WordRecognized += stt_WordRecognized;
            _stt.ChangeSTTEngine(new MSSpeechPlatformSTT());
 
            var recognizers = _stt.GetRecognizers();
            foreach (var recognizer in recognizers)
            {
                if (recognizer.Culture.Name == "en-GB")
                    _stt.ChangeRecognizer(recognizer.ID);
            }
 
            _connector.Connect(_microphone, _stt);
            _microphone.Start();
        }
 
        static void stt_WordRecognized(object sender, SpeechDetectionEventArgs e)
        {
            Console.WriteLine("Word recognized: {0}", e.Word);
        }
    }
}

Initial URL
http://www.voip-sip-sdk.com

Initial Description
In my previous snippet I have written about converting text to speech using C#. This code snippet can be used not just for allowing your computer to read txt aloud, but also for speech recognition.  To implement this functionality I used Microsoft Speech Platform 11 along with Ozeki VoIP SIP SDK. The first one provides two classes (MSSpeechPlatformSTT, MSSpeechPlatformTTS) for text-to-speech and speech-to-text, and the VoIP SDK ensures the necessary VoIP components. The source code below is ready for use, so you only need to copy&paste it to your Visual Studio, then modify the necessary fields. (Do not forget to add the necessary DLL files to your references: http://www.voip-sip-sdk.com, http://www.microsoft.com/en-us/download/details.aspx?id=27226 ) 

After creating the necessary using lines and media handler objects, you can implement the text-to-speech  and the voice recognition features by using the SetupTextToSpeech() and the SetupSpeechToText() methods.

Have a good time!

Initial Title
How to add text-to-speech and speech-to-text features to your SIP software by using Microsoft Speech Platform in C#?

Initial Tags
phone, text, convert, c#

Initial Language
C#