Tong Li
Committed by GitHub

push to maven center (#2463)

正在显示 90 个修改的文件 包含 891 行增加12 行删除
[win.env]
set JAVA_HOME=D:\java\jdk1.8.0_121
[win.build]
mvn clean install -DskipTests -Dgpg.skip=true
[linux.env]
export JAVA_HOME=/usr/java/jdk1.8.0_121
[linux.build]
mvn clean install -DskipTests -Dgpg.skip=true
[mac.env]
export JAVA_HOME=~/java/jdk1.8.0_121
[mac.build]
mvn clean install -DskipTests -Dgpg.skip=true
\ No newline at end of file
... ...
.idea
java-api.iml
out
META-INF
build
*.jar
### Eclipse template
*.pydevproject
.metadata
.gradle*
classes/
bin/
tmp/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.settings/
.loadpath
rebel.xml
# Eclipse Core
.project
generatedsources
# External tool builders
.externalToolBuilders/
# Locally stored "Eclipse launch configurations"
*.launch
# CDT-specific
.cproject
# JDT-specific (Eclipse Java Development Tools)
.classpath
# PDT-specific
.buildpath
# sbteclipse plugin
.target
# TeXlipse plugin
.texlipse
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm
*.iml
.flattened-pom.xml
## Directory-based project format:
.idea/
# if you remove the above rule, at least ignore the following:
# User-specific stuff:
# .idea/workspace.xml
# .idea/tasks.xml
# .idea/dictionaries
# Sensitive or high-churn files:
# .idea/dataSources.ids
# .idea/dataSources.xml
# .idea/sqlDataSources.xml
# .idea/dynamic.xml
# .idea/uiDesigner.xml
# Gradle:
# .idea/gradle.xml
# .idea/libraries
# Mongo Explorer plugin:
# .idea/mongoSettings.xml
## File-based project format:
*.ipr
*.iws
## Plugin-specific files:
# IntelliJ
/out/
# mpeltonen/sbt-idea plugin
.idea_modules/
# JIRA plugin
atlassian-ide-plugin.xml
# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
build/
# Ignore Gradle GUI config
gradle-app.setting
# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
!gradle-wrapper.jar
db
### Java template
*.class
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
#*.jar
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
### Leiningen template
classes/
target/
logs/
checkouts/
.lein-deps-sum
.lein-repl-history
.lein-plugins/
.lein-failures
.nrepl-port
querydsl/
.DS_Store
*.exe
*.out
*.log
node_modules/
dist/
dist.zip
package-lock.json
*.lock
local.properties
.cxx
.externalNativeBuild
/captures
/build
__pycache__/
*.pyc
cmake-build-debug/
cmake-build-debug-mingw/
venv/
.vs/
Debug/
vcpkg_installed/
.env
.next/
app.zip
secrets.txt
src.zip
\ No newline at end of file
... ...
... ... @@ -101,7 +101,7 @@ java_files += OfflineSpeechDenoiser.java
class_files := $(java_files:%.java=%.class)
java_files := $(addprefix src/$(package_dir)/,$(java_files))
java_files := $(addprefix src/main/java/$(package_dir)/,$(java_files))
class_files := $(addprefix $(out_dir)/$(package_dir)/,$(class_files))
$(info -- java files $(java_files))
... ... @@ -119,6 +119,6 @@ $(out_jar): $(class_files)
clean:
$(RM) -rfv $(out_dir)
$(class_files): $(out_dir)/$(package_dir)/%.class: src/$(package_dir)/%.java
$(class_files): $(out_dir)/$(package_dir)/%.class: src/main/java/$(package_dir)/%.java
mkdir -p build
javac -d $(out_dir) -cp $(out_dir) $<
... ...
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.litongjava</groupId>
<artifactId>sherpa-onnx-java-api</artifactId>
<version>1.0.1</version>
<packaging>jar</packaging>
<name>sherpa-onnx-java-api</name>
<description>sherpa-onnx-java-api</description>
<url>https://github.com/k2-fsa/sherpa-onnx/tree/master/sherpa-onnx/java-api</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://apache.org/licenses/LICENSE-2.0.txt</url>
</license>
</licenses>
<developers>
<developer>
<id>litongjava</id>
<name>Tong Li</name>
<email>litongjava001@gmail.com</email>
<url>https://github.com/litongjava</url>
</developer>
</developers>
<scm>
<connection>scm:git:git@github.com:k2-fsa/sherpa-onnx.git</connection>
<developerConnection>scm:git:git@github.com:k2-fsa/sherpa-onnx.git</developerConnection>
<url>git@github.com:k2-fsa/sherpa-onnx.git</url>
</scm>
<build>
<plugins>
<!-- Source -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Javadoc -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<configuration>
<!-- 添加这个压制JavaDoc检查 -->
<additionalparam>-Xdoclint:none</additionalparam>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- GPG mvn clean deploy -Dgpg.passphrase=YourPassphase -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.sonatype.central</groupId>
<artifactId>central-publishing-maven-plugin</artifactId>
<version>0.7.0</version>
<extensions>true</extensions>
<configuration>
<publishingServerId>central</publishingServerId>
</configuration>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
... ...
# User Guide
*Applicable to Windows / macOS / Linux (using Windows as an example for dynamic library loading)*
## 1. Prerequisites
* Java 1.8+ environment
* Download and prepare the following:
* Sherpa-ONNX Java API (Maven dependency)
* Kokoro TTS model files (including `model.onnx`, etc.)
---
## 2. Add Maven Dependency
In your `pom.xml`, add:
```xml
<dependency>
<groupId>com.litongjava</groupId>
<artifactId>sherpa-onnx-java-api</artifactId>
<version>1.0.1</version>
</dependency>
```
---
## 3. Obtain and Configure Native Dynamic Libraries (JNI)
### 3.1 Install ONNX Runtime
#### Windows 10
Starting from Windows 10 v1809 and all versions of Windows 11, the system comes with built-in ONNX Runtime as part of Windows ML (WinRT API), exposed through Windows.AI.MachineLearning.dll. You can directly use WinML to load and run ONNX models without additional downloads or installations.
[run-onnx-models](https://learn.microsoft.com/en-us/windows/ai/new-windows-ml/run-onnx-models)
#### Linux
Sherpa-ONNX does **not** bundle ONNX Runtime. To install it manually:
1. Download the Linux x64 binary from Microsoft’s GitHub Releases:
```bash
wget https://github.com/microsoft/onnxruntime/releases/download/v1.17.1/onnxruntime-linux-x64-1.17.1.tgz
tar -xzf onnxruntime-linux-x64-1.17.1.tgz
```
2. Copy and symlink the library into a system directory:
```bash
sudo cp onnxruntime-linux-x64-1.17.1/lib/libonnxruntime.so* /usr/local/lib/
sudo ln -sf /usr/local/lib/libonnxruntime.so.1.17.1 /usr/local/lib/libonnxruntime.so
```
3. Update the shared-library cache and verify:
```bash
sudo ldconfig
ldconfig -p | grep onnxruntime
```
#### macOS
Sherpa-ONNX also requires you to install ONNX Runtime on macOS:
1. Download the macOS ARM64 binary:
```bash
wget https://github.com/microsoft/onnxruntime/releases/download/v1.17.1/onnxruntime-osx-arm64-1.17.1.tgz
tar -xzf onnxruntime-osx-arm64-1.17.1.tgz
```
2. Copy the dylib into `/usr/local/lib`:
```bash
sudo cp onnxruntime-osx-arm64-1.17.1/lib/libonnxruntime.1.17.1.dylib /usr/local/lib/
```
3. Add `/usr/local/lib` to `dyld`’s search path:
```bash
export DYLD_LIBRARY_PATH=/usr/local/lib:$DYLD_LIBRARY_PATH
```
4. Verify with `otool`:
```bash
otool -L /Users/ping/lib/darwin_arm64/libsherpa-onnx-jni.dylib
```
---
### 3.2 Common Errors & Troubleshooting
**Error Example:**
```text
Exception in thread "main" java.lang.UnsatisfiedLinkError: no sherpa-onnx-jni in java.library.path: ...
```
This means the JVM couldn’t locate the native library in `java.library.path`.
**Troubleshooting steps:**
1. Ensure you downloaded the build matching your OS and architecture (e.g. win-x64 vs. arm64).
2. Test with an absolute path:
```bash
java -Djava.library.path=C:\full\path\to\jni -jar your-app.jar
```
3. Print or inspect `java.library.path` at runtime (e.g. `System.out.println(System.getProperty("java.library.path"));`).
4. **Do not** hack the internal `sys_paths` via reflection (it may throw `NoSuchFieldException`). Use `-Djava.library.path` instead.
---
## 4. Download & Prepare the Kokoro Model
Fetch the model package from the official release (example: Kokoro v0.19 English):
```
https://k2-fsa.github.io/sherpa/onnx/tts/pretrained_models/kokoro.html
```
```bash
# Download (manually or via script)
wget https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/kokoro-en-v0_19.tar.bz2
# Extract
tar -xjf kokoro-en-v0_19.tar.bz2
# Inspect
ls -lh kokoro-en-v0_19/
```
You should see:
```
LICENSE
README.md
espeak-ng-data/ # speech data directory
model.onnx # TTS model
tokens.txt # token mapping
voices.bin # voice embeddings
```
Make sure your Java code points to these files (using either relative or absolute paths).
---
## 5. Test Code (Java Example)
```java
package com.litongjava.linux.tts;
import com.k2fsa.sherpa.onnx.GeneratedAudio;
import com.k2fsa.sherpa.onnx.OfflineTts;
import com.k2fsa.sherpa.onnx.OfflineTtsConfig;
import com.k2fsa.sherpa.onnx.OfflineTtsKokoroModelConfig;
import com.k2fsa.sherpa.onnx.OfflineTtsModelConfig;
public class NonStreamingTtsKokoroEn {
public static void main(String[] args) {
String model = "./kokoro-en-v0_19/model.onnx";
String voices = "./kokoro-en-v0_19/voices.bin";
String tokens = "./kokoro-en-v0_19/tokens.txt";
String dataDir = "./kokoro-en-v0_19/espeak-ng-data";
String text = "Today as always, men fall into two groups: slaves and free men. Whoever does not have"
+ " two-thirds of his day for himself, is a slave, whatever he may be: a statesman, a"
+ " businessman, an official, or a scholar.";
OfflineTtsKokoroModelConfig kokoroConfig = OfflineTtsKokoroModelConfig.builder()
.setModel(model)
.setVoices(voices)
.setTokens(tokens)
.setDataDir(dataDir)
.build();
OfflineTtsModelConfig modelConfig = OfflineTtsModelConfig.builder()
.setKokoro(kokoroConfig)
.setNumThreads(2)
.setDebug(true)
.build();
OfflineTtsConfig config = OfflineTtsConfig.builder()
.setModel(modelConfig)
.build();
OfflineTts tts = new OfflineTts(config);
int sid = 0;
float speed = 1.0f;
long start = System.currentTimeMillis();
GeneratedAudio audio = tts.generate(text, sid, speed);
long stop = System.currentTimeMillis();
float elapsed = (stop - start) / 1000.0f;
float duration = audio.getSamples().length / (float) audio.getSampleRate();
float rtf = elapsed / duration;
String outFile = "tts-kokoro-en.wav";
audio.save(outFile);
System.out.printf("-- elapsed : %.3f seconds%n", elapsed);
System.out.printf("-- audio duration : %.3f seconds%n", duration);
System.out.printf("-- real-time factor : %.3f%n", rtf);
System.out.printf("-- text : %s%n", text);
System.out.printf("-- Saved to : %s%n", outFile);
tts.release();
}
}
```
### Output Explanation
After successful execution, you should see something like:
```
-- elapsed : 6.739 seconds
-- audio duration : 6.739 seconds
-- real-time factor : 0.563
-- text : ...
-- Saved to : tts-kokoro-en.wav
```
A file named `tts-kokoro-en.wav` will appear in the current directory—play it with any audio player to verify.
... ...
# 使用指南
*适用于 Windows / macOS / Linux(以 Windows 为例说明动态库加载)*
## 1. 前提条件
* Java 1.8+ 环境
* 下载并准备好以下内容:
* Sherpa-ONNX Java API(Maven 依赖)
* Kokoro TTS 模型文件(包含 `model.onnx` 等)
---
## 2. 添加 Maven 依赖
在你的 `pom.xml` 中添加如下依赖:
```xml
<dependency>
<groupId>com.litongjava</groupId>
<artifactId>sherpa-onnx-java-api</artifactId>
<version>1.0.1</version>
</dependency>
```
---
## 3. 获取并配置本地动态链接库(JNI)
### 3.1 安装 ONNX Runtime
#### 1. Windows 11
Starting from Windows 10 v1809 and all versions of Windows 11, the system comes with built-in ONNX Runtime as part of Windows ML (WinRT API), exposed through Windows.AI.MachineLearning.dll. You can directly use WinML to load and run ONNX models without additional downloads or installations.
(run-onnx-models)[https://learn.microsoft.com/en-us/windows/ai/new-windows-ml/run-onnx-models]
#### 2. Linux
Sherpa-ONNX 并不包含 ONNX Runtime,需要手动下载并配置:
1. 从微软官方 GitHub Releases 下载 Linux 64 位二进制包:
```bash
wget https://github.com/microsoft/onnxruntime/releases/download/v1.17.1/onnxruntime-linux-x64-1.17.1.tgz
tar -xzf onnxruntime-linux-x64-1.17.1.tgz
```
2. 将解压后的 `libonnxruntime.so` 文件复制到系统库目录,并创建软链接:
```bash
sudo cp onnxruntime-linux-x64-1.17.1/lib/libonnxruntime.so* /usr/local/lib/
sudo ln -sf /usr/local/lib/libonnxruntime.so.1.17.1 /usr/local/lib/libonnxruntime.so
```
3. 更新共享库缓存并验证安装:
```bash
sudo ldconfig
ldconfig -p | grep onnxruntime
```
#### 3. macOS
Sherpa-ONNX 同样不包含 ONNX Runtime,需要从官方获取并配置:
1. 下载 macOS ARM64 版本二进制包:
```bash
wget https://github.com/microsoft/onnxruntime/releases/download/v1.17.1/onnxruntime-osx-arm64-1.17.1.tgz
tar -xzf onnxruntime-osx-arm64-1.17.1.tgz
```
2.`libonnxruntime.1.17.1.dylib` 复制到 `/usr/local/lib`
```bash
sudo cp onnxruntime-osx-arm64-1.17.1/lib/libonnxruntime.1.17.1.dylib /usr/local/lib/
```
3.`/usr/local/lib` 添加到 `dyld` 的搜索路径:
```bash
export DYLD_LIBRARY_PATH=/usr/local/lib:$DYLD_LIBRARY_PATH
```
4. 使用 `otool` 验证:
```bash
otool -L /Users/ping/lib/darwin_arm64/libsherpa-onnx-jni.dylib
```
---
### 3.2 常见错误与排查
**错误示例:**
```text
Exception in thread "main" java.lang.UnsatisfiedLinkError: no sherpa-onnx-jni in java.library.path: ...
```
说明 JVM 没有在 `java.library.path` 中找到本地库。
排查步骤:
1. 确认下载的是与你操作系统与架构匹配的版本(如 win-x64 vs arm64 等)。
2. 用绝对路径测试:将 `.dll` 放在某个目录并运行:
```sh
java -Djava.library.path=C:\full\path\to\jni -jar your-app.jar
```
3. 打印或检查 `java.library.path` 内容(示例代码里可输出 `System.getProperty("java.library.path")`)。
4. 避免通过反射修改 `sys_paths`(不要尝试 hack `java.library.path` 的内部字段,容易引发 `NoSuchFieldException: sys_paths`,建议直接用 `-Djava.library.path`)。
---
## 4. 下载并准备 Kokoro 模型
从官方 release 获取模型包(以英文 Kokoro v0.19 为例):
```
https://k2-fsa.github.io/sherpa/onnx/tts/pretrained_models/kokoro.html
```
```sh
# 下载(手工或脚本)
# 例如从 GitHub releases:
wget https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/kokoro-en-v0_19.tar.bz2
# 解压
tar -xjf kokoro-en-v0_19.tar.bz2
# 查看结构
ls -lh kokoro-en-v0_19/
```
该目录结构示例(解压后应包含):
```
LICENSE
README.md
espeak-ng-data/ # 语音数据目录
model.onnx # TTS 模型
tokens.txt # token 映射
voices.bin # voice embedding
```
确保这些路径在你的 Java 程序中指向正确的位置(相对或绝对皆可)。
---
## 5. 测试代码(Java 示例)
```java
package com.litongjava.linux.tts;
import com.k2fsa.sherpa.onnx.GeneratedAudio;
import com.k2fsa.sherpa.onnx.OfflineTts;
import com.k2fsa.sherpa.onnx.OfflineTtsConfig;
import com.k2fsa.sherpa.onnx.OfflineTtsKokoroModelConfig;
import com.k2fsa.sherpa.onnx.OfflineTtsModelConfig;
public class NonStreamingTtsKokoroEn {
public static void main(String[] args) {
String model = "./kokoro-en-v0_19/model.onnx";
String voices = "./kokoro-en-v0_19/voices.bin";
String tokens = "./kokoro-en-v0_19/tokens.txt";
String dataDir = "./kokoro-en-v0_19/espeak-ng-data";
String text = "Today as always, men fall into two groups: slaves and free men. Whoever does not have"
+ " two-thirds of his day for himself, is a slave, whatever he may be: a statesman, a"
+ " businessman, an official, or a scholar.";
OfflineTtsKokoroModelConfig kokoroModelConfig = OfflineTtsKokoroModelConfig.builder()
.setModel(model)
.setVoices(voices)
.setTokens(tokens)
.setDataDir(dataDir)
.build();
OfflineTtsModelConfig modelConfig = OfflineTtsModelConfig.builder()
.setKokoro(kokoroModelConfig)
.setNumThreads(2)
.setDebug(true)
.build();
OfflineTtsConfig config = OfflineTtsConfig.builder()
.setModel(modelConfig)
.build();
OfflineTts tts = new OfflineTts(config);
int sid = 0;
float speed = 1.0f;
long start = System.currentTimeMillis();
GeneratedAudio audio = tts.generate(text, sid, speed);
long stop = System.currentTimeMillis();
float timeElapsedSeconds = (stop - start) / 1000.0f;
float audioDuration = audio.getSamples().length / (float) audio.getSampleRate();
float real_time_factor = timeElapsedSeconds / audioDuration;
String waveFilename = "tts-kokoro-en.wav";
audio.save(waveFilename);
System.out.printf("-- elapsed : %.3f seconds\n", timeElapsedSeconds);
System.out.printf("-- audio duration: %.3f seconds\n", audioDuration);
System.out.printf("-- real-time factor (RTF): %.3f\n", real_time_factor);
System.out.printf("-- text: %s\n", text);
System.out.printf("-- Saved to %s\n", waveFilename);
tts.release();
}
}
```
### 输出说明
成功执行后会输出类似:
```
-- elapsed : 6.739 seconds
-- audio duration: 6.739 seconds
-- real-time factor (RTF): 0.563
-- text: ...
-- Saved to tts-kokoro-en.wav
```
并在当前目录生成 `tts-kokoro-en.wav`,可以用任意音频播放器播放验证。
---
\ No newline at end of file
... ...
... ... @@ -63,7 +63,7 @@ public class EndpointConfig {
return this;
}
public Builder setRul3(EndpointRule rule) {
public Builder setRule3(EndpointRule rule) {
this.rule3 = rule;
return this;
}
... ...
package com.k2fsa.sherpa.onnx;
import com.k2fsa.sherpa.onnx.utils.LibraryUtils;
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");
LibraryUtils.load();
isLoaded = true;
}
}
... ...
... ... @@ -16,6 +16,9 @@ public class OnlineRecognizer {
}
public void decode(OnlineStream[] ss) {
if (ss == null || ss.length == 0) {
throw new IllegalArgumentException("Stream array must be non-empty");
}
long[] streamPtrs = new long[ss.length];
for (int i = 0; i < ss.length; ++i) {
streamPtrs[i] = ss[i].getPtr();
... ... @@ -46,7 +49,7 @@ public class OnlineRecognizer {
}
// You'd better call it manually if it is not used anymore
public void release() {
protected void close() {
if (this.ptr == 0) {
return;
}
... ... @@ -54,6 +57,10 @@ public class OnlineRecognizer {
this.ptr = 0;
}
public void release() {
this.close();
}
public OnlineRecognizerResult getResult(OnlineStream s) {
Object[] arr = getResult(ptr, s.getPtr());
String text = (String) arr[0];
... ...
... ... @@ -32,6 +32,10 @@ public class OnlineStream {
}
public void release() {
close();
}
public void close() {
// stream object must be release after used
if (this.ptr == 0) {
return;
... ... @@ -42,7 +46,7 @@ public class OnlineStream {
@Override
protected void finalize() throws Throwable {
release();
close();
super.finalize();
}
... ...
package com.k2fsa.sherpa.onnx.core;
public interface Core {
String NATIVE_LIBRARY_NAME = "sherpa-onnx-jni";
}
... ...
package com.k2fsa.sherpa.onnx.utils;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.Locale;
import com.k2fsa.sherpa.onnx.core.Core;
public class LibraryUtils {
// System property to override native library path
private static final String NATIVE_PATH_PROP = "sherpa_onnx.native.path";
public static void load() {
String libFileName = System.mapLibraryName(Core.NATIVE_LIBRARY_NAME);
// 1. Try loading from external directory if provided
String nativePath = System.getProperty(NATIVE_PATH_PROP);
if (nativePath != null) {
File nativeDir = new File(nativePath);
File libInDir = new File(nativeDir, libFileName);
if (nativeDir.isDirectory() && libInDir.exists()) {
System.out.println("Loading native lib from external directory: " + libInDir.getAbsolutePath());
System.load(libInDir.getAbsolutePath());
return;
}
}
// 2. Fallback to extracting and loading from the JAR
File libFile = init(libFileName);
System.out.println("Loading native lib from: " + libFile.getAbsolutePath());
System.load(libFile.getAbsolutePath());
}
/* Computes and initializes OS_ARCH_STR (such as linux-x64) */
private static String initOsArch() {
String detectedOS = null;
String os = System.getProperty("os.name", "generic").toLowerCase(Locale.ENGLISH);
if (os.contains("mac") || os.contains("darwin")) {
detectedOS = "osx";
} else if (os.contains("win")) {
detectedOS = "win";
} else if (os.contains("nux")) {
detectedOS = "linux";
} else {
throw new IllegalStateException("Unsupported os:" + os);
}
String detectedArch = null;
String arch = System.getProperty("os.arch", "generic").toLowerCase(Locale.ENGLISH);
if (arch.startsWith("amd64") || arch.startsWith("x86_64")) {
detectedArch = "x64";
} else if (arch.startsWith("x86")) {
// 32-bit x86 is not supported by the Java API
detectedArch = "x86";
} else if (arch.startsWith("aarch64")) {
detectedArch = "aarch64";
} else {
throw new IllegalStateException("Unsupported arch:" + arch);
}
return detectedOS + '-' + detectedArch;
}
private static File init(String libFileName) {
String osName = System.getProperty("os.name").toLowerCase();
String osArch = System.getProperty("os.arch").toLowerCase();
String userHome = System.getProperty("user.home");
System.out.printf("Detected OS=%s, ARCH=%s, HOME=%s%n", osName, osArch, userHome);
String archName = initOsArch();
// Prepare destination directory under ~/lib/<archName>/
String dstDir = userHome + File.separator + "lib" + File.separator + archName;
File libFile = new File(dstDir, libFileName);
File parentDir = libFile.getParentFile();
if (!parentDir.exists() && !parentDir.mkdirs()) {
throw new RuntimeException("Unable to create directory: " + parentDir);
}
// Extract the native library from JAR
extractResource("/native/" + archName + "/" + libFileName, libFile);
return libFile;
}
/**
* Copies a resource file from the jar to the specified destination.
*
* @param resourcePath The resource path inside the jar, e.g.:
* /native/linux_x64/libonnxruntime.so
* @param destination The destination file on disk
*/
private static void extractResource(String resourcePath, File destination) {
try (InputStream in = LibraryUtils.class.getResourceAsStream(resourcePath)) {
if (in == null) {
throw new RuntimeException("Resource not found: " + resourcePath);
}
Files.copy(in, destination.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new RuntimeException("Failed to extract resource " + resourcePath + " to " + destination.getAbsolutePath(),
e);
}
}
}
... ...
lib/
native/
\ No newline at end of file
... ...
please downlaod file and put in folder
[donwload link](https://huggingface.co/csukuangfj/sherpa-onnx-libs/tree/main/jni)
- sherpa-onnx-v1.12.7-linux-aarch64-jni.tar.bz2
- sherpa-onnx-v1.12.7-linux-x64-jni.tar.bz2
- sherpa-onnx-v1.12.7-osx-arm64-jni.tar.bz2
- sherpa-onnx-v1.12.7-osx-x86_64-jni.tar.bz2
- sherpa-onnx-v1.12.7-win-x64-jni.tar.bz2
- linux_arm64
- linux_x64
- darwin_arm64
- darwin_x64
- windows_x64
add to src/main/resources
```
.
├── native
│ ├── linux-aarch64
│ │ ├── libsherpa-onnx-jni.so
│ ├── linux-x64
│ │ ├── libsherpa-onnx-jni.so
│ ├── osx-aarch64
│ │ ├── libsherpa-onnx-jni.dylib
│ ├── osx-x64
│ │ ├── libsherpa-onnx-jni.dylib
│ ├── win-x64
│ │ ├── sherpa-onnx-jni.dll
```
... ...