📄 speechui.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using SpeechLib;
using System.Data.OleDb;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
namespace SpeechUI
{
public class SpeechUI : System.Windows.Forms.Form
{
// UI - Recognition panel
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.CheckBox recognition_checkbox;
private System.Windows.Forms.TextBox recognition_textbox;
// UI - Voice output panel
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.Panel panel3;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.TrackBar rate_trackbar;
private System.Windows.Forms.ListBox voices_listbox;
private System.Windows.Forms.Button speak_button;
// UI - Other
private System.Windows.Forms.Button exit_button;
private System.ComponentModel.Container components = null;
// Speech recognition objects
private const int maingrammarId = 10;
private bool speechEnabled = false;
private bool speechInitialized = false;
private SpeechLib.SpSharedRecoContext objRecoContext;
private SpeechLib.ISpeechRecoGrammar grammar;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label2;
private SpObjectToken selectedVoice = null;
public SpeechUI()
{
InitializeComponent();
PopulateVoiceList();
}
private void PopulateVoiceList()
{
SpVoice Voice = new SpVoice();
ISpeechObjectTokens voices = Voice.GetVoices("","");
for(int i=0; i<voices.Count; i++)
{
SpObjectToken v = voices.Item(i);
voices_listbox.Items.Add(v.Id.Substring(v.Id.LastIndexOf("\\")+1));
}
}
private void SpeakText(string text)
{
// Create a Text To Speech voice and speak the specified text.
try
{
//SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFDefault ;
SpVoice Voice = new SpVoice();
if(selectedVoice != null)
{
Voice.Voice = selectedVoice;
}
Voice.Rate = rate_trackbar.Value;
Voice.Speak(text, SpFlags);
}
catch
{
MessageBox.Show("Speak error", "SpeechUI", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void speak_button_Click(object sender, System.EventArgs e)
{
DisableSpeechRecognition();
SpeakText(recognition_textbox.Text);
EnableSpeechRecognition();
}
private void recognition_checkbox_CheckedChanged(object sender, System.EventArgs e)
{
if(recognition_checkbox.Checked)
{
if(!speechEnabled)
if(EnableSpeechRecognition())
{
speechEnabled = true;
MessageBox.Show("Speech On");
}
else
recognition_checkbox.Checked = false;
}
else
{
if(speechEnabled)
if(DisableSpeechRecognition())
{
speechEnabled = false;
MessageBox.Show("Speech Off");
}
else
recognition_checkbox.Checked = true;
}
}
private void VoiceSelected(object sender, EventArgs e)
{
string voicename = (string)((ListBox)sender).SelectedItem;
SpVoice Voice = new SpVoice();
ISpeechObjectTokens voices = Voice.GetVoices("","");
for(int i=0; i<voices.Count; i++)
{
SpObjectToken v = voices.Item(i);
string voicetotest = v.Id.Substring(v.Id.LastIndexOf("\\")+1);
if( string.Compare(voicetotest, voicename) == 0 )
{
selectedVoice = v;
i=voices.Count;
}
}
}
private void OutputlistSelect(object sender, EventArgs e)
{
SpeakText( (string)((ComboBox)sender).SelectedItem );
}
#region Speech Recognition
// Hypothesis output event handler for the SpSharedRecoContext object's Hypothesis event.
public void RecoContext_Hypothesis(int StreamNumber,
object StreamPosition,
ISpeechRecoResult Result)
{
//recognition_textbox.AppendText(StreamPosition.ToString()+" Hypothesis: " + Result.PhraseInfo.GetText(0, -1, true) +"\r\n");
}
// Recognition event handler for the SpSharedRecoContext object's Recognition event.
public void RecoContext_Recognition(int StreamNumber,
object StreamPosition,
SpeechRecognitionType RecognitionType,
ISpeechRecoResult Result)
{
string word=Result.PhraseInfo.GetText(0, -1, true);
if(word.ToUpper()=="CLEAR")
{
recognition_textbox.Clear();
return;
}
DisableSpeechRecognition();
word=word.Replace(".","").Replace("-","");
if(word.Length <6) word=word.Replace(" ","");
recognition_textbox.AppendText(word + "...");
SpeakText(word + " " + "One moment please.");
recognition_textbox.Clear();
recognition_textbox.AppendText(GetStockQuote(word));
SpeakText(recognition_textbox.Text);
EnableSpeechRecognition();
}
private void InitializeSpeechRecognition()
{
try
{
// Create the main recognition context object.
// This sample uses a shared recognition context.
// Inproc recognition context is also available.
objRecoContext = new SpeechLib.SpSharedRecoContext();
// Attach event handlers for Hypothesis and Recognition events.
objRecoContext.Hypothesis += new
_ISpeechRecoContextEvents_HypothesisEventHandler(
RecoContext_Hypothesis);
objRecoContext.Recognition += new
_ISpeechRecoContextEvents_RecognitionEventHandler(
RecoContext_Recognition);
grammar = objRecoContext.CreateGrammar(maingrammarId);
grammar.DictationSetState(SpeechRuleState.SGDSActive);
object PropValue = "";
PropValue = "";
speechInitialized = true;
}
catch(Exception e)
{
System.Windows.Forms.MessageBox.Show(
"Exception caught when initializing SAPI."
+ " This application may not run correctly.\r\n\r\n"
+ e.ToString(),
"Error");
}
}
/// <summary>
/// RebuildGrammar() will update grammar object with current items.
/// </summary>
/// <returns>
/// true if grammar is updated.
/// false if grammar is not updated, which can happen if speech is
/// not enabled or if it's in design mode.
/// </returns>
public bool RebuildGrammar()
{
// not used here
return true;
}
private bool EnableSpeechRecognition()
{
if (speechInitialized == false)
InitializeSpeechRecognition();
else
objRecoContext.State = SpeechRecoContextState.SRCS_Enabled;
return true;
}
private bool DisableSpeechRecognition()
{
if( speechInitialized )
objRecoContext.State = SpeechRecoContextState.SRCS_Disabled;
return true;
}
#endregion
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.recognition_textbox = new System.Windows.Forms.TextBox();
this.recognition_checkbox = new System.Windows.Forms.CheckBox();
this.label1 = new System.Windows.Forms.Label();
this.panel2 = new System.Windows.Forms.Panel();
this.label2 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.panel3 = new System.Windows.Forms.Panel();
this.label5 = new System.Windows.Forms.Label();
this.voices_listbox = new System.Windows.Forms.ListBox();
this.label4 = new System.Windows.Forms.Label();
this.rate_trackbar = new System.Windows.Forms.TrackBar();
this.speak_button = new System.Windows.Forms.Button();
this.label3 = new System.Windows.Forms.Label();
this.exit_button = new System.Windows.Forms.Button();
this.panel1.SuspendLayout();
this.panel2.SuspendLayout();
this.panel3.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.rate_trackbar)).BeginInit();
this.SuspendLayout();
//
// panel1
//
this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.panel1.Controls.Add(this.recognition_textbox);
this.panel1.Controls.Add(this.recognition_checkbox);
this.panel1.Controls.Add(this.label1);
this.panel1.Location = new System.Drawing.Point(8, 8);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(629, 199);
this.panel1.TabIndex = 0;
//
// recognition_textbox
//
this.recognition_textbox.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.recognition_textbox.Location = new System.Drawing.Point(10, 25);
this.recognition_textbox.Multiline = true;
this.recognition_textbox.Name = "recognition_textbox";
this.recognition_textbox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.recognition_textbox.Size = new System.Drawing.Size(608, 161);
this.recognition_textbox.TabIndex = 2;
this.recognition_textbox.Text = "";
//
// recognition_checkbox
//
this.recognition_checkbox.Checked = true;
this.recognition_checkbox.CheckState = System.Windows.Forms.CheckState.Checked;
this.recognition_checkbox.Location = new System.Drawing.Point(463, 6);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -