STT 数据流处理
This commit is contained in:
@@ -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
|
@OnMessage
|
||||||
public void onBinaryMessage(Session session, @PathParam("clientId") String clientId, ByteBuffer byteBuffer) {
|
public void onBinaryMessage(Session session, @PathParam("clientId") String clientId, ByteBuffer byteBuffer) {
|
||||||
@@ -256,17 +207,66 @@ public class ChatWebSocketHandler {
|
|||||||
byte[] bytes = new byte[byteBuffer.remaining()];
|
byte[] bytes = new byte[byteBuffer.remaining()];
|
||||||
//从缓冲区中读取数据并存储到指定的字节数组中
|
//从缓冲区中读取数据并存储到指定的字节数组中
|
||||||
byteBuffer.get(bytes);
|
byteBuffer.get(bytes);
|
||||||
|
log.info("2、开始接收数据流时间:{}",System.currentTimeMillis()/1000);
|
||||||
// 1. 获取当前会话的缓存
|
// 生成唯一文件名
|
||||||
List<byte[]> fragments = fragmentCache.get(clientId);
|
String fileName = clientId + "_" + System.currentTimeMillis() + ".wav";
|
||||||
if (fragments == null) {
|
String pathUrl = RuoYiConfig.getProfile()+VOICE_STORAGE_DIR + fileName;
|
||||||
fragments = new ArrayList<>();
|
log.info("文件路径为:{}", pathUrl);
|
||||||
fragmentCache.put(clientId, fragments);
|
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<byte[]> fragments = fragmentCache.get(clientId);
|
||||||
|
// if (fragments == null) {
|
||||||
|
// fragments = new ArrayList<>();
|
||||||
|
// fragmentCache.put(clientId, fragments);
|
||||||
|
// }
|
||||||
|
// fragments.add(bytes);
|
||||||
|
// fragmentCache.put(clientId, fragments);
|
||||||
|
// }
|
||||||
|
|
||||||
// 连接关闭时调用
|
// 连接关闭时调用
|
||||||
@OnClose
|
@OnClose
|
||||||
public void onClose(Session session, CloseReason reason) {
|
public void onClose(Session session, CloseReason reason) {
|
||||||
|
|||||||
Reference in New Issue
Block a user