Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Sample code for KeenASR speech recognition engine usage from Unity
using UnityEngine;
using System.Collections;
using KeenResearch;
public class TestKeenASR : MonoBehaviour {
// Use this for initialization
void Start () {
Debug.Log ("Initializing KeenASR Plugin");
KeenASR.Initialize ("librispeech-nnet2-en-us");
KeenASR recognizer = KeenASR.Instance;
KeenASR.Instance.SetLogLevel (KeenASR.LogLevelDebug);
string dgName = "numbers"; // this will be decoding graph name, which we case use later on
if (!recognizer.CustomDecodingGraphWithNameExists (dgName)) {
Debug.Log ("Creating decoding graph");
string[] phrases = new string[] { "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN" };
recognizer.CreateCustomDecodingGraphFromSentences (dgName, phrases);
} else {
Debug.Log ("Decoding graph already exists (IF YOU CHANGE THE WORDLIST, YOU WILL NEED TO RECREATE THE DECODING GRAPH");
}
recognizer.PrepareForListeningWithCustomDecodingGraph(dgName);
// when set to true, SDK will create audio recordings capturing audio that was
// passed to the engine (between start and end listening)
// you can get the file pat via GetLastRecordingFilename(), once the recognizer
// stopped listening (e.g. in onFinalASRResultReceived callback
recognizer.SetCreateAudioRecordings (true);
// final result will be reported after 1sec of end silence
recognizer.SetVADParameter (KeenASR.VadParamTimeoutEndSilenceForGoodMatch, 1f);
recognizer.SetVADParameter (KeenASR.VadParamTimeoutEndSilenceForAnyMatch, 1f);
recognizer.onFinalASRResultReceived += FinalASRResult;
recognizer.onPartialASRResultReceived += PartialASRResult;
}
// Update is called once per frame
void Update () {
if (KeenASR.Instance.IsListening ()) {
// here we just show how input levels change; you would poll the input levels somewhere else
// and use it to drive a UI component (not print in the log file)
//Debug.Log ("\tLevel: " + KeenASR.Instance.InputLevel ());
}
}
public void FinalASRResult(ASRResult result) {
Debug.Log ("FINAL RESULT:" + result.cleanText + ", conf: " + result.confidence + ", numWords: " + result.words.Length);
foreach (ASRWord word in result.words) {
if (word.isTag)
Debug.Log ("Word " + word.text + " is a tag word");
if (word.confidence < 0.8)
Debug.Log ("Word " + word.text + " has LOW confidence");
}
// For testing/demo purposes only; it's unlikely you would need to call this method from within
// the FinalASRResult callback
Debug.Log("final callback isListening returns: " + KeenASR.Instance.IsListening());
Debug.Log("Audio file saved in: " + KeenASR.Instance.GetLastRecordingFilename());
}
public void PartialASRResult(string result) {
Debug.Log ("PARTIAL RESULT:" + result);
// For testing/demo purposes only; it's unlikely you would need to call this method from within
// the FinalASRResult callback
Debug.Log("partial callback, isListening returns: " + KeenASR.Instance.IsListening());
}
void OnGUI() {
if (GUI.Button(new Rect(100, 450, 200, 100), "Start Listening")) {
#if UNITY_IPHONE
KeenASR.Instance.StartListening();
#endif
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment