From 7b0584719094306a8e15d986773ffd7d9bd09bbd Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Mon, 23 Oct 2023 12:30:25 +0800 Subject: [PATCH] small fixes --- kotlin-api-examples/Tts.kt | 19 ++++++++++++++++++- sherpa-onnx/jni/jni.cc | 7 ++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/kotlin-api-examples/Tts.kt b/kotlin-api-examples/Tts.kt index 50cb795d5..09f3eb901 100644 --- a/kotlin-api-examples/Tts.kt +++ b/kotlin-api-examples/Tts.kt @@ -40,7 +40,7 @@ class OfflineTts( assetManager: AssetManager? = null, var config: OfflineTtsConfig, ) { - private val ptr: Long + private var ptr: Long init { if (assetManager != null) { @@ -60,6 +60,23 @@ class OfflineTts( sampleRate=objArray[1] as Int) } + fun allocate(assetManager: AssetManager? = null) { + if (ptr == 0L) { + if (assetManager != null) { + ptr = new(assetManager, config) + } else { + ptr = newFromFile(config) + } + } + } + + fun free() { + if (ptr != 0L) { + delete(ptr) + ptr = 0 + } + } + protected fun finalize() { delete(ptr) } diff --git a/sherpa-onnx/jni/jni.cc b/sherpa-onnx/jni/jni.cc index 4e26a404e..299f7d751 100644 --- a/sherpa-onnx/jni/jni.cc +++ b/sherpa-onnx/jni/jni.cc @@ -601,6 +601,12 @@ JNIEXPORT jlong JNICALL Java_com_k2fsa_sherpa_onnx_OfflineTts_newFromFile( return (jlong)tts; } +SHERPA_ONNX_EXTERN_C +JNIEXPORT void JNICALL Java_com_k2fsa_sherpa_onnx_OfflineTts_delete( + JNIEnv *env, jobject /*obj*/, jlong ptr) { + delete reinterpret_cast(ptr); +} + // see // https://stackoverflow.com/questions/29043872/android-jni-return-multiple-variables static jobject NewInteger(JNIEnv *env, int32_t value) { @@ -614,7 +620,6 @@ JNIEXPORT jobjectArray JNICALL Java_com_k2fsa_sherpa_onnx_OfflineTts_generateImpl(JNIEnv *env, jobject /*obj*/, jlong ptr, jstring text, jint sid, jfloat speed) { - SHERPA_ONNX_LOGE("get string"); const char *p_text = env->GetStringUTFChars(text, nullptr); SHERPA_ONNX_LOGE("string is: %s", p_text);