Csharp: speech to text, text to speech in win
2020-11-16 00:19
标签:des com class blog code div size tar string ext art Csharp: speech to text, text to speech in win,搜素材,soscw.com Csharp: speech to text, text to speech in win 标签:des com class blog code div size tar string ext art 原文地址:http://www.cnblogs.com/geovindu/p/3694910.html
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
using
System.Threading;
using
SpeechLib;
//NET2.0 引用 Speech sdk 5.1 在COM选项卡里面的Microsoft Speech object library引用 已经有11.0版本
using
System.Speech;
using
System.Speech.Recognition;
using
System.Speech.Synthesis;
namespace
Speech
{
///
/// 20140427
/// 涂聚文
///
///
public
partial
class
Form1 : Form
{
private
enum
State
{
Idle = 0,
Accepting = 1,
Off = 2,
}
private
State RecogState = State.Off;
private
SpeechRecognitionEngine recognizer;
private
SpeechSynthesizer synthesizer =
null
;
private
int
Hypothesized = 0;
private
int
Recognized = 0;
///
///
///
public
Form1()
{
InitializeComponent();
}
///
///
///
///
///
private
void
Form1_Load(
object
sender, EventArgs e)
{
// SpVoice voice = new SpVoice();//SAPI 5.4
//SpeechLib.ISpeechObjectTokens obj = voice.GetVoices(string.Empty, string.Empty);
//int count = obj.Count;//获取语音库总数
//bool result = false;
//for (int i = 0; i
//{
// string desc = obj.Item(i).GetDescription(i);//.GetDescription(); //遍历语音库
// comboBox1.Items.Add(desc);
//}
//SpVoiceClass voice = new SpVoiceClass();//SAPI 5.1
////voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(0);
////voice.Speak("你要说的话",SpeechVoiceSpeakFlags.SVSFlagsAsync);
//SpVoice voice1 = new SpVoice();//SAPI 5.4
//voice1.Volume = 100;//音量
//voice1.Voice = voice1.GetVoices(string.Empty, string.Empty).Item(0);
//voice1.Rate = 2;//速度语音朗读速度
// voice1.Speak("你要说的话", SpeechVoiceSpeakFlags.SVSFlagsAsync);
//voice1.Speak("speech sdk 5.1", SpeechVoiceSpeakFlags.SVSFlagsAsync);
//SpeechSynthesizer syn = new SpeechSynthesizer();
//syn.SelectVoice("Microsoft Lili");
//initialize recognizer and synthesizer
InitializeRecognizerSynthesizer();
//if input device found then proceed
if
(SelectInputDevice())
{
LoadDictationGrammar();
ReadAloud(
"中华人民共和国"
);
//中文方式Speech Engine Ready for Input
}
}
///
/// 中文
///
///
///
private
void
button1_Click(
object
sender, EventArgs e)
{
SpVoiceClass voice =
new
SpVoiceClass();
voice.Voice = voice.GetVoices(
string
.Empty,
string
.Empty).Item(0);
//0,为系统默认,中文
voice.Speak(
this
.textBox1.Text.Trim(), SpeechVoiceSpeakFlags.SVSFlagsAsync);
}
///
/// 英文
///
///
///
private
void
button2_Click(
object
sender, EventArgs e)
{
SpVoiceClass voice =
new
SpVoiceClass();
voice.Voice = voice.GetVoices(
string
.Empty,
string
.Empty).Item(1);
//
voice.Speak(
this
.textBox2.Text.Trim(), SpeechVoiceSpeakFlags.SVSFlagsAsync);
}
///
/// 输入中文语音输出中文文字
///
///
///
private
void
button3_Click(
object
sender, EventArgs e)
{
switch
(RecogState)
{
case
State.Off:
RecogState = State.Accepting;
button3.Text =
"Stop"
;
recognizer.RecognizeAsync(RecognizeMode.Multiple);
break
;
case
State.Accepting:
RecogState = State.Off;
button3.Text =
"Start"
;
recognizer.RecognizeAsyncStop();
break
;
}
}
///
/// pause recognition and speak the text sent
///
///
public
void
ReadAloud(
string
speakText)
{
try
{
recognizer.RecognizeAsyncCancel();
synthesizer.SpeakAsync(speakText);
}
catch
{ }
}
///
/// initialize recognizer and synthesizer along with their events
/// ///
private
void
InitializeRecognizerSynthesizer()
{
var
selectedRecognizer = (
from
e
in
SpeechRecognitionEngine.InstalledRecognizers()
where
e.Culture.Equals(Thread.CurrentThread.CurrentCulture)
select
e).FirstOrDefault();
recognizer =
new
SpeechRecognitionEngine(selectedRecognizer);
recognizer.AudioStateChanged +=
new
EventHandler(recognizer_AudioStateChanged);
recognizer.SpeechHypothesized +=
new
EventHandler
recognizer.SpeechRecognized +=
new
EventHandler
synthesizer =
new
SpeechSynthesizer();
}
#region Recognizer events
private
void
recognizer_AudioStateChanged(
object
sender, AudioStateChangedEventArgs e)
{
switch
(e.AudioState)
{
case
AudioState.Speech:
LabelStatus.Text =
"Listening"
;
break
;
case
AudioState.Silence:
LabelStatus.Text =
"Idle"
;
break
;
case
AudioState.Stopped:
LabelStatus.Text =
"Stopped"
;
break
;
}
}
///
///
///
///
///
private
void
recognizer_SpeechHypothesized(
object
sender, SpeechHypothesizedEventArgs e)
{
Hypothesized++;
LabelHypothesized.Text =
"Hypothesized: "
+ Hypothesized.ToString();
}
///
///
///
///
///
private
void
recognizer_SpeechRecognized(
object
sender, SpeechRecognizedEventArgs e)
{
Recognized++;
string
s =
"Recognized: "
+ Recognized.ToString();
if
(RecogState == State.Off)
return
;
float
accuracy = (
float
)e.Result.Confidence;
string
phrase = e.Result.Text;
{
if
(phrase ==
"End Dictate"
)
{
RecogState = State.Off;
recognizer.RecognizeAsyncStop();
ReadAloud(
"Dictation Ended"
);
return
;
}
textBox1.AppendText(
" "
+ e.Result.Text);
}
}
#endregion
///
/// select input device if available
///
///
private
bool
SelectInputDevice()
{
bool
proceedLoading =
true
;
//if OS is above XP
if
(IsOscompatible())
{
try
{
recognizer.SetInputToDefaultAudioDevice();
}
catch
{
proceedLoading =
false
;
//no audio input device
}
}
//if OS is XP or below
else
ThreadPool.QueueUserWorkItem(InitSpeechRecogniser);
return
proceedLoading;
}
///
/// Findout if OS is compatible.
///
///
private
bool
IsOscompatible()
{
OperatingSystem osInfo = Environment.OSVersion;
if
(osInfo.Version >
new
Version(
"6.0"
))
return
true
;
else
return
false
;
}
///
///
///
///
private
void
InitSpeechRecogniser(
object
o)
{
recognizer.SetInputToDefaultAudioDevice();
}
///
/// Load grammars, one for command and other for dictation
///
private
void
LoadDictationGrammar()
{
GrammarBuilder grammarBuilder =
new
GrammarBuilder();
grammarBuilder.Append(
new
Choices(
"End Dictate"
));
Grammar commandGrammar =
new
Grammar(grammarBuilder);
commandGrammar.Name =
"main command grammar"
;
recognizer.LoadGrammar(commandGrammar);
DictationGrammar dictationGrammar =
new
DictationGrammar();
dictationGrammar.Name =
"dictation"
;
recognizer.LoadGrammar(dictationGrammar);
}
}
}
上一篇:Hadoop:Windows 7 32 Bit 编译与运行
下一篇:将Excel导入DataGridView 中的"select * from [Sheet1$]"中[ ]里面表单名的动态获取
文章标题:Csharp: speech to text, text to speech in win
文章链接:http://soscw.com/index.php/essay/21570.html