博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
文本转换成音频流
阅读量:6858 次
发布时间:2019-06-26

本文共 6423 字,大约阅读时间需要 21 分钟。

 

1.生成声音文件

DotNetSpeech.SpeechVoiceSpeakFlags SSF = DotNetSpeech.SpeechVoiceSpeakFlags.SVSFlagsAsync;

DotNetSpeech.SpVoice vo = new SpVoiceClass();
System.Windows.Forms.SaveFileDialog SFD = new System.Windows.Forms.SaveFileDialog();
SFD.Filter = "All files (*.*)|*.*|wav files (*.wav)|*.wav";
SFD.Title = "Save to a wav file";
SFD.FilterIndex = 2;
SFD.RestoreDirectory = true;
if(SFD.ShowDialog()==System.Windows.Forms.DialogResult.OK)
{
DotNetSpeech.SpeechStreamFileMode SSFM = DotNetSpeech.SpeechStreamFileMode.SSFMCreateForWrite;
DotNetSpeech.SpFileStream SFS = new DotNetSpeech.SpFileStreamClass();
SFS.Open(SFD.FileName,SSFM,false);
vo.AudioOutputStream = SFS;
vo.Speak(this.textBox1.Text,SSF);
vo.WaitUntilDone(System.Threading.Timeout.Infinite);
SFS.Close();

2.朗读

DotNetSpeech.SpeechVoiceSpeakFlags SSF = DotNetSpeech.SpeechVoiceSpeakFlags.SVSFlagsAsync;

DotNetSpeech.SpVoice vo = new SpVoiceClass();
vo.Speak(this.textBox1.Text,SSF);

 

3.

/// /// 朗读/// /// 要朗读的文本private void Read(string text){    SpVoice sv = new SpVoice();    sv.Rate = 0;//设置朗读速度    SpeechVoiceSpeakFlags SSF = SpeechVoiceSpeakFlags.SVSFlagsAsync;    sv.Speak(text, SSF);}/// /// 生成声音文件/// /// 要朗读的文本/// 生成声音文件的路径/// 生成声音文件的名称private void CreateFile(string text, string filePath,string fileName){    if (!Directory.Exists(filePath))        Directory.CreateDirectory(filePath);    SpVoice sv = new SpVoice();    SpeechVoiceSpeakFlags SVSF = SpeechVoiceSpeakFlags.SVSFlagsAsync;    SpeechStreamFileMode SSFM = SpeechStreamFileMode.SSFMCreateForWrite;    SpFileStream SFS = new SpFileStream();    SFS.Open(filePath+fileName, SSFM, false);    sv.AudioOutputStream = SFS;    sv.Speak(text, SVSF);    sv.WaitUntilDone(System.Threading.Timeout.Infinite);    SFS.Close();}2、 使用System.SpeechSpeechSynthesizer ss = new SpeechSynthesizer();//播放if (ss != null){    ss.Dispose();    ss.SpeakAsync("朗读的文本");}//暂停if (ss.State == SynthesizerState.Speaking){    ss.Pause();}//继续if (reader.State == SynthesizerState.Paused){    ss.Resume();}//停止if (ss != null){    ss.Dispose();}

 

4.

using System;using System.Collections.Generic;using System.Text;using DotNetSpeech;using System.Threading;using System.IO;namespace SpVoiceDemo{    class SpVoiceUtil    {        SpVoice voice = new DotNetSpeech.SpVoiceClass();        public delegate void CallBack(bool b,int InputWordPosition, int InputWordLength);         ///         /// 朗读文本        ///         /// 要朗读的文本        /// 回调地址        /// 
返回bool
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); } /// /// 回调函数线程子程序 /// /// 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); } /// /// 获取语音库 /// ///
List
public List
getDescription() { List
list = new List
(); 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; } ///
/// 设置当前使用语音库 /// ///
bool
public bool setDescription(string name) { List
list = new List
(); 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; } ///
/// 设置语速 /// ///
public void setRate(int n) { voice.Rate = n; } ///
/// 设置声音大小 /// ///
public void setVolume(int n) { voice.Volume = n; } ///
/// 暂停 /// public void Pause() { voice.Pause(); } ///
/// 继续 /// public void Resume() { voice.Resume(); } ///
/// 停止 /// public void Stop() { voice.Speak(string.Empty, SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak); } ///
/// 输出WAV /// ///
保存路径 ///
要转换的文本内容 ///
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); } }}

 

转载于:https://www.cnblogs.com/ChineseMoonGod/p/3700762.html

你可能感兴趣的文章
用过的一些服务器集成软件
查看>>
一键拨打
查看>>
20120522:ERROR - ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务
查看>>
Maven构建war项目添加版本号
查看>>
更新 手淘 flexible 布局 rem 单位适配问题
查看>>
第三次作业
查看>>
新浪微博登录接口实例
查看>>
wcf技术剖析_会话
查看>>
AngularJS 指令的 Scope (作用域)
查看>>
gitlab的使用
查看>>
iOS 生成本地验证码
查看>>
找不到 javax.servlet.http.HttpServletResponse 和 javax.servlet.http.HttpServletRequest 问题解决...
查看>>
Flip Game(枚举)
查看>>
WebWorker与WebSocket实现前端消息总线
查看>>
Selector
查看>>
Unity 2018.3.1 SyncVar没有同步服务器变量
查看>>
Linux命令(2) - 查看目录和文件大小: du -sh
查看>>
python的一些常用标准库
查看>>
最短路径--Floyd、Dijkstra、Bellman、SPFA算法
查看>>
gunzip
查看>>