初始化文字语音转换的代码
parent
0cbf32143a
commit
77ba20f827
@ -0,0 +1,21 @@
|
||||
package com.supervision.pojo.paddlespeech.req;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class AsrReqDTO {
|
||||
|
||||
private final String audio;
|
||||
|
||||
private final String audio_format = "wav";
|
||||
|
||||
private final Integer sample_rate = 16000;
|
||||
|
||||
private final String lang = "zh_cn";
|
||||
|
||||
private final Integer punc = 0;
|
||||
|
||||
public AsrReqDTO(String audio) {
|
||||
this.audio = audio;
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package com.supervision.pojo.paddlespeech.req;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Data
|
||||
public class TtsReqDTO {
|
||||
|
||||
private final String text;
|
||||
|
||||
private final Integer spk_id = 0;
|
||||
|
||||
private final Double speed = 1.0;
|
||||
|
||||
private final Double volume = 1.0;
|
||||
|
||||
private final Integer sample_rate = 16000;
|
||||
|
||||
private final String save_path = "./tts.wav";
|
||||
|
||||
public TtsReqDTO(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.supervision.pojo.paddlespeech.res;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class AsrResultDTO {
|
||||
|
||||
private String transcription;
|
||||
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.supervision.pojo.paddlespeech.res;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class PaddleSpeechResDTO<T> {
|
||||
|
||||
private Boolean success;
|
||||
|
||||
private Integer code;
|
||||
|
||||
private String message;
|
||||
|
||||
private T result;
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package com.supervision.pojo.paddlespeech.res;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class TtsResultDTO {
|
||||
|
||||
private String lang;
|
||||
|
||||
private String spk_id;
|
||||
|
||||
private String speed;
|
||||
|
||||
private String volume;
|
||||
|
||||
private String sample_rate;
|
||||
|
||||
private String duration;
|
||||
|
||||
private String save_path;
|
||||
|
||||
private String audio;
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package com.supervision.pojo.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class ReplyVoiceResVO {
|
||||
|
||||
private String voice;
|
||||
|
||||
private String text;
|
||||
}
|
@ -1,22 +1,85 @@
|
||||
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(file.getBytes());
|
||||
// TODO 这里调用Python的接口,将文字转换为语音
|
||||
return encode;
|
||||
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("语音转换文字失败");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue