Add JNI (#57)
This commit is contained in:
2
.github/scripts/.gitignore
vendored
Normal file
2
.github/scripts/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
Makefile
|
||||
*.jar
|
||||
4
.github/scripts/AssetManager.kt
vendored
Normal file
4
.github/scripts/AssetManager.kt
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
package android.content.res
|
||||
|
||||
// a dummy class for testing only
|
||||
class AssetManager
|
||||
45
.github/scripts/Main.kt
vendored
Normal file
45
.github/scripts/Main.kt
vendored
Normal file
@@ -0,0 +1,45 @@
|
||||
package com.k2fsa.sherpa.onnx
|
||||
|
||||
import android.content.res.AssetManager
|
||||
|
||||
fun main() {
|
||||
var featConfig = FeatureConfig(
|
||||
sampleRate=16000.0f,
|
||||
featureDim=80,
|
||||
)
|
||||
|
||||
var modelConfig = OnlineTransducerModelConfig(
|
||||
encoder="./sherpa-onnx-streaming-zipformer-en-2023-02-21/encoder-epoch-99-avg-1.onnx",
|
||||
decoder="./sherpa-onnx-streaming-zipformer-en-2023-02-21/decoder-epoch-99-avg-1.onnx",
|
||||
joiner="./sherpa-onnx-streaming-zipformer-en-2023-02-21/joiner-epoch-99-avg-1.onnx",
|
||||
numThreads=4,
|
||||
debug=false,
|
||||
)
|
||||
|
||||
var endpointConfig = EndpointConfig()
|
||||
|
||||
var config = OnlineRecognizerConfig(
|
||||
modelConfig=modelConfig,
|
||||
featConfig=featConfig,
|
||||
endpointConfig=endpointConfig,
|
||||
tokens="./sherpa-onnx-streaming-zipformer-en-2023-02-21/tokens.txt",
|
||||
enableEndpoint=true,
|
||||
)
|
||||
|
||||
var model = SherpaOnnx(
|
||||
assetManager = AssetManager(),
|
||||
config = config,
|
||||
)
|
||||
var samples = WaveReader.readWave(
|
||||
assetManager = AssetManager(),
|
||||
filename = "./sherpa-onnx-streaming-zipformer-en-2023-02-21/test_wavs/1089-134686-0001.wav",
|
||||
)
|
||||
|
||||
model.decodeSamples(samples!!)
|
||||
|
||||
var tail_paddings = FloatArray(8000) // 0.5 seconds
|
||||
model.decodeSamples(tail_paddings)
|
||||
|
||||
model.inputFinished()
|
||||
println(model.text)
|
||||
}
|
||||
89
.github/scripts/SherpaOnnx.kt
vendored
Normal file
89
.github/scripts/SherpaOnnx.kt
vendored
Normal file
@@ -0,0 +1,89 @@
|
||||
package com.k2fsa.sherpa.onnx
|
||||
|
||||
import android.content.res.AssetManager
|
||||
|
||||
data class EndpointRule(
|
||||
var mustContainNonSilence: Boolean,
|
||||
var minTrailingSilence: Float,
|
||||
var minUtteranceLength: Float,
|
||||
)
|
||||
|
||||
data class EndpointConfig(
|
||||
var rule1: EndpointRule = EndpointRule(false, 2.4f, 0.0f),
|
||||
var rule2: EndpointRule = EndpointRule(true, 1.4f, 0.0f),
|
||||
var rule3: EndpointRule = EndpointRule(false, 0.0f, 20.0f)
|
||||
)
|
||||
|
||||
data class OnlineTransducerModelConfig(
|
||||
var encoder: String,
|
||||
var decoder: String,
|
||||
var joiner: String,
|
||||
var numThreads: Int = 4,
|
||||
var debug: Boolean = false,
|
||||
)
|
||||
|
||||
data class FeatureConfig(
|
||||
var sampleRate: Float = 16000.0f,
|
||||
var featureDim: Int = 80,
|
||||
)
|
||||
|
||||
data class OnlineRecognizerConfig(
|
||||
var featConfig: FeatureConfig = FeatureConfig(),
|
||||
var modelConfig: OnlineTransducerModelConfig,
|
||||
var tokens: String,
|
||||
var endpointConfig: EndpointConfig = EndpointConfig(),
|
||||
var enableEndpoint: Boolean,
|
||||
)
|
||||
|
||||
class SherpaOnnx(
|
||||
assetManager: AssetManager,
|
||||
var config: OnlineRecognizerConfig
|
||||
) {
|
||||
private val ptr: Long
|
||||
|
||||
init {
|
||||
ptr = new(assetManager, config)
|
||||
}
|
||||
|
||||
protected fun finalize() {
|
||||
delete(ptr)
|
||||
}
|
||||
|
||||
|
||||
fun decodeSamples(samples: FloatArray) =
|
||||
decodeSamples(ptr, samples, sampleRate = config.featConfig.sampleRate)
|
||||
|
||||
fun inputFinished() = inputFinished(ptr)
|
||||
fun reset() = reset(ptr)
|
||||
fun isEndpoint(): Boolean = isEndpoint(ptr)
|
||||
|
||||
val text: String
|
||||
get() = getText(ptr)
|
||||
|
||||
private external fun delete(ptr: Long)
|
||||
|
||||
private external fun new(
|
||||
assetManager: AssetManager,
|
||||
config: OnlineRecognizerConfig,
|
||||
): Long
|
||||
|
||||
private external fun decodeSamples(ptr: Long, samples: FloatArray, sampleRate: Float)
|
||||
private external fun inputFinished(ptr: Long)
|
||||
private external fun getText(ptr: Long): String
|
||||
private external fun reset(ptr: Long)
|
||||
private external fun isEndpoint(ptr: Long): Boolean
|
||||
|
||||
companion object {
|
||||
init {
|
||||
System.loadLibrary("sherpa-onnx-jni")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun getFeatureConfig(): FeatureConfig {
|
||||
val featConfig = FeatureConfig()
|
||||
featConfig.sampleRate = 16000.0f
|
||||
featConfig.featureDim = 80
|
||||
|
||||
return featConfig
|
||||
}
|
||||
17
.github/scripts/WaveReader.kt
vendored
Normal file
17
.github/scripts/WaveReader.kt
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
package com.k2fsa.sherpa.onnx
|
||||
|
||||
import android.content.res.AssetManager
|
||||
|
||||
class WaveReader {
|
||||
companion object {
|
||||
// Read a mono wave file.
|
||||
// No resampling is made.
|
||||
external fun readWave(
|
||||
assetManager: AssetManager, filename: String, expected_sample_rate: Float = 16000.0f
|
||||
): FloatArray?
|
||||
|
||||
init {
|
||||
System.loadLibrary("sherpa-onnx-jni")
|
||||
}
|
||||
}
|
||||
}
|
||||
33
.github/scripts/test-jni.sh
vendored
Executable file
33
.github/scripts/test-jni.sh
vendored
Executable file
@@ -0,0 +1,33 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -e
|
||||
|
||||
mkdir -p build
|
||||
cd build
|
||||
|
||||
cmake \
|
||||
-DSHERPA_ONNX_ENABLE_PYTHON=OFF \
|
||||
-DSHERPA_ONNX_ENABLE_TESTS=OFF \
|
||||
-DSHERPA_ONNX_ENABLE_CHECK=OFF \
|
||||
-DBUILD_SHARED_LIBS=ON \
|
||||
-DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \
|
||||
-DSHERPA_ONNX_ENABLE_JNI=ON \
|
||||
..
|
||||
|
||||
make -j4
|
||||
ls -lh lib
|
||||
|
||||
cd ..
|
||||
|
||||
export LD_LIBRARY_PATH=$PWD/build/lib:$LD_LIBRARY_PATH
|
||||
|
||||
cd .github/scripts/
|
||||
|
||||
git lfs install
|
||||
git clone https://huggingface.co/csukuangfj/sherpa-onnx-streaming-zipformer-en-2023-02-21
|
||||
|
||||
kotlinc-jvm -include-runtime -d main.jar Main.kt WaveReader.kt SherpaOnnx.kt AssetManager.kt
|
||||
|
||||
ls -lh main.jar
|
||||
|
||||
java -Djava.library.path=../../build/lib -jar main.jar
|
||||
Reference in New Issue
Block a user