发布:2021/2/7 9:55:05作者:管理员 来源:本站 浏览次数:1229
应表哥要求,写一个简单的TTS软件,他们单位上用于广播通知用。源码如下:
简单说明:
- public partial class frmMain : Form
- {
- public frmMain()
- {
- InitializeComponent();
- comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
- }
- SpVoiceUtil SpVoiceUtil = new SpVoiceUtil();
- private void frmMain_Load(object sender, EventArgs e)
- {
- Control.CheckForIllegalCrossThreadCalls = false;
- List<string> list = SpVoiceUtil.getDescription();
- foreach (var item in list)
- {
- comboBox1.Items.Add(item);
- }
- if (comboBox1.Items.Count > 0)
- {
- comboBox1.SelectedIndex = 0;
- }
- }
- private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
- {
- SpVoiceUtil.setDescription(this.Text);
- }
- //暂停
- private void button2_Click(object sender, EventArgs e)
- {
- SpVoiceUtil.Pause();
- }
- //继续
- private void button5_Click(object sender, EventArgs e)
- {
- SpVoiceUtil.Resume();
- }
- //停止
- private void button3_Click(object sender, EventArgs e)
- {
- SpVoiceUtil.Stop();
- }
- //设置语速
- private void trackBar1_Scroll(object sender, EventArgs e)
- {
- lab_Rate.Text = trackBar1.Value.ToString();
- SpVoiceUtil.setRate(trackBar1.Value);
- }
- //设置音量
- private void trackBar2_Scroll(object sender, EventArgs e)
- {
- lab_Volume.Text = trackBar2.Value.ToString();
- SpVoiceUtil.setVolume(trackBar2.Value);
- }
- //开始朗读
- private void button1_Click(object sender, EventArgs e)
- {
- SpVoiceUtil.Speak(txt_str.Text, CallBack);
- }
- //写出WAV
- private void button4_Click(object sender, EventArgs e)
- {
- bool isTrue = false;
- SaveFileDialog dialog = new SaveFileDialog();
- dialog.Filter = "All files (*.*)|*.*|wav files (*.wav)|*.wav";
- dialog.Title = "保存WAV文件";
- dialog.FilterIndex = 2;
- dialog.RestoreDirectory = true;
- if (dialog.ShowDialog() == DialogResult.OK)
- {
- isTrue = SpVoiceUtil.WreiteToWAV(dialog.FileName, txt_str.Text, DotNetSpeech.SpeechAudioFormatType.SAFT11kHz16BitMono);
- }
- if (isTrue)
- {
- MessageBox.Show("输出成功");
- }
- else {
- MessageBox.Show("输出失败");
- }
- }
- //回调信息
- private void CallBack(bool b, int InputWordPosition, int InputWordLength)
- {
- textBox1.AppendText("是否读完:"+b.ToString()+"\r\n");
- textBox1.AppendText("朗读长度:" + InputWordPosition.ToString() + "\r\n");
- textBox1.AppendText("朗读位置:" + InputWordLength.ToString() + "\r\n");
- }
- }
- namespace SpVoiceDemo
- {
- class SpVoiceUtil
- {
- SpVoice voice = new DotNetSpeech.SpVoiceClass();
- public delegate void CallBack(bool b,int InputWordPosition, int InputWordLength);
- /// <summary>
- /// 朗读文本
- /// </summary>
- /// <param name="str">要朗读的文本</param>
- /// <param name="CallBack">回调地址</param>
- /// <returns>返回bool</returns>
- public bool Speak(string str, CallBack CallBack)
- {
- int n = voice.Speak(str, SpeechVoiceSpeakFlags.SVSFlagsAsync);
- Thread thread = new Thread(new ParameterizedThreadStart(Call));
- thread.IsBackground = true;
- thread.Start((Object)CallBack);
- return !(n!=1);
- }
- /// <summary>
- /// 回调函数线程子程序
- /// </summary>
- /// <param name="callBack"></param>
- private void Call(Object callBack)
- {
- int InputWordLength = 0; //局部_朗读长度
- int InputWordPosition = 0; //局部_朗读位置
- CallBack CallBack = (CallBack)callBack;
- while ((int)voice.Status.RunningState != 1)
- {
- if (InputWordPosition != voice.Status.InputWordPosition || InputWordLength != voice.Status.InputWordLength)
- {
- InputWordPosition = voice.Status.InputWordPosition;
- InputWordLength = voice.Status.InputWordLength;
- //回调
- CallBack(false, InputWordPosition, InputWordLength);
- }
- }
- CallBack(true, InputWordPosition, InputWordLength);
- }
- /// <summary>
- /// 获取语音库
- /// </summary>
- /// <returns>List<string></returns>
- public List<string> getDescription()
- {
- List<string> list = new List<string>();
- DotNetSpeech.ISpeechObjectTokens obj = voice.GetVoices();
- int count = obj.Count;//获取语音库总数
- for (int i = 0; i < count; i++)
- {
- string desc = obj.Item(i).GetDescription(); //遍历语音库
- list.Add(desc);
- }
- return list;
- }
- /// <summary>
- /// 设置当前使用语音库
- /// </summary>
- /// <returns>bool</returns>
- public bool setDescription(string name)
- {
- List<string> list = new List<string>();
- DotNetSpeech.ISpeechObjectTokens obj = voice.GetVoices();
- int count = obj.Count;//获取语音库总数
- bool result = false;
- for (int i = 0; i < count; i++)
- {
- string desc = obj.Item(i).GetDescription(); //遍历语音库
- if (desc.Equals(name))
- {
- voice.Voice = obj.Item(i);
- result = true;
- }
- }
- return result;
- }
- /// <summary>
- /// 设置语速
- /// </summary>
- /// <param name="n"></param>
- public void setRate(int n)
- {
- voice.Rate = n;
- }
- /// <summary>
- /// 设置声音大小
- /// </summary>
- /// <param name="n"></param>
- public void setVolume(int n)
- {
- voice.Volume = n;
- }
- /// <summary>
- /// 暂停
- /// </summary>
- public void Pause()
- {
- voice.Pause();
- }
- /// <summary>
- /// 继续
- /// </summary>
- public void Resume()
- {
- voice.Resume();
- }
- /// <summary>
- /// 停止
- /// </summary>
- public void Stop()
- {
- voice.Speak(string.Empty, SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak);
- }
- /// <summary>
- /// 输出WAV
- /// </summary>
- /// <param name="path">保存路径</param>
- /// <param name="str">要转换的文本内容</param>
- /// <returns></returns>
- public bool WreiteToWAV(string path,string str,SpeechAudioFormatType SpAudioType)
- {
- SpeechStreamFileMode SpFileMode = SpeechStreamFileMode.SSFMCreateForWrite;
- SpFileStream SpFileStream = new SpFileStream();
- SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
- SpAudioFormat SpAudio = new DotNetSpeech.SpAudioFormat();
- SpAudio.Type = SpAudioType;
- SpFileStream.Format = SpAudio;
- SpFileStream.Open(path, SpFileMode, false);
- voice.AudioOutputStream = SpFileStream;
- voice.Speak(str, SpFlags);
- voice.WaitUntilDone(Timeout.Infinite);
- SpFileStream.Close();
- return File.Exists(path);
- }
- }
- }
© Copyright 2014 - 2025 柏港建站平台 ejk5.com. 渝ICP备16000791号-4