Skip to content

Commit

Permalink
small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
csukuangfj committed Oct 23, 2023
1 parent cd2fa82 commit 7b05847
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
19 changes: 18 additions & 1 deletion kotlin-api-examples/Tts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class OfflineTts(
assetManager: AssetManager? = null,
var config: OfflineTtsConfig,
) {
private val ptr: Long
private var ptr: Long

init {
if (assetManager != null) {
Expand All @@ -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)
}
Expand Down
7 changes: 6 additions & 1 deletion sherpa-onnx/jni/jni.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<sherpa_onnx::SherpaOnnxOfflineTts *>(ptr);
}

// see
// https://stackoverflow.com/questions/29043872/android-jni-return-multiple-variables
static jobject NewInteger(JNIEnv *env, int32_t value) {
Expand All @@ -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);

Expand Down

0 comments on commit 7b05847

Please sign in to comment.