Committed by
GitHub
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
正在显示
20 个修改的文件
包含
44 行增加
和
73 行删除
| @@ -6,7 +6,9 @@ out_jar := $(out_dir)/sherpa-onnx.jar | @@ -6,7 +6,9 @@ out_jar := $(out_dir)/sherpa-onnx.jar | ||
| 6 | 6 | ||
| 7 | package_dir := com/k2fsa/sherpa/onnx | 7 | package_dir := com/k2fsa/sherpa/onnx |
| 8 | 8 | ||
| 9 | -java_files := WaveReader.java | 9 | +java_files := LibraryLoader.java |
| 10 | + | ||
| 11 | +java_files += WaveReader.java | ||
| 10 | java_files += WaveWriter.java | 12 | java_files += WaveWriter.java |
| 11 | java_files += EndpointRule.java | 13 | java_files += EndpointRule.java |
| 12 | java_files += EndpointConfig.java | 14 | java_files += EndpointConfig.java |
| @@ -3,13 +3,10 @@ | @@ -3,13 +3,10 @@ | ||
| 3 | package com.k2fsa.sherpa.onnx; | 3 | package com.k2fsa.sherpa.onnx; |
| 4 | 4 | ||
| 5 | public class AudioTagging { | 5 | public class AudioTagging { |
| 6 | - static { | ||
| 7 | - System.loadLibrary("sherpa-onnx-jni"); | ||
| 8 | - } | ||
| 9 | - | ||
| 10 | private long ptr = 0; | 6 | private long ptr = 0; |
| 11 | 7 | ||
| 12 | public AudioTagging(AudioTaggingConfig config) { | 8 | public AudioTagging(AudioTaggingConfig config) { |
| 9 | + LibraryLoader.maybeLoad(); | ||
| 13 | ptr = newFromFile(config); | 10 | ptr = newFromFile(config); |
| 14 | } | 11 | } |
| 15 | 12 |
| @@ -3,14 +3,11 @@ | @@ -3,14 +3,11 @@ | ||
| 3 | package com.k2fsa.sherpa.onnx; | 3 | package com.k2fsa.sherpa.onnx; |
| 4 | 4 | ||
| 5 | public class DenoisedAudio { | 5 | public class DenoisedAudio { |
| 6 | - static { | ||
| 7 | - System.loadLibrary("sherpa-onnx-jni"); | ||
| 8 | - } | ||
| 9 | - | ||
| 10 | private final float[] samples; | 6 | private final float[] samples; |
| 11 | private final int sampleRate; | 7 | private final int sampleRate; |
| 12 | 8 | ||
| 13 | public DenoisedAudio(float[] samples, int sampleRate) { | 9 | public DenoisedAudio(float[] samples, int sampleRate) { |
| 10 | + LibraryLoader.maybeLoad(); | ||
| 14 | this.samples = samples; | 11 | this.samples = samples; |
| 15 | this.sampleRate = sampleRate; | 12 | this.sampleRate = sampleRate; |
| 16 | } | 13 | } |
| @@ -3,14 +3,11 @@ | @@ -3,14 +3,11 @@ | ||
| 3 | package com.k2fsa.sherpa.onnx; | 3 | package com.k2fsa.sherpa.onnx; |
| 4 | 4 | ||
| 5 | public class GeneratedAudio { | 5 | public class GeneratedAudio { |
| 6 | - static { | ||
| 7 | - System.loadLibrary("sherpa-onnx-jni"); | ||
| 8 | - } | ||
| 9 | - | ||
| 10 | private final float[] samples; | 6 | private final float[] samples; |
| 11 | private final int sampleRate; | 7 | private final int sampleRate; |
| 12 | 8 | ||
| 13 | public GeneratedAudio(float[] samples, int sampleRate) { | 9 | public GeneratedAudio(float[] samples, int sampleRate) { |
| 10 | + LibraryLoader.maybeLoad(); | ||
| 14 | this.samples = samples; | 11 | this.samples = samples; |
| 15 | this.sampleRate = sampleRate; | 12 | this.sampleRate = sampleRate; |
| 16 | } | 13 | } |
| @@ -3,13 +3,10 @@ | @@ -3,13 +3,10 @@ | ||
| 3 | package com.k2fsa.sherpa.onnx; | 3 | package com.k2fsa.sherpa.onnx; |
| 4 | 4 | ||
| 5 | public class KeywordSpotter { | 5 | public class KeywordSpotter { |
| 6 | - static { | ||
| 7 | - System.loadLibrary("sherpa-onnx-jni"); | ||
| 8 | - } | ||
| 9 | - | ||
| 10 | private long ptr = 0; | 6 | private long ptr = 0; |
| 11 | 7 | ||
| 12 | public KeywordSpotter(KeywordSpotterConfig config) { | 8 | public KeywordSpotter(KeywordSpotterConfig config) { |
| 9 | + LibraryLoader.maybeLoad(); | ||
| 13 | ptr = newFromFile(config); | 10 | ptr = newFromFile(config); |
| 14 | } | 11 | } |
| 15 | 12 |
| 1 | +package com.k2fsa.sherpa.onnx; | ||
| 2 | + | ||
| 3 | +public class LibraryLoader { | ||
| 4 | + private static volatile boolean autoLoadEnabled = true; | ||
| 5 | + private static volatile boolean isLoaded = false; | ||
| 6 | + | ||
| 7 | + static synchronized void loadLibrary() { | ||
| 8 | + if (!isLoaded) { | ||
| 9 | + System.loadLibrary("sherpa-onnx-jni"); | ||
| 10 | + isLoaded = true; | ||
| 11 | + } | ||
| 12 | + } | ||
| 13 | + | ||
| 14 | + public static void setAutoLoadEnabled(boolean enabled) { | ||
| 15 | + autoLoadEnabled = enabled; | ||
| 16 | + } | ||
| 17 | + | ||
| 18 | + static void maybeLoad() { | ||
| 19 | + if (autoLoadEnabled) { | ||
| 20 | + loadLibrary(); | ||
| 21 | + } | ||
| 22 | + } | ||
| 23 | +} |
| @@ -3,13 +3,10 @@ | @@ -3,13 +3,10 @@ | ||
| 3 | package com.k2fsa.sherpa.onnx; | 3 | package com.k2fsa.sherpa.onnx; |
| 4 | 4 | ||
| 5 | public class OfflinePunctuation { | 5 | public class OfflinePunctuation { |
| 6 | - static { | ||
| 7 | - System.loadLibrary("sherpa-onnx-jni"); | ||
| 8 | - } | ||
| 9 | - | ||
| 10 | private long ptr = 0; | 6 | private long ptr = 0; |
| 11 | 7 | ||
| 12 | public OfflinePunctuation(OfflinePunctuationConfig config) { | 8 | public OfflinePunctuation(OfflinePunctuationConfig config) { |
| 9 | + LibraryLoader.maybeLoad(); | ||
| 13 | ptr = newFromFile(config); | 10 | ptr = newFromFile(config); |
| 14 | } | 11 | } |
| 15 | 12 |
| @@ -3,13 +3,10 @@ | @@ -3,13 +3,10 @@ | ||
| 3 | package com.k2fsa.sherpa.onnx; | 3 | package com.k2fsa.sherpa.onnx; |
| 4 | 4 | ||
| 5 | public class OfflineRecognizer { | 5 | public class OfflineRecognizer { |
| 6 | - static { | ||
| 7 | - System.loadLibrary("sherpa-onnx-jni"); | ||
| 8 | - } | ||
| 9 | - | ||
| 10 | private long ptr = 0; | 6 | private long ptr = 0; |
| 11 | 7 | ||
| 12 | public OfflineRecognizer(OfflineRecognizerConfig config) { | 8 | public OfflineRecognizer(OfflineRecognizerConfig config) { |
| 9 | + LibraryLoader.maybeLoad(); | ||
| 13 | ptr = newFromFile(config); | 10 | ptr = newFromFile(config); |
| 14 | } | 11 | } |
| 15 | 12 |
| @@ -3,13 +3,10 @@ | @@ -3,13 +3,10 @@ | ||
| 3 | package com.k2fsa.sherpa.onnx; | 3 | package com.k2fsa.sherpa.onnx; |
| 4 | 4 | ||
| 5 | public class OfflineSpeakerDiarization { | 5 | public class OfflineSpeakerDiarization { |
| 6 | - static { | ||
| 7 | - System.loadLibrary("sherpa-onnx-jni"); | ||
| 8 | - } | ||
| 9 | - | ||
| 10 | private long ptr = 0; | 6 | private long ptr = 0; |
| 11 | 7 | ||
| 12 | public OfflineSpeakerDiarization(OfflineSpeakerDiarizationConfig config) { | 8 | public OfflineSpeakerDiarization(OfflineSpeakerDiarizationConfig config) { |
| 9 | + LibraryLoader.maybeLoad(); | ||
| 13 | ptr = newFromFile(config); | 10 | ptr = newFromFile(config); |
| 14 | } | 11 | } |
| 15 | 12 |
| @@ -3,13 +3,10 @@ | @@ -3,13 +3,10 @@ | ||
| 3 | package com.k2fsa.sherpa.onnx; | 3 | package com.k2fsa.sherpa.onnx; |
| 4 | 4 | ||
| 5 | public class OfflineSpeechDenoiser { | 5 | public class OfflineSpeechDenoiser { |
| 6 | - static { | ||
| 7 | - System.loadLibrary("sherpa-onnx-jni"); | ||
| 8 | - } | ||
| 9 | - | ||
| 10 | private long ptr = 0; | 6 | private long ptr = 0; |
| 11 | 7 | ||
| 12 | public OfflineSpeechDenoiser(OfflineSpeechDenoiserConfig config) { | 8 | public OfflineSpeechDenoiser(OfflineSpeechDenoiserConfig config) { |
| 9 | + LibraryLoader.maybeLoad(); | ||
| 13 | ptr = newFromFile(config); | 10 | ptr = newFromFile(config); |
| 14 | } | 11 | } |
| 15 | 12 |
| @@ -3,13 +3,10 @@ | @@ -3,13 +3,10 @@ | ||
| 3 | package com.k2fsa.sherpa.onnx; | 3 | package com.k2fsa.sherpa.onnx; |
| 4 | 4 | ||
| 5 | public class OfflineStream { | 5 | public class OfflineStream { |
| 6 | - static { | ||
| 7 | - System.loadLibrary("sherpa-onnx-jni"); | ||
| 8 | - } | ||
| 9 | - | ||
| 10 | private long ptr = 0; | 6 | private long ptr = 0; |
| 11 | 7 | ||
| 12 | public OfflineStream() { | 8 | public OfflineStream() { |
| 9 | + LibraryLoader.maybeLoad(); | ||
| 13 | this.ptr = 0; | 10 | this.ptr = 0; |
| 14 | } | 11 | } |
| 15 | 12 |
| @@ -4,13 +4,10 @@ package com.k2fsa.sherpa.onnx; | @@ -4,13 +4,10 @@ package com.k2fsa.sherpa.onnx; | ||
| 4 | 4 | ||
| 5 | 5 | ||
| 6 | public class OfflineTts { | 6 | public class OfflineTts { |
| 7 | - static { | ||
| 8 | - System.loadLibrary("sherpa-onnx-jni"); | ||
| 9 | - } | ||
| 10 | - | ||
| 11 | private long ptr = 0; | 7 | private long ptr = 0; |
| 12 | 8 | ||
| 13 | public OfflineTts(OfflineTtsConfig config) { | 9 | public OfflineTts(OfflineTtsConfig config) { |
| 10 | + LibraryLoader.maybeLoad(); | ||
| 14 | ptr = newFromFile(config); | 11 | ptr = newFromFile(config); |
| 15 | } | 12 | } |
| 16 | 13 |
| @@ -3,13 +3,10 @@ | @@ -3,13 +3,10 @@ | ||
| 3 | package com.k2fsa.sherpa.onnx; | 3 | package com.k2fsa.sherpa.onnx; |
| 4 | 4 | ||
| 5 | public class OnlinePunctuation { | 5 | public class OnlinePunctuation { |
| 6 | - static { | ||
| 7 | - System.loadLibrary("sherpa-onnx-jni"); | ||
| 8 | - } | ||
| 9 | - | ||
| 10 | private long ptr = 0; | 6 | private long ptr = 0; |
| 11 | 7 | ||
| 12 | public OnlinePunctuation(OnlinePunctuationConfig config) { | 8 | public OnlinePunctuation(OnlinePunctuationConfig config) { |
| 9 | + LibraryLoader.maybeLoad(); | ||
| 13 | ptr = newFromFile(config); | 10 | ptr = newFromFile(config); |
| 14 | } | 11 | } |
| 15 | 12 |
| @@ -4,13 +4,10 @@ | @@ -4,13 +4,10 @@ | ||
| 4 | package com.k2fsa.sherpa.onnx; | 4 | package com.k2fsa.sherpa.onnx; |
| 5 | 5 | ||
| 6 | public class OnlineRecognizer { | 6 | public class OnlineRecognizer { |
| 7 | - static { | ||
| 8 | - System.loadLibrary("sherpa-onnx-jni"); | ||
| 9 | - } | ||
| 10 | - | ||
| 11 | private long ptr = 0; | 7 | private long ptr = 0; |
| 12 | 8 | ||
| 13 | public OnlineRecognizer(OnlineRecognizerConfig config) { | 9 | public OnlineRecognizer(OnlineRecognizerConfig config) { |
| 10 | + LibraryLoader.maybeLoad(); | ||
| 14 | ptr = newFromFile(config); | 11 | ptr = newFromFile(config); |
| 15 | } | 12 | } |
| 16 | 13 |
| @@ -4,13 +4,10 @@ | @@ -4,13 +4,10 @@ | ||
| 4 | package com.k2fsa.sherpa.onnx; | 4 | package com.k2fsa.sherpa.onnx; |
| 5 | 5 | ||
| 6 | public class OnlineStream { | 6 | public class OnlineStream { |
| 7 | - static { | ||
| 8 | - System.loadLibrary("sherpa-onnx-jni"); | ||
| 9 | - } | ||
| 10 | - | ||
| 11 | private long ptr = 0; | 7 | private long ptr = 0; |
| 12 | 8 | ||
| 13 | public OnlineStream() { | 9 | public OnlineStream() { |
| 10 | + LibraryLoader.maybeLoad(); | ||
| 14 | this.ptr = 0; | 11 | this.ptr = 0; |
| 15 | } | 12 | } |
| 16 | 13 |
| @@ -3,13 +3,10 @@ | @@ -3,13 +3,10 @@ | ||
| 3 | package com.k2fsa.sherpa.onnx; | 3 | package com.k2fsa.sherpa.onnx; |
| 4 | 4 | ||
| 5 | public class SpeakerEmbeddingExtractor { | 5 | public class SpeakerEmbeddingExtractor { |
| 6 | - static { | ||
| 7 | - System.loadLibrary("sherpa-onnx-jni"); | ||
| 8 | - } | ||
| 9 | - | ||
| 10 | private long ptr = 0; | 6 | private long ptr = 0; |
| 11 | 7 | ||
| 12 | public SpeakerEmbeddingExtractor(SpeakerEmbeddingExtractorConfig config) { | 8 | public SpeakerEmbeddingExtractor(SpeakerEmbeddingExtractorConfig config) { |
| 9 | + LibraryLoader.maybeLoad(); | ||
| 13 | ptr = newFromFile(config); | 10 | ptr = newFromFile(config); |
| 14 | } | 11 | } |
| 15 | 12 |
| @@ -3,13 +3,10 @@ | @@ -3,13 +3,10 @@ | ||
| 3 | package com.k2fsa.sherpa.onnx; | 3 | package com.k2fsa.sherpa.onnx; |
| 4 | 4 | ||
| 5 | public class SpeakerEmbeddingManager { | 5 | public class SpeakerEmbeddingManager { |
| 6 | - static { | ||
| 7 | - System.loadLibrary("sherpa-onnx-jni"); | ||
| 8 | - } | ||
| 9 | - | ||
| 10 | private long ptr = 0; | 6 | private long ptr = 0; |
| 11 | 7 | ||
| 12 | public SpeakerEmbeddingManager(int dim) { | 8 | public SpeakerEmbeddingManager(int dim) { |
| 9 | + LibraryLoader.maybeLoad(); | ||
| 13 | ptr = create(dim); | 10 | ptr = create(dim); |
| 14 | } | 11 | } |
| 15 | 12 |
| @@ -7,14 +7,11 @@ import java.util.Locale; | @@ -7,14 +7,11 @@ import java.util.Locale; | ||
| 7 | import java.util.Map; | 7 | import java.util.Map; |
| 8 | 8 | ||
| 9 | public class SpokenLanguageIdentification { | 9 | public class SpokenLanguageIdentification { |
| 10 | - static { | ||
| 11 | - System.loadLibrary("sherpa-onnx-jni"); | ||
| 12 | - } | ||
| 13 | - | ||
| 14 | private final Map<String, String> localeMap; | 10 | private final Map<String, String> localeMap; |
| 15 | private long ptr = 0; | 11 | private long ptr = 0; |
| 16 | 12 | ||
| 17 | public SpokenLanguageIdentification(SpokenLanguageIdentificationConfig config) { | 13 | public SpokenLanguageIdentification(SpokenLanguageIdentificationConfig config) { |
| 14 | + LibraryLoader.maybeLoad(); | ||
| 18 | ptr = newFromFile(config); | 15 | ptr = newFromFile(config); |
| 19 | 16 | ||
| 20 | String[] languages = Locale.getISOLanguages(); | 17 | String[] languages = Locale.getISOLanguages(); |
| @@ -3,13 +3,10 @@ | @@ -3,13 +3,10 @@ | ||
| 3 | package com.k2fsa.sherpa.onnx; | 3 | package com.k2fsa.sherpa.onnx; |
| 4 | 4 | ||
| 5 | public class Vad { | 5 | public class Vad { |
| 6 | - static { | ||
| 7 | - System.loadLibrary("sherpa-onnx-jni"); | ||
| 8 | - } | ||
| 9 | - | ||
| 10 | private long ptr = 0; | 6 | private long ptr = 0; |
| 11 | 7 | ||
| 12 | public Vad(VadModelConfig config) { | 8 | public Vad(VadModelConfig config) { |
| 9 | + LibraryLoader.maybeLoad(); | ||
| 13 | ptr = newFromFile(config); | 10 | ptr = newFromFile(config); |
| 14 | } | 11 | } |
| 15 | 12 |
| @@ -3,16 +3,13 @@ | @@ -3,16 +3,13 @@ | ||
| 3 | package com.k2fsa.sherpa.onnx; | 3 | package com.k2fsa.sherpa.onnx; |
| 4 | 4 | ||
| 5 | public class WaveReader { | 5 | public class WaveReader { |
| 6 | - static { | ||
| 7 | - System.loadLibrary("sherpa-onnx-jni"); | ||
| 8 | - } | ||
| 9 | - | ||
| 10 | private final int sampleRate; | 6 | private final int sampleRate; |
| 11 | private final float[] samples; | 7 | private final float[] samples; |
| 12 | 8 | ||
| 13 | // It supports only single channel, 16-bit wave file. | 9 | // It supports only single channel, 16-bit wave file. |
| 14 | // It will exit the program if the given file has a wrong format | 10 | // It will exit the program if the given file has a wrong format |
| 15 | public WaveReader(String filename) { | 11 | public WaveReader(String filename) { |
| 12 | + LibraryLoader.maybeLoad(); | ||
| 16 | Object[] arr = readWaveFromFile(filename); | 13 | Object[] arr = readWaveFromFile(filename); |
| 17 | samples = (float[]) arr[0]; | 14 | samples = (float[]) arr[0]; |
| 18 | sampleRate = (int) arr[1]; | 15 | sampleRate = (int) arr[1]; |
-
请 注册 或 登录 后发表评论