Home › Forums › OpenEars › Switching between runRecognitionOnWavFile & standard setup › Reply To: Switching between runRecognitionOnWavFile & standard setup
January 15, 2019 at 2:16 pm
#1032686
Tapestes
Participant
Either speech rec is active and we autodetect start of speech, or it’s suspended and we send the .wav to the parseWav function in the code below…
open class OpenEarsManager: NSObject, OEEventsObserverDelegate {
public static let sharedInstance = OpenEarsManager()
var openEarsEventsObserver: OEEventsObserver!
var words: Array<String> = []
var lmGenerator = OELanguageModelGenerator()
var resumeOnFinished = true
var generalLangaugeModel = ""
var generalDictionary = ""
var readBackLangaugeModel = ""
var readBackDictionary = ""
var useReadback = false
override init() {
super.init()
OEPocketsphinxController.sharedInstance().disableMixing = true
openEarsEventsObserver = OEEventsObserver()
openEarsEventsObserver.delegate = self
//setup general recognition
let content = try! Data(contentsOf: Bundle.main.url(forResource: "general", withExtension: "grmr")!)
let jsonDictionary = try! JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as! [AnyHashable: Any]
let _ = lmGenerator.generateGrammar(from: jsonDictionary, withFilesNamed: "GENERAL", forAcousticModelAtPath: OEAcousticModel.path(toModel: "AcousticModelEnglish"))
self.generalLangaugeModel = lmGenerator.pathToSuccessfullyGeneratedGrammar(withRequestedName: "GENERAL")
self.generalDictionary = lmGenerator.pathToSuccessfullyGeneratedDictionary(withRequestedName: "GENERAL")
//setup readback recognition
let rBcontent = try! Data(contentsOf: Bundle.main.url(forResource: "readback", withExtension: "grmr")!)
let rBjsonDictionary = try! JSONSerialization.jsonObject(with: rBcontent, options: JSONSerialization.ReadingOptions.mutableContainers) as! [AnyHashable: Any]
let _ = lmGenerator.generateGrammar(from: rBjsonDictionary, withFilesNamed: "READBACK", forAcousticModelAtPath: OEAcousticModel.path(toModel: "AcousticModelEnglish"))
self.readBackLangaugeModel = lmGenerator.pathToSuccessfullyGeneratedGrammar(withRequestedName: "READBACK")
self.readBackDictionary = lmGenerator.pathToSuccessfullyGeneratedDictionary(withRequestedName: "READBACK")
}
/**
Method for manually parsing wav for speech rec
- parameter wavPath: path to wav to be parsed
*/
open func parseWav(_ wavPath: String) {
OEPocketsphinxController.sharedInstance().runRecognitionOnWavFile(atPath: wavPath, usingLanguageModelAtPath: self.generalLangaugeModel, dictionaryAtPath: self.generalDictionary, acousticModelAtPath: OEAcousticModel.path(toModel: "AcousticModelEnglish"), languageModelIsJSGF: true)
}
/**
Method for spooling up OpenEars speech rec
- parameter grammarType: grammar type to be used for speech rec
- parameter appendGeneral: whether to append the general grammar to the grammar file
*/
open func startDetectingSpeech() {
try! AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playAndRecord, mode: AVAudioSession.sharedInstance().mode)
let inputs = AVAudioSession.sharedInstance().availableInputs
for input in inputs! {
if(input.portType.rawValue == "MicrophoneWired") {
try! AVAudioSession.sharedInstance().setPreferredInput(input)
}
}
if (OEPocketsphinxController.sharedInstance().isListening) {
self.stopDetectingSpeech()
}
//might delete and use default - VoiceChat includes system sounds...
OEPocketsphinxController.sharedInstance().audioMode = "VoiceChat"
do { try OEPocketsphinxController.sharedInstance().setActive(true) } catch {}
OEPocketsphinxController.sharedInstance().startListeningWithLanguageModel(atPath: self.generalLangaugeModel, dictionaryAtPath: self.generalDictionary, acousticModelAtPath: OEAcousticModel.path(toModel: "AcousticModelEnglish"), languageModelIsJSGF: true)
}
/**
Method for OpenEars tear down
*/
open func stopDetectingSpeech() {
OEPocketsphinxController.sharedInstance().stopListening()
}
/**
Method for suspending speech rec without tear down
*/
open func suspendDetectingSpeech() {
print("SUSPEND")
OEPocketsphinxController.sharedInstance().suspendRecognition()
}
/**
Method for resuming suspended speech rec
*/
open func resumeDetectingSpeech() {
print("RESUME")
OEPocketsphinxController.sharedInstance().resumeRecognition()
}
/**
Method handler for speech rec hypothesis
- parameter hypothesis: Open ears text from speech
- parameter recognitionScore: confidence
- parameter utteranceID: utterance id
*/
open func pocketsphinxDidReceiveHypothesis(_ hypothesis: String!, recognitionScore: String!, utteranceID: String!) {
sharedSpeechRecParser.parseSpeechRec(hypothesis)
}
/**
Method handler for detecting of speech
*/
open func pocketsphinxDidDetectSpeech() {
NotificationCenter.default.post(name: Foundation.Notification.Name(rawValue: "SpeechStarted"), object: nil, userInfo: nil) //listened for by MicButton
}
/**
Method handler for catching speech rec tear down
*/
open func pocketsphinxDidStopListening() {
NotificationCenter.default.post(name: Foundation.Notification.Name(rawValue: "SpeechStopped"), object: nil, userInfo: nil) //listened for by MicButton
}
/**
Method handler for catching suspend of speech rec
*/
open func pocketsphinxDidSuspendRecognition() {
NotificationCenter.default.post(name: Foundation.Notification.Name(rawValue: "SpeechSuspended"), object: nil, userInfo: nil) //listened for by MicButton
}
/**
Method handler for catching resume of speech rec
*/
open func pocketsphinxDidResumeRecognition() {
NotificationCenter.default.post(name: Foundation.Notification.Name(rawValue: "SpeechResumed"), object: nil, userInfo: nil) //listened for by MicButton
}
}