Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 8 additions & 23 deletions java-api-examples/PocketTts.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -92,34 +90,21 @@ public Integer invoke(float[] samples) {
tts.generateWithConfigAndCallback(
text,
genConfig,
(OfflineTtsCallback)
samples -> {
System.out.println("Lambda Integer callback: " + samples.length);
return 1; // continue
});
}

if (false) {
audio =
tts.generateWithConfigAndCallback(
text,
genConfig,
(Consumer<float[]>)
samples -> {
System.out.println("Consumer: " + samples.length);
});
samples -> {
System.out.println("Lambda Integer callback: " + samples.length);
return 1; // continue
});
}

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ public class AudioTagging {
public AudioTagging(AudioTaggingConfig config) {
LibraryLoader.maybeLoad();
ptr = newFromFile(config);
if (ptr == 0) {
throw new IllegalArgumentException("Invalid AudioTaggingConfig: failed to create native AudioTagging");
}
}

public OfflineStream createStream() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ public class KeywordSpotter {
public KeywordSpotter(KeywordSpotterConfig config) {
LibraryLoader.maybeLoad();
ptr = newFromFile(config);
if (ptr == 0) {
throw new IllegalArgumentException("Invalid KeywordSpotterConfig: failed to create native KeywordSpotter");
}
}

public OnlineStream createStream(String keywords) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ public class OfflinePunctuation {
public OfflinePunctuation(OfflinePunctuationConfig config) {
LibraryLoader.maybeLoad();
ptr = newFromFile(config);
if (ptr == 0) {
throw new IllegalArgumentException("Invalid OfflinePunctuationConfig: failed to create native OfflinePunctuation");
}
}

public String addPunctuation(String text) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ public class OfflineRecognizer {
public OfflineRecognizer(OfflineRecognizerConfig config) {
LibraryLoader.maybeLoad();
ptr = newFromFile(config);
if (ptr == 0) {
throw new IllegalArgumentException("Invalid OfflineRecognizerConfig: failed to create native OfflineRecognizer");
}

this.config = config;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ public class OfflineSpeakerDiarization {
public OfflineSpeakerDiarization(OfflineSpeakerDiarizationConfig config) {
LibraryLoader.maybeLoad();
ptr = newFromFile(config);
if (ptr == 0) {
throw new IllegalArgumentException("Invalid OfflineSpeakerDiarizationConfig: failed to create native OfflineSpeakerDiarization");
}
}

public int getSampleRate() {
Expand Down Expand Up @@ -55,4 +58,4 @@ public void release() {
private native OfflineSpeakerDiarizationSegment[] process(long ptr, float[] samples);

private native OfflineSpeakerDiarizationSegment[] processWithCallback(long ptr, float[] samples, OfflineSpeakerDiarizationCallback callback, long arg);
}
}

Copilot AI Jan 29, 2026

Copy link

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.

Copilot uses AI. Check for mistakes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ public class OfflineSpeechDenoiser {
public OfflineSpeechDenoiser(OfflineSpeechDenoiserConfig config) {
LibraryLoader.maybeLoad();
ptr = newFromFile(config);
if (ptr == 0) {
throw new IllegalArgumentException("Invalid OfflineSpeechDenoiserConfig: failed to create native OfflineSpeechDenoiser");
}
}

public int getSampleRate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copilot AI Jan 29, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing documentation for this public method. Consider adding Javadoc to explain its purpose and return value, similar to the getSampleRate method above it.

Copilot uses AI. Check for mistakes.

/** 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);
Expand All @@ -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
) {

Copilot AI Jan 29, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This extra blank line is inconsistent with the formatting of other methods in this file. Consider removing it for consistency.

Copilot uses AI. Check for mistakes.
return generateWithCallback(text, sid, 1.0f, consumer);
Comment on lines +61 to +63

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This method contains an unnecessary blank line. Please remove it to maintain consistent code formatting.

Suggested change
) {
return generateWithCallback(text, sid, 1.0f, consumer);
) {
return generateWithCallback(text, sid, 1.0f, consumer);

}

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.
*
Expand All @@ -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();
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,9 @@

@FunctionalInterface
public interface OfflineTtsCallback {
/**
* @param samples audio chunk
* @return 1 to continue, 0 to stop
*/
Integer invoke(float[] samples);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ public class OnlineRecognizer {
public OnlineRecognizer(OnlineRecognizerConfig config) {
LibraryLoader.maybeLoad();
ptr = newFromFile(config);
if (ptr == 0) {
throw new IllegalArgumentException("Invalid OnlineRecognizerConfig: failed to create native OnlineRecognizer");
}
}

public void decode(OnlineStream s) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ public class SpeakerEmbeddingExtractor {
public SpeakerEmbeddingExtractor(SpeakerEmbeddingExtractorConfig config) {
LibraryLoader.maybeLoad();
ptr = newFromFile(config);
if (ptr == 0) {
throw new IllegalArgumentException("Invalid SpeakerEmbeddingExtractorConfig: failed to create native SpeakerEmbeddingExtractor");
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public class SpokenLanguageIdentification {
public SpokenLanguageIdentification(SpokenLanguageIdentificationConfig config) {
LibraryLoader.maybeLoad();
ptr = newFromFile(config);
if (ptr == 0) {
throw new IllegalArgumentException("Invalid SpokenLanguageIdentificationConfig: failed to create native SpokenLanguageIdentification");
}

String[] languages = Locale.getISOLanguages();
localeMap = new HashMap<String, String>(languages.length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ public class Vad {
public Vad(VadModelConfig config) {
LibraryLoader.maybeLoad();
ptr = newFromFile(config);
if (ptr == 0) {
throw new IllegalArgumentException("Invalid VadModelConfig: failed to create native Vad");
}
}

@Override
Expand Down
Loading
Loading