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

namespace MSTTSImp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            System.IO.Directory.CreateDirectory("c:\\temp\\");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (File.Exists("c:\\temp\\tts.wav"))
            {
                File.Delete("c:\\temp\\tts.wav");
            }

            string FileName = null;

            SpFileStream FileStream = new SpFileStream();

            SpVoice Voice = default(SpVoice);

            //Create a  SAPI voice
            Voice = new SpVoice();



            //The output audio data will be saved to ttstemp.wav file

            FileName = "c:\\temp\\tts.wav";

            FileStream.Format.Type = SpeechAudioFormatType.SAFT8kHz16BitMono;



            //Create a file; set DoEvents=True so TTS events will be saved to the file

            FileStream.Open(FileName, SpeechStreamFileMode.SSFMCreateForWrite, true);



            //Set the output to the FileStream

            Voice.AudioOutputStream = FileStream;



            //Speak the text

            Voice.Speak(richTextBox1.Text, SpeechVoiceSpeakFlags.SVSFDefault);



            //Close the Stream

            FileStream.Close();



            //Release the objects

            FileStream = null;

            Voice = null;

        }
    }
}