Refactor JNI (#3107)

This pull request significantly refactors the Java Native Interface (JNI) layer for the `sherpa-onnx` library, primarily enhancing the `OfflineTts` component. The changes introduce more robust error handling in various API constructors by validating native object initialization. Furthermore, the callback mechanism for Text-to-Speech operations has been streamlined, offering more convenient `java.util.function.Consumer` based interfaces in Java while simplifying the underlying C++ JNI implementation.

### Highlights

* **Improved Error Handling in Constructors**: Added `IllegalArgumentException` throws in constructors of several `sherpa-onnx` Java API classes (e.g., `AudioTagging`, `KeywordSpotter`, `OfflineTts`) if the native object creation (`newFromFile`) returns a null pointer (0). This makes the API more robust by failing fast on invalid configurations.
* **Streamlined Offline TTS Callbacks**: The `OfflineTts` class now provides new `generateWithCallback` and `generateWithConfigAndCallback` overloads that accept `java.util.function.Consumer<float[]>` for simpler, more idiomatic Java callback usage. This reduces boilerplate for users who only need to consume audio samples.
* **Simplified JNI Callback Implementation**: The native C++ code (`offline-tts.cc`) has been refactored to remove separate handling for `Consumer` and `Function` interfaces. All callbacks are now handled uniformly through the `OfflineTtsCallback` interface, simplifying the JNI bridge logic.
* **Removed Unused Imports and Code**: The `PocketTts.java` example file was cleaned up by removing unused `Consumer` and `Function` imports and simplifying lambda expressions, reflecting the changes in the `OfflineTts` API.
* **Added `getNumSpeakers()` to `OfflineTts`**: A new method `getNumSpeakers()` was added to the `OfflineTts` class, providing access to the number of speakers supported by the TTS engine.
This commit is contained in:
Fangjun Kuang authored and GitHub committed 2026-01-29 10:32:46 +08:00
1 parent bd48533638
commit 8dc6244e9d
14 files changed
+134 -127

No files matched your search

+8 -23
View File
@@ -5,8 +5,6 @@
import com.k2fsa.sherpa.onnx.*;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
public class PocketTts {
public static void main(String[] args) {
@@ -92,11 +90,10 @@ public class PocketTts {
tts.generateWithConfigAndCallback(
text,
genConfig,
(OfflineTtsCallback)
samples -> {
System.out.println("Lambda Integer callback: " + samples.length);
return 1; // continue
});
samples -> {
System.out.println("Lambda Integer callback: " + samples.length);
return 1; // continue
});
}
if (false) {
@@ -104,22 +101,10 @@ public class PocketTts {
tts.generateWithConfigAndCallback(
text,
genConfig,
(Consumer<float[]>)
samples -> {
System.out.println("Consumer: " + samples.length);
});
}
if (false) {
audio =
tts.generateWithConfigAndCallback(
text,
genConfig,
(Function<float[], Integer>)
samples -> {
System.out.println("Function: " + samples.length);
return 1;
});
samples -> {
System.out.println("Consumer: " + samples.length);
// implicitly, it returns 1 internally
});
}
if (audio == null) {