Просмотр исходного кода

优化音色克隆相关代码

zyy 1 день назад
Родитель
Сommit
cc6b30c902

+ 28 - 2
fs-company/src/main/java/com/fs/company/controller/company/EasyCallController.java

@@ -1,11 +1,14 @@
 package com.fs.company.controller.company;
 
 import com.fs.aicall.domain.CcLlmAgentAccount;
+import com.fs.aicall.domain.CcTtsAliyun;
 import com.fs.aicall.service.ICcLlmAgentAccountService;
 import com.fs.aicall.service.ICompanyBindAiModelService;
 import com.fs.common.core.controller.BaseController;
 import com.fs.common.core.domain.R;
 import com.fs.common.utils.ServletUtils;
+import com.fs.company.mapper.CompanyVoiceCloneRefMapper;
+import com.fs.company.service.ICompanyVoiceCloneService;
 import com.fs.company.service.easycall.IEasyCallService;
 import com.fs.company.vo.easycall.*;
 import com.fs.framework.security.LoginUser;
@@ -18,6 +21,7 @@ import org.springframework.web.bind.annotation.*;
 import java.util.ArrayList;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.stream.Collectors;
 
 /**
  * @author MixLiu
@@ -46,6 +50,11 @@ public class EasyCallController extends BaseController {
     @Autowired
     private ICcLlmAgentAccountService ccLlmAgentAccountService;
 
+    @Autowired
+    private CompanyVoiceCloneRefMapper companyVoiceCloneRefMapper;
+
+
+
     // =================== 基础数据查询 ===================
 
     /**
@@ -94,8 +103,25 @@ public class EasyCallController extends BaseController {
     public R getVoiceCodeList() {
         LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
         Long companyId = loginUser.getUser().getCompanyId();
-        List<EasyCallVoiceCodeVO> list = easyCallService.getVoiceCodeList(companyId);
-        return R.ok().put("data", list);
+
+        List<Long> ttsIds = companyVoiceCloneRefMapper.selectByCompanyIdAndCompanyUserId(companyId, loginUser.getCompany().getUserId());
+        List<CcTtsAliyun> ccTtsAliyuns = companyVoiceCloneRefMapper.selectCcTtsAliyunList();
+
+        List<EasyCallVoiceCodeVO> result = ccTtsAliyuns.stream()
+                .filter(item ->
+                        item.getPriority() == 1 || (item.getPriority() == 0 && ttsIds.contains(item.getId()))
+                )
+                .map(item -> {
+                    EasyCallVoiceCodeVO vo = new EasyCallVoiceCodeVO();
+                    vo.setVoiceCode(item.getVoiceCode());
+                    vo.setVoiceName(item.getVoiceName());
+                    vo.setVoiceSource(item.getVoiceSource());
+                    return vo;
+                })
+                .collect(Collectors.toList());
+
+//        List<EasyCallVoiceCodeVO> list = easyCallService.getVoiceCodeList(companyId);
+        return R.ok().put("data", result);
     }
 
     /**

+ 4 - 0
fs-service/src/main/java/com/fs/company/domain/CompanyVoiceCloneRef.java

@@ -8,6 +8,10 @@ public class CompanyVoiceCloneRef {
 
     private Long id;
 
+    private String voiceName;
+
+    private String voiceCode;
+
     /** 公司ID */
     private Long companyId;
 

+ 18 - 0
fs-service/src/main/java/com/fs/company/mapper/CompanyVoiceCloneRefMapper.java

@@ -1,8 +1,13 @@
 package com.fs.company.mapper;
 
+import com.fs.aicall.domain.CcTtsAliyun;
+import com.fs.common.annotation.DataSource;
+import com.fs.common.enums.DataSourceType;
 import com.fs.company.domain.CompanyVoiceCloneRef;
 import org.apache.ibatis.annotations.Param;
 
