From c977e4c90d896d7d3e8a8ec0b7c8e57037c69181 Mon Sep 17 00:00:00 2001 From: joylink_zhangsai <1021828630@qq.com> Date: Fri, 5 Feb 2021 18:06:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E7=94=A8=E5=8D=8E=E4=B8=BA=E8=AF=AD?= =?UTF-8?q?=E9=9F=B3=E8=AF=86=E5=88=AB=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../joylink/rtss/services/IVoiceService.java | 7 +++- .../voice/baidu/VoiceServiceImpl.java | 41 +++++++++++++++++++ .../ConversationManagerService.java | 7 ++-- 3 files changed, 50 insertions(+), 5 deletions(-) diff --git a/src/main/java/club/joylink/rtss/services/IVoiceService.java b/src/main/java/club/joylink/rtss/services/IVoiceService.java index 2c6f27cd4..878853b21 100644 --- a/src/main/java/club/joylink/rtss/services/IVoiceService.java +++ b/src/main/java/club/joylink/rtss/services/IVoiceService.java @@ -80,7 +80,7 @@ public interface IVoiceService { os.close(); } if (saveFile != null) { - saveFile.delete(); +// saveFile.delete(); } } } @@ -106,6 +106,11 @@ public interface IVoiceService { */ String synthesis(String message, String per); + /** + * 华为语音识别 + */ + VoiceRecognitionResult huaweiVoiceRecognition(MultipartFile file, String lang); + @Getter @Setter @NoArgsConstructor diff --git a/src/main/java/club/joylink/rtss/services/voice/baidu/VoiceServiceImpl.java b/src/main/java/club/joylink/rtss/services/voice/baidu/VoiceServiceImpl.java index ff6f30577..540cad9d8 100644 --- a/src/main/java/club/joylink/rtss/services/voice/baidu/VoiceServiceImpl.java +++ b/src/main/java/club/joylink/rtss/services/voice/baidu/VoiceServiceImpl.java @@ -3,6 +3,12 @@ package club.joylink.rtss.services.voice.baidu; import club.joylink.rtss.exception.BusinessExceptionAssertEnum; import club.joylink.rtss.services.IVoiceService; import club.joylink.rtss.vo.client.VoiceRecognitionResult; +import com.huawei.sis.bean.AuthInfo; +import com.huawei.sis.bean.SisConfig; +import com.huawei.sis.bean.request.AsrCustomShortRequest; +import com.huawei.sis.bean.response.AsrCustomShortResponse; +import com.huawei.sis.client.AsrCustomizationClient; +import com.huawei.sis.exception.SisException; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -12,11 +18,20 @@ import org.springframework.web.multipart.MultipartFile; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; +import java.util.Base64; @Slf4j @Service("baiDuVoiceService") public class VoiceServiceImpl implements IVoiceService { + /** + * 华为语音识别配置 + */ + private final String ak = "YDUXTXRYGAHGPHAIXZCU"; + private final String sk = "Kcbm3sTDCYEou8kGeAhKxfBkgWybIn6IjJyGBX3p"; + private final String region = "cn-north-4"; + private final String projectId = "0aada8176180f28c2f34c0196f5394e8"; + @Autowired private AsrService asrService; @@ -50,4 +65,30 @@ public class VoiceServiceImpl implements IVoiceService { } } + @Override + public VoiceRecognitionResult huaweiVoiceRecognition(MultipartFile file, String lang) { + String filePath; + try { + filePath = IVoiceService.handleAndSaveFile(file); + } catch (IOException e) { + throw BusinessExceptionAssertEnum.SYSTEM_EXCEPTION.exception("语音文件上传失败", e); + } + AuthInfo authInfo = new AuthInfo(ak, sk, region, projectId); + SisConfig sisConfig = new SisConfig(); + AsrCustomizationClient client = new AsrCustomizationClient(authInfo, sisConfig); + String data; + try { + data = Base64.getEncoder().encodeToString(file.getBytes()); + } catch (IOException e) { + throw BusinessExceptionAssertEnum.SYSTEM_EXCEPTION.exception("语音文件编码失败", e); + } + try { + AsrCustomShortRequest request = new AsrCustomShortRequest(data, "pcm16k16bit", "chinese_16k_common"); + AsrCustomShortResponse response = client.getAsrShortResponse(request); + return new VoiceRecognitionResult(filePath, response.getResult().getText()); + } catch (SisException e) { + throw BusinessExceptionAssertEnum.SYSTEM_EXCEPTION.exception("语音识别失败", e); + } + } + } diff --git a/src/main/java/club/joylink/rtss/simulation/cbtc/conversation/ConversationManagerService.java b/src/main/java/club/joylink/rtss/simulation/cbtc/conversation/ConversationManagerService.java index cf70f80bf..472e0d610 100644 --- a/src/main/java/club/joylink/rtss/simulation/cbtc/conversation/ConversationManagerService.java +++ b/src/main/java/club/joylink/rtss/simulation/cbtc/conversation/ConversationManagerService.java @@ -1,16 +1,15 @@ package club.joylink.rtss.simulation.cbtc.conversation; +import club.joylink.rtss.services.IVoiceService; import club.joylink.rtss.simulation.cbtc.GroupSimulationCache; import club.joylink.rtss.simulation.cbtc.Simulation; -import club.joylink.rtss.simulation.cbtc.data.SimulationDataRepository; +import club.joylink.rtss.simulation.cbtc.competition.ScriptExecuteService; import club.joylink.rtss.simulation.cbtc.data.vo.ConversationVO; import club.joylink.rtss.simulation.cbtc.event.*; import club.joylink.rtss.simulation.cbtc.exception.SimulationException; import club.joylink.rtss.simulation.cbtc.exception.SimulationExceptionType; import club.joylink.rtss.simulation.cbtc.member.SimulationMember; import club.joylink.rtss.simulation.cbtc.member.SimulationUser; -import club.joylink.rtss.services.IVoiceService; -import club.joylink.rtss.simulation.cbtc.competition.ScriptExecuteService; import club.joylink.rtss.vo.UserVO; import club.joylink.rtss.vo.client.VoiceRecognitionResult; import lombok.extern.slf4j.Slf4j; @@ -256,7 +255,7 @@ public class ConversationManagerService { Simulation simulation = this.groupSimulationCache.getSimulationByGroup(group); Conversation conversation = simulation.getSimulationConversationById(conversationId); SimulationMember member = simulation.getSimulationMemberByUserId(userVO.getId()); - VoiceRecognitionResult recognitionResult = this.iVoiceService.voiceRecognition(file, ""); + VoiceRecognitionResult recognitionResult = this.iVoiceService.huaweiVoiceRecognition(file, ""); String upperCaseResult = recognitionResult.getResult().toUpperCase(); String handledContent = this.simulationVoiceHandler.handle(upperCaseResult); this.chat(simulation, conversation, member, handledContent, recognitionResult.getFilePath());