VoiceController.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package com.yingyangfly.app.controller;
  2. import com.yingyangfly.app.util.AsrMain;
  3. import com.yingyangfly.app.util.BaiduVoiceUtil;
  4. import com.yingyangfly.common.dto.ResultResponse;
  5. import com.yingyangfly.common.log.annotation.TraceLog;
  6. import com.yingyangfly.common.utils.MD5Util;
  7. import com.yingyangfly.core.annotation.Log;
  8. import com.yingyangfly.core.dto.SpeechSynthesis;
  9. import com.yingyangfly.core.enums.OperatorType;
  10. import com.yingyangfly.core.vo.AsrVo;
  11. import com.yingyangfly.redis.client.RedisClient;
  12. import io.swagger.annotations.Api;
  13. import io.swagger.annotations.ApiOperation;
  14. import lombok.extern.slf4j.Slf4j;
  15. import org.apache.commons.lang3.StringUtils;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.web.bind.annotation.PostMapping;
  18. import org.springframework.web.bind.annotation.RequestBody;
  19. import org.springframework.web.bind.annotation.RequestMapping;
  20. import org.springframework.web.bind.annotation.RestController;
  21. import java.nio.ByteBuffer;
  22. import java.nio.ByteOrder;
  23. import java.util.HashMap;
  24. import java.util.Map;
  25. /**
  26. * 声音语音合成
  27. *
  28. * @author jiangqian
  29. * @date 2023/9/9 0009 14:48
  30. */
  31. @RestController
  32. @RequestMapping("/app/video")
  33. @Api(tags = "声音语音合成")
  34. @Slf4j
  35. public class VoiceController {
  36. @Autowired
  37. private RedisClient redisClient;
  38. @Autowired
  39. private BaiduVoiceUtil baiduVoiceUtil;
  40. @Log(title = "声音语言合成",operatorType = OperatorType.MOBILE)
  41. @ApiOperation("声音语音合成")
  42. @RequestMapping("/getVoiceUrl")
  43. @TraceLog
  44. public ResultResponse getVoiceUrl(@RequestBody SpeechSynthesis speechSynthesis){
  45. String voiceUrl = redisClient.get("hcp:voice:"+MD5Util.md5(speechSynthesis.getVoiceMsg()),"");
  46. if(!StringUtils.isEmpty(voiceUrl)){
  47. return ResultResponse.success(voiceUrl);
  48. }else{
  49. return baiduVoiceUtil.getVoiceUrl(speechSynthesis.getVoiceMsg(),speechSynthesis.getSed());
  50. }
  51. }
  52. @Log(title = "游戏介绍语言合成",operatorType = OperatorType.MOBILE)
  53. @ApiOperation("游戏介绍语言合成")
  54. @RequestMapping("/game/getVoiceUrl")
  55. @TraceLog
  56. public ResultResponse getGameVoiceUrl(String voiceMsg){
  57. String voiceUrl = redisClient.get("hcp:voice:"+MD5Util.md5(voiceMsg),"");
  58. if(!StringUtils.isEmpty(voiceUrl)){
  59. return ResultResponse.success(voiceUrl);
  60. }else{
  61. return baiduVoiceUtil.getVoiceUrl(voiceMsg,"3");
  62. }
  63. }
  64. @PostMapping("/vs")
  65. public Map sendAsr(@RequestBody AsrVo p) {
  66. log.info("【***************收到语音识别请求****************】");
  67. Map ret = new HashMap();
  68. try {
  69. AsrMain demo = new AsrMain();
  70. // 填写下面信息
  71. String result = demo.run(this.shortArrayToByteArray(p.getContent(), ByteOrder.LITTLE_ENDIAN));
  72. ret.put("code", "0");
  73. ret.put("result", result);
  74. } catch (Exception e) {
  75. e.printStackTrace();
  76. log.error("【语音识别系统错误】");
  77. ret.put("code", "900");
  78. ret.put("msg", "调用语音接口失败");
  79. }
  80. log.info("【***************结果已发送给客户端****************】");
  81. return ret;
  82. }
  83. private byte[] shortArrayToByteArray(short[] shorts, ByteOrder order) {
  84. byte[] bytes = new byte[shorts.length * 2];
  85. ByteBuffer.wrap(bytes)
  86. .order(order)
  87. .asShortBuffer()
  88. .put(shorts);
  89. return bytes;
  90. }
  91. }