add speed parameter

This commit is contained in:
ANLGBOY
2025-11-19 19:42:24 +09:00
parent c31b6745e4
commit 8518b839c1
30 changed files with 246 additions and 61 deletions

View File

@@ -15,6 +15,7 @@ public class ExampleONNX {
boolean useGpu = false;
String onnxDir = "assets/onnx";
int totalStep = 5;
float speed = 1.05f;
int nTest = 4;
List<String> voiceStyle = Arrays.asList("assets/voice_styles/M1.json");
List<String> text = Arrays.asList(
@@ -41,6 +42,9 @@ public class ExampleONNX {
case "--total-step":
if (i + 1 < args.length) result.totalStep = Integer.parseInt(args[++i]);
break;
case "--speed":
if (i + 1 < args.length) result.speed = Float.parseFloat(args[++i]);
break;
case "--n-test":
if (i + 1 < args.length) result.nTest = Integer.parseInt(args[++i]);
break;
@@ -76,6 +80,7 @@ public class ExampleONNX {
// --- 1. Parse arguments --- //
Args parsedArgs = parseArgs(args);
int totalStep = parsedArgs.totalStep;
float speed = parsedArgs.speed;
int nTest = parsedArgs.nTest;
String saveDir = parsedArgs.saveDir;
List<String> voiceStylePaths = parsedArgs.voiceStyle;
@@ -111,7 +116,7 @@ public class ExampleONNX {
if (batch) {
ttsResult = Helper.timer("Generating speech from text", () -> {
try {
return textToSpeech.batch(textList, style, totalStep, env);
return textToSpeech.batch(textList, style, totalStep, speed, env);
} catch (Exception e) {
throw new RuntimeException(e);
}
@@ -119,7 +124,7 @@ public class ExampleONNX {
} else {
ttsResult = Helper.timer("Generating speech from text", () -> {
try {
return textToSpeech.call(textList.get(0), style, totalStep, 0.3f, env);
return textToSpeech.call(textList.get(0), style, totalStep, speed, 0.3f, env);
} catch (Exception e) {
throw new RuntimeException(e);
}

View File

@@ -154,7 +154,7 @@ class TextToSpeech {
this.ldim = config.ttl.latentDim;
}
private TTSResult _infer(List<String> textList, Style style, int totalStep, OrtEnvironment env)
private TTSResult _infer(List<String> textList, Style style, int totalStep, float speed, OrtEnvironment env)
throws OrtException {
int bsz = textList.size();
@@ -182,6 +182,11 @@ class TextToSpeech {
duration = (float[]) dpValue;
}
// Apply speed factor to duration
for (int i = 0; i < duration.length; i++) {
duration[i] /= speed;
}
// Encode text
Map<String, OnnxTensor> textEncInputs = new HashMap<>();
textEncInputs.put("text_ids", textIdsTensor);
@@ -301,7 +306,7 @@ class TextToSpeech {
/**
* Synthesize speech from a single text with automatic chunking
*/
public TTSResult call(String text, Style style, int totalStep, float silenceDuration, OrtEnvironment env)
public TTSResult call(String text, Style style, int totalStep, float speed, float silenceDuration, OrtEnvironment env)
throws OrtException {
List<String> chunks = Helper.chunkText(text, 0);
@@ -309,7 +314,7 @@ class TextToSpeech {
float durCat = 0.0f;
for (int i = 0; i < chunks.size(); i++) {
TTSResult result = _infer(Arrays.asList(chunks.get(i)), style, totalStep, env);
TTSResult result = _infer(Arrays.asList(chunks.get(i)), style, totalStep, speed, env);
float dur = result.duration[0];
int wavLen = (int) (sampleRate * dur);
@@ -344,9 +349,9 @@ class TextToSpeech {
/**
* Batch synthesize speech from multiple texts
*/
public TTSResult batch(List<String> textList, Style style, int totalStep, OrtEnvironment env)
public TTSResult batch(List<String> textList, Style style, int totalStep, float speed, OrtEnvironment env)
throws OrtException {
return _infer(textList, style, totalStep, env);
return _infer(textList, style, totalStep, speed, env);
}
public void close() throws OrtException {

View File

@@ -4,6 +4,8 @@ This guide provides examples for running TTS inference using `ExampleONNX.java`.
## 📰 Update News
**2025.11.19** - Added `--speed` parameter to control speech synthesis speed (default: 1.05, recommended range: 0.9-1.5).
**2025.11.19** - Added automatic text chunking for long-form inference. Long texts are split into chunks and synthesized with natural pauses.
## Installation