| | |
| | | |
| | | import cn.hutool.core.lang.UUID; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.hutool.http.HttpRequest; |
| | | import cn.hutool.http.HttpResponse; |
| | | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| | | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| | | import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils; |
| | | import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils; |
| | | import cn.iocoder.yudao.module.digitalcourse.controller.admin.voices.vo.VoicesPageReqVO; |
| | | import cn.iocoder.yudao.module.digitalcourse.controller.admin.voices.vo.VoicesSaveReqVO; |
| | | import cn.iocoder.yudao.module.digitalcourse.controller.admin.voices.vo.VoicesTrailVO; |
| | | import cn.iocoder.yudao.module.digitalcourse.dal.dataobject.voices.AuditionDO; |
| | | import cn.iocoder.yudao.module.digitalcourse.dal.dataobject.voices.TTSDTO; |
| | | import cn.iocoder.yudao.module.digitalcourse.dal.dataobject.voices.VoicesDO; |
| | | import cn.iocoder.yudao.module.digitalcourse.dal.dataobject.digitalhumans.DigitalHumansDO; |
| | | import cn.iocoder.yudao.module.digitalcourse.dal.dataobject.voices.*; |
| | | import cn.iocoder.yudao.module.digitalcourse.dal.mysql.digitalhumans.DigitalHumansMapper; |
| | | import cn.iocoder.yudao.module.digitalcourse.dal.mysql.voices.VoicesMapper; |
| | | import cn.iocoder.yudao.module.infra.api.config.ConfigApi; |
| | | import cn.iocoder.yudao.module.infra.api.file.FileApi; |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.fasterxml.jackson.databind.JsonNode; |
| | | import com.fasterxml.jackson.core.JsonProcessingException; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import jakarta.annotation.Resource; |
| | | import org.apache.commons.codec.binary.Base64; |
| | | import org.apache.hc.client5.http.classic.methods.HttpPost; |
| | | import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; |
| | | import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; |
| | | import org.apache.hc.client5.http.impl.classic.HttpClients; |
| | | import org.apache.hc.core5.http.ContentType; |
| | | import org.apache.hc.core5.http.ParseException; |
| | | import org.apache.hc.core5.http.io.entity.EntityUtils; |
| | | import org.apache.hc.core5.http.io.entity.StringEntity; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.validation.annotation.Validated; |
| | | |
| | | import java.io.IOException; |
| | | |
| | | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | import static cn.iocoder.yudao.module.digitalcourse.enums.ErrorCodeConstants.VOICES_NOT_EXISTS; |
| | |
| | | }else { |
| | | pageReqVO.setVoiceType(0); |
| | | } |
| | | |
| | | |
| | | return voicesMapper.selectPage(pageReqVO); |
| | | } |
| | | @Override |
| | |
| | | return (auditing == null || auditing == 0); |
| | | } |
| | | |
| | | @Resource |
| | | private DigitalHumansMapper digitalHumansMapper; |
| | | private static final String EASEGEN_URL = "easegen.url"; |
| | | @Override |
| | | public String audition(AuditionDO auditionDO) { |
| | | VoicesDO voice = voicesMapper.selectById(auditionDO.getVoiceId()); |
| | | TTSDTO ttsdto = new TTSDTO(); |
| | | if (voice != null) { |
| | | ttsdto.setModel_code(voice.getCode()); |
| | | ttsdto.setSentence(auditionDO.getText()); |
| | | ttsdto.setRequest_id(StrUtil.uuid()); |
| | | ttsdto.setUser_id(String.valueOf(SecurityFrameworkUtils.getLoginUser().getId())); |
| | | ttsdto.setVoice_type(String.valueOf(voice.getVoiceType())); |
| | | ttsdto.setPitch(auditionDO.getPitch()); |
| | | ttsdto.setSpeed(auditionDO.getSpeed()); |
| | | ttsdto.setVolume(auditionDO.getVolume()); |
| | | } else { |
| | | throw exception(VOICES_NOT_EXISTS); |
| | | public String audition(AuditionVO auditionVO) { |
| | | DigitalHumansDO digitalHumansDO = digitalHumansMapper.selectById(auditionVO.getHumanId()); |
| | | InvokeVO invokeVO = new InvokeVO(); |
| | | invokeVO.setSpeaker(InvokeVO.generateUUID()); |
| | | invokeVO.setReferenceText(digitalHumansDO.getReferenceAudioText()); |
| | | invokeVO.setReferenceAudio(digitalHumansDO.getAsrFormatAudioUrl()); |
| | | invokeVO.setText(auditionVO.getText()); |
| | | ObjectMapper objectMapper = new ObjectMapper(); |
| | | String jsonString = null; |
| | | try { |
| | | jsonString = objectMapper.writeValueAsString(invokeVO); |
| | | } catch (JsonProcessingException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | HttpResponse execute = HttpRequest.post("http://192.168.3.161:18180/v1/invoke") |
| | | .body(jsonString) |
| | | .execute(); |
| | | |
| | | if (execute.getStatus() != 200) { |
| | | return null; |
| | | } |
| | | |
| | | String apiUrl = configApi.getConfigValueByKey(EASEGEN_CORE_URL)+"/api/tts"; |
| | | String apiKey = configApi.getConfigValueByKey(EASEGEN_CORE_KEY); |
| | | // å建HTTP客æ·ç«¯ |
| | | try (CloseableHttpClient httpClient = HttpClients.createDefault()) { |
| | | // å建POSTè¯·æ± |
| | | HttpPost httpPost = new HttpPost(apiUrl); |
| | | httpPost.setHeader("Content-Type", "application/json"); |
| | | httpPost.setHeader("X-API-Key", apiKey); |
| | | httpPost.setEntity(new StringEntity(JSON.toJSONString(ttsdto), ContentType.APPLICATION_JSON)); |
| | | // æ§è¡è¯·æ± |
| | | try (CloseableHttpResponse response = httpClient.execute(httpPost)) { |
| | | int statusCode = response.getCode(); |
| | | String responseString = EntityUtils.toString(response.getEntity()); |
| | | if (statusCode == 200) { |
| | | // è§£æJSONååº |
| | | ObjectMapper objectMapper = new ObjectMapper(); |
| | | JsonNode jsonResponse = objectMapper.readTree(responseString); |
| | | String audioBase64 = jsonResponse.get("audio").asText(); |
| | | // è§£ç Base64é³é¢å符串 |
| | | byte[] audioBytes = Base64.decodeBase64(audioBase64); |
| | | // ä¿åé³é¢æä»¶ |
| | | // æå®æä»¶è·¯å¾ï¼ä½ å¯ä»¥æ ¹æ®å®é
æ
åµè®¾ç½®ï¼ |
| | | String fileName = StrUtil.uuid() + ".wav"; |
| | | String filePath = "temp/wav/" + fileName; |
| | | return fileApi.createFile(fileName, filePath, audioBytes); |
| | | // try (FileOutputStream fos = new FileOutputStream("output.wav")) { |
| | | // fos.write(audioBytes); |
| | | // String path = fileApi.createFile("output.wav", null, audioBytes); |
| | | // return path; |
| | | // } |
| | | } else { |
| | | System.out.println("Error: " + responseString); |
| | | } |
| | | } |
| | | } catch (IOException | ParseException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return null; |
| | | String fileName = UUID.randomUUID().toString() + ".wav"; |
| | | |
| | | // è·åé³é¢æä»¶çäºè¿å¶æ°æ® |
| | | byte[] content = execute.bodyBytes(); |
| | | |
| | | // ä½¿ç¨ `createFile` æ¹æ³å卿件ï¼å¹¶è·å URL |
| | | String fileUrl = fileApi.createFile(fileName, null, content); |
| | | |
| | | return fileUrl; // è¿ååå¨çæä»¶ URL |
| | | // è¿åé³é¢æä»¶è·¯å¾ |
| | | |
| | | |
| | | } |
| | | |
| | | } |
| | | } |