JAVA-API: Manual Library Loading Support for Restricted Environments (#2253)
* feat: Added LibraryLoader that allows loading to be skipped * feat: Changed static call to new LibraryLoader * feat: Makefile adjustment
This commit is contained in:
@@ -6,7 +6,9 @@ out_jar := $(out_dir)/sherpa-onnx.jar
|
|||||||
|
|
||||||
package_dir := com/k2fsa/sherpa/onnx
|
package_dir := com/k2fsa/sherpa/onnx
|
||||||
|
|
||||||
java_files := WaveReader.java
|
java_files := LibraryLoader.java
|
||||||
|
|
||||||
|
java_files += WaveReader.java
|
||||||
java_files += WaveWriter.java
|
java_files += WaveWriter.java
|
||||||
java_files += EndpointRule.java
|
java_files += EndpointRule.java
|
||||||
java_files += EndpointConfig.java
|
java_files += EndpointConfig.java
|
||||||
|
|||||||
@@ -3,13 +3,10 @@
|
|||||||
package com.k2fsa.sherpa.onnx;
|
package com.k2fsa.sherpa.onnx;
|
||||||
|
|
||||||
public class AudioTagging {
|
public class AudioTagging {
|
||||||
static {
|
|
||||||
System.loadLibrary("sherpa-onnx-jni");
|
|
||||||
}
|
|
||||||
|
|
||||||
private long ptr = 0;
|
private long ptr = 0;
|
||||||
|
|
||||||
public AudioTagging(AudioTaggingConfig config) {
|
public AudioTagging(AudioTaggingConfig config) {
|
||||||
|
LibraryLoader.maybeLoad();
|
||||||
ptr = newFromFile(config);
|
ptr = newFromFile(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,14 +3,11 @@
|
|||||||
package com.k2fsa.sherpa.onnx;
|
package com.k2fsa.sherpa.onnx;
|
||||||
|
|
||||||
public class DenoisedAudio {
|
public class DenoisedAudio {
|
||||||
static {
|
|
||||||
System.loadLibrary("sherpa-onnx-jni");
|
|
||||||
}
|
|
||||||
|
|
||||||
private final float[] samples;
|
private final float[] samples;
|
||||||
private final int sampleRate;
|
private final int sampleRate;
|
||||||
|
|
||||||
public DenoisedAudio(float[] samples, int sampleRate) {
|
public DenoisedAudio(float[] samples, int sampleRate) {
|
||||||
|
LibraryLoader.maybeLoad();
|
||||||
this.samples = samples;
|
this.samples = samples;
|
||||||
this.sampleRate = sampleRate;
|
this.sampleRate = sampleRate;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,14 +3,11 @@
|
|||||||
package com.k2fsa.sherpa.onnx;
|
package com.k2fsa.sherpa.onnx;
|
||||||
|
|
||||||
public class GeneratedAudio {
|
public class GeneratedAudio {
|
||||||
static {
|
|
||||||
System.loadLibrary("sherpa-onnx-jni");
|
|
||||||
}
|
|
||||||
|
|
||||||
private final float[] samples;
|
private final float[] samples;
|
||||||
private final int sampleRate;
|
private final int sampleRate;
|
||||||
|
|
||||||
public GeneratedAudio(float[] samples, int sampleRate) {
|
public GeneratedAudio(float[] samples, int sampleRate) {
|
||||||
|
LibraryLoader.maybeLoad();
|
||||||
this.samples = samples;
|
this.samples = samples;
|
||||||
this.sampleRate = sampleRate;
|
this.sampleRate = sampleRate;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,13 +3,10 @@
|
|||||||
package com.k2fsa.sherpa.onnx;
|
package com.k2fsa.sherpa.onnx;
|
||||||
|
|
||||||
public class KeywordSpotter {
|
public class KeywordSpotter {
|
||||||
static {
|
|
||||||
System.loadLibrary("sherpa-onnx-jni");
|
|
||||||
}
|
|
||||||
|
|
||||||
private long ptr = 0;
|
private long ptr = 0;
|
||||||
|
|
||||||
public KeywordSpotter(KeywordSpotterConfig config) {
|
public KeywordSpotter(KeywordSpotterConfig config) {
|
||||||
|
LibraryLoader.maybeLoad();
|
||||||
ptr = newFromFile(config);
|
ptr = newFromFile(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package com.k2fsa.sherpa.onnx;
|
||||||
|
|
||||||
|
public class LibraryLoader {
|
||||||
|
private static volatile boolean autoLoadEnabled = true;
|
||||||
|
private static volatile boolean isLoaded = false;
|
||||||
|
|
||||||
|
static synchronized void loadLibrary() {
|
||||||
|
if (!isLoaded) {
|
||||||
|
System.loadLibrary("sherpa-onnx-jni");
|
||||||
|
isLoaded = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setAutoLoadEnabled(boolean enabled) {
|
||||||
|
autoLoadEnabled = enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void maybeLoad() {
|
||||||
|
if (autoLoadEnabled) {
|
||||||
|
loadLibrary();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,13 +3,10 @@
|
|||||||
package com.k2fsa.sherpa.onnx;
|
package com.k2fsa.sherpa.onnx;
|
||||||
|
|
||||||
public class OfflinePunctuation {
|
public class OfflinePunctuation {
|
||||||
static {
|
|
||||||
System.loadLibrary("sherpa-onnx-jni");
|
|
||||||
}
|
|
||||||
|
|
||||||
private long ptr = 0;
|
private long ptr = 0;
|
||||||
|
|
||||||
public OfflinePunctuation(OfflinePunctuationConfig config) {
|
public OfflinePunctuation(OfflinePunctuationConfig config) {
|
||||||
|
LibraryLoader.maybeLoad();
|
||||||
ptr = newFromFile(config);
|
ptr = newFromFile(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,13 +3,10 @@
|
|||||||
package com.k2fsa.sherpa.onnx;
|
package com.k2fsa.sherpa.onnx;
|
||||||
|
|
||||||
public class OfflineRecognizer {
|
public class OfflineRecognizer {
|
||||||
static {
|
|
||||||
System.loadLibrary("sherpa-onnx-jni");
|
|
||||||
}
|
|
||||||
|
|
||||||
private long ptr = 0;
|
private long ptr = 0;
|
||||||
|
|
||||||
public OfflineRecognizer(OfflineRecognizerConfig config) {
|
public OfflineRecognizer(OfflineRecognizerConfig config) {
|
||||||
|
LibraryLoader.maybeLoad();
|
||||||
ptr = newFromFile(config);
|
ptr = newFromFile(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,13 +3,10 @@
|
|||||||
package com.k2fsa.sherpa.onnx;
|
package com.k2fsa.sherpa.onnx;
|
||||||
|
|
||||||
public class OfflineSpeakerDiarization {
|
public class OfflineSpeakerDiarization {
|
||||||
static {
|
|
||||||
System.loadLibrary("sherpa-onnx-jni");
|
|
||||||
}
|
|
||||||
|
|
||||||
private long ptr = 0;
|
private long ptr = 0;
|
||||||
|
|
||||||
public OfflineSpeakerDiarization(OfflineSpeakerDiarizationConfig config) {
|
public OfflineSpeakerDiarization(OfflineSpeakerDiarizationConfig config) {
|
||||||
|
LibraryLoader.maybeLoad();
|
||||||
ptr = newFromFile(config);
|
ptr = newFromFile(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,13 +3,10 @@
|
|||||||
package com.k2fsa.sherpa.onnx;
|
package com.k2fsa.sherpa.onnx;
|
||||||
|
|
||||||
public class OfflineSpeechDenoiser {
|
public class OfflineSpeechDenoiser {
|
||||||
static {
|
|
||||||
System.loadLibrary("sherpa-onnx-jni");
|
|
||||||
}
|
|
||||||
|
|
||||||
private long ptr = 0;
|
private long ptr = 0;
|
||||||
|
|
||||||
public OfflineSpeechDenoiser(OfflineSpeechDenoiserConfig config) {
|
public OfflineSpeechDenoiser(OfflineSpeechDenoiserConfig config) {
|
||||||
|
LibraryLoader.maybeLoad();
|
||||||
ptr = newFromFile(config);
|
ptr = newFromFile(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,13 +3,10 @@
|
|||||||
package com.k2fsa.sherpa.onnx;
|
package com.k2fsa.sherpa.onnx;
|
||||||
|
|
||||||
public class OfflineStream {
|
public class OfflineStream {
|
||||||
static {
|
|
||||||
System.loadLibrary("sherpa-onnx-jni");
|
|
||||||
}
|
|
||||||
|
|
||||||
private long ptr = 0;
|
private long ptr = 0;
|
||||||
|
|
||||||
public OfflineStream() {
|
public OfflineStream() {
|
||||||
|
LibraryLoader.maybeLoad();
|
||||||
this.ptr = 0;
|
this.ptr = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,13 +4,10 @@ package com.k2fsa.sherpa.onnx;
|
|||||||
|
|
||||||
|
|
||||||
public class OfflineTts {
|
public class OfflineTts {
|
||||||
static {
|
|
||||||
System.loadLibrary("sherpa-onnx-jni");
|
|
||||||
}
|
|
||||||
|
|
||||||
private long ptr = 0;
|
private long ptr = 0;
|
||||||
|
|
||||||
public OfflineTts(OfflineTtsConfig config) {
|
public OfflineTts(OfflineTtsConfig config) {
|
||||||
|
LibraryLoader.maybeLoad();
|
||||||
ptr = newFromFile(config);
|
ptr = newFromFile(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,13 +3,10 @@
|
|||||||
package com.k2fsa.sherpa.onnx;
|
package com.k2fsa.sherpa.onnx;
|
||||||
|
|
||||||
public class OnlinePunctuation {
|
public class OnlinePunctuation {
|
||||||
static {
|
|
||||||
System.loadLibrary("sherpa-onnx-jni");
|
|
||||||
}
|
|
||||||
|
|
||||||
private long ptr = 0;
|
private long ptr = 0;
|
||||||
|
|
||||||
public OnlinePunctuation(OnlinePunctuationConfig config) {
|
public OnlinePunctuation(OnlinePunctuationConfig config) {
|
||||||
|
LibraryLoader.maybeLoad();
|
||||||
ptr = newFromFile(config);
|
ptr = newFromFile(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,13 +4,10 @@
|
|||||||
package com.k2fsa.sherpa.onnx;
|
package com.k2fsa.sherpa.onnx;
|
||||||
|
|
||||||
public class OnlineRecognizer {
|
public class OnlineRecognizer {
|
||||||
static {
|
|
||||||
System.loadLibrary("sherpa-onnx-jni");
|
|
||||||
}
|
|
||||||
|
|
||||||
private long ptr = 0;
|
private long ptr = 0;
|
||||||
|
|
||||||
public OnlineRecognizer(OnlineRecognizerConfig config) {
|
public OnlineRecognizer(OnlineRecognizerConfig config) {
|
||||||
|
LibraryLoader.maybeLoad();
|
||||||
ptr = newFromFile(config);
|
ptr = newFromFile(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,13 +4,10 @@
|
|||||||
package com.k2fsa.sherpa.onnx;
|
package com.k2fsa.sherpa.onnx;
|
||||||
|
|
||||||
public class OnlineStream {
|
public class OnlineStream {
|
||||||
static {
|
|
||||||
System.loadLibrary("sherpa-onnx-jni");
|
|
||||||
}
|
|
||||||
|
|
||||||
private long ptr = 0;
|
private long ptr = 0;
|
||||||
|
|
||||||
public OnlineStream() {
|
public OnlineStream() {
|
||||||
|
LibraryLoader.maybeLoad();
|
||||||
this.ptr = 0;
|
this.ptr = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,13 +3,10 @@
|
|||||||
package com.k2fsa.sherpa.onnx;
|
package com.k2fsa.sherpa.onnx;
|
||||||
|
|
||||||
public class SpeakerEmbeddingExtractor {
|
public class SpeakerEmbeddingExtractor {
|
||||||
static {
|
|
||||||
System.loadLibrary("sherpa-onnx-jni");
|
|
||||||
}
|
|
||||||
|
|
||||||
private long ptr = 0;
|
private long ptr = 0;
|
||||||
|
|
||||||
public SpeakerEmbeddingExtractor(SpeakerEmbeddingExtractorConfig config) {
|
public SpeakerEmbeddingExtractor(SpeakerEmbeddingExtractorConfig config) {
|
||||||
|
LibraryLoader.maybeLoad();
|
||||||
ptr = newFromFile(config);
|
ptr = newFromFile(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,13 +3,10 @@
|
|||||||
package com.k2fsa.sherpa.onnx;
|
package com.k2fsa.sherpa.onnx;
|
||||||
|
|
||||||
public class SpeakerEmbeddingManager {
|
public class SpeakerEmbeddingManager {
|
||||||
static {
|
|
||||||
System.loadLibrary("sherpa-onnx-jni");
|
|
||||||
}
|
|
||||||
|
|
||||||
private long ptr = 0;
|
private long ptr = 0;
|
||||||
|
|
||||||
public SpeakerEmbeddingManager(int dim) {
|
public SpeakerEmbeddingManager(int dim) {
|
||||||
|
LibraryLoader.maybeLoad();
|
||||||
ptr = create(dim);
|
ptr = create(dim);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,14 +7,11 @@ import java.util.Locale;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class SpokenLanguageIdentification {
|
public class SpokenLanguageIdentification {
|
||||||
static {
|
|
||||||
System.loadLibrary("sherpa-onnx-jni");
|
|
||||||
}
|
|
||||||
|
|
||||||
private final Map<String, String> localeMap;
|
private final Map<String, String> localeMap;
|
||||||
private long ptr = 0;
|
private long ptr = 0;
|
||||||
|
|
||||||
public SpokenLanguageIdentification(SpokenLanguageIdentificationConfig config) {
|
public SpokenLanguageIdentification(SpokenLanguageIdentificationConfig config) {
|
||||||
|
LibraryLoader.maybeLoad();
|
||||||
ptr = newFromFile(config);
|
ptr = newFromFile(config);
|
||||||
|
|
||||||
String[] languages = Locale.getISOLanguages();
|
String[] languages = Locale.getISOLanguages();
|
||||||
|
|||||||
@@ -3,13 +3,10 @@
|
|||||||
package com.k2fsa.sherpa.onnx;
|
package com.k2fsa.sherpa.onnx;
|
||||||
|
|
||||||
public class Vad {
|
public class Vad {
|
||||||
static {
|
|
||||||
System.loadLibrary("sherpa-onnx-jni");
|
|
||||||
}
|
|
||||||
|
|
||||||
private long ptr = 0;
|
private long ptr = 0;
|
||||||
|
|
||||||
public Vad(VadModelConfig config) {
|
public Vad(VadModelConfig config) {
|
||||||
|
LibraryLoader.maybeLoad();
|
||||||
ptr = newFromFile(config);
|
ptr = newFromFile(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,16 +3,13 @@
|
|||||||
package com.k2fsa.sherpa.onnx;
|
package com.k2fsa.sherpa.onnx;
|
||||||
|
|
||||||
public class WaveReader {
|
public class WaveReader {
|
||||||
static {
|
|
||||||
System.loadLibrary("sherpa-onnx-jni");
|
|
||||||
}
|
|
||||||
|
|
||||||
private final int sampleRate;
|
private final int sampleRate;
|
||||||
private final float[] samples;
|
private final float[] samples;
|
||||||
|
|
||||||
// It supports only single channel, 16-bit wave file.
|
// It supports only single channel, 16-bit wave file.
|
||||||
// It will exit the program if the given file has a wrong format
|
// It will exit the program if the given file has a wrong format
|
||||||
public WaveReader(String filename) {
|
public WaveReader(String filename) {
|
||||||
|
LibraryLoader.maybeLoad();
|
||||||
Object[] arr = readWaveFromFile(filename);
|
Object[] arr = readWaveFromFile(filename);
|
||||||
samples = (float[]) arr[0];
|
samples = (float[]) arr[0];
|
||||||
sampleRate = (int) arr[1];
|
sampleRate = (int) arr[1];
|
||||||
|
|||||||
Reference in New Issue
Block a user