update kotlin api for better release native object and add user-friendly apis. (#1275)

This commit is contained in:
Robin Zhong
2024-08-22 19:18:11 +08:00
committed by GitHub
parent 5a2aa110b8
commit d8001d6edc
8 changed files with 100 additions and 15 deletions

View File

@@ -24,7 +24,7 @@ class KeywordSpotter(
assetManager: AssetManager? = null, assetManager: AssetManager? = null,
val config: KeywordSpotterConfig, val config: KeywordSpotterConfig,
) { ) {
private val ptr: Long private var ptr: Long
init { init {
ptr = if (assetManager != null) { ptr = if (assetManager != null) {
@@ -35,7 +35,10 @@ class KeywordSpotter(
} }
protected fun finalize() { protected fun finalize() {
if (ptr != 0L) {
delete(ptr) delete(ptr)
ptr = 0
}
} }
fun release() = finalize() fun release() = finalize()

View File

@@ -18,7 +18,7 @@ class OfflinePunctuation(
assetManager: AssetManager? = null, assetManager: AssetManager? = null,
config: OfflinePunctuationConfig, config: OfflinePunctuationConfig,
) { ) {
private val ptr: Long private var ptr: Long
init { init {
ptr = if (assetManager != null) { ptr = if (assetManager != null) {
@@ -29,7 +29,10 @@ class OfflinePunctuation(
} }
protected fun finalize() { protected fun finalize() {
if (ptr != 0L) {
delete(ptr) delete(ptr)
ptr = 0
}
} }
fun release() = finalize() fun release() = finalize()

View File

@@ -72,7 +72,7 @@ class OfflineRecognizer(
assetManager: AssetManager? = null, assetManager: AssetManager? = null,
config: OfflineRecognizerConfig, config: OfflineRecognizerConfig,
) { ) {
private val ptr: Long private var ptr: Long
init { init {
ptr = if (assetManager != null) { ptr = if (assetManager != null) {
@@ -83,7 +83,10 @@ class OfflineRecognizer(
} }
protected fun finalize() { protected fun finalize() {
if (ptr != 0L) {
delete(ptr) delete(ptr)
ptr = 0
}
} }
fun release() = finalize() fun release() = finalize()
@@ -102,7 +105,14 @@ class OfflineRecognizer(
val lang = objArray[3] as String val lang = objArray[3] as String
val emotion = objArray[4] as String val emotion = objArray[4] as String
val event = objArray[5] as String val event = objArray[5] as String
return OfflineRecognizerResult(text = text, tokens = tokens, timestamps = timestamps, lang = lang, emotion = emotion, event = event) return OfflineRecognizerResult(
text = text,
tokens = tokens,
timestamps = timestamps,
lang = lang,
emotion = emotion,
event = event
)
} }
fun decode(stream: OfflineStream) = decode(ptr, stream.ptr) fun decode(stream: OfflineStream) = decode(ptr, stream.ptr)

View File

@@ -13,6 +13,14 @@ class OfflineStream(var ptr: Long) {
fun release() = finalize() fun release() = finalize()
fun use(block: (OfflineStream) -> Unit) {
try {
block(this)
} finally {
release()
}
}
private external fun acceptWaveform(ptr: Long, samples: FloatArray, sampleRate: Int) private external fun acceptWaveform(ptr: Long, samples: FloatArray, sampleRate: Int)
private external fun delete(ptr: Long) private external fun delete(ptr: Long)

View File

@@ -85,7 +85,7 @@ class OnlineRecognizer(
assetManager: AssetManager? = null, assetManager: AssetManager? = null,
val config: OnlineRecognizerConfig, val config: OnlineRecognizerConfig,
) { ) {
private val ptr: Long private var ptr: Long
init { init {
ptr = if (assetManager != null) { ptr = if (assetManager != null) {
@@ -96,7 +96,10 @@ class OnlineRecognizer(
} }
protected fun finalize() { protected fun finalize() {
if (ptr != 0L) {
delete(ptr) delete(ptr)
ptr = 0
}
} }
fun release() = finalize() fun release() = finalize()

View File

@@ -15,10 +15,19 @@ class OnlineStream(var ptr: Long = 0) {
fun release() = finalize() fun release() = finalize()
fun use(block: (OnlineStream) -> Unit) {
try {
block(this)
} finally {
release()
}
}
private external fun acceptWaveform(ptr: Long, samples: FloatArray, sampleRate: Int) private external fun acceptWaveform(ptr: Long, samples: FloatArray, sampleRate: Int)
private external fun inputFinished(ptr: Long) private external fun inputFinished(ptr: Long)
private external fun delete(ptr: Long) private external fun delete(ptr: Long)
companion object { companion object {
init { init {
System.loadLibrary("sherpa-onnx-jni") System.loadLibrary("sherpa-onnx-jni")

View File

@@ -19,11 +19,13 @@ data class VadModelConfig(
var debug: Boolean = false, var debug: Boolean = false,
) )
class SpeechSegment(val start: Int, val samples: FloatArray)
class Vad( class Vad(
assetManager: AssetManager? = null, assetManager: AssetManager? = null,
var config: VadModelConfig, var config: VadModelConfig,
) { ) {
private val ptr: Long private var ptr: Long
init { init {
if (assetManager != null) { if (assetManager != null) {
@@ -34,17 +36,23 @@ class Vad(
} }
protected fun finalize() { protected fun finalize() {
if (ptr != 0L) {
delete(ptr) delete(ptr)
ptr = 0
} }
}
fun release() = finalize()
fun acceptWaveform(samples: FloatArray) = acceptWaveform(ptr, samples) fun acceptWaveform(samples: FloatArray) = acceptWaveform(ptr, samples)
fun empty(): Boolean = empty(ptr) fun empty(): Boolean = empty(ptr)
fun pop() = pop(ptr) fun pop() = pop(ptr)
// return an array containing fun front(): SpeechSegment {
// [start: Int, samples: FloatArray] val segment = front(ptr)
fun front() = front(ptr) return SpeechSegment(segment[0] as Int, segment[1] as FloatArray)
}
fun clear() = clear(ptr) fun clear() = clear(ptr)

View File

@@ -3,8 +3,49 @@ package com.k2fsa.sherpa.onnx
import android.content.res.AssetManager import android.content.res.AssetManager
data class WaveData(
val samples: FloatArray,
val sampleRate: Int,
) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as WaveData
if (!samples.contentEquals(other.samples)) return false
if (sampleRate != other.sampleRate) return false
return true
}
override fun hashCode(): Int {
var result = samples.contentHashCode()
result = 31 * result + sampleRate
return result
}
}
class WaveReader { class WaveReader {
companion object { companion object {
fun readWave(
assetManager: AssetManager,
filename: String,
): WaveData {
return readWaveFromAsset(assetManager, filename).let {
WaveData(it[0] as FloatArray, it[1] as Int)
}
}
fun readWave(
filename: String,
): WaveData {
return readWaveFromFile(filename).let {
WaveData(it[0] as FloatArray, it[1] as Int)
}
}
// Read a mono wave file asset // Read a mono wave file asset
// The returned array has two entries: // The returned array has two entries:
// - the first entry contains an 1-D float array // - the first entry contains an 1-D float array