From 9e47d0787e84e2a3e7fb4a34bc5a2c41c9946825 Mon Sep 17 00:00:00 2001 From: wangxiangshun Date: Sun, 19 Oct 2025 15:44:04 +0800 Subject: [PATCH] =?UTF-8?q?TTS=20=E8=BF=94=E5=9B=9E=E8=AF=AD=E9=9F=B3?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../vetti/socket/ChatWebSocketHandler.java | 27 +++++++++++++++++-- .../ai/elevenLabs/ElevenLabsClient.java | 2 +- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/vetti-admin/src/main/java/com/vetti/socket/ChatWebSocketHandler.java b/vetti-admin/src/main/java/com/vetti/socket/ChatWebSocketHandler.java index a26794d..472a2e5 100644 --- a/vetti-admin/src/main/java/com/vetti/socket/ChatWebSocketHandler.java +++ b/vetti-admin/src/main/java/com/vetti/socket/ChatWebSocketHandler.java @@ -136,7 +136,7 @@ public class ChatWebSocketHandler { log.info("2、开始进行AI回答时间:{}",System.currentTimeMillis()/1000); //把结果文字转成语音文件 //生成文件 - // 生成唯一文件名 + //生成唯一文件名 String resultFileName = clientId + "_" + System.currentTimeMillis() + ".opus"; String resultPathUrl = RuoYiConfig.getProfile() + VOICE_STORAGE_RESULT_DIR + resultFileName; ElevenLabsClient elevenLabsClient = SpringUtils.getBean(ElevenLabsClient.class); @@ -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(); + } + } + } diff --git a/vetti-common/src/main/java/com/vetti/common/ai/elevenLabs/ElevenLabsClient.java b/vetti-common/src/main/java/com/vetti/common/ai/elevenLabs/ElevenLabsClient.java index 9d31dff..d8d41ef 100644 --- a/vetti-common/src/main/java/com/vetti/common/ai/elevenLabs/ElevenLabsClient.java +++ b/vetti-common/src/main/java/com/vetti/common/ai/elevenLabs/ElevenLabsClient.java @@ -53,7 +53,7 @@ public class ElevenLabsClient { Map 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);