Воспроизведение текста голосом

 

Для того, чтобы воспроизвести свободный текст голосом можно воспользоваться библиотекой Microsoft Speech.

При создании проекта нужно подключить COM-объект Microsoft Speech Object

 

Пример реализции на C#

 

Исходный код примера: SpeechAPI.zip

 

 

 

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Threading;

using System.Windows.Forms;

using System.Data;

using SpeechLib;

 

namespace SAPI5

{

      /// <summary>

      /// Summary description for SpeechForm.

      /// </summary>

      public class SpeechForm : System.Windows.Forms.Form

      {

 

            /// <summary>

            /// The main entry point for the application.

            /// </summary>

            [STAThread]

            static void Main()

            {

                  Application.Run(new SpeechForm());

            }

 

            #region ...

            #endregion

 

        /// <summary>

            /// Required designer variable.

            /// </summary>

            private System.ComponentModel.Container components = null;

 

            SpVoice m_voice;

 

 

            #region SpeechForm

            public SpeechForm()

            {

                  InitializeComponent();

            }

            #endregion

 

            #region Dispose

            /// <summary>

            /// Clean up any resources being used.

            /// </summary>

            protected override void Dispose( bool disposing )

            {

                  if( disposing )

                  {

                        if (components != null)

                        {

                             components.Dispose();

                        }

                  }

                  base.Dispose( disposing );

            }

            #endregion

 

            #region Windows Form Designer generated code

            endregion

 

            #region SpeechForm_Load

            /// <summary>

            /// SpeechForm_Load

            /// </summary>

            /// <param name="sender"></param>

            /// <param name="e"></param>

            private void SpeechForm_Load(object sender, System.EventArgs e)

            {

                  m_voice = new SpVoice();

                  m_voice.EndStream+=

new _ISpeechVoiceEvents_EndStreamEventHandler(m_voice_EndStream);

 

                  foreach(ISpeechObjectToken t in m_voice.GetVoices("",""))

                  {

                        cbVoices.Items.Add(t.GetAttribute("Name"));

                  }

            }

            #endregion

 

            /// <summary>

            /// btnSpeak_Click

            /// </summary>

            /// <param name="sender"></param>

            /// <param name="e"></param>

            private void btnSpeak_Click(object sender, System.EventArgs e)

            {

                  if ((Convert.ToInt32(btnSpeak.Tag) == 0) && (sender != null))

                  {

                        btnSpeak.Tag  = 1;

                        btnSpeak.Text = "STOP";

 

                        try

                        {

                             if (cbVoices.Text.Length >0)

                             {

                                   m_voice.Voice = m_voice.GetVoices(

string.Format("Name={0}", cbVoices.Text),

"Language=409").Item(0);// 409

                             }

 

                       if (cbSaveToWave.Checked)

                       {

                           sfd = new SaveFileDialog();

                           sfd.Filter =

"All files (*.*)|*.*|wav files (*.wav)|*.wav";

                           sfd.Title = "Save to a wave file";

                           sfd.FilterIndex = 2;

                           sfd.RestoreDirectory = true;

                           if (sfd.ShowDialog()== DialogResult.OK)

                            {

                              SpeechStreamFileMode SpFileMode =

SpeechStreamFileMode.SSFMCreateForWrite;

                               SpFileStream SpFileStream = new SpFileStream();

                              SpFileStream.Open(sfd.FileName, SpFileMode, false);

                              m_voice.AudioOutputStream = SpFileStream;

                              m_voice.Speak(textBox1.Text,

SpeechVoiceSpeakFlags.SVSFlagsAsync);

                             m_voice.WaitUntilDone(Timeout.Infinite);

                              SpFileStream.Close();

                            }

                        }

                        else

                       {

                                   m_voice.Speak(textBox1.Text,

SpeechVoiceSpeakFlags.SVSFlagsAsync);

                             }

                        }

                        catch

                        {

                             MessageBox.Show("Speak error");

                        }

                  }

                  else

                  {

                        btnSpeak.Tag = 0;

                        btnSpeak.Text = "SPEAK";

                        m_voice.Speak(null, (SpeechVoiceSpeakFlags)2);

                        m_voice.Resume();

                  }

           

            }

 

            private void btnExit_Click(object sender, System.EventArgs e)

            {

                  this.Close();

            }

 

     

 

            private void Volume_Scroll(object sender, System.EventArgs e)

            {

                  m_voice.Volume = Volume.Value;

            }

 

            private void Rate_Scroll(object sender, System.EventArgs e)

            {

                  m_voice.Rate   = Rate.Value;

            }

 

            /// <summary>

            /// m_voice_EndStream

            /// </summary>

            /// <param name="StreamNumber"></param>

            /// <param name="StreamPosition"></param>

            private void m_voice_EndStream(int StreamNumber, object StreamPosition)

            {

                  btnSpeak_Click(null, null);

            }

      }

}

 


 

 

 
26.11.2008

Отзывы и комментарии

 


 
Тема
Ваше имя
Почтовый адрес
Текст сообщения
Ключ защиты:
Защита от спама
 
 
 
 
10.12  .NET Reactor
15.11  n
15.11  C# ClickOnce
 
11.10  GAC и ngen
10.10  SqlTypes