修复tts问题

This commit is contained in:
poal
2025-10-25 13:57:04 +08:00
parent de2f57716d
commit 9d741f3122
2 changed files with 27 additions and 3 deletions

View File

@@ -81,10 +81,10 @@ public class SherpaTtsModel implements TtsModel{
}
}
try {
int sid = 100;
int sid = 0;
float speed = 1.0f;
if(params != null){
sid = sherpaTtsParams.getSpeakerId() > 0 ? sherpaTtsParams.getSpeakerId() : 100;
sid = sherpaTtsParams.getSpeakerId();
speed = sherpaTtsParams.getSpeed() > 0.0f ? sherpaTtsParams.getSpeed() : 1.0f;
}
GeneratedAudio audio = null;
@@ -104,7 +104,7 @@ public class SherpaTtsModel implements TtsModel{
@Override
public R<Audio> generate(String text, TtsParams params) {
GeneratedAudio audio = generateCore(text, params);
return R.ok(new Audio(audio.getSamples()));
return R.ok(new Audio(audio.getSamples(), audio.getSampleRate(), 1));
}
@Override

View File

@@ -238,6 +238,30 @@ public class AudioUtils {
}
}
/**
* 将 float[] 音频数据保存为 WAV 文件
*/
public static void saveToWav(Audio audio, String savePath) throws IOException {
AudioFormat audioFormat = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
audio.getSampleRate(),
16,
1,
2,
audio.getSampleRate(),
false
);
// 1. 转换为 16-bit PCM
byte[] bytes = floatsToPCM16(audio.getData());
// 2. 使用 ByteArrayInputStream 封装为音频流
try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
AudioInputStream ais = new AudioInputStream(bais, audioFormat, audio.getData().length)) {
// 3. 保存到本地文件
AudioSystem.write(ais, AudioFileFormat.Type.WAVE, new File(savePath));
}
}
public static AudioFormat getDefaultAudioFormat(){
return new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,