初始化文字语音转换的代码

dev_v1.0.1
liu
parent 0cbf32143a
commit 77ba20f827

@ -4,6 +4,7 @@ import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.annotation.MapperScans;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
@EnableWebSocket

@ -1,5 +1,6 @@
package com.supervision.controller;
import com.supervision.pojo.vo.ReplyVoiceResVO;
import com.supervision.service.AskService;
import com.supervision.websocket.cache.WebSocketUserCache;
import io.swagger.annotations.Api;
@ -35,6 +36,12 @@ public class AskController {
return askService.receiveVoiceFile(file);
}
@ApiOperation("回复语音及文字消息")
@GetMapping("replyVoice")
public ReplyVoiceResVO replyVoice(){
return askService.replyVoice();
}
@ApiOperation("查询对话历史")
public void queryAskHistory(String processId){

@ -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,5 +1,7 @@
package com.supervision.service;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.supervision.pojo.vo.ReplyVoiceResVO;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
@ -8,4 +10,8 @@ import java.io.IOException;
public interface AskService {
String receiveVoiceFile(MultipartFile file) throws IOException;
ReplyVoiceResVO replyVoice() ;
}

@ -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("语音转换文字失败");
}
}
}

@ -55,4 +55,9 @@ spring:
mybatis-plus:
mapper-locations: classpath*:mapper/**/*.xml
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
paddle-speech:
# https://github.com/PaddlePaddle/PaddleSpeech/wiki/PaddleSpeech-Server-RESTful-API
tts: http://192.168.10.137:8090/paddlespeech/tts
asr: http://192.168.10.137:8090/paddlespeech/asr
Loading…
Cancel
Save