Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleaning recognizer properly on android #32

Merged
merged 2 commits into from
Mar 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 24 additions & 22 deletions android/src/main/java/com/reactnativevosk/VoskModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class VoskModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMo
private var model: Model? = null
private var speechService: SpeechService? = null
private var context: ReactApplicationContext? = reactContext
private var recognizer: Recognizer? = null

override fun getName(): String {
return "Vosk"
Expand All @@ -23,10 +24,8 @@ class VoskModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMo
val text = getHypothesisText(hypothesis)

// Stop recording if data found
if (text != null && text.isNotEmpty() && speechService != null) {
speechService!!.stop()
speechService = null
// Display data
if (text != null && text.isNotEmpty()) {
cleanRecognizer();
Comment on lines +27 to +28

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@riderodd I am facing a problem where the onFinalResult event is never triggered, and I believe this change is the reason since everything is cleaned up on the first result. Does it make sense?

sendEvent("onResult", text)
}
}
Expand Down Expand Up @@ -93,10 +92,7 @@ class VoskModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMo

@ReactMethod
fun loadModel(path: String, promise: Promise) {
if (this.model != null) {
this.model!!.close(); // unload model
this.model = null;
}
cleanModel();
StorageService.unpack(context, path, "models",
{ model: Model? ->
this.model = model
Expand All @@ -118,13 +114,13 @@ class VoskModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMo
sendEvent("onError", "Recognizer is already in use")
} else {
try {
val rec =
recognizer =
if (grammar != null)
Recognizer(model, 16000.0f, makeGrammar(grammar))
else
Recognizer(model, 16000.0f)

speechService = SpeechService(rec, 16000.0f)
speechService = SpeechService(recognizer, 16000.0f)
speechService!!.startListening(this)
sendEvent("onStart")

Expand All @@ -133,27 +129,33 @@ class VoskModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMo
}
}
}

@ReactMethod
fun stop() {

private fun cleanRecognizer() {
if (speechService != null) {
speechService!!.stop()
speechService!!.shutdown();
speechService = null
}
if (recognizer != null) {
recognizer!!.close();
recognizer = null;
}
}

@ReactMethod
fun unload() {

private fun cleanModel() {
if (this.model != null) {
this.model!!.close(); // unload model
this.model!!.close();
this.model = null;
}
}

if (speechService != null) {
speechService!!.stop();
speechService!!.shutdown();
}
@ReactMethod
fun stop() {
cleanRecognizer();
}

@ReactMethod
fun unload() {
cleanRecognizer();
cleanModel();
}
}