TTS 返回语音优化

This commit is contained in:
2025-10-19 15:44:04 +08:00
parent a2e7e78bee
commit 9e47d0787e
2 changed files with 26 additions and 3 deletions

View File

@@ -144,8 +144,10 @@ public class ChatWebSocketHandler {
log.info("3、开始进行AI回答时间:{}",System.currentTimeMillis()/1000);
//持续返回数据流给客户端
try {
String resultOutPathUrl = RuoYiConfig.getProfile() + VOICE_STORAGE_RESULT_DIR + "123_"+resultFileName;
removeSilence(resultPathUrl, resultOutPathUrl);
//文件转换成文件流
ByteBuffer outByteBuffer = convertFileToByteBuffer(resultPathUrl);
ByteBuffer outByteBuffer = convertFileToByteBuffer(resultOutPathUrl);
//发送文件流数据
session.getBasicRemote().sendBinary(outByteBuffer);
// 发送响应确认
@@ -404,5 +406,26 @@ public class ChatWebSocketHandler {
}
}
public void removeSilence(String inputFile, String outputFile) {
try {
// FFmpeg 命令,去除音频后面的静音部分
String command = "ffmpeg -i " + inputFile +
" -af silenceremove=start_periods=1:start_duration=0.2:start_threshold=-40dB " +
"-af silenceremove=end_periods=1:end_duration=0.2:end_threshold=-40dB " +
outputFile;
// 执行命令
Process process = Runtime.getRuntime().exec(command);
int exitCode = process.waitFor();
if (exitCode == 0) {
System.out.println("静音部分已去除");
} else {
System.out.println("音频处理失败");
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}

View File

@@ -53,7 +53,7 @@ public class ElevenLabsClient {
Map<String, Object> payload = new HashMap<>();
payload.put("text", text);
payload.put("model_id", modelId);
payload.put("voice_settings", new VoiceSettings(0.85, 0.5,1,0,0.9));
payload.put("voice_settings", new VoiceSettings(0.85, 0.5,1.1,0,0.9));
Gson gson = new Gson();
StringEntity entity = new StringEntity(gson.toJson(payload), ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);