+import java.util.List;
+
 public interface CompanyVoiceCloneRefMapper {
 
     CompanyVoiceCloneRef selectByCompanyIdAndTtsId(@Param("companyId") Long companyId,
@@ -12,4 +17,17 @@ public interface CompanyVoiceCloneRefMapper {
     int insertCompanyVoiceCloneRef(CompanyVoiceCloneRef ref);
 
     int updateCompanyVoiceCloneRef(CompanyVoiceCloneRef ref);
+
+    List<Long> selectByCompanyIdAndCompanyUserId(@Param("companyId") Long companyId,
+                                   @Param("companyUserId") Long companyUserId);
+
+
+    @DataSource(DataSourceType.EASYCALL)
+    List<CcTtsAliyun> selectCcTtsAliyunList();
+
+
+
+
+
+
 }

+ 17 - 0
fs-service/src/main/java/com/fs/company/service/ICompanyVoiceCloneRefService.java

@@ -0,0 +1,17 @@
+package com.fs.company.service;
+
+import com.fs.common.core.domain.AjaxResult;
+import com.fs.company.vo.easycall.EasyCallVoiceCodeVO;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.util.List;
+
+/**
+ * 豆包声音克隆 Service 接口
+ *
+ * @author fs
+ */
+public interface ICompanyVoiceCloneRefService {
+
+    List<EasyCallVoiceCodeVO> getVoiceCodeList(Long companyId);
+}

+ 50 - 0
fs-service/src/main/java/com/fs/company/service/impl/CompanyVoiceCloneRefServiceImpl.java

@@ -0,0 +1,50 @@
+package com.fs.company.service.impl;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import com.fs.aicall.domain.CcTtsAliyun;
+import com.fs.aicall.service.ICcParamsService;
+import com.fs.aicall.service.ICcTtsAliyunService;
+import com.fs.common.core.domain.AjaxResult;
+import com.fs.common.utils.StringUtils;
+import com.fs.company.domain.CompanyVoiceCloneRef;
+import com.fs.company.mapper.CompanyVoiceCloneRefMapper;
+import com.fs.company.service.ICompanyVoiceCloneRefService;
+import com.fs.company.service.ICompanyVoiceCloneService;
+import com.fs.company.vo.easycall.EasyCallVoiceCodeVO;
+import lombok.extern.slf4j.Slf4j;
+import okhttp3.*;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.IOException;
+import java.util.*;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * 豆包声音克隆 Service 实现
+ * <p>
+ * 参照 DoubaoVclController 实现:
+ * 1. uploadAndTrain: 从 EASYCALL 读取账号,HTTP 调用豆包训练接口,成功后写入音色表
+ * 2. doubaoTtsTest: HTTP 调用豆包 TTS 接口,返回 base64 音频数据
+ * </p>
+ *
+ * @author fs
+ */
+@Service
+@Slf4j
+public class CompanyVoiceCloneRefServiceImpl implements ICompanyVoiceCloneRefService {
+
+    @Autowired
+    private CompanyVoiceCloneRefMapper companyVoiceCloneRefMapper;
+
+    @Override
+    public List<EasyCallVoiceCodeVO> getVoiceCodeList(Long companyId) {
+
+        return Collections.emptyList();
+    }
+
+
+}

+ 4 - 2
fs-service/src/main/java/com/fs/company/service/impl/CompanyVoiceCloneServiceImpl.java

@@ -133,7 +133,7 @@ public class CompanyVoiceCloneServiceImpl implements ICompanyVoiceCloneService {
                 if (statusCode == 0) {
                     // 训练成功,写入音色表
                     CcTtsAliyun ttsAliyun = addSpeakerId(voiceName, speakerId);
-                    addCompanyVoiceRelation(companyId, ttsAliyun.getId(), companyUserId);
+                    addCompanyVoiceRelation(ttsAliyun.getVoiceCode(),voiceName,companyId, ttsAliyun.getId(), companyUserId);
 
                     // 查询训练状态
                     boolean modelReady = getStatus(appid, token, speakerId);
@@ -400,7 +400,7 @@ public class CompanyVoiceCloneServiceImpl implements ICompanyVoiceCloneService {
                 .build();
     }
 
-    private synchronized void addCompanyVoiceRelation(Long companyId, Integer ttsId,Long companyUserId) {
+    private synchronized void addCompanyVoiceRelation(String code,String name,Long companyId, Integer ttsId,Long companyUserId) {
         if (companyId == null) {
             throw new RuntimeException("companyId为空,无法保存公司音色关联");
         }
@@ -412,6 +412,8 @@ public class CompanyVoiceCloneServiceImpl implements ICompanyVoiceCloneService {
             CompanyVoiceCloneRef ref = companyVoiceCloneRefMapper.selectByCompanyIdAndTtsId(companyId ,companyUserId, ttsId);
             if (ref == null) {
                 ref = new CompanyVoiceCloneRef();
+                ref.setVoiceName(name);
+                ref.setVoiceCode(code);
                 ref.setCompanyId(companyId);
                 ref.setCompanyUserId(companyUserId);
                 ref.setTtsId(ttsId);

+ 33 - 1
fs-service/src/main/resources/mapper/company/CompanyVoiceCloneRefMapper.xml

@@ -8,6 +8,8 @@
         <id property="id" column="id"/>
         <result property="companyId" column="company_id"/>
         <result property="companyUserId" column="company_user_id"/>
+        <result property="voiceName" column="voice_name"/>
+        <result property="voiceCode" column="voice_code"/>
         <result property="ttsId" column="tts_id"/>
         <result property="status" column="status"/>
         <result property="createBy" column="create_by"/>
@@ -18,18 +20,46 @@
     </resultMap>
 
     <select id="selectByCompanyIdAndTtsId" resultMap="CompanyVoiceCloneRefResult">
-        select id, company_id,company_user_id, tts_id, status, create_by, create_time, update_by, update_time, remark
+        select id,voice_name,voice_code, company_id,company_user_id, tts_id, status, create_by, create_time, update_by, update_time, remark
         from company_voice_clone_ref
         where company_id = #{companyId}
           and company_user_id = #{companyUserId}
           and tts_id = #{ttsId}
             limit 1
     </select>
+    <select id="selectByCompanyIdAndCompanyUserId" resultType="java.lang.Long">
+        select tts_id
+        from company_voice_clone_ref
+        where company_id = #{companyId}
+          and company_user_id = #{companyUserId}
+            limit 1
+    </select>
+
+    <sql id="selectCcTtsAliyunVo">
+        select id, voice_name, voice_code, voice_enabled, voice_source, priority, provider from cc_tts_aliyun
+    </sql>
+
+    <select id="selectCcTtsAliyunList" resultType="com.fs.aicall.domain.CcTtsAliyun">
+        <include refid="selectCcTtsAliyunVo"/>
+        <where>
+            <if test="voiceName != null  and voiceName != ''"> and voice_name like concat('%', #{voiceName}, '%')</if>
+            <if test="voiceCode != null  and voiceCode != ''"> and voice_code = #{voiceCode}</if>
+            <if test="voiceEnabled != null "> and voice_enabled = #{voiceEnabled}</if>
+            <if test="voiceSource != null  and voiceSource != ''"> and voice_source = #{voiceSource}</if>
+            <if test="priority != null "> and priority = #{priority}</if>
+            <if test="provider != null  and provider != ''"> and provider = #{provider}</if>
+        </where>
+        order by priority asc
+    </select>
+
+
 
     <insert id="insertCompanyVoiceCloneRef" parameterType="com.fs.company.domain.CompanyVoiceCloneRef"
             useGeneratedKeys="true" keyProperty="id">
         insert into company_voice_clone_ref
         (
+            voice_name,
+            voice_code,
             company_id,
             company_user_id,
             tts_id,
@@ -42,6 +72,8 @@
         )
         values
             (
+                #{voiceName},
+                #{voiceCode},
                 #{companyId},
                 #{companyUserId},
                 #{ttsId},