c# - Naudio and Syn speech null reference error when reading from memory stream -
i'm pretty sure i'm doing right can't seem figure out causing error or how fix it. appreciated. code below error below that.
code. posted of since not know relevant error.
public class voicestuff { public waveinevent waveinstream; private static streamspeechrecognizer _recognizer; wavefilewriter writer; private memorystream mem; public void record() { waveinstream.numberofbuffers = 2; waveinstream.startrecording(); recognize(); } public void recognize() { console.readkey(); waveinstream.stoprecording(); } public voicestuff() { logger.logreceived += logger_logreceived; waveinstream = new waveinevent(); waveinstream.numberofbuffers =2; waveinstream.waveformat = new waveformat(16000, 1); mem = new memorystream(); writer = new wavefilewriter(mem, waveinstream.waveformat); waveinstream.dataavailable += ondataavailable; var modelpath = path.combine(directory.getcurrentdirectory(), "model/en-us"); var dictionarypath = path.combine(modelpath, "cmudict-en-us.dict"); var languagemodelpath = path.combine(modelpath, "en-us.lm.dmp"); var configuration = new configuration { acousticmodelpath = modelpath, dictionarypath = dictionarypath, languagemodelpath = languagemodelpath, usegrammar = true, grammarpath = "models", grammarname = "hello" }; _recognizer = new streamspeechrecognizer(configuration); waveinstream.startrecording(); console.readkey(); waveinstream.stoprecording(); thread.sleep(50); _recognizer.startrecognition(mem, new timeframe(mem.length)); speechresult result = _recognizer.getresult(); _recognizer.stoprecognition(); console.writeline("result: " + result?.gethypothesis()); //syn speech } public void ondataavailable(object sender, waveineventargs e) { writer.write(e.buffer, 0, e.bytesrecorded); } static void logger_logreceived(object sender, logreceivedeventargs e) { console.writeline(e.message); } }
error
9/12/2016 9:55:42 pm error streamspeechrecognizer system.nullreferenceexception: object reference not set instance of object. @ syn.speech.linguist.dictionary.textdictionary.allocate() @ syn.speech.linguist.language.grammar.grammar.allocate() @ syn.speech.linguist.flat.flatlinguist.allocate() @ syn.speech.decoders.search.simplebreadthfirstsearchmanager.allocate() @ syn.speech.recognizers.recognizer.allocate() @ syn.speech.api.streamspeechrecognizer.startrecognition(stream stream, timeframe timeframe) 9/12/2016 9:55:42 pm error streamspeechrecognizer syn.speech.helper.illegalstateexception: expected state ready actual state allocating @ syn.speech.recognizers.recognizer.checkstate(state desiredstate) @ syn.speech.recognizers.recognizer.recognize(string referencetext) @ syn.speech.api.abstractspeechrecognizer.getresult() 9/12/2016 9:55:42 pm error streamspeechrecognizer syn.speech.helper.illegalstateexception: expected state ready actual state allocating @ syn.speech.recognizers.recognizer.checkstate(state desiredstate) @ syn.speech.recognizers.recognizer.deallocate() @ syn.speech.api.streamspeechrecognizer.stoprecognition()
at least need dispose
wavefilewriter
in order finalise wav file structure. however, beware of disposing memory stream well. wrap in naudio's utility class ignoredisposestream
then need rewind memorystream
start before passing on speech recognition.
Comments
Post a Comment