隐藏

.NET C# 语音合成源码(TTS)微软库

发布:2021/2/7 9:55:05作者:管理员 来源:本站 浏览次数:1095

应表哥要求,写一个简单的TTS软件,他们单位上用于广播通知用。源码如下:


zip.pngVIKI-TTS-Voice.zip


简单说明:


  1.     public partial class frmMain : Form
  2.     {
  3.         public frmMain()
  4.         {
  5.             InitializeComponent();
  6.             comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
  7.         }
  8.  
  9.         SpVoiceUtil SpVoiceUtil = new SpVoiceUtil();
  10.  
  11.  
  12.         private void frmMain_Load(object sender, EventArgs e)
  13.         {
  14.             Control.CheckForIllegalCrossThreadCalls = false;
  15.  
  16.             List<string> list = SpVoiceUtil.getDescription();
  17.  
  18.             foreach (var item in list)
  19.         {
  20.                 comboBox1.Items.Add(item); 
  21.         }
  22.             if (comboBox1.Items.Count > 0)
  23.             {
  24.                 comboBox1.SelectedIndex = 0;
  25.             }
  26.         }
  27.  
  28.         private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  29.         {
  30.             SpVoiceUtil.setDescription(this.Text);
  31.         }
  32.  
  33.          //暂停
  34.         private void button2_Click(object sender, EventArgs e)
  35.         {
  36.             SpVoiceUtil.Pause();
  37.         }
  38.  
  39.         //继续
  40.         private void button5_Click(object sender, EventArgs e)
  41.         {
  42.             SpVoiceUtil.Resume();
  43.         }
  44.  
  45.         //停止
  46.         private void button3_Click(object sender, EventArgs e)
  47.         {
  48.             SpVoiceUtil.Stop();
  49.         }
  50.  
  51.  
  52.         //设置语速
  53.         private void trackBar1_Scroll(object sender, EventArgs e)
  54.         {
  55.             lab_Rate.Text = trackBar1.Value.ToString();
  56.             SpVoiceUtil.setRate(trackBar1.Value);
  57.         }
  58.  
  59.         //设置音量
  60.         private void trackBar2_Scroll(object sender, EventArgs e)
  61.         {
  62.             lab_Volume.Text = trackBar2.Value.ToString();
  63.             SpVoiceUtil.setVolume(trackBar2.Value);
  64.         }
  65.  
  66.  
  67.         //开始朗读
  68.         private void button1_Click(object sender, EventArgs e)
  69.         {
  70.             SpVoiceUtil.Speak(txt_str.Text, CallBack);
  71.         }
  72.  
  73.  
  74.         //写出WAV
  75.         private void button4_Click(object sender, EventArgs e)
  76.         {
  77.             bool isTrue = false;
  78.             SaveFileDialog dialog = new SaveFileDialog();
  79.             dialog.Filter = "All files (*.*)|*.*|wav files (*.wav)|*.wav";
  80.             dialog.Title = "保存WAV文件";
  81.             dialog.FilterIndex = 2;
  82.             dialog.RestoreDirectory = true;
  83.             if (dialog.ShowDialog() == DialogResult.OK)
  84.             {
  85.                 isTrue = SpVoiceUtil.WreiteToWAV(dialog.FileName, txt_str.Text, DotNetSpeech.SpeechAudioFormatType.SAFT11kHz16BitMono);
  86.             }
  87.  
  88.             if (isTrue)
  89.             {
  90.                 MessageBox.Show("输出成功");
  91.             }
  92.             else {
  93.                 MessageBox.Show("输出失败");
  94.             }
  95.         }
  96.  
  97.  
  98.         //回调信息
  99.         private void CallBack(bool b, int InputWordPosition, int InputWordLength)
  100.         {
  101.             textBox1.AppendText("是否读完:"+b.ToString()+"\r\n");
  102.             textBox1.AppendText("朗读长度:" + InputWordPosition.ToString() + "\r\n");
  103.             textBox1.AppendText("朗读位置:" + InputWordLength.ToString() + "\r\n");
  104.         }
  105.     }



  1. namespace SpVoiceDemo
  2. {
  3.     class SpVoiceUtil
  4.     {
  5.         SpVoice voice = new DotNetSpeech.SpVoiceClass();
  6.  
  7.         public delegate void CallBack(bool b,int InputWordPosition, int InputWordLength); 
  8.  
  9.         /// <summary>
  10.         /// 朗读文本
  11.         /// </summary>
  12.         /// <param name="str">要朗读的文本</param>
  13.         /// <param name="CallBack">回调地址</param>
  14.         /// <returns>返回bool</returns>
  15.         public bool Speak(string str, CallBack CallBack)
  16.         {
  17.             int n = voice.Speak(str, SpeechVoiceSpeakFlags.SVSFlagsAsync);
  18.             Thread thread = new Thread(new ParameterizedThreadStart(Call));
  19.             thread.IsBackground = true;
  20.             thread.Start((Object)CallBack);
  21.             return !(n!=1);
  22.         }
  23.  
  24.  
  25.         /// <summary>
  26.         /// 回调函数线程子程序
  27.         /// </summary>
  28.         /// <param name="callBack"></param>
  29.         private void Call(Object callBack)
  30.         {
  31.             int InputWordLength = 0;    //局部_朗读长度
  32.             int InputWordPosition = 0; //局部_朗读位置
  33.  
  34.             CallBack CallBack = (CallBack)callBack;
  35.  
  36.             while ((int)voice.Status.RunningState != 1)
  37.             {
  38.                 if (InputWordPosition != voice.Status.InputWordPosition || InputWordLength != voice.Status.InputWordLength)
  39.                 {
  40.                     InputWordPosition = voice.Status.InputWordPosition;
  41.                     InputWordLength = voice.Status.InputWordLength;
  42.  
  43.                     //回调                  
  44.                     CallBack(false, InputWordPosition, InputWordLength);
  45.                 }
  46.             }
  47.             CallBack(true, InputWordPosition, InputWordLength);
  48.         }
  49.  
  50.         /// <summary>
  51.         /// 获取语音库
  52.         /// </summary>
  53.         /// <returns>List<string></returns>
  54.         public List<string> getDescription()
  55.         {
  56.             List<string> list = new List<string>();
  57.             DotNetSpeech.ISpeechObjectTokens obj = voice.GetVoices();
  58.             int count = obj.Count;//获取语音库总数
  59.             for (int i = 0; i < count; i++)
  60.             {
  61.                string desc = obj.Item(i).GetDescription(); //遍历语音库
  62.                list.Add(desc);
  63.             }
  64.             return list;
  65.         }
  66.  
  67.         /// <summary>
  68.         /// 设置当前使用语音库
  69.         /// </summary>
  70.         /// <returns>bool</returns>
  71.         public bool setDescription(string name)
  72.         {
  73.             List<string> list = new List<string>();
  74.             DotNetSpeech.ISpeechObjectTokens obj = voice.GetVoices();
  75.             int count = obj.Count;//获取语音库总数
  76.             bool result = false;
  77.             for (int i = 0; i < count; i++)
  78.             {
  79.                 string desc = obj.Item(i).GetDescription(); //遍历语音库
  80.                 if (desc.Equals(name))
  81.                 {
  82.                     voice.Voice = obj.Item(i);
  83.                     result = true;
  84.                 }
  85.             }
  86.             return result;
  87.         }
  88.  
  89.         /// <summary>
  90.         /// 设置语速
  91.         /// </summary>
  92.         /// <param name="n"></param>
  93.         public void setRate(int n)
  94.         {
  95.             voice.Rate = n;
  96.         }
  97.  
  98.         /// <summary>
  99.         /// 设置声音大小
  100.         /// </summary>
  101.         /// <param name="n"></param>
  102.         public void setVolume(int n)
  103.         {
  104.             voice.Volume = n;
  105.         }
  106.  
  107.         /// <summary>
  108.         /// 暂停
  109.         /// </summary>
  110.         public void Pause()
  111.         {
  112.             voice.Pause();
  113.         }
  114.  
  115.         /// <summary>
  116.         /// 继续
  117.         /// </summary>
  118.         public void Resume()
  119.         {
  120.             voice.Resume();
  121.         }
  122.  
  123.         /// <summary>
  124.         /// 停止
  125.         /// </summary>
  126.         public void Stop()
  127.         {
  128.             voice.Speak(string.Empty, SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak);
  129.         }
  130.  
  131.  
  132.         /// <summary>
  133.         /// 输出WAV
  134.         /// </summary>
  135.         /// <param name="path">保存路径</param>
  136.         /// <param name="str">要转换的文本内容</param>
  137.         /// <returns></returns>
  138.         public bool WreiteToWAV(string path,string str,SpeechAudioFormatType SpAudioType)
  139.         {
  140.             SpeechStreamFileMode SpFileMode = SpeechStreamFileMode.SSFMCreateForWrite;
  141.             SpFileStream SpFileStream = new SpFileStream();
  142.             SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
  143.             SpAudioFormat SpAudio = new DotNetSpeech.SpAudioFormat();
  144.             SpAudio.Type = SpAudioType;
  145.             SpFileStream.Format = SpAudio;
  146.             SpFileStream.Open(path, SpFileMode, false);
  147.             voice.AudioOutputStream = SpFileStream;
  148.             voice.Speak(str, SpFlags);
  149.             voice.WaitUntilDone(Timeout.Infinite);
  150.             SpFileStream.Close();
  151.             return File.Exists(path);
  152.         }
  153.     }
  154. }