private void ProgramSpeechInit() { //ルール認識 音声認識オブジェクトの生成 this.ProgramRule = new SpInProcRecoContext(); bool hit = false; foreach (SpObjectToken recoperson in this.ProgramRule.Recognizer.GetRecognizers()) //'Go through the SR enumeration { string language = recoperson.GetAttribute("Language"); if (language == "411") { //日本語を聴き取れる人だ this.ProgramRule.Recognizer.Recognizer = recoperson; //君に聞いていて欲しい hit = true; break; } } if (!hit) { MessageBox.Show("日本語認識が利用できません。\r\n日本語音声認識 MSSpeech_SR_ja-JP_TELE をインストールしてください。\r\n"); Application.Exit(); } //マイクから拾ってね。 this.ProgramRule.Recognizer.AudioInput = this.CreateMicrofon(); //音声認識イベントで、デリゲートによるコールバックを受ける. //認識完了 this.ProgramRule.Recognition += delegate(int streamNumber, object streamPosition, SpeechLib.SpeechRecognitionType srt, SpeechLib.ISpeechRecoResult isrr) { string strText = isrr.PhraseInfo.GetText(0, -1, true); ProgramRun(strText); }; //認識失敗 this.ProgramRule.FalseRecognition += delegate(int streamNumber, object streamPosition, SpeechLib.ISpeechRecoResult isrr) { label1.Text = "??"; show("what", 0); }; //言語モデルの作成 this.ProgramGrammarRule = this.ProgramRule.CreateGrammar(0); this.ProgramGrammarRule.Reset(0); //言語モデルのルールのトップレベルを作成する. this.ProgramGrammarRuleGrammarRule = this.ProgramGrammarRule.Rules.Add("ProgramRule", SpeechRuleAttributes.SRATopLevel | SpeechRuleAttributes.SRADynamic); //認証用文字列の追加. ArrayList voicelist = list.GetProgramCommand(); foreach (string voice in voicelist) { this.ProgramGrammarRuleGrammarRule.InitialState.AddWordTransition(null, voice); } this.ProgramGrammarRuleGrammarRule.InitialState.AddWordTransition(null, Program.skinini.GetValue("skininfo", "clock", "時計に戻して")); //ルールを反映させる。 this.ProgramGrammarRule.Rules.Commit(); }
//音声認識初期化 private void AlwaysSpeechInit() { //ルール認識 音声認識オブジェクトの生成 this.AlwaysRule = new SpInProcRecoContext(); bool hit = false; foreach (SpObjectToken recoperson in this.AlwaysRule.Recognizer.GetRecognizers()) //'Go through the SR enumeration { string language = recoperson.GetAttribute("Language"); if (language == "411") { //日本語を聴き取れる人だ this.AlwaysRule.Recognizer.Recognizer = recoperson; //君に聞いていて欲しい hit = true; break; } } if (!hit) { MessageBox.Show("日本語認識が利用できません。\r\n日本語音声認識 MSSpeech_SR_ja-JP_TELE をインストールしてください。\r\n"); Application.Exit(); } //マイクから拾ってね。 this.AlwaysRule.Recognizer.AudioInput = this.CreateMicrofon(); //音声認識イベントで、デリゲートによるコールバックを受ける. //認識完了 this.AlwaysRule.Recognition += delegate(int streamNumber, object streamPosition, SpeechLib.SpeechRecognitionType srt, SpeechLib.ISpeechRecoResult isrr) { //音声認識終了 this.AlwaysGrammarRule.CmdSetRuleState("AlwaysRule", SpeechRuleState.SGDSInactive); //ウィンドウをアクティブにする this.Activate(); //聞き取り開始時間取得 starttime = Environment.TickCount & int.MaxValue; //聞き取り開始 label1_MouseUp(null, null); }; //言語モデルの作成 this.AlwaysGrammarRule = this.AlwaysRule.CreateGrammar(0); this.AlwaysGrammarRule.Reset(0); //言語モデルのルールのトップレベルを作成する. this.AlwaysGrammarRuleGrammarRule = this.AlwaysGrammarRule.Rules.Add("AlwaysRule", SpeechRuleAttributes.SRATopLevel | SpeechRuleAttributes.SRADynamic); //認証用文字列の追加. this.AlwaysGrammarRuleGrammarRule.InitialState.AddWordTransition(null, Program.skinini.GetValue("skininfo", "response", "テスト")); //ルールを反映させる。 this.AlwaysGrammarRule.Rules.Commit(); }
//音声認識初期化 private void VoiceCommandInit() { //ルール認識 音声認識オブジェクトの生成 this.DialogRule = new SpInProcRecoContext(); bool hit = false; foreach (SpObjectToken recoperson in this.DialogRule.Recognizer.GetRecognizers()) //'Go through the SR enumeration { string language = recoperson.GetAttribute("Language"); if (language == "411") //日本語を聴き取れる人だ { this.DialogRule.Recognizer.Recognizer = recoperson; //君に聞いていて欲しい hit = true; break; } } if (!hit) { MessageBox.Show("日本語認識が利用できません。\r\n日本語音声認識 MSSpeech_SR_ja-JP_TELE をインストールしてください。\r\n"); Application.Exit(); } //マイクから拾ってね。 this.DialogRule.Recognizer.AudioInput = this.CreateMicrofon(); //音声認識イベントで、デリゲートによるコールバックを受ける. //認識完了 this.DialogRule.Recognition += delegate(int streamNumber, object streamPosition, SpeechLib.SpeechRecognitionType srt, SpeechLib.ISpeechRecoResult isrr) { string strText = isrr.PhraseInfo.GetText(0, -1, true); this.SpeechTextBranch(strText); }; //言語モデルの作成 this.DialogGrammarRule = this.DialogRule.CreateGrammar(0); this.DialogGrammarRule.Reset(0); //言語モデルのルールのトップレベルを作成する. this.DialogGrammarRuleGrammarRule = this.DialogGrammarRule.Rules.Add("DialogRule", SpeechRuleAttributes.SRATopLevel | SpeechRuleAttributes.SRADynamic); //認証用文字列の追加. this.DialogGrammarRuleGrammarRule.InitialState.AddWordTransition(null, "音声入力"); this.DialogGrammarRuleGrammarRule.InitialState.AddWordTransition(null, "オーケー"); this.DialogGrammarRuleGrammarRule.InitialState.AddWordTransition(null, "キャンセル"); //ルールを反映させる。 this.DialogGrammarRule.Rules.Commit(); }
/// <summary> /// Starts the listening /// </summary> public void Start() { recoContext = new SpSharedRecoContextClass(); recoContext.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(recoContext_Recognition); recoGrammar = recoContext.CreateGrammar(0); recoGrammarRule = recoGrammar.Rules.Add("VoiceCommands", SpeechRuleAttributes.SRATopLevel | SpeechRuleAttributes.SRADynamic, 1); object propValue = ""; for (int i = 0; i < commands.Count; i++) { recoGrammarRule.InitialState.AddWordTransition(null, commands[i].ToString(), " ", SpeechGrammarWordType.SGLexical, Commands[i].ToString(), i, ref propValue, 1.0F); } recoGrammar.Rules.Commit(); recoGrammar.CmdSetRuleState("VoiceCommands", SpeechRuleState.SGDSActive); }
protected void BuildDictionnary(string[] Words) { //If words list has changed, we rebuild the dictionnary this.FGrammar.Reset(0); this.FMenuRule = this.FGrammar.Rules.Add("Commands", SpeechRuleAttributes.SRATopLevel | SpeechRuleAttributes.SRADynamic, 1); object PropValue = ""; foreach (string word in Words) { //Add all words in the recognition list if (word.Trim().Length > 0) { this.FMenuRule.InitialState.AddWordTransition(null, word, " ", SpeechGrammarWordType.SGLexical, word, 1, ref PropValue, 1.0F); } } this.FGrammar.Rules.Commit(); this.FGrammar.CmdSetRuleState("Commands", SpeechRuleState.SGDSActive); }
/*ADDS WORDS TO THE GRAMMAR*/ private void SAPIGrammarFromArrayList(ArrayList phraseList) { object propertyValue = ""; int i; for (i = 0; i < phraseList.Count; i++) { rule = grammar.Rules.Add(phraseList[i].ToString(), SpeechRuleAttributes.SRATopLevel, i + 100); //add new word to the rule state = rule.InitialState; propertyValue = ""; //state.AddWordTransition(null, command1.phrase, " ", // SpeechGrammarWordType.SGLexical, "", // 0, ref propertyValue, 1F); state.AddWordTransition(null, phraseList[i].ToString(), " ", SpeechGrammarWordType.SGLexical, "", 0, ref propertyValue, 1F); //commit rules grammar.Rules.Commit(); //make rule active (needed for each rule) grammar.CmdSetRuleState(phraseList[i].ToString(), SpeechRuleState.SGDSActive); } }
/// <summary> /// Stops the listening /// </summary> public void Stop() { recoContext = null; recoGrammar = null; recoGrammarRule = null; }
private void ControlSpeechInit() { //ルール認識 音声認識オブジェクトの生成 this.ControlRule = new SpInProcRecoContext(); bool hit = false; foreach (SpObjectToken recoperson in this.ControlRule.Recognizer.GetRecognizers()) //'Go through the SR enumeration { string language = recoperson.GetAttribute("Language"); if (language == "411") { //日本語を聴き取れる人だ this.ControlRule.Recognizer.Recognizer = recoperson; //君に聞いていて欲しい hit = true; break; } } if (!hit) { MessageBox.Show("日本語認識が利用できません。\r\n日本語音声認識 MSSpeech_SR_ja-JP_TELE をインストールしてください。\r\n"); Application.Exit(); } //マイクから拾ってね。 this.ControlRule.Recognizer.AudioInput = this.CreateMicrofon(); //音声認識イベントで、デリゲートによるコールバックを受ける. //認識完了 this.ControlRule.Recognition += delegate(int streamNumber, object streamPosition, SpeechLib.SpeechRecognitionType srt, SpeechLib.ISpeechRecoResult isrr) { string strText = isrr.PhraseInfo.GetText(0, -1, true); SpeechTextBranch(strText); }; //認識失敗 this.ControlRule.FalseRecognition += delegate(int streamNumber, object streamPosition, SpeechLib.ISpeechRecoResult isrr) { label1.Text = "??"; show(filelist.GetPath("what", 0)); }; //言語モデルの作成 this.ControlGrammarRule = this.ControlRule.CreateGrammar(0); this.ControlGrammarRule.Reset(0); //言語モデルのルールのトップレベルを作成する. this.ControlGrammarRuleGrammarRule = this.ControlGrammarRule.Rules.Add("ControlRule", SpeechRuleAttributes.SRATopLevel | SpeechRuleAttributes.SRADynamic); //認証用文字列の追加. this.ControlGrammarRuleGrammarRule.InitialState.AddWordTransition(null, "プログラムを実行したい"); this.ControlGrammarRuleGrammarRule.InitialState.AddWordTransition(null, "ツイートしたい"); this.ControlGrammarRuleGrammarRule.InitialState.AddWordTransition(null, "検索したい"); this.ControlGrammarRuleGrammarRule.InitialState.AddWordTransition(null, "席をはずす"); this.ControlGrammarRuleGrammarRule.InitialState.AddWordTransition(null, "バッテリー残量は"); this.ControlGrammarRuleGrammarRule.InitialState.AddWordTransition(null, "プログラムリスト更新"); this.ControlGrammarRuleGrammarRule.InitialState.AddWordTransition(null, "設定を開いて"); this.ControlGrammarRuleGrammarRule.InitialState.AddWordTransition(null, "時計に戻して"); this.ControlGrammarRuleGrammarRule.InitialState.AddWordTransition(null, "君の名前は"); this.ControlGrammarRuleGrammarRule.InitialState.AddWordTransition(null, "疲れた"); this.ControlGrammarRuleGrammarRule.InitialState.AddWordTransition(null, "終了"); //ルールを反映させる。 this.ControlGrammarRule.Rules.Commit(); }
//音声認識分岐 private async void SpeechTextBranch(string speechtext) { //音声認識終了 this.ControlGrammarRule.CmdSetRuleState("ControlRule", SpeechRuleState.SGDSInactive); if (speechtext == Program.skinini.GetValue("skininfo", "clock", "時計に戻して")) { label1_MouseUp(null, null); return; } DataRow[] rows = commandlist.dt.Select("voice = '" + speechtext + "'"); switch ((string)rows[0][1]) { case "runprogram": show("search", 0); label1.Text = "何を実行する?"; if (!Program.UseSpeech) { textBox1.Location = new Point(12, 43); button1.Location = new Point(136, 40); textBox1.Visible = true; button1.Visible = true; } mode = "file"; if (Program.UseSpeech) { this.ProgramGrammarRule.CmdSetRuleState("ProgramRule", SpeechRuleState.SGDSActive); } break; case "tweet": show("chair", 0); label1.Text = "なんてつぶやく?"; if (!Program.UseSpeech) { textBox1.Visible = false; button1.Visible = false; textBox1.Text = ""; } mode = "twitter"; if (twitter == null) { MessageBox.Show("TwitterAPIが指定されていません", "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error); label1_MouseUp(null, null); } IniSave(); Form3 tweet = new Form3(); tweet.ShowDialog(this); if (Program.tweetdata != "" && twitter != null) { label1.Text = "ツイートを送信中"; label1.Refresh(); try { twitter.Statuses.Update(new { status = Program.tweetdata }); } catch { MessageBox.Show("ツイートに失敗しました", "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error); } } label1_MouseUp(null, null); break; case "search": show("search", 0); label1.Text = "何を検索する?"; if (!Program.UseSpeech) { textBox1.Visible = false; button1.Visible = false; textBox1.Text = ""; } mode = "search"; IniSave(); Form4 search = new Form4(); search.ShowDialog(this); label1_MouseUp(null, null); break; case "lockpc": mode = "lockpc"; try { DateTime dtNow = DateTime.Now; twitter.Statuses.Update(new { status = "ご主人が席を外されました【有栖ちゃん】\n" + dtNow.ToString("HH:mm:ss") }); } catch { } label1_MouseUp(null, null); LockWorkStation(); break; case "shutdownpc": mode = "shutdownpc"; try { DateTime dtNow = DateTime.Now; twitter.Statuses.Update(new { status = "ご主人が外出されました【有栖ちゃん】\n" + dtNow.ToString("HH:mm:ss") }); } catch { } show("smile", 0); label1.Text = "シャットダウンするね"; await Task.Delay(5 * 1000); label1_MouseUp(null, null); //強制的にシャットダウンする Program.AdjustToken(); ExitWindowsEx(ExitWindows.EWX_POWEROFF, 0); break; case "battery": show("find", 0); PowerLineStatus pls = SystemInformation.PowerStatus.PowerLineStatus; if (pls == PowerLineStatus.Online) { label1.Text = "AC電源駆動だよ"; } else { float blp = SystemInformation.PowerStatus.BatteryLifePercent; label1.Text = "バッテリー残量は\n" + blp * 100 + "% だよ"; } mode = "battery"; if (Program.UseSpeech) { this.ControlGrammarRule.CmdSetRuleState("ControlRule", SpeechRuleState.SGDSActive); } break; case "listreload": label1.Text = "読み直してるよ"; show("sleep", 3); mode = "refresh"; //ルール削除 this.ProgramGrammarRule.Reset(0); //言語モデルのルールのトップレベルを作成する. this.ProgramGrammarRuleGrammarRule = this.ProgramGrammarRule.Rules.Add("ProgramRule", SpeechRuleAttributes.SRATopLevel | SpeechRuleAttributes.SRADynamic); //認証用文字列の追加. ArrayList voicelist = list.GetProgramCommand(); foreach (string voice in voicelist) { this.ProgramGrammarRuleGrammarRule.InitialState.AddWordTransition(null, voice); } this.ProgramGrammarRuleGrammarRule.InitialState.AddWordTransition(null, Program.skinini.GetValue("skininfo", "clock", "時計に戻して")); //ルールを反映させる。 this.ProgramGrammarRule.Rules.Commit(); MessageBox.Show("更新完了"); GC.Collect(); label1_MouseUp(null, null); break; case "opensetting": label1_MouseUp(null, null); //音声認識ストップ this.AlwaysGrammarRule.CmdSetRuleState("AlwaysRule", SpeechRuleState.SGDSInactive); mode = "setting"; //現在の位置を一旦保存 IniSave(); show("kusonemi", 0); Form2 setting = new Form2(); setting.ShowDialog(this); //変更された情報を読み込む IniLoad(); InitTwitter(); show(lasttype, lastno); //音声認識再開 if (Program.UseSpeech) { this.AlwaysGrammarRule.CmdSetRuleState("AlwaysRule", SpeechRuleState.SGDSActive); } break; case "tired": show("tere", 0); if (!Program.UseSpeech) { textBox1.Visible = false; button1.Visible = false; textBox1.Text = ""; } label1.Text = "大丈夫?\nおっぱい揉む?"; mode = "tired"; if (Program.UseSpeech) { this.ControlGrammarRule.CmdSetRuleState("ControlRule", SpeechRuleState.SGDSActive); } break; case "exit": label1.Text = "終了しちゃうの?"; show("naki", 0); DialogResult result = MessageBox.Show("終了しますか?", "確認", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { IniSave(); Application.Exit(); } mode = "exit"; label1_MouseUp(null, null); break; } }
void add_transition(object prop, ISpeechGrammarRuleState source, ISpeechGrammarRuleState destination, ISpeechGrammarRule rule) { source.AddRuleTransition(destination, rule, (string)prop, ++cx, ref prop, 1.0f); }
//private void initSpeech() { // //Debug.WriteLine("Initializing SAPI"); // try { // //create Main context Obj // objRecoContext = new SpSharedRecoContext(); // objRecoContext.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(objRecoContext_Recognition); // grammar = objRecoContext.CreateGrammar(0); // string path = "Grammar.xml"; // grammar.CmdLoadFromFile(path, SpeechLoadOption.SLODynamic); // //activate Top Level Rule // grammar.CmdSetRuleIdState(0, SpeechRuleState.SGDSActive); // //speechInitialized = true; // } catch (Exception e) { // logOut("Exception: " + e.ToString()); // } //} private void InitializeSpeech() { Debug.WriteLine("Initializing SAPI objects..."); try { // First of all, let's create the main reco context object. // In this sample, we are using shared reco context. Inproc reco // context is also available. Please see the document to decide // which is best for your application. objRecoContext = new SpeechLib.SpSharedRecoContext(); // Then, let's set up the event handler. We only care about // Hypothesis and Recognition events in this sample. objRecoContext.Hypothesis += new _ISpeechRecoContextEvents_HypothesisEventHandler( RecoContext_Hypothesis); objRecoContext.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler( RecoContext_Recognition); // Now let's build the grammar. // The top level rule consists of two parts: "select <items>". // So we first add a word transition for the "select" part, then // a rule transition for the "<items>" part, which is dynamically // built as items are added or removed from the listbox. //grammar = objRecoContext.CreateGrammar(grammarId); //ruleTopLevel = grammar.Rules.Add("TopLevelRule", // SpeechRuleAttributes.SRATopLevel | SpeechRuleAttributes.SRADynamic, 1); //ruleListItems = grammar.Rules.Add("ListItemsRule", // SpeechRuleAttributes.SRADynamic, 2); //SpeechLib.ISpeechGrammarRuleState stateAfterSelect; //stateAfterSelect = ruleTopLevel.AddState(); //object PropValue = ""; //ruleTopLevel.InitialState.AddWordTransition(stateAfterSelect, // PreCommandString, " ", SpeechGrammarWordType.SGLexical, // "", 0, ref PropValue, 1.0F); //PropValue = ""; //stateAfterSelect.AddRuleTransition(null, ruleListItems, "", // 1, ref PropValue, 0F); // Now add existing list items to the ruleListItems grammar = objRecoContext.CreateGrammar(10); ruleTopLevel = grammar.Rules.Add("TopLevelRule", SpeechRuleAttributes.SRATopLevel | SpeechRuleAttributes.SRADynamic, 1); ruleListItemsDefault = grammar.Rules.Add("ListItemsRule", SpeechRuleAttributes.SRADynamic, 2); SpeechLib.ISpeechGrammarRuleState stateAfterSelect; stateAfterSelect = ruleTopLevel.AddState(); object PropValue = ""; ruleTopLevel.InitialState.AddWordTransition(stateAfterSelect, "", " ", SpeechGrammarWordType.SGLexical, "", 0, ref PropValue, 1.0F); PropValue = ""; stateAfterSelect.AddRuleTransition(null, ruleListItemsDefault, "", 1, ref PropValue, 0F); voiceInfoAutomat.RebuildGrammar(this.grammar, this.speechEnabled, this.objRecoContext, ruleListItemsDefault); // Now we can activate the top level rule. In this sample, only // the top level rule needs to activated. The ListItemsRule is // referenced by the top level rule. grammar.CmdSetRuleState("TopLevelRule", SpeechRuleState.SGDSActive); 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> /// Starts the listening /// </summary> public void Start() { recoContext = new SpSharedRecoContextClass(); recoContext.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(recoContext_Recognition); recoGrammar = recoContext.CreateGrammar(0); recoGrammarRule = recoGrammar.Rules.Add("VoiceCommands", SpeechRuleAttributes.SRATopLevel | SpeechRuleAttributes.SRADynamic, 1); object propValue = ""; for (int i = 0; i < commands.Count; i++) recoGrammarRule.InitialState.AddWordTransition(null, commands[i].ToString(), " ", SpeechGrammarWordType.SGLexical, Commands[i].ToString(), i, ref propValue, 1.0F); recoGrammar.Rules.Commit(); recoGrammar.CmdSetRuleState("VoiceCommands", SpeechRuleState.SGDSActive); }
/// <summary> /// Generate() will be called only once if there is no typed-text; it /// should use dictation. Generate() will be called a second time if /// there is typed-text; the second pass should use both dictation and /// context-free-grammar (ie, Command and Control: a Rule that's based /// on the typed-text). /// </summary> void Generate() { #if DEBUG logfile.Log(); logfile.Log("Generate() _generato= " + _generato); #endif _offset = 0; Confidence_def_count = 0; // was "2" but MS doc says not needed on its end. // and I don't see grammar id #2 defined on this end either. _recoGrammar = _recoContext.CreateGrammar(); // _recoGrammar.DictationLoad(); // ("Pronunciation") <- causes orthemes to print as phonemes instead of words switch (_generato) { case Generator.Dictati: if (_recoGrammar.Rules.FindRule(RULE) != null) { #if DEBUG logfile.Log(". set Rule INACTIVE"); #endif _recoGrammar.CmdSetRuleState(RULE, SpeechRuleState.SGDSInactive); } #if DEBUG logfile.Log(". set Dictation ACTIVE"); #endif _recoGrammar.DictationSetState(SpeechRuleState.SGDSActive); break; case Generator.Dialogi: #if DEBUG logfile.Log(". set Dictation INACTIVE"); #endif _recoGrammar.DictationSetState(SpeechRuleState.SGDSInactive); if (_recoGrammar.Rules.FindRule(RULE) == null) { #if DEBUG logfile.Log(". . add \"" + RULE + "\" Rule"); #endif ISpeechGrammarRule rule = _recoGrammar.Rules.Add(RULE, SpeechRuleAttributes.SRATopLevel, 1); rule.InitialState.AddWordTransition(null, _text, " ", SpeechGrammarWordType.SGLexical, RULE, 1); _recoGrammar.Rules.Commit(); } #if DEBUG logfile.Log(". set Rule ACTIVE"); #endif _recoGrammar.CmdSetRuleState(RULE, SpeechRuleState.SGDSActive); // logfile.Log(". max alternates(pre)= " + _recoContext.CmdMaxAlternates); // _recoContext.CmdMaxAlternates = 3; // logfile.Log(". max alternates(pos)= " + _recoContext.CmdMaxAlternates); break; } #if DEBUG logfile.Log(". create (SpFileStream)_fs"); #endif _fs = new SpFileStream(); #if DEBUG logfile.Log(". (SpFileStream)_fs CREATED"); #endif // _fs.Format.Type = SpeechAudioFormatType.SAFT44kHz16BitMono; #if DEBUG logfile.Log(". Open Wavefile _fs"); #endif _fs.Open(Wavefile); #if DEBUG logfile.Log(". _fs.Format.Type= " + _fs.Format.Type); // SpeechAudioFormatType.SAFT44kHz16BitMono SpWaveFormatEx data = _fs.Format.GetWaveFormatEx(); logfile.Log(". . SamplesPerSec= " + data.SamplesPerSec); logfile.Log(". . BitsPerSample= " + data.BitsPerSample); logfile.Log(". . AvgBytesPerSec= " + data.AvgBytesPerSec); logfile.Log(". . Channels= " + data.Channels); logfile.Log(". . BlockAlign= " + data.BlockAlign); logfile.Log(". . FormatTag= " + data.FormatTag); logfile.Log(". . ExtraData= " + data.ExtraData); // filestream byte-data -> // int bytes, pos = 0; // object o = new byte[2]; // while ((bytes = _fs.Read(out o, 2)) > 0) // { // var buffer = (byte[])o; // logfile.Log(pos + " : " + buffer[1] + " " + buffer[0]); // treat as little-endian shorts // pos += bytes; // } // _fs.Seek(0); logfile.Log(". assign _fs to _recognizer.AudioInputStream"); #endif _recognizer.AudioInputStream = _fs; // <- start Recognition <-- #if DEBUG logfile.Log("Generate() DONE"); logfile.Log(); #endif }
void add_transition(MetaNode item, ISpeechGrammarRuleState source, ISpeechGrammarRuleState destination, ISpeechGrammarRule rule) { object prop = item.get_path(); source.AddRuleTransition(destination, rule, (string)prop, ++cx, ref prop, 1.0f); }
public bool RebuildGrammar(ISpeechRecoGrammar grammar, bool speechEnabled, SpSharedRecoContext objRecoContext, ISpeechGrammarRule ruleListItemsDefault) { Debug.WriteLine("RebuildGrammar " + this.state.ToString()); if (!speechEnabled) { return false; } //grammar = objRecoContext.CreateGrammar(grammarId); ////ruleTopLevel = grammar.Rules.Add("TopLevelRule", SpeechRuleAttributes.SRATopLevel | SpeechRuleAttributes.SRADynamic, 1); object PropValue = ""; switch (this.state) { case State.init: //ruleListItemsNames = grammar.Rules.Add("ListItemsNameRule", SpeechRuleAttributes.SRADynamic, 2); //ruleListItemsIDs = grammar.Rules.Add("ListItemsIDRule", SpeechRuleAttributes.SRADynamic, 3); //ruleListItemsDefault = grammar.Rules.Add("ListItemsDefaultRule", SpeechRuleAttributes.SRADynamic, 2); //SpeechLib.ISpeechGrammarRuleState stateName; //SpeechLib.ISpeechGrammarRuleState stateID; ////SpeechLib.ISpeechGrammarRuleState stateDefault; //stateName = ruleTopLevel.AddState(); //stateID = ruleTopLevel.AddState(); ////stateDefault = ruleListItemsDefault.AddState(); //object PropValue1 = ""; //object PropValue2 = ""; ////PropValue = ""; //ruleTopLevel.InitialState.AddWordTransition(stateName, "name", " ", SpeechGrammarWordType.SGLexical, "", 0, ref PropValue1, 1.0F); //ruleTopLevel.InitialState.AddWordTransition(stateID, "id", " ", SpeechGrammarWordType.SGLexical, "", 0, ref PropValue2, 1.0F); ////ruleTopLevel.InitialState.AddWordTransition(stateDefault, " ", " ", SpeechGrammarWordType.SGLexical, "", 0, ref PropValue, 1.0F); //PropValue1 = ""; //PropValue2 = ""; ////PropValue = ""; //stateName.AddRuleTransition(null, ruleListItemsNames, "", 1, ref PropValue1, 0F); //stateID.AddRuleTransition(null, ruleListItemsIDs, "", 1, ref PropValue2, 0F); ////stateDefault.AddRuleTransition(null, ruleListItemsDefault, "", 1, ref PropValue, 0F); try { //ruleListItemsNames.Clear(); //ruleListItemsIDs.Clear(); ruleListItemsDefault.Clear(); //int i = 0; //foreach (string patientName in patientenNameList) { // string word = patientName; // ruleListItemsNames.InitialState.AddWordTransition(null, word, " ", SpeechGrammarWordType.SGLexical, word, i, ref PropValue1, 1F); // i++; //} //i = 0; //foreach (long patientID in patientIDList) { // long ID = patientID; // ruleListItemsIDs.InitialState.AddWordTransition(null, ID.ToString(), " ", SpeechGrammarWordType.SGLexical, ID.ToString(), i, ref PropValue2, 1F); // i++; //} int i = 0; foreach (string patientName in patientenNameList) { string word = "name " + patientName; ruleListItemsDefault.InitialState.AddWordTransition(null, word, " ", SpeechGrammarWordType.SGLexical, word, i, ref PropValue, 1F); i++; } foreach (long patientID in patientIDList) { string word = "id " + patientID.ToString(); ruleListItemsDefault.InitialState.AddWordTransition(null, word, " ", SpeechGrammarWordType.SGLexical, word, i, ref PropValue, 1F); i++; } grammar.Rules.Commit(); ////grammar.CmdSetRuleState("TopLevelRule", SpeechRuleState.SGDSActive); } catch (Exception e) { System.Windows.Forms.MessageBox.Show( "Exception caught when rebuilding dynamic listbox rule.\r\n\r\n" + e.ToString(), "Error"); } break; case State.cognitied: //ruleListItemsDefault = grammar.Rules.Add("ListItemsDefaultRule", SpeechRuleAttributes.SRADynamic, 2); //SpeechLib.ISpeechGrammarRuleState stateCognitied; //stateCognitied = ruleTopLevel.AddState(); //PropValue = ""; //ruleTopLevel.InitialState.AddWordTransition(stateCognitied, "", " ", SpeechGrammarWordType.SGLexical, "", 0, ref PropValue, 1.0F); //PropValue = ""; //stateCognitied.AddRuleTransition(null, ruleListItemsDefault, "", 1, ref PropValue, 0F); try { ruleListItemsDefault.Clear(); ruleListItemsDefault.InitialState.AddWordTransition(null, "master", " ", SpeechGrammarWordType.SGLexical, "master", 0, ref PropValue, 1F); ruleListItemsDefault.InitialState.AddWordTransition(null, "operation", " ", SpeechGrammarWordType.SGLexical, "operation", 1, ref PropValue, 1F); ruleListItemsDefault.InitialState.AddWordTransition(null, "visit", " ", SpeechGrammarWordType.SGLexical, "visit", 2, ref PropValue, 1F); ruleListItemsDefault.InitialState.AddWordTransition(null, "reset", " ", SpeechGrammarWordType.SGLexical, "reset", 3, ref PropValue, 1F); grammar.Rules.Commit(); //grammar.CmdSetRuleState("TopLevelRule", SpeechRuleState.SGDSActive); } catch (Exception e) { System.Windows.Forms.MessageBox.Show( "Exception caught when rebuilding dynamic listbox rule.\r\n\r\n" + e.ToString(), "Error"); } break; case State.master: try { ruleListItemsDefault.Clear(); ruleListItemsDefault.InitialState.AddWordTransition(null, "repeat", " ", SpeechGrammarWordType.SGLexical, "master", 0, ref PropValue, 1F); ruleListItemsDefault.InitialState.AddWordTransition(null, "back", " ", SpeechGrammarWordType.SGLexical, "operation", 1, ref PropValue, 1F); ruleListItemsDefault.InitialState.AddWordTransition(null, "reset", " ", SpeechGrammarWordType.SGLexical, "reset", 2, ref PropValue, 1F); grammar.Rules.Commit(); //grammar.CmdSetRuleState("TopLevelRule", SpeechRuleState.SGDSActive); } catch (Exception e) { System.Windows.Forms.MessageBox.Show( "Exception caught when rebuilding dynamic listbox rule.\r\n\r\n" + e.ToString(), "Error"); } break; case State.lastOperation: try { ruleListItemsDefault.Clear(); ruleListItemsDefault.InitialState.AddWordTransition(null, "repeat", " ", SpeechGrammarWordType.SGLexical, "master", 0, ref PropValue, 1F); ruleListItemsDefault.InitialState.AddWordTransition(null, "back", " ", SpeechGrammarWordType.SGLexical, "operation", 1, ref PropValue, 1F); ruleListItemsDefault.InitialState.AddWordTransition(null, "reset", " ", SpeechGrammarWordType.SGLexical, "reset", 2, ref PropValue, 1F); ruleListItemsDefault.InitialState.AddWordTransition(null, "more", " ", SpeechGrammarWordType.SGLexical, "reset", 3, ref PropValue, 1F); grammar.Rules.Commit(); //grammar.CmdSetRuleState("TopLevelRule", SpeechRuleState.SGDSActive); } catch (Exception e) { System.Windows.Forms.MessageBox.Show( "Exception caught when rebuilding dynamic listbox rule.\r\n\r\n" + e.ToString(), "Error"); } break; case State.lastVisit: try { ruleListItemsDefault.Clear(); ruleListItemsDefault.InitialState.AddWordTransition(null, "repeat", " ", SpeechGrammarWordType.SGLexical, "master", 0, ref PropValue, 1F); ruleListItemsDefault.InitialState.AddWordTransition(null, "back", " ", SpeechGrammarWordType.SGLexical, "operation", 1, ref PropValue, 1F); ruleListItemsDefault.InitialState.AddWordTransition(null, "reset", " ", SpeechGrammarWordType.SGLexical, "reset", 2, ref PropValue, 1F); ruleListItemsDefault.InitialState.AddWordTransition(null, "more", " ", SpeechGrammarWordType.SGLexical, "reset", 3, ref PropValue, 1F); grammar.Rules.Commit(); } catch (Exception e) { System.Windows.Forms.MessageBox.Show( "Exception caught when rebuilding dynamic listbox rule.\r\n\r\n" + e.ToString(), "Error"); } break; case State.allOperation: try { ruleListItemsDefault.Clear(); ruleListItemsDefault.InitialState.AddWordTransition(null, "repeat", " ", SpeechGrammarWordType.SGLexical, "master", 0, ref PropValue, 1F); ruleListItemsDefault.InitialState.AddWordTransition(null, "back", " ", SpeechGrammarWordType.SGLexical, "operation", 1, ref PropValue, 1F); ruleListItemsDefault.InitialState.AddWordTransition(null, "reset", " ", SpeechGrammarWordType.SGLexical, "reset", 2, ref PropValue, 1F); grammar.Rules.Commit(); } catch (Exception e) { System.Windows.Forms.MessageBox.Show( "Exception caught when rebuilding dynamic listbox rule.\r\n\r\n" + e.ToString(), "Error"); } break; case State.allVisit: try { ruleListItemsDefault.Clear(); ruleListItemsDefault.InitialState.AddWordTransition(null, "repeat", " ", SpeechGrammarWordType.SGLexical, "master", 0, ref PropValue, 1F); ruleListItemsDefault.InitialState.AddWordTransition(null, "back", " ", SpeechGrammarWordType.SGLexical, "operation", 1, ref PropValue, 1F); ruleListItemsDefault.InitialState.AddWordTransition(null, "reset", " ", SpeechGrammarWordType.SGLexical, "reset", 2, ref PropValue, 1F); grammar.Rules.Commit(); } catch (Exception e) { System.Windows.Forms.MessageBox.Show( "Exception caught when rebuilding dynamic listbox rule.\r\n\r\n" + e.ToString(), "Error"); } break; default: break; } return true; }
private void button3_Click(object sender, EventArgs e) { if (this.recRule != null) { return; } //this.button3.Enabled = false; this.recRule = new SpInProcRecoContext(); bool hit = false; foreach (SpObjectToken recPerson in this.recRule.Recognizer.GetRecognizers()) { string lang = recPerson.GetAttribute("Language"); if (lang == "411") { this.recRule.Recognizer.Recognizer = recPerson; hit = true; break; } } if (!hit) { MessageBox.Show("日本語認識が利用できません。"); } else { Console.WriteLine("マイク取得開始\n"); Console.WriteLine(this.recRule.Recognizer.Status.ClsidEngine); this.recRule.Recognizer.AudioInput = this.CreateMicrofon(); Console.WriteLine("マイク取得完了\n"); Console.WriteLine("デリゲート登録\n"); //認識の途中 this.recRule.Hypothesis += delegate(int streamNumber, object streamPosition, SpeechLib.ISpeechRecoResult result) { string strText = result.PhraseInfo.GetText(0, -1, true); this._SourceViewLabel.Text = strText; }; //認識完了 this.recRule.Recognition += delegate(int streamNumber, object streamPosition, SpeechLib.SpeechRecognitionType srt, SpeechLib.ISpeechRecoResult isrr) { string strText = isrr.PhraseInfo.GetText(0, -1, true); this._ResultLabel.Text = strText; }; //ストリームに何かデータが来た(?) this.recRule.StartStream += delegate(int streamNumber, object streamPosition) { this._SourceViewLabel.Text = "認識?"; this._ResultLabel.Text = "認識?"; }; //認識失敗 this.recRule.FalseRecognition += delegate(int streamNumber, object streamPosition, SpeechLib.ISpeechRecoResult isrr) { this._ResultLabel.Text = "--ERROR!--"; }; Console.WriteLine("デリゲート登録完了\n"); Console.WriteLine("モデル作成\n"); //言語モデルの作成 this.recGrammerRule = this.recRule.CreateGrammar(0); Console.WriteLine("モデル作成完了\n"); this.recGrammerRule.Reset(0); //言語モデルのルールのトップレベルを作成する. this.recGRGrammerRule = this.recGrammerRule.Rules.Add("TopLevelRule", SpeechRuleAttributes.SRATopLevel | SpeechRuleAttributes.SRADynamic); // 読み込む対象文字列をここですべて読み込んでおく必要がある this.recGRGrammerRule.InitialState.AddWordTransition(null, "私は"); //ルールを反映させる。 this.recGrammerRule.Rules.Commit(); //音声認識開始。(トップレベルのオブジェクトの名前で SpeechRuleState.SGDSActive を指定する.) this.recGrammerRule.CmdSetRuleState("TopLevelRule", SpeechRuleState.SGDSActive); Console.WriteLine("音声認識開始"); } }