注释阿里云语音服务
This commit is contained in:
@@ -1,77 +1,77 @@
|
||||
package com.ruoyi.cms.controller.app;
|
||||
|
||||
import com.ruoyi.common.annotation.BussinessLog;
|
||||
import com.ruoyi.common.config.AudioTextRequestClient;
|
||||
import com.ruoyi.common.core.controller.BaseController;
|
||||
import com.ruoyi.common.core.domain.AjaxResult;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
|
||||
import static com.ruoyi.common.enums.BusinessType.OTHER;
|
||||
|
||||
/**
|
||||
* app语音Controller
|
||||
*
|
||||
* @author lishundong
|
||||
* @date 2024-09-03
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/app/speech")
|
||||
@Api(tags = "移动端:用户相关")
|
||||
public class AppSpeechController extends BaseController
|
||||
{
|
||||
/*private String appKey = "4lFYn2yPsQymwGu8";
|
||||
private String id = "LTAI5t9hhSqdDHqwH3RjgyYj";
|
||||
private String secret = "ni5aW3vxrWouMwcGqJPfh9Uu56PBuv";
|
||||
private String url = System.getenv().getOrDefault("NLS_GATEWAY_URL", AliyunNlsUtils.getNlsUrl()+"/ws/v1/");*/
|
||||
|
||||
@Autowired
|
||||
AudioTextRequestClient audioTextRequestClient;
|
||||
|
||||
@BussinessLog(title = "语音转文字",businessType = OTHER)
|
||||
@ApiOperation("语音转文字")
|
||||
@PostMapping(value = "/asr")
|
||||
public AjaxResult asr(@RequestParam("file") MultipartFile file){
|
||||
try {
|
||||
return AjaxResult.success("请求成功",audioTextRequestClient.getTextFromAudioFile(file));
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
return AjaxResult.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@BussinessLog(title = "文字转语音",businessType = OTHER)
|
||||
@ApiOperation("文字转语音")
|
||||
@GetMapping(value = "/tts")
|
||||
public ResponseEntity<byte[]> tts(@RequestParam("text") String text) throws UnsupportedEncodingException {
|
||||
byte[] wavData = audioTextRequestClient.getAudioInputStreamFromText(text);
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
// WAV音频的标准MIME类型
|
||||
headers.setContentType(MediaType.parseMediaType("audio/wav"));
|
||||
// inline:让浏览器在线播放(而非下载)
|
||||
headers.setContentDispositionFormData("inline", System.currentTimeMillis() + ".wav");
|
||||
// 设置内容长度
|
||||
headers.setContentLength(wavData.length);
|
||||
// 4. 返回音频数据
|
||||
return new ResponseEntity<>(wavData, headers, HttpStatus.OK);
|
||||
}
|
||||
|
||||
/*@ApiOperation("统计")
|
||||
@GetMapping("/getToken")
|
||||
public AjaxResult getToken()
|
||||
{
|
||||
SpeechRecognizerAI recognizerDemo = new SpeechRecognizerAI(appKey, id, secret, url);
|
||||
AccessToken accessToken = recognizerDemo.getAccessToken();
|
||||
String token = AliyunNlsUtils.getNlsUrl()+"/ws/v1/?appkey="+appKey+"&token="+accessToken.getToken();
|
||||
return AjaxResult.success(token);
|
||||
}*/
|
||||
}
|
||||
//package com.ruoyi.cms.controller.app;
|
||||
//
|
||||
//import com.ruoyi.common.annotation.BussinessLog;
|
||||
//import com.ruoyi.common.config.AudioTextRequestClient;
|
||||
//import com.ruoyi.common.core.controller.BaseController;
|
||||
//import com.ruoyi.common.core.domain.AjaxResult;
|
||||
//import io.swagger.annotations.Api;
|
||||
//import io.swagger.annotations.ApiOperation;
|
||||
//import org.springframework.beans.factory.annotation.Autowired;
|
||||
//import org.springframework.http.HttpHeaders;
|
||||
//import org.springframework.http.HttpStatus;
|
||||
//import org.springframework.http.MediaType;
|
||||
//import org.springframework.http.ResponseEntity;
|
||||
//import org.springframework.web.bind.annotation.*;
|
||||
//import org.springframework.web.multipart.MultipartFile;
|
||||
//
|
||||
//import java.io.UnsupportedEncodingException;
|
||||
//
|
||||
//import static com.ruoyi.common.enums.BusinessType.OTHER;
|
||||
//
|
||||
///**
|
||||
// * app语音Controller
|
||||
// *
|
||||
// * @author lishundong
|
||||
// * @date 2024-09-03
|
||||
// */
|
||||
//@RestController
|
||||
//@RequestMapping("/app/speech")
|
||||
//@Api(tags = "移动端:用户相关")
|
||||
//public class AppSpeechController extends BaseController
|
||||
//{
|
||||
// /*private String appKey = "4lFYn2yPsQymwGu8";
|
||||
// private String id = "LTAI5t9hhSqdDHqwH3RjgyYj";
|
||||
// private String secret = "ni5aW3vxrWouMwcGqJPfh9Uu56PBuv";
|
||||
// private String url = System.getenv().getOrDefault("NLS_GATEWAY_URL", AliyunNlsUtils.getNlsUrl()+"/ws/v1/");*/
|
||||
//
|
||||
// @Autowired
|
||||
// AudioTextRequestClient audioTextRequestClient;
|
||||
//
|
||||
// @BussinessLog(title = "语音转文字",businessType = OTHER)
|
||||
// @ApiOperation("语音转文字")
|
||||
// @PostMapping(value = "/asr")
|
||||
// public AjaxResult asr(@RequestParam("file") MultipartFile file){
|
||||
// try {
|
||||
// return AjaxResult.success("请求成功",audioTextRequestClient.getTextFromAudioFile(file));
|
||||
// }catch (Exception e){
|
||||
// e.printStackTrace();
|
||||
// return AjaxResult.error(e.getMessage());
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// @BussinessLog(title = "文字转语音",businessType = OTHER)
|
||||
// @ApiOperation("文字转语音")
|
||||
// @GetMapping(value = "/tts")
|
||||
// public ResponseEntity<byte[]> tts(@RequestParam("text") String text) throws UnsupportedEncodingException {
|
||||
// byte[] wavData = audioTextRequestClient.getAudioInputStreamFromText(text);
|
||||
// HttpHeaders headers = new HttpHeaders();
|
||||
// // WAV音频的标准MIME类型
|
||||
// headers.setContentType(MediaType.parseMediaType("audio/wav"));
|
||||
// // inline:让浏览器在线播放(而非下载)
|
||||
// headers.setContentDispositionFormData("inline", System.currentTimeMillis() + ".wav");
|
||||
// // 设置内容长度
|
||||
// headers.setContentLength(wavData.length);
|
||||
// // 4. 返回音频数据
|
||||
// return new ResponseEntity<>(wavData, headers, HttpStatus.OK);
|
||||
// }
|
||||
//
|
||||
// /*@ApiOperation("统计")
|
||||
// @GetMapping("/getToken")
|
||||
// public AjaxResult getToken()
|
||||
// {
|
||||
// SpeechRecognizerAI recognizerDemo = new SpeechRecognizerAI(appKey, id, secret, url);
|
||||
// AccessToken accessToken = recognizerDemo.getAccessToken();
|
||||
// String token = AliyunNlsUtils.getNlsUrl()+"/ws/v1/?appkey="+appKey+"&token="+accessToken.getToken();
|
||||
// return AjaxResult.success(token);
|
||||
// }*/
|
||||
//}
|
||||
|
||||
@@ -1,64 +1,64 @@
|
||||
package com.ruoyi.cms.handler;
|
||||
|
||||
import com.ruoyi.cms.util.AliyunNlsUtils;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.websocket.*;
|
||||
import javax.websocket.server.ServerEndpoint;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
@Component
|
||||
@ServerEndpoint("/speech-recognition")
|
||||
public class SpeechRecognitionWebSocketHandler {
|
||||
|
||||
private SpeechRecognizerAI recognizerDemo;
|
||||
|
||||
public SpeechRecognitionWebSocketHandler() {
|
||||
// 初始化语音识别器
|
||||
String appKey = "4lFYn2yPsQymwGu8";
|
||||
String id = "LTAI5t9hhSqdDHqwH3RjgyYj";
|
||||
String secret = "ni5aW3vxrWouMwcGqJPfh9Uu56PBuv";
|
||||
String url = System.getenv().getOrDefault("NLS_GATEWAY_URL", AliyunNlsUtils.getNlsUrl()+"/ws/v1/");
|
||||
recognizerDemo = new SpeechRecognizerAI(appKey, id, secret, url);
|
||||
}
|
||||
|
||||
/**
|
||||
* 连接建立成功调用的方法
|
||||
*/
|
||||
@OnOpen
|
||||
public void onOpen(Session session) {
|
||||
System.out.println("WebSocket 连接建立成功,sessionId = " + session.getId());
|
||||
}
|
||||
|
||||
/**
|
||||
* 收到客户端消息后调用的方法
|
||||
*/
|
||||
@OnMessage(maxMessageSize=5242880)
|
||||
public void onMessage(ByteBuffer message, Session session) throws IOException {
|
||||
byte[] audioData = new byte[message.remaining()];
|
||||
message.get(audioData);
|
||||
|
||||
// 处理音频数据
|
||||
recognizerDemo.processStream(session, new ByteArrayInputStream(audioData), 16000);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 连接关闭调用的方法
|
||||
*/
|
||||
@OnClose
|
||||
public void onClose(Session session) {
|
||||
System.out.println("WebSocket 连接关闭,sessionId = " + session.getId());
|
||||
}
|
||||
|
||||
/**
|
||||
* 发生错误时调用的方法
|
||||
*/
|
||||
@OnError
|
||||
public void onError(Session session, Throwable error) {
|
||||
System.err.println("WebSocket 发生错误:" + error.getMessage());
|
||||
error.printStackTrace();
|
||||
}
|
||||
}
|
||||
//package com.ruoyi.cms.handler;
|
||||
//
|
||||
//import com.ruoyi.cms.util.AliyunNlsUtils;
|
||||
//import org.springframework.stereotype.Component;
|
||||
//
|
||||
//import javax.websocket.*;
|
||||
//import javax.websocket.server.ServerEndpoint;
|
||||
//import java.io.ByteArrayInputStream;
|
||||
//import java.io.IOException;
|
||||
//import java.nio.ByteBuffer;
|
||||
//
|
||||
//@Component
|
||||
//@ServerEndpoint("/speech-recognition")
|
||||
//public class SpeechRecognitionWebSocketHandler {
|
||||
//
|
||||
// private SpeechRecognizerAI recognizerDemo;
|
||||
//
|
||||
// public SpeechRecognitionWebSocketHandler() {
|
||||
// // 初始化语音识别器
|
||||
// String appKey = "4lFYn2yPsQymwGu8";
|
||||
// String id = "LTAI5t9hhSqdDHqwH3RjgyYj";
|
||||
// String secret = "ni5aW3vxrWouMwcGqJPfh9Uu56PBuv";
|
||||
// String url = System.getenv().getOrDefault("NLS_GATEWAY_URL", AliyunNlsUtils.getNlsUrl()+"/ws/v1/");
|
||||
// recognizerDemo = new SpeechRecognizerAI(appKey, id, secret, url);
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 连接建立成功调用的方法
|
||||
// */
|
||||
// @OnOpen
|
||||
// public void onOpen(Session session) {
|
||||
// System.out.println("WebSocket 连接建立成功,sessionId = " + session.getId());
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 收到客户端消息后调用的方法
|
||||
// */
|
||||
// @OnMessage(maxMessageSize=5242880)
|
||||
// public void onMessage(ByteBuffer message, Session session) throws IOException {
|
||||
// byte[] audioData = new byte[message.remaining()];
|
||||
// message.get(audioData);
|
||||
//
|
||||
// // 处理音频数据
|
||||
// recognizerDemo.processStream(session, new ByteArrayInputStream(audioData), 16000);
|
||||
// }
|
||||
//
|
||||
//
|
||||
// /**
|
||||
// * 连接关闭调用的方法
|
||||
// */
|
||||
// @OnClose
|
||||
// public void onClose(Session session) {
|
||||
// System.out.println("WebSocket 连接关闭,sessionId = " + session.getId());
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 发生错误时调用的方法
|
||||
// */
|
||||
// @OnError
|
||||
// public void onError(Session session, Throwable error) {
|
||||
// System.err.println("WebSocket 发生错误:" + error.getMessage());
|
||||
// error.printStackTrace();
|
||||
// }
|
||||
//}
|
||||
@@ -1,151 +1,151 @@
|
||||
package com.ruoyi.cms.handler;
|
||||
|
||||
import com.alibaba.nls.client.AccessToken;
|
||||
import com.alibaba.nls.client.protocol.NlsClient;
|
||||
import com.alibaba.nls.client.protocol.OutputFormatEnum;
|
||||
import com.alibaba.nls.client.protocol.SampleRateEnum;
|
||||
import com.alibaba.nls.client.protocol.tts.SpeechSynthesizer;
|
||||
import com.alibaba.nls.client.protocol.tts.SpeechSynthesizerListener;
|
||||
import com.alibaba.nls.client.protocol.tts.SpeechSynthesizerResponse;
|
||||
import com.ruoyi.cms.util.AliyunNlsUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.websocket.*;
|
||||
import javax.websocket.server.ServerEndpoint;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
@Component
|
||||
@ServerEndpoint("/speech-synthesis")
|
||||
public class SpeechSynthesisWebSocketHandler {
|
||||
private static final Logger logger = LoggerFactory.getLogger(SpeechSynthesisWebSocketHandler.class);
|
||||
|
||||
private NlsClient client;
|
||||
private String appKey = "mtA2pwmvCeefHT3Y";
|
||||
private String accessKeyId = "LTAI5tRBahK93vPNF1JDVEPA";
|
||||
private String accessKeySecret = "x95OWb4cV6ccQVtbEJ2Gxm2Uwl2thJ";
|
||||
private String url = AliyunNlsUtils.getNlsUrl()+"/ws/v1/";
|
||||
|
||||
public SpeechSynthesisWebSocketHandler() {
|
||||
// Initialize NLS client with token
|
||||
AccessToken accessToken = new AccessToken(accessKeyId, accessKeySecret);
|
||||
try {
|
||||
if(AliyunNlsUtils.USE_TEST_ENV){
|
||||
accessToken.apply();
|
||||
}else{
|
||||
AliyunNlsTokenUtil.generateToken(accessKeyId, accessKeySecret, accessToken);
|
||||
}
|
||||
//accessToken.apply();
|
||||
String token = accessToken.getToken();
|
||||
if(url.isEmpty()) {
|
||||
this.client = new NlsClient(token);
|
||||
} else {
|
||||
this.client = new NlsClient(url, token);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("Failed to initialize NLS client", e);
|
||||
}
|
||||
}
|
||||
|
||||
@OnOpen
|
||||
public void onOpen(Session session) {
|
||||
logger.info("WebSocket connected for speech synthesis, sessionId: {}", session.getId());
|
||||
}
|
||||
|
||||
@OnMessage(maxMessageSize=5242880)
|
||||
public void onMessage(String text, Session session) {
|
||||
logger.info("Received text for synthesis: {}", text);
|
||||
|
||||
SpeechSynthesizer synthesizer = null;
|
||||
try {
|
||||
// Create synthesizer with a session-specific listener
|
||||
synthesizer = new SpeechSynthesizer(client, createSynthesizerListener(session));
|
||||
|
||||
// Configure synthesizer
|
||||
synthesizer.setAppKey(appKey);
|
||||
synthesizer.setFormat(OutputFormatEnum.WAV);
|
||||
synthesizer.setSampleRate(SampleRateEnum.SAMPLE_RATE_16K);
|
||||
synthesizer.setVoice("aiqi");
|
||||
synthesizer.setPitchRate(0);
|
||||
synthesizer.setSpeechRate(0);
|
||||
|
||||
// Use long text synthesis
|
||||
synthesizer.setLongText(text);
|
||||
|
||||
// Start synthesis
|
||||
synthesizer.start();
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("Error during speech synthesis", e);
|
||||
try {
|
||||
session.close(new CloseReason(CloseReason.CloseCodes.UNEXPECTED_CONDITION, "Synthesis error"));
|
||||
} catch (IOException ioException) {
|
||||
logger.error("Error closing session", ioException);
|
||||
}
|
||||
} finally {
|
||||
// Note: We can't close the synthesizer here because synthesis is async
|
||||
// It should be closed in the listener's onComplete/onFail methods
|
||||
}
|
||||
}
|
||||
|
||||
@OnClose
|
||||
public void onClose(Session session) {
|
||||
logger.info("WebSocket closed for speech synthesis, sessionId: {}", session.getId());
|
||||
}
|
||||
|
||||
@OnError
|
||||
public void onError(Session session, Throwable error) {
|
||||
logger.error("WebSocket error for session {}: {}", session.getId(), error.getMessage(), error);
|
||||
}
|
||||
|
||||
private SpeechSynthesizerListener createSynthesizerListener(Session session) {
|
||||
return new SpeechSynthesizerListener() {
|
||||
private boolean firstRecvBinary = true;
|
||||
private long startTime;
|
||||
|
||||
@Override
|
||||
public void onComplete(SpeechSynthesizerResponse response) {
|
||||
logger.info("Synthesis completed for session {}, status: {}", session.getId(), response.getStatus());
|
||||
try {
|
||||
// Send a close message or marker to indicate completion
|
||||
session.getBasicRemote().sendText("{\"status\":\"complete\"}");
|
||||
} catch (IOException e) {
|
||||
logger.error("Error sending completion message", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessage(ByteBuffer message) {
|
||||
try {
|
||||
if (firstRecvBinary) {
|
||||
firstRecvBinary = false;
|
||||
startTime = System.currentTimeMillis();
|
||||
logger.info("First audio packet received for session {}", session.getId());
|
||||
}
|
||||
|
||||
// Send audio data to client
|
||||
byte[] bytesArray = new byte[message.remaining()];
|
||||
message.get(bytesArray, 0, bytesArray.length);
|
||||
session.getBasicRemote().sendBinary(ByteBuffer.wrap(bytesArray));
|
||||
|
||||
} catch (IOException e) {
|
||||
logger.error("Error sending audio data to client", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFail(SpeechSynthesizerResponse response) {
|
||||
logger.error("Synthesis failed for session {}: task_id: {}, status: {}, status_text: {}",
|
||||
session.getId(), response.getTaskId(), response.getStatus(), response.getStatusText());
|
||||
try {
|
||||
session.close(new CloseReason(CloseReason.CloseCodes.UNEXPECTED_CONDITION,
|
||||
"Synthesis failed: " + response.getStatusText()));
|
||||
} catch (IOException e) {
|
||||
logger.error("Error closing failed session", e);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
//package com.ruoyi.cms.handler;
|
||||
//
|
||||
//import com.alibaba.nls.client.AccessToken;
|
||||
//import com.alibaba.nls.client.protocol.NlsClient;
|
||||
//import com.alibaba.nls.client.protocol.OutputFormatEnum;
|
||||
//import com.alibaba.nls.client.protocol.SampleRateEnum;
|
||||
//import com.alibaba.nls.client.protocol.tts.SpeechSynthesizer;
|
||||
//import com.alibaba.nls.client.protocol.tts.SpeechSynthesizerListener;
|
||||
//import com.alibaba.nls.client.protocol.tts.SpeechSynthesizerResponse;
|
||||
//import com.ruoyi.cms.util.AliyunNlsUtils;
|
||||
//import org.slf4j.Logger;
|
||||
//import org.slf4j.LoggerFactory;
|
||||
//import org.springframework.stereotype.Component;
|
||||
//
|
||||
//import javax.websocket.*;
|
||||
//import javax.websocket.server.ServerEndpoint;
|
||||
//import java.io.IOException;
|
||||
//import java.nio.ByteBuffer;
|
||||
//
|
||||
//@Component
|
||||
//@ServerEndpoint("/speech-synthesis")
|
||||
//public class SpeechSynthesisWebSocketHandler {
|
||||
// private static final Logger logger = LoggerFactory.getLogger(SpeechSynthesisWebSocketHandler.class);
|
||||
//
|
||||
// private NlsClient client;
|
||||
// private String appKey = "mtA2pwmvCeefHT3Y";
|
||||
// private String accessKeyId = "LTAI5tRBahK93vPNF1JDVEPA";
|
||||
// private String accessKeySecret = "x95OWb4cV6ccQVtbEJ2Gxm2Uwl2thJ";
|
||||
// private String url = AliyunNlsUtils.getNlsUrl()+"/ws/v1/";
|
||||
//
|
||||
// public SpeechSynthesisWebSocketHandler() {
|
||||
// // Initialize NLS client with token
|
||||
// AccessToken accessToken = new AccessToken(accessKeyId, accessKeySecret);
|
||||
// try {
|
||||
// if(AliyunNlsUtils.USE_TEST_ENV){
|
||||
// accessToken.apply();
|
||||
// }else{
|
||||
// AliyunNlsTokenUtil.generateToken(accessKeyId, accessKeySecret, accessToken);
|
||||
// }
|
||||
// //accessToken.apply();
|
||||
// String token = accessToken.getToken();
|
||||
// if(url.isEmpty()) {
|
||||
// this.client = new NlsClient(token);
|
||||
// } else {
|
||||
// this.client = new NlsClient(url, token);
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// logger.error("Failed to initialize NLS client", e);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// @OnOpen
|
||||
// public void onOpen(Session session) {
|
||||
// logger.info("WebSocket connected for speech synthesis, sessionId: {}", session.getId());
|
||||
// }
|
||||
//
|
||||
// @OnMessage(maxMessageSize=5242880)
|
||||
// public void onMessage(String text, Session session) {
|
||||
// logger.info("Received text for synthesis: {}", text);
|
||||
//
|
||||
// SpeechSynthesizer synthesizer = null;
|
||||
// try {
|
||||
// // Create synthesizer with a session-specific listener
|
||||
// synthesizer = new SpeechSynthesizer(client, createSynthesizerListener(session));
|
||||
//
|
||||
// // Configure synthesizer
|
||||
// synthesizer.setAppKey(appKey);
|
||||
// synthesizer.setFormat(OutputFormatEnum.WAV);
|
||||
// synthesizer.setSampleRate(SampleRateEnum.SAMPLE_RATE_16K);
|
||||
// synthesizer.setVoice("aiqi");
|
||||
// synthesizer.setPitchRate(0);
|
||||
// synthesizer.setSpeechRate(0);
|
||||
//
|
||||
// // Use long text synthesis
|
||||
// synthesizer.setLongText(text);
|
||||
//
|
||||
// // Start synthesis
|
||||
// synthesizer.start();
|
||||
//
|
||||
// } catch (Exception e) {
|
||||
// logger.error("Error during speech synthesis", e);
|
||||
// try {
|
||||
// session.close(new CloseReason(CloseReason.CloseCodes.UNEXPECTED_CONDITION, "Synthesis error"));
|
||||
// } catch (IOException ioException) {
|
||||
// logger.error("Error closing session", ioException);
|
||||
// }
|
||||
// } finally {
|
||||
// // Note: We can't close the synthesizer here because synthesis is async
|
||||
// // It should be closed in the listener's onComplete/onFail methods
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// @OnClose
|
||||
// public void onClose(Session session) {
|
||||
// logger.info("WebSocket closed for speech synthesis, sessionId: {}", session.getId());
|
||||
// }
|
||||
//
|
||||
// @OnError
|
||||
// public void onError(Session session, Throwable error) {
|
||||
// logger.error("WebSocket error for session {}: {}", session.getId(), error.getMessage(), error);
|
||||
// }
|
||||
//
|
||||
// private SpeechSynthesizerListener createSynthesizerListener(Session session) {
|
||||
// return new SpeechSynthesizerListener() {
|
||||
// private boolean firstRecvBinary = true;
|
||||
// private long startTime;
|
||||
//
|
||||
// @Override
|
||||
// public void onComplete(SpeechSynthesizerResponse response) {
|
||||
// logger.info("Synthesis completed for session {}, status: {}", session.getId(), response.getStatus());
|
||||
// try {
|
||||
// // Send a close message or marker to indicate completion
|
||||
// session.getBasicRemote().sendText("{\"status\":\"complete\"}");
|
||||
// } catch (IOException e) {
|
||||
// logger.error("Error sending completion message", e);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void onMessage(ByteBuffer message) {
|
||||
// try {
|
||||
// if (firstRecvBinary) {
|
||||
// firstRecvBinary = false;
|
||||
// startTime = System.currentTimeMillis();
|
||||
// logger.info("First audio packet received for session {}", session.getId());
|
||||
// }
|
||||
//
|
||||
// // Send audio data to client
|
||||
// byte[] bytesArray = new byte[message.remaining()];
|
||||
// message.get(bytesArray, 0, bytesArray.length);
|
||||
// session.getBasicRemote().sendBinary(ByteBuffer.wrap(bytesArray));
|
||||
//
|
||||
// } catch (IOException e) {
|
||||
// logger.error("Error sending audio data to client", e);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void onFail(SpeechSynthesizerResponse response) {
|
||||
// logger.error("Synthesis failed for session {}: task_id: {}, status: {}, status_text: {}",
|
||||
// session.getId(), response.getTaskId(), response.getStatus(), response.getStatusText());
|
||||
// try {
|
||||
// session.close(new CloseReason(CloseReason.CloseCodes.UNEXPECTED_CONDITION,
|
||||
// "Synthesis failed: " + response.getStatusText()));
|
||||
// } catch (IOException e) {
|
||||
// logger.error("Error closing failed session", e);
|
||||
// }
|
||||
// }
|
||||
// };
|
||||
// }
|
||||
//}
|
||||
Reference in New Issue
Block a user