-
Notifications
You must be signed in to change notification settings - Fork 1
For Developers
This wiki will assume you already have a mod setup and know the basics of modding and C#.
VoiceRecognitionAPI currently doesn't have a nuget package. Instead you'll need to download the latest release and add a reference to the .dll.
Then register VoiceRecognitionAPI as a BepInDependency
[BepInDependency("me.loaforc.voicerecognitionapi")]
public class YourModPlugin : BaseUnityPlugin {Or you can register it as a soft dependency if you want to add functionality to VoiceRecognitionAPI to your mod without requiring people use it
[BepInDependency("me.loaforc.voicerecognitionapi", BepInDependency.DependencyFlags.SoftDependency)]
public class YourModPlugin : BaseUnityPlugin {Because of a limitation in Microsoft's Speech Recognition you need to register your phrases before the game finishes loading (or to be more specific before GameNetworkManager.Start)
To register a simple phrase you can use Voice.ListenForPhrase
Voice.ListenForPhrase("my cool phrase", (message) => {
Plugin.logger.LogInfo("my cool voice phrase was said!");
});With using the default Voice.ListenForPhrase you can supply a float value between 0 - 1 on the Confidence value. The Confidence value determines how easy/hard it should be for the Speech Recognition Engine to recognize your phrase. Higher values means it needs to be more confident, lower values will activate more and therefore at more false positives. The default is .7f and that will be fine for most use cases.
Voice.ListenForPhrase("my cool phrase", (message) => {
Plugin.logger.LogInfo("Because the confidence is set to .2f, this logging line will activate more frequently.");
Plugin.logger.LogInfo("That doesn't necessarily mean it's better to have a lower confidence because more false positives will happen");
}, .2f);For example if you have the following script:
Voice.ListenForPhrase("my cool phrase", (message) => {
Plugin.logger.LogInfo("do thing");
});
Voice.ListenForPhrase("my epic phrase", (message) => {
Plugin.logger.LogInfo("do thing");
});You can simplify it by using Voice.ListenForPhrases (notice the s at the end)
Voice.ListenForPhrases(new string[] { "my cool phrase", "my epic phrase" }, (message) => {
Plugin.logger.LogInfo("do thing");
});
Voice.ListenForPhrases(new string[] { "my other cool phrase", "my other epic phrase" }, (message) => {
Plugin.logger.LogInfo("do other thing");
}, .5f);This binds multiple phrases to the same action. If you want voice phrases to do different things then you'll want to keep them seperate.
If you need more customisability (like changing the confidence at runtime) you can use Voice.RegisterPhrases and Voice.RegisterCustomHandler. Keep in mind that this is harder and you'll need to check what was said in your event (to make sure it doesn't fire at any voice recognition result). For example:
Voice.RegisterPhrases(new string[] { "test phrase" });
Voice.RegisterCustomHandler((obj, recognized) => {
if(recognized.Message != "test phrase") return;
if(recognized.Confidence > someValueThatChangesAtRuntime) {
Plugin.logger.LogInfo("test phrase was said!");
}
});