Repository navigation
Refactor JNI #3107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor JNI #3107
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,19 +2,28 @@ | |
|
|
||
| package com.k2fsa.sherpa.onnx; | ||
|
|
||
| import java.util.function.Consumer; | ||
|
|
||
| public class OfflineTts { | ||
| private long ptr = 0; | ||
|
|
||
| public OfflineTts(OfflineTtsConfig config) { | ||
| LibraryLoader.maybeLoad(); | ||
| ptr = newFromFile(config); | ||
| if (ptr == 0) { | ||
| throw new IllegalArgumentException("Invalid OfflineTtsConfig: failed to create native OfflineTts"); | ||
| } | ||
| } | ||
|
|
||
| /** Returns the sample rate of the TTS engine. */ | ||
| public int getSampleRate() { | ||
| return getSampleRate(ptr); | ||
| } | ||
|
|
||
| public int getNumSpeakers() { | ||
| return getNumSpeakers(ptr); | ||
| } | ||
|
Comment on lines
+23
to
+25
|
||
|
|
||
| /** Generates audio for the given text using the default speaker (sid=0) and speed=1.0. */ | ||
| public GeneratedAudio generate(String text) { | ||
| return generate(text, 0, 1.0f); | ||
|
|
@@ -30,18 +39,47 @@ public GeneratedAudio generate(String text, int sid, float speed) { | |
| return generateImpl(ptr, text, sid, speed); | ||
| } | ||
|
|
||
| public GeneratedAudio generateWithCallback(String text, Object callback) { | ||
| public GeneratedAudio generateWithCallback(String text, OfflineTtsCallback callback) { | ||
| return generateWithCallback(text, 0, 1.0f, callback); | ||
| } | ||
|
|
||
| public GeneratedAudio generateWithCallback(String text, int sid, Object callback) { | ||
| public GeneratedAudio generateWithCallback( | ||
| String text, | ||
| Consumer<float[]> consumer | ||
| ) { | ||
| return generateWithCallback(text, 0, 1.0f, consumer); | ||
| } | ||
|
|
||
| public GeneratedAudio generateWithCallback(String text, int sid, OfflineTtsCallback callback) { | ||
| return generateWithCallback(text, sid, 1.0f, callback); | ||
| } | ||
|
|
||
| public GeneratedAudio generateWithCallback(String text, int sid, float speed, Object callback) { | ||
| public GeneratedAudio generateWithCallback( | ||
| String text, | ||
| int sid, | ||
| Consumer<float[]> consumer | ||
| ) { | ||
|
|
||
|
||
| return generateWithCallback(text, sid, 1.0f, consumer); | ||
|
Comment on lines
+61
to
+63
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
|
|
||
| public GeneratedAudio generateWithCallback(String text, int sid, float speed, OfflineTtsCallback callback) { | ||
| return generateWithCallbackImpl(ptr, text, sid, speed, callback); | ||
| } | ||
|
|
||
| public GeneratedAudio generateWithCallback( | ||
| String text, | ||
| int sid, | ||
| float speed, | ||
| Consumer<float[]> consumer | ||
| ) { | ||
| OfflineTtsCallback cb = samples -> { | ||
| consumer.accept(samples); | ||
| return 1; | ||
| }; | ||
| return generateWithCallback(text, sid, speed, cb); | ||
| } | ||
|
|
||
| /** | ||
| * Generate audio using a GenerationConfig and a callback. | ||
| * | ||
|
|
@@ -53,12 +91,24 @@ public GeneratedAudio generateWithCallback(String text, int sid, float speed, Ob | |
| public GeneratedAudio generateWithConfigAndCallback( | ||
| String text, | ||
| GenerationConfig config, | ||
| Object callback | ||
| OfflineTtsCallback callback | ||
| ) { | ||
| return generateWithConfigImpl(ptr, text, config, callback); | ||
| } | ||
|
|
||
|
|
||
| public GeneratedAudio generateWithConfigAndCallback( | ||
| String text, | ||
| GenerationConfig config, | ||
| Consumer<float[]> consumer | ||
| ) { | ||
| OfflineTtsCallback cb = samples -> { | ||
| consumer.accept(samples); | ||
| return 1; | ||
| }; | ||
| return generateWithConfigAndCallback(text, config, cb); | ||
| } | ||
|
|
||
| @Override | ||
| protected void finalize() throws Throwable { | ||
| release(); | ||
|
|
@@ -80,13 +130,13 @@ public void release() { | |
|
|
||
| private native GeneratedAudio generateImpl(long ptr, String text, int sid, float speed); | ||
|
|
||
| private native GeneratedAudio generateWithCallbackImpl(long ptr, String text, int sid, float speed, Object callback); | ||
| private native GeneratedAudio generateWithCallbackImpl(long ptr, String text, int sid, float speed, OfflineTtsCallback callback); | ||
|
|
||
| private native GeneratedAudio generateWithConfigImpl( | ||
| long ptr, | ||
| String text, | ||
| GenerationConfig config, | ||
| Object callback | ||
| OfflineTtsCallback callback | ||
| ); | ||
|
|
||
| private native long newFromFile(OfflineTtsConfig config); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This appears to be an unintentional whitespace-only change. The closing brace was moved from line 60 to line 61 without any functional modification.