Merge remote-tracking branch 'origin/main'
commit
55985907dc
@ -0,0 +1,33 @@
|
||||
package com.supervision.controller;
|
||||
|
||||
import com.supervision.service.VoiceService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@Slf4j
|
||||
@Api(tags = "音频处理")
|
||||
@RequestMapping("voice")
|
||||
@RequiredArgsConstructor
|
||||
public class VoiceController {
|
||||
|
||||
private final VoiceService voiceService;
|
||||
|
||||
@ApiOperation("语音转文字")
|
||||
@PostMapping("voiceToText")
|
||||
public String voiceToText(MultipartFile file, String sessionId) throws IOException {
|
||||
return voiceService.voiceToText(file,sessionId);
|
||||
}
|
||||
|
||||
@ApiOperation("文字转语音")
|
||||
@PostMapping("textToVoice")
|
||||
public String textToVoice(String text){
|
||||
return voiceService.textToVoice(text);
|
||||
}
|
||||
}
|
@ -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,23 @@
|
||||
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;
|
||||
|
||||
private String audio;
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.supervision.service;
|
||||
|
||||
import com.supervision.vo.voice.VoiceReqVO;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public interface VoiceService {
|
||||
|
||||
String voiceToText(MultipartFile file, String sessionId) throws IOException;
|
||||
|
||||
String textToVoice(String text);
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package com.supervision.service.impl;
|
||||
|
||||
import cn.hutool.core.date.DateTime;
|
||||
import cn.hutool.core.date.DateUnit;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import com.supervision.service.VoiceService;
|
||||
import com.supervision.util.AsrUtil;
|
||||
import com.supervision.util.TtsUtil;
|
||||
import com.supervision.vo.voice.VoiceReqVO;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class VoiceServiceImpl implements VoiceService {
|
||||
|
||||
@Override
|
||||
public String voiceToText(MultipartFile file, String sessionId) throws IOException {
|
||||
log.info("语音转文字开始");
|
||||
return AsrUtil.asrTransformByBytes(file.getBytes());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String textToVoice(String text) {
|
||||
log.info("文字转语音开始");
|
||||
return TtsUtil.ttsTransform(text);
|
||||
}
|
||||
}
|
@ -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 String 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().getAudio();
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException("语音转换文字失败", e);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package com.supervision.vo.voice;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class VoiceReqVO {
|
||||
|
||||
private String voiceBase64;
|
||||
|
||||
private String sessionId;
|
||||
}
|
Loading…
Reference in New Issue