| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- package com.yingyangfly.app.controller;
- import com.yingyangfly.app.util.AsrMain;
- import com.yingyangfly.app.util.BaiduVoiceUtil;
- import com.yingyangfly.common.dto.ResultResponse;
- import com.yingyangfly.common.log.annotation.TraceLog;
- import com.yingyangfly.common.utils.MD5Util;
- import com.yingyangfly.core.annotation.Log;
- import com.yingyangfly.core.dto.SpeechSynthesis;
- import com.yingyangfly.core.enums.OperatorType;
- import com.yingyangfly.core.vo.AsrVo;
- import com.yingyangfly.redis.client.RedisClient;
- import io.swagger.annotations.Api;
- import io.swagger.annotations.ApiOperation;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import java.nio.ByteBuffer;
- import java.nio.ByteOrder;
- import java.util.HashMap;
- import java.util.Map;
- /**
- * 声音语音合成
- *
- * @author jiangqian
- * @date 2023/9/9 0009 14:48
- */
- @RestController
- @RequestMapping("/app/video")
- @Api(tags = "声音语音合成")
- @Slf4j
- public class VoiceController {
- @Autowired
- private RedisClient redisClient;
- @Autowired
- private BaiduVoiceUtil baiduVoiceUtil;
- @Log(title = "声音语言合成",operatorType = OperatorType.MOBILE)
- @ApiOperation("声音语音合成")
- @RequestMapping("/getVoiceUrl")
- @TraceLog
- public ResultResponse getVoiceUrl(@RequestBody SpeechSynthesis speechSynthesis){
- String voiceUrl = redisClient.get("hcp:voice:"+MD5Util.md5(speechSynthesis.getVoiceMsg()),"");
- if(!StringUtils.isEmpty(voiceUrl)){
- return ResultResponse.success(voiceUrl);
- }else{
- return baiduVoiceUtil.getVoiceUrl(speechSynthesis.getVoiceMsg(),speechSynthesis.getSed());
- }
- }
- @Log(title = "游戏介绍语言合成",operatorType = OperatorType.MOBILE)
- @ApiOperation("游戏介绍语言合成")
- @RequestMapping("/game/getVoiceUrl")
- @TraceLog
- public ResultResponse getGameVoiceUrl(String voiceMsg){
- String voiceUrl = redisClient.get("hcp:voice:"+MD5Util.md5(voiceMsg),"");
- if(!StringUtils.isEmpty(voiceUrl)){
- return ResultResponse.success(voiceUrl);
- }else{
- return baiduVoiceUtil.getVoiceUrl(voiceMsg,"3");
- }
- }
- @PostMapping("/vs")
- public Map sendAsr(@RequestBody AsrVo p) {
- log.info("【***************收到语音识别请求****************】");
- Map ret = new HashMap();
- try {
- AsrMain demo = new AsrMain();
- // 填写下面信息
- String result = demo.run(this.shortArrayToByteArray(p.getContent(), ByteOrder.LITTLE_ENDIAN));
- ret.put("code", "0");
- ret.put("result", result);
- } catch (Exception e) {
- e.printStackTrace();
- log.error("【语音识别系统错误】");
- ret.put("code", "900");
- ret.put("msg", "调用语音接口失败");
- }
- log.info("【***************结果已发送给客户端****************】");
- return ret;
- }
- private byte[] shortArrayToByteArray(short[] shorts, ByteOrder order) {
- byte[] bytes = new byte[shorts.length * 2];
- ByteBuffer.wrap(bytes)
- .order(order)
- .asShortBuffer()
- .put(shorts);
- return bytes;
- }
- }
|