添加paddle 工具
parent
f0ccce3626
commit
beb6ce57dd
@ -0,0 +1,21 @@
|
|||||||
|
package com.supervision.dto.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,23 @@
|
|||||||
|
package com.supervision.dto.paddlespeech.req;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@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.dto.paddlespeech.res;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class AsrResultDTO {
|
||||||
|
|
||||||
|
private String transcription;
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.supervision.dto.paddlespeech.res;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class PaddleSpeechResDTO<T> {
|
||||||
|
|
||||||
|
private Boolean success;
|
||||||
|
|
||||||
|
private Integer code;
|
||||||
|
|
||||||
|
private Object message;
|
||||||
|
|
||||||
|
private T result;
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
package com.supervision.dto.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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 语音的base64编码
|
||||||
|
*/
|
||||||
|
private String audio;
|
||||||
|
}
|
@ -0,0 +1,76 @@
|
|||||||
|
/*
|
||||||
|
* 文 件 名: CustomException
|
||||||
|
* 版 权:
|
||||||
|
* 描 述: <描述>
|
||||||
|
* 修 改 人: RedName
|
||||||
|
* 修改时间: 2022/8/5
|
||||||
|
* 跟踪单号: <跟踪单号>
|
||||||
|
* 修改单号: <修改单号>
|
||||||
|
* 修改内容: <修改内容>
|
||||||
|
*/
|
||||||
|
package com.supervision.exception;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <功能详细描述>
|
||||||
|
* 自定义异常
|
||||||
|
*
|
||||||
|
* @author ljt
|
||||||
|
* @version [版本号, 2022/8/5]
|
||||||
|
* @see [相关类/方法]
|
||||||
|
* @since [产品/模块版本]
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class BusinessException extends RuntimeException {
|
||||||
|
/**
|
||||||
|
* 异常编码
|
||||||
|
*/
|
||||||
|
private final Integer code;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 异常信息
|
||||||
|
*/
|
||||||
|
private final String message;
|
||||||
|
|
||||||
|
public BusinessException(Throwable cause) {
|
||||||
|
super(cause);
|
||||||
|
this.code = HttpStatus.INTERNAL_SERVER_ERROR.value();
|
||||||
|
this.message = null;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public BusinessException(Throwable cause, String message) {
|
||||||
|
super(cause);
|
||||||
|
this.code = HttpStatus.INTERNAL_SERVER_ERROR.value();
|
||||||
|
this.message = message;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public BusinessException(String message) {
|
||||||
|
this.code = HttpStatus.INTERNAL_SERVER_ERROR.value();
|
||||||
|
this.message = message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BusinessException(String message, Integer code) {
|
||||||
|
this.message = message;
|
||||||
|
this.code = code;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BusinessException(String message, Throwable e) {
|
||||||
|
super(message, e);
|
||||||
|
log.error(message, e);
|
||||||
|
this.code = HttpStatus.INTERNAL_SERVER_ERROR.value();
|
||||||
|
this.message = message;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getMessage() {
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getCode() {
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,46 @@
|
|||||||
|
package com.supervision.util;
|
||||||
|
|
||||||
|
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.type.TypeReference;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.supervision.dto.paddlespeech.req.AsrReqDTO;
|
||||||
|
import com.supervision.dto.paddlespeech.res.AsrResultDTO;
|
||||||
|
import com.supervision.dto.paddlespeech.res.PaddleSpeechResDTO;
|
||||||
|
import com.supervision.exception.BusinessException;
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
|
|
||||||
|
public class AsrUtil {
|
||||||
|
|
||||||
|
private static final String ASR_URL = SpringBeanUtil.getBean(Environment.class).getProperty("paddle-speech.asr");
|
||||||
|
|
||||||
|
private static final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 语音转文字
|
||||||
|
*/
|
||||||
|
public static String asrTransformByBytes(byte[] bytes) {
|
||||||
|
// 首先编码为base64编码
|
||||||
|
String encode = Base64.encode(bytes);
|
||||||
|
return asrTransformByBytes(encode);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String asrTransformByBytes(String voiceBase64){
|
||||||
|
// 这里调用Python的接口,将文字转换为语音
|
||||||
|
String post = HttpUtil.post(ASR_URL, JSONUtil.toJsonStr(new AsrReqDTO(voiceBase64)));
|
||||||
|
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("语音转换文字失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,35 @@
|
|||||||
|
package com.supervision.util;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
|
import cn.hutool.http.HttpUtil;
|
||||||
|
import cn.hutool.json.JSONUtil;
|
||||||
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.supervision.dto.paddlespeech.req.TtsReqDTO;
|
||||||
|
import com.supervision.dto.paddlespeech.res.PaddleSpeechResDTO;
|
||||||
|
import com.supervision.dto.paddlespeech.res.TtsResultDTO;
|
||||||
|
import com.supervision.exception.BusinessException;
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
|
|
||||||
|
public class TtsUtil {
|
||||||
|
|
||||||
|
private static final String TTS_URL = SpringBeanUtil.getBean(Environment.class).getProperty("paddle-speech.tts");
|
||||||
|
|
||||||
|
private static final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
|
||||||
|
public static TtsResultDTO ttsTransform(String str) {
|
||||||
|
// 构建
|
||||||
|
String post = HttpUtil.post(TTS_URL, 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("文字转换语音失败");
|
||||||
|
}
|
||||||
|
return response.getResult();
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new BusinessException("语音转换文字失败", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue