using System; using System.Drawing; using System.Speech.Recognition;//append to the stuff between the quotes References textbox: " | C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Speech.dll" public static class VoiceBotScript { //--------------------------------------------------------------------------- /// <summary> /// long winded example of verbal confirmation macroscript, where the response /// must be "valid". To accept any response is much simpler, just remove the stuff /// between //<Complex - validated response> and //</Complex - validated response> /// then uncomment the commented line //<simple void prompt> for the simple voice prompt /// </summary> /// <param name="windowHandle"></param> public static void Run(IntPtr windowHandle) { string str_valid_sites = "Google,Home,Eve-Gate,Discussions";//<<--some words may be harder to properly understand, so training may be needed to improve accuracy, or adding the false positives to the list of valid responses to mitigate them string str_urls = "https://www.google.com/advanced_search,http://www.WhereEverYouHomePageIs.net,https://gate.eveonline.com/,https://www.voicebot.net/Discussions/"; string str_url = ""; //<<--selected url based on user response string str_confirmation = "";//<<--verbal response converted to string bool bln_valid_response = true;//<<--validation flag bool bln_quit = false; //<<--failsafe flag bool bln_heard_correctly = true; //<<--flag to retry without using a retry int int_retries = 0;//<<--failsafe counter int int_index = 0; //<<--used to link valid response to the associated url //<simple void prompt>string str_confirmation = (string)Speech.SpeechRecognizer("Please select the site you would like to go to from the following {SITES}".Replace("{SITES}", str_valid_sites)); //<Complex - validated response> bln_valid_response = false; BFS.Speech.TextToSpeech("Please select the site you would like to go to from the following"); BFS.Speech.TextToSpeech("{SITES}".Replace("{SITES}", str_valid_sites)); while(bln_valid_response == false || bln_quit == false) { str_confirmation = (string)Speech.SpeechRecogizer("");//<<--we've moved the voice prompt out side the loop so the system waits for a valid response without further "prompt" BFS.Speech.TextToSpeech("I heard: " + str_confirmation); bln_heard_correctly = BFS.Dialog.GetUserConfirm("I heard: " + str_confirmation);//<<--so use can see the recognized speech, and update the valid list, or whatever... . if(!bln_heard_correctly){continue;}else{;}//<<--skip processing, and try again, we were not heard correctly BFS.Speech.TextToSpeech("looking for a matching valid site"); int_index = 0; // foreach(string str_valid_site in str_valid_sites.Split(',')) { BFS.Speech.TextToSpeech("site: " + str_valid_site); if(str_confirmation.Trim().ToLower() == str_valid_site.Trim().ToLower()) { BFS.Speech.TextToSpeech("match found."); str_url = str_urls.Split(',')[int_index]; bln_valid_response = true; break;//<<--breach the loop } else{BFS.Speech.TextToSpeech("wasn't a match, continuing.");} //</if(str_confirmation.ToLower() == str_valid_site.ToLower())> int_index++; }//</foreach(string str_valid_site in str_valid_sites.Split(','))> //>>>>>was the response valid? if(bln_valid_response == false) { int_retries++;//<<--increament failsafe value, this will allow the prompt to fail after so many retries //>>>>>see if we want to provide "help" to the user... switch(int_retries) { case 1: BFS.Speech.TextToSpeech("I require a site chosen from the provided list: {SITES}.".Replace("{SITES}", str_valid_sites)); break; case 3: BFS.Speech.TextToSpeech("If I may, the following is the list of valid responses: {SITES}.".Replace("{SITES}",str_valid_sites)); break; default: if(int_retries >= 5) { BFS.Speech.TextToSpeech("As, I am unable to understand your response, I will accept this as a failure to respond and abort. There may be a technical fault with the voice input system if you were indeed responding."); bln_quit = true; } else{;} //</if(int_retries >= 5)> break; }//</switch(true)> } else { break;//<<--breach the loop } //</if(bln_valid_response == false)> }//</while(bln_valid_response == false)> //</Complex - validated response> if(bln_valid_response) { if(BFS.Dialog.GetUserConfirm("You selected site \"{SITE}\" End of line.".Replace("{SITE}", str_url))) {BFS.Web.OpenUrl(str_url);} else{;}//>>>>>do nothing //</if(BFS.Dialog.GetUserConfirm("You selected site "{SITE}" End of line.".Replace("{SITE}", str_url)))> } else{;}//>>>>>do nothing }//</Run(IntPtr windowHandle)> }/*</class VoiceBotScript>*/ //===================================================================== /// <summary> /// Author: Darkstrumn /// Function: Aux speech functions, such as voice prompting /// </summary> public static class Speech { //--------------------------------------------------------------------- /// <summary> /// SpeechRecogizer provide voice prompts, where the user can be prompted /// aurally to respond verbally and the response is converted to string and /// returned for further processing /// </summary> /// <param name=""str_voice_prompt""></param> /// <returns></returns> public static string SpeechRecogizer(string str_voice_prompt) { string str_return = ""; SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine(); Grammar dictationGrammar = new DictationGrammar(); recognizer.LoadGrammar(dictationGrammar); BFS.Speech.TextToSpeech(str_voice_prompt); // try { recognizer.SetInputToDefaultAudioDevice(); RecognitionResult result = recognizer.Recognize(); str_return = result.Text; } catch (InvalidOperationException exception) { BFS.Dialog.ShowMessageError("Error detected during sound acquisition: {SOURCE} - {MESSAGF}.".Replace("{SOURCE}",exception.Source).Replace("{MESSAGF}",exception.Message)); } finally { recognizer.UnloadAllGrammars(); }//</try> return (str_return); }//</SpeachRecognizer()> //------------------------------------------------------------------------ }/*</class Speech>*/
System.Core.dll | System.Data.dll | System.dll | System.Drawing.dll | System.Management.dll | System.Web.dll | System.Windows.Forms.dll | System.Xml.dll | C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Speech.dll
using System; using System.Drawing; using System.Speech.Recognition;//append the stuff between the quotes to References textbox: " | C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Speech.dll" public static class VoiceBotScript { //--------------------------------------------------------------------------- /// <summary> /// long winded example of verbal confirmation macroscript, where the response /// must be "valid". To accept any response is much simpler, just remove the stuff /// between //<Complex - validated response> and //</Complex - validated response> /// then uncomment the commented line //<simple void prompt> for the simple voice prompt /// v2: cleaned up some for both clarity and updated coding style /// </summary> /// <param name="windowHandle"></param> public static void Run(IntPtr windowHandle) { string str_valid_sites = "Google,Home,Eve-Gate,Discussions";//<<--some words may be harder to properly understand, so training may be needed to improve accuracy, or adding the false positives to the list of valid responses to mitigate them string str_urls = "https://www.google.com/advanced_search,http://www.WhereEverYouHomePageIs.net,https://gate.eveonline.com/,https://www.voicebot.net/Discussions/"; string str_url = ""; //<<--selected url based on user response string str_confirmation = "";//<<--verbal response converted to string bool bln_valid_response = true;//<<--validation flag bool bln_quit = false; //<<--failsafe flag bool bln_heard_correctly = true; //<<--flag to retry without using a retry int int_retries = 0;//<<--failsafe counter int int_index = 0; //<<--used to link valid response to the associated url //<simple void prompt>string str_confirmation = (string)Speech.SpeechRecognizer("Please select the site you would like to go to from the following {str_valid_sites}".Replace("{str_valid_sites}", str_valid_sites)); //<Complex - validated response> bln_valid_response = false; BFS.Speech.TextToSpeech("Please select the site you would like to go to from the following"); BFS.Speech.TextToSpeech("{str_valid_sites}".Replace("{str_valid_sites}", str_valid_sites)); while( bln_valid_response == false || bln_quit == false ) { str_confirmation = (string)Speech.SpeechRecogizer(string.Empty);//<<--we've moved the voice prompt out side the loop so the system waits for a valid response without further "prompt" BFS.Speech.TextToSpeech("I heard: {str_confirmation}".Replace("{str_confirmation}", str_confirmation)); bln_heard_correctly = BFS.Dialog.GetUserConfirm("I heard: {str_confirmation}".Replace("{str_confirmation}", str_confirmation));//<<--so use can see the recognized speech, and update the valid list, or whatever... . if( !bln_heard_correctly ) { continue; } else {/*>>>>>do nothing*/; }//<<--skip processing, and try again, we were not heard correctly BFS.Speech.TextToSpeech("looking for a matching valid site"); int_index = 0; // foreach( string str_valid_site in str_valid_sites.Split(',') ) { BFS.Speech.TextToSpeech("site: {str_valid_site}".Replace("{str_valid_site}", str_valid_site)); if( str_confirmation.Trim().ToLower() == str_valid_site.Trim().ToLower() ) { BFS.Speech.TextToSpeech("match found."); str_url = str_urls.Split(',')[int_index]; bln_valid_response = true; break;//<<--breach the loop } else { BFS.Speech.TextToSpeech("wasn't a match, continuing."); }//</if> int_index++; }//</foreach> //>>>>>was the response valid? if( bln_valid_response == false ) { int_retries++;//<<--increment failsafe value, this will allow the prompt to fail after so many retries //>>>>>see if we want to provide "help" to the user... switch( int_retries ) { case 1: BFS.Speech.TextToSpeech("I require a site chosen from the provided list: {str_valid_sites}.".Replace("{str_valid_sites}", str_valid_sites)); break; case 3: BFS.Speech.TextToSpeech("If I may, the following is the list of valid responses: {str_valid_sites}.".Replace("{str_valid_sites}", str_valid_sites)); break; default: if( int_retries >= 5 ) { BFS.Speech.TextToSpeech("As, I am unable to understand your response, I will accept this as a failure to respond and abort. There may be a technical fault with the voice input system if you were indeed responding."); bln_quit = true; } else {/*>>>>>do nothing*/; } break; }//</switch> } else { break;//<<--breach the loop }//</if> }//</while> //</Complex - validated response> if( bln_valid_response ) { if( BFS.Dialog.GetUserConfirm("You selected site \"{str_url}\" End of line.".Replace("{str_url}", str_url)) ) { BFS.Web.OpenUrl(str_url); } else {/*>>>>>do nothing*/; } //</if> } else {/*>>>>>do nothing*/; } }//</Run> }/*</class VoiceBotScript>*/ //===================================================================== /// <summary> /// Author: Darkstrumn /// Function: Aux speech functions, such as voice prompting /// </summary> public static class Speech { //--------------------------------------------------------------------- /// <summary> /// SpeechRecogizer provide voice prompts, where the user can be prompted /// aurally to respond verbally and the response is converted to string and /// returned for further processing /// </summary> /// <param name="str_voice_prompt"></param> /// <returns></returns> public static string SpeechRecogizer(string str_voice_prompt) { string str_return = ""; SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine(); Grammar dictationGrammar = new DictationGrammar(); recognizer.LoadGrammar(dictationGrammar); BFS.Speech.TextToSpeech(str_voice_prompt); // try { recognizer.SetInputToDefaultAudioDevice(); RecognitionResult result = recognizer.Recognize(); str_return = result.Text; } catch( InvalidOperationException exception ) { BFS.Dialog.ShowMessageError("Error detected during sound acquisition: {exception.Source} - {exception.Message}.".Replace("{exception.Source}", exception.Source).Replace("{exception.Message}", exception.Message)); } finally { recognizer.UnloadAllGrammars(); }//</try> return (str_return); }//</SpeachRecognizer()> //------------------------------------------------------------------------ }/*</class Speech>*/