Toggle navigation
Toggle navigation
此项目
正在载入...
Sign in
xuning
/
sherpaonnx
转到一个项目
Toggle navigation
项目
群组
代码片段
帮助
Toggle navigation pinning
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
Robin Zhong
2024-08-22 19:18:11 +0800
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Committed by
GitHub
2024-08-22 19:18:11 +0800
Commit
d8001d6edc4ba65baa181b733906b1152cd8b182
d8001d6e
1 parent
5a2aa110
update kotlin api for better release native object and add user-friendly apis. (#1275)
隐藏空白字符变更
内嵌
并排对比
正在显示
8 个修改的文件
包含
100 行增加
和
15 行删除
sherpa-onnx/kotlin-api/KeywordSpotter.kt
sherpa-onnx/kotlin-api/OfflinePunctuation.kt
sherpa-onnx/kotlin-api/OfflineRecognizer.kt
sherpa-onnx/kotlin-api/OfflineStream.kt
sherpa-onnx/kotlin-api/OnlineRecognizer.kt
sherpa-onnx/kotlin-api/OnlineStream.kt
sherpa-onnx/kotlin-api/Vad.kt
sherpa-onnx/kotlin-api/WaveReader.kt
sherpa-onnx/kotlin-api/KeywordSpotter.kt
查看文件 @
d8001d6
...
...
@@ -24,7 +24,7 @@ class KeywordSpotter(
assetManager: AssetManager? = null,
val config: KeywordSpotterConfig,
) {
private va
l
ptr: Long
private va
r
ptr: Long
init {
ptr = if (assetManager != null) {
...
...
@@ -35,7 +35,10 @@ class KeywordSpotter(
}
protected fun finalize() {
delete(ptr)
if (ptr != 0L) {
delete(ptr)
ptr = 0
}
}
fun release() = finalize()
...
...
sherpa-onnx/kotlin-api/OfflinePunctuation.kt
查看文件 @
d8001d6
...
...
@@ -18,7 +18,7 @@ class OfflinePunctuation(
assetManager: AssetManager? = null,
config: OfflinePunctuationConfig,
) {
private va
l
ptr: Long
private va
r
ptr: Long
init {
ptr = if (assetManager != null) {
...
...
@@ -29,7 +29,10 @@ class OfflinePunctuation(
}
protected fun finalize() {
delete(ptr)
if (ptr != 0L) {
delete(ptr)
ptr = 0
}
}
fun release() = finalize()
...
...
sherpa-onnx/kotlin-api/OfflineRecognizer.kt
查看文件 @
d8001d6
...
...
@@ -72,7 +72,7 @@ class OfflineRecognizer(
assetManager: AssetManager? = null,
config: OfflineRecognizerConfig,
) {
private va
l
ptr: Long
private va
r
ptr: Long
init {
ptr = if (assetManager != null) {
...
...
@@ -83,7 +83,10 @@ class OfflineRecognizer(
}
protected fun finalize() {
delete(ptr)
if (ptr != 0L) {
delete(ptr)
ptr = 0
}
}
fun release() = finalize()
...
...
@@ -102,7 +105,14 @@ class OfflineRecognizer(
val lang = objArray[3] as String
val emotion = objArray[4] 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)
...
...
sherpa-onnx/kotlin-api/OfflineStream.kt
查看文件 @
d8001d6
...
...
@@ -13,6 +13,14 @@ class OfflineStream(var ptr: Long) {
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 delete(ptr: Long)
...
...
sherpa-onnx/kotlin-api/OnlineRecognizer.kt
查看文件 @
d8001d6
...
...
@@ -62,7 +62,7 @@ data class OnlineRecognizerConfig(
var featConfig: FeatureConfig = FeatureConfig(),
var modelConfig: OnlineModelConfig,
var lmConfig: OnlineLMConfig = OnlineLMConfig(),
var ctcFstDecoderConfig
: OnlineCtcFstDecoderConfig = OnlineCtcFstDecoderConfig(),
var ctcFstDecoderConfig: OnlineCtcFstDecoderConfig = OnlineCtcFstDecoderConfig(),
var endpointConfig: EndpointConfig = EndpointConfig(),
var enableEndpoint: Boolean = true,
var decodingMethod: String = "greedy_search",
...
...
@@ -85,7 +85,7 @@ class OnlineRecognizer(
assetManager: AssetManager? = null,
val config: OnlineRecognizerConfig,
) {
private va
l
ptr: Long
private va
r
ptr: Long
init {
ptr = if (assetManager != null) {
...
...
@@ -96,7 +96,10 @@ class OnlineRecognizer(
}
protected fun finalize() {
delete(ptr)
if (ptr != 0L) {
delete(ptr)
ptr = 0
}
}
fun release() = finalize()
...
...
sherpa-onnx/kotlin-api/OnlineStream.kt
查看文件 @
d8001d6
...
...
@@ -15,10 +15,19 @@ class OnlineStream(var ptr: Long = 0) {
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 inputFinished(ptr: Long)
private external fun delete(ptr: Long)
companion object {
init {
System.loadLibrary("sherpa-onnx-jni")
...
...
sherpa-onnx/kotlin-api/Vad.kt
查看文件 @
d8001d6
...
...
@@ -19,11 +19,13 @@ data class VadModelConfig(
var debug: Boolean = false,
)
class SpeechSegment(val start: Int, val samples: FloatArray)
class Vad(
assetManager: AssetManager? = null,
var config: VadModelConfig,
) {
private va
l
ptr: Long
private va
r
ptr: Long
init {
if (assetManager != null) {
...
...
@@ -34,17 +36,23 @@ class Vad(
}
protected fun finalize() {
delete(ptr)
if (ptr != 0L) {
delete(ptr)
ptr = 0
}
}
fun release() = finalize()
fun acceptWaveform(samples: FloatArray) = acceptWaveform(ptr, samples)
fun empty(): Boolean = empty(ptr)
fun pop() = pop(ptr)
// return an array containing
// [start: Int, samples: FloatArray]
fun front() = front(ptr)
fun front(): SpeechSegment {
val segment = front(ptr)
return SpeechSegment(segment[0] as Int, segment[1] as FloatArray)
}
fun clear() = clear(ptr)
...
...
sherpa-onnx/kotlin-api/WaveReader.kt
查看文件 @
d8001d6
...
...
@@ -3,8 +3,49 @@ package com.k2fsa.sherpa.onnx
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 {
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
// The returned array has two entries:
// - the first entry contains an 1-D float array
...
...
请
注册
或
登录
后发表评论