添加paddle 工具

main
xueqingkun 2 months ago
parent f0ccce3626
commit beb6ce57dd

@ -54,22 +54,15 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.26</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>

@ -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,127 @@
package com.supervision.util;
import cn.hutool.core.util.ArrayUtil;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import java.util.Map;
/***
* @author ljt
* @since 2020/8/4 17:37
*
*/
@Component
@Lazy(false)
public class SpringBeanUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
/**
* applicationContext
*
* @return ApplicationContext
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
SpringBeanUtil.applicationContext = applicationContext;
}
//通过name获取 Bean.
/**
* name Bean
*
* @param <T> Bean
* @param name Bean
* @return Bean
*/
@SuppressWarnings("unchecked")
public static <T> T getBean(String name) {
return (T) applicationContext.getBean(name);
}
/**
* classBean
*
* @param <T> Bean
* @param clazz Bean
* @return Bean
*/
public static <T> T getBean(Class<T> clazz) {
return applicationContext.getBean(clazz);
}
/**
* name,ClazzBean
*
* @param <T> bean
* @param name Bean
* @param clazz bean
* @return Bean
*/
public static <T> T getBean(String name, Class<T> clazz) {
return applicationContext.getBean(name, clazz);
}
/**
* Bean
*
* @param <T> Bean
* @param type nullbean
* @return beankeybeannamevalueBean
* @since 5.3.3
*/
public static <T> Map<String, T> getBeansOfType(Class<T> type) {
return applicationContext.getBeansOfType(type);
}
/**
* Bean
*
* @param type nullbean
* @return bean
* @since 5.3.3
*/
public static String[] getBeanNamesForType(Class<?> type) {
return applicationContext.getBeanNamesForType(type);
}
/**
*
*
* @param key key
* @return
* @since 5.3.3
*/
public static String getProperty(String key) {
return applicationContext.getEnvironment().getProperty(key);
}
/**
* null
*
* @return
* @since 5.3.3
*/
public static String[] getActiveProfiles() {
return applicationContext.getEnvironment().getActiveProfiles();
}
/**
*
*
* @return
* @since 5.3.3
*/
public static String getActiveProfile() {
final String[] activeProfiles = getActiveProfiles();
return ArrayUtil.isNotEmpty(activeProfiles) ? activeProfiles[0] : null;
}
}

@ -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

@ -1,6 +1,12 @@
package com.supervision;
import cn.hutool.json.JSONUtil;
import com.supervision.dto.paddlespeech.res.TtsResultDTO;
import com.supervision.util.AsrUtil;
import com.supervision.util.TtsUtil;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
@ -10,4 +16,21 @@ class SpeechDemoServiceApplicationTests {
void contextLoads() {
}
@Test
void testTtsTransform() {
TtsResultDTO ttsResultDTO = TtsUtil.ttsTransform("你好,我是小爱同学");
System.out.println(JSONUtil.toJsonStr(ttsResultDTO));
}
@Value("${paddle-speech.text:xx}")
private String voiceBase64;
@Test
void testAsrTransform2() {
String text = AsrUtil.asrTransformByBytes(voiceBase64);
System.out.println(text);
}
}

Loading…
Cancel
Save