86 lines
3.1 KiB
Java

package com.supervision.service.impl;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.supervision.exception.BusinessException;
import com.supervision.pojo.paddlespeech.req.AsrReqDTO;
import com.supervision.pojo.paddlespeech.req.TtsReqDTO;
import com.supervision.pojo.paddlespeech.res.AsrResultDTO;
import com.supervision.pojo.paddlespeech.res.PaddleSpeechResDTO;
import com.supervision.pojo.paddlespeech.res.TtsResultDTO;
import com.supervision.pojo.vo.ReplyVoiceResVO;
import com.supervision.service.AskService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
@Service
@RequiredArgsConstructor
public class AskServiceImpl implements AskService {
@Value("${paddle-speech.tts}")
private String tts;
@Value("${paddle-speech.asr}")
private String asr;
private final ObjectMapper objectMapper = new ObjectMapper();
@Override
public String receiveVoiceFile(MultipartFile file) throws IOException {
return asrTransform(file.getBytes());
}
@Override
public ReplyVoiceResVO replyVoice() {
String text = "测试:这是文字转语音的测试,测试是否OK";
return ttsTransform(text);
}
private String asrTransform(byte[] bytes) {
// 首先编码为base64编码
String encode = Base64.encode(bytes);
// 这里调用Python的接口,将文字转换为语音
String post = HttpUtil.post(asr, JSONUtil.toJsonStr(new AsrReqDTO(encode)));
try {
PaddleSpeechResDTO<AsrResultDTO> response = objectMapper.readValue(post, new TypeReference<PaddleSpeechResDTO<AsrResultDTO>>() {});
if (!response.getSuccess() || ObjectUtil.isEmpty(response.getResult())) {
throw new BusinessException("语音转换文字失败");
}
return response.getResult().getTranscription();
}catch (Exception e){
throw new BusinessException("语音转换文字失败");
}
}
private ReplyVoiceResVO ttsTransform(String str) {
// 构建
String post = HttpUtil.post(tts, JSONUtil.toJsonStr(new TtsReqDTO(str)));
try {
PaddleSpeechResDTO<TtsResultDTO> response = objectMapper.readValue(post, new TypeReference<PaddleSpeechResDTO<TtsResultDTO>>() {
});
if (!response.getSuccess() || ObjectUtil.isEmpty(response.getResult())) {
throw new BusinessException("文字转换语音失败");
}
ReplyVoiceResVO resVO = new ReplyVoiceResVO();
resVO.setVoice(response.getResult().getAudio());
resVO.setText(str);
return resVO;
}catch (Exception e){
throw new BusinessException("语音转换文字失败");
}
}
}