SpokenLanguageIdentification.kt
2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package com.k2fsa.sherpa.onnx
import android.content.res.AssetManager
data class SpokenLanguageIdentificationWhisperConfig(
var encoder: String = "",
var decoder: String = "",
var tailPaddings: Int = -1,
)
data class SpokenLanguageIdentificationConfig(
var whisper: SpokenLanguageIdentificationWhisperConfig = SpokenLanguageIdentificationWhisperConfig(),
var numThreads: Int = 1,
var debug: Boolean = false,
var provider: String = "cpu",
)
class SpokenLanguageIdentification(
assetManager: AssetManager? = null,
config: SpokenLanguageIdentificationConfig,
) {
private var ptr: Long
init {
ptr = if (assetManager != null) {
newFromAsset(assetManager, config)
} else {
newFromFile(config)
}
}
protected fun finalize() {
if (ptr != 0L) {
delete(ptr)
ptr = 0
}
}
fun release() = finalize()
fun createStream(): OfflineStream {
val p = createStream(ptr)
return OfflineStream(p)
}
fun compute(stream: OfflineStream) = compute(ptr, stream.ptr)
private external fun newFromAsset(
assetManager: AssetManager,
config: SpokenLanguageIdentificationConfig,
): Long
private external fun newFromFile(
config: SpokenLanguageIdentificationConfig,
): Long
private external fun delete(ptr: Long)
private external fun createStream(ptr: Long): Long
private external fun compute(ptr: Long, streamPtr: Long): String
companion object {
init {
System.loadLibrary("sherpa-onnx-jni")
}
}
}
// please refer to
// https://k2-fsa.github.io/sherpa/onnx/spolken-language-identification/pretrained_models.html#whisper
// to download more models
fun getSpokenLanguageIdentificationConfig(
type: Int,
numThreads: Int = 1
): SpokenLanguageIdentificationConfig? {
when (type) {
0 -> {
val modelDir = "sherpa-onnx-whisper-tiny"
return SpokenLanguageIdentificationConfig(
whisper = SpokenLanguageIdentificationWhisperConfig(
encoder = "$modelDir/tiny-encoder.int8.onnx",
decoder = "$modelDir/tiny-decoder.int8.onnx",
),
numThreads = numThreads,
debug = true,
)
}
1 -> {
val modelDir = "sherpa-onnx-whisper-base"
return SpokenLanguageIdentificationConfig(
whisper = SpokenLanguageIdentificationWhisperConfig(
encoder = "$modelDir/tiny-encoder.int8.onnx",
decoder = "$modelDir/tiny-decoder.int8.onnx",
),
numThreads = 1,
debug = true,
)
}
}
return null
}