From c806410d4199abcf53f915306f16d12b86adc7a9 Mon Sep 17 00:00:00 2001 From: wangxiangshun Date: Mon, 20 Oct 2025 14:54:00 +0800 Subject: [PATCH] =?UTF-8?q?STT=20=E6=95=B0=E6=8D=AE=E6=B5=81=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../vetti/socket/ChatWebSocketHandler.java | 114 +++++++++--------- 1 file changed, 57 insertions(+), 57 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 120b121..f6ededd 100644 --- a/vetti-admin/src/main/java/com/vetti/socket/ChatWebSocketHandler.java +++ b/vetti-admin/src/main/java/com/vetti/socket/ChatWebSocketHandler.java @@ -198,55 +198,6 @@ public class ChatWebSocketHandler { } } - // 接收二进制消息(流数据) -// @OnMessage -// public void onBinaryMessage(Session session, @PathParam("clientId") String clientId, ByteBuffer byteBuffer) { -// log.info("1、开始接收数据流时间:{}",System.currentTimeMillis()/1000); -// log.info("客户端ID为:{}", clientId); -// // 处理二进制流数据 -// byte[] bytes = new byte[byteBuffer.remaining()]; -// //从缓冲区中读取数据并存储到指定的字节数组中 -// byteBuffer.get(bytes); -// log.info("2、开始接收数据流时间:{}",System.currentTimeMillis()/1000); -// // 生成唯一文件名 -// String fileName = clientId + "_" + System.currentTimeMillis() + ".wav"; -// String pathUrl = RuoYiConfig.getProfile()+VOICE_STORAGE_DIR + fileName; -// log.info("文件路径为:{}", pathUrl); -// log.info("3、开始接收数据流时间:{}",System.currentTimeMillis()/1000); -// try{ -// log.info("文件流的大小为:{}",bytes.length); -// saveAsWebM(bytes,pathUrl); -// //接收到数据流后直接就进行SST处理 -// //语音格式转换 -// String fileOutName = clientId + "_" + System.currentTimeMillis() + ".pcm"; -// String pathOutUrl = RuoYiConfig.getProfile()+VOICE_STORAGE_DIR + fileOutName; -// handleAudioToPCM(pathUrl,pathOutUrl); -// //发送消息 -// WebSocket webSocket = cacheWebSocket.get(clientId); -// log.info("获取的socket对象为:{}",webSocket); -// if(webSocket != null){ -//// 1. 启动音频缓冲 -//// webSocket.send("{\"type\": \"input_audio_buffer.start\"}"); -// log.info("3.1 开始发送数据音频流啦"); -// File outputFile = new File(pathOutUrl); // 输出PCM格式文件 -// ByteBuffer buffer = ByteBuffer.wrap(FileUtils.readFileToByteArray(outputFile)); -// byte[] outBytes = new byte[buffer.remaining()]; -// //从缓冲区中读取数据并存储到指定的字节数组中 -// buffer.get(outBytes); -// String base64Audio = Base64.getEncoder().encodeToString(outBytes); -// String message = "{ \"type\": \"input_audio_buffer.append\", \"audio\": \"" + base64Audio + "\" }"; -// webSocket.send(message); -// log.info("4、开始接收数据流时间:{}",System.currentTimeMillis()/1000); -// // 3. 提交音频并请求转录 -//// webSocket.send("{\"type\": \"input_audio_buffer.commit\"}"); -//// webSocket.send("{\"type\": \"response.create\"}"); -// } -// }catch (Exception e){ -// e.printStackTrace(); -// } -// -// } - // 接收二进制消息(流数据) @OnMessage public void onBinaryMessage(Session session, @PathParam("clientId") String clientId, ByteBuffer byteBuffer) { @@ -256,17 +207,66 @@ public class ChatWebSocketHandler { byte[] bytes = new byte[byteBuffer.remaining()]; //从缓冲区中读取数据并存储到指定的字节数组中 byteBuffer.get(bytes); - - // 1. 获取当前会话的缓存 - List fragments = fragmentCache.get(clientId); - if (fragments == null) { - fragments = new ArrayList<>(); - fragmentCache.put(clientId, fragments); + log.info("2、开始接收数据流时间:{}",System.currentTimeMillis()/1000); + // 生成唯一文件名 + String fileName = clientId + "_" + System.currentTimeMillis() + ".wav"; + String pathUrl = RuoYiConfig.getProfile()+VOICE_STORAGE_DIR + fileName; + log.info("文件路径为:{}", pathUrl); + log.info("3、开始接收数据流时间:{}",System.currentTimeMillis()/1000); + try{ + log.info("文件流的大小为:{}",bytes.length); + saveAsWebM(bytes,pathUrl); + //接收到数据流后直接就进行SST处理 + //语音格式转换 + String fileOutName = clientId + "_" + System.currentTimeMillis() + ".pcm"; + String pathOutUrl = RuoYiConfig.getProfile()+VOICE_STORAGE_DIR + fileOutName; + handleAudioToPCM(pathUrl,pathOutUrl); + //发送消息 + WebSocket webSocket = cacheWebSocket.get(clientId); + log.info("获取的socket对象为:{}",webSocket); + if(webSocket != null){ +// 1. 启动音频缓冲 +// webSocket.send("{\"type\": \"input_audio_buffer.start\"}"); + log.info("3.1 开始发送数据音频流啦"); + File outputFile = new File(pathOutUrl); // 输出PCM格式文件 + ByteBuffer buffer = ByteBuffer.wrap(FileUtils.readFileToByteArray(outputFile)); + byte[] outBytes = new byte[buffer.remaining()]; + //从缓冲区中读取数据并存储到指定的字节数组中 + buffer.get(outBytes); + String base64Audio = Base64.getEncoder().encodeToString(outBytes); + String message = "{ \"type\": \"input_audio_buffer.append\", \"audio\": \"" + base64Audio + "\" }"; + webSocket.send(message); + log.info("4、开始接收数据流时间:{}",System.currentTimeMillis()/1000); + // 3. 提交音频并请求转录 +// webSocket.send("{\"type\": \"input_audio_buffer.commit\"}"); +// webSocket.send("{\"type\": \"response.create\"}"); + } + }catch (Exception e){ + e.printStackTrace(); } - fragments.add(bytes); - fragmentCache.put(clientId, fragments); + } + // 接收二进制消息(流数据) +// @OnMessage +// public void onBinaryMessage(Session session, @PathParam("clientId") String clientId, ByteBuffer byteBuffer) { +// log.info("1、开始接收数据流时间:{}",System.currentTimeMillis()/1000); +// log.info("客户端ID为:{}", clientId); +// // 处理二进制流数据 +// byte[] bytes = new byte[byteBuffer.remaining()]; +// //从缓冲区中读取数据并存储到指定的字节数组中 +// byteBuffer.get(bytes); +// +// // 1. 获取当前会话的缓存 +// List fragments = fragmentCache.get(clientId); +// if (fragments == null) { +// fragments = new ArrayList<>(); +// fragmentCache.put(clientId, fragments); +// } +// fragments.add(bytes); +// fragmentCache.put(clientId, fragments); +// } + // 连接关闭时调用 @OnClose public void onClose(Session session, CloseReason reason) {