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);
        }
    }
}