Explorar el Código

音色克隆相关代码

zyy hace 1 día
padre
commit
dd934a6d95

+ 8 - 1
fs-company/src/main/java/com/fs/company/controller/company/CompanyVoiceCloneController.java

@@ -4,7 +4,10 @@ import com.fs.common.annotation.Log;
 import com.fs.common.core.controller.BaseController;
 import com.fs.common.core.domain.AjaxResult;
 import com.fs.common.enums.BusinessType;
+import com.fs.common.utils.ServletUtils;
 import com.fs.company.service.ICompanyVoiceCloneService;
+import com.fs.framework.security.LoginUser;
+import com.fs.framework.service.TokenService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
@@ -20,6 +23,8 @@ public class CompanyVoiceCloneController extends BaseController {
 
     @Autowired
     private ICompanyVoiceCloneService companyVoiceCloneService;
+    @Autowired
+    private TokenService tokenService;
 
     /**
      * 上传音频文件并训练声音克隆音色
@@ -38,7 +43,9 @@ public class CompanyVoiceCloneController extends BaseController {
             @RequestParam("speaker_id") String speakerId,
             @RequestParam(value = "language", defaultValue = "0") Integer language,
             @RequestParam(value = "model_type", defaultValue = "2") Integer modelType) {
-        return companyVoiceCloneService.uploadAndTrain(voiceName, speakerId, language, modelType, file);
+        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
+        return companyVoiceCloneService.uploadAndTrain(voiceName, speakerId, language, modelType, file,loginUser.getCompany().getCompanyId(),
+                loginUser.getCompany().getUserId());
     }
 
     /**

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

@@ -0,0 +1,37 @@
+package com.fs.company.domain;
+
+import lombok.Data;
+import java.util.Date;
+
+@Data
+public class CompanyVoiceCloneRef {
+
+    private Long id;
+
+    /** 公司ID */
+    private Long companyId;
+
+    /** 公司ID */
+    private Long companyUserId;
+
+    /** 音色ID,对应 cc_tts_aliyun.id */
+    private Integer ttsId;
+
+    /** 状态 */
+    private Integer status;
+
+    /** 创建人 */
+    private String createBy;
+
+    /** 创建时间 */
+    private Date createTime;
+
+    /** 更新人 */
+    private String updateBy;
+
+    /** 更新时间 */
+    private Date updateTime;
+
+    /** 备注 */
+    private String remark;
+}

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

@@ -0,0 +1,15 @@
+package com.fs.company.mapper;
+
+import com.fs.company.domain.CompanyVoiceCloneRef;
+import org.apache.ibatis.annotations.Param;
+
+public interface CompanyVoiceCloneRefMapper {
+
+    CompanyVoiceCloneRef selectByCompanyIdAndTtsId(@Param("companyId") Long companyId,
+                                                   @Param("companyUserId") Long companyUserId,
+                                                   @Param("ttsId") Integer ttsId);
+
+    int insertCompanyVoiceCloneRef(CompanyVoiceCloneRef ref);
+
+    int updateCompanyVoiceCloneRef(CompanyVoiceCloneRef ref);
+}

+ 1 - 1
fs-service/src/main/java/com/fs/company/service/ICompanyVoiceCloneService.java

@@ -22,7 +22,7 @@ public interface ICompanyVoiceCloneService {
      */
     AjaxResult uploadAndTrain(String voiceName, String speakerId,
                               Integer language, Integer modelType,
-                              MultipartFile file);
+                              MultipartFile file,Long companyId,Long companyUserId);
 
     /**
      * TTS 语音合成测试

+ 45 - 3
fs-service/src/main/java/com/fs/company/service/impl/CompanyVoiceCloneServiceImpl.java

@@ -8,6 +8,8 @@ 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.ICompanyVoiceCloneService;
 import lombok.extern.slf4j.Slf4j;
 import okhttp3.*;
@@ -72,6 +74,9 @@ public class CompanyVoiceCloneServiceImpl implements ICompanyVoiceCloneService {
     @Autowired
     private ICcTtsAliyunService ccTtsAliyunService;
 
+    @Autowired
+    private CompanyVoiceCloneRefMapper companyVoiceCloneRefMapper;
+
     /**
      * 上传并训练
      * @param voiceName 音色名称
@@ -84,7 +89,7 @@ public class CompanyVoiceCloneServiceImpl implements ICompanyVoiceCloneService {
     @Override
     public AjaxResult uploadAndTrain(String voiceName, String speakerId,
                                      Integer language, Integer modelType,
-                                     MultipartFile file) {
+                                     MultipartFile file,Long companyId,Long companyUserId) {
         if (file == null || file.isEmpty()) {
             return AjaxResult.error("请选择音频文件");
         }
@@ -127,7 +132,8 @@ public class CompanyVoiceCloneServiceImpl implements ICompanyVoiceCloneService {
 
                 if (statusCode == 0) {
                     // 训练成功,写入音色表
-                    addSpeakerId(voiceName, speakerId);
+                    CcTtsAliyun ttsAliyun = addSpeakerId(voiceName, speakerId);
+                    addCompanyVoiceRelation(companyId, ttsAliyun.getId(), companyUserId);
 
                     // 查询训练状态
                     boolean modelReady = getStatus(appid, token, speakerId);
@@ -260,7 +266,7 @@ public class CompanyVoiceCloneServiceImpl implements ICompanyVoiceCloneService {
      * @param nameParam
      * @param speakerId
      */
-    private synchronized void addSpeakerId(String nameParam, String speakerId) {
+    private synchronized CcTtsAliyun addSpeakerId(String nameParam, String speakerId) {
         CcTtsAliyun ttsSpeaker = ccTtsAliyunService.selectCcTtsAliyunByVoiceCode(speakerId);
         String name = nameParam.replace("'", "").replace(" ", "");
         if (name.length() > 20) {
@@ -287,9 +293,14 @@ public class CompanyVoiceCloneServiceImpl implements ICompanyVoiceCloneService {
             } else {
                 ccTtsAliyunService.insertCcTtsAliyun(ttsSpeaker);
             }
+
+            // 重新查一次,确保拿到最新id
+            CcTtsAliyun saved = ccTtsAliyunService.selectCcTtsAliyunByVoiceCode(speakerId);
             log.info("save doubaovcl speakerId succeed. {} {}", name, speakerId);
+            return saved;
         } catch (Exception e) {
             log.error("save doubaovcl speakerId error: {}", e.getMessage(), e);
+            throw new RuntimeException("保存音色失败", e);
         }
     }
 
@@ -388,4 +399,35 @@ public class CompanyVoiceCloneServiceImpl implements ICompanyVoiceCloneService {
                 .writeTimeout(60, TimeUnit.SECONDS)
                 .build();
     }
+
+    private synchronized void addCompanyVoiceRelation(Long companyId, Integer ttsId,Long companyUserId) {
+        if (companyId == null) {
+            throw new RuntimeException("companyId为空,无法保存公司音色关联");
+        }
+        if (ttsId == null) {
+            throw new RuntimeException("ttsId为空,无法保存公司音色关联");
+        }
+
+        try {
+            CompanyVoiceCloneRef ref = companyVoiceCloneRefMapper.selectByCompanyIdAndTtsId(companyId ,companyUserId, ttsId);
+            if (ref == null) {
+                ref = new CompanyVoiceCloneRef();
+                ref.setCompanyId(companyId);
+                ref.setCompanyUserId(companyUserId);
+                ref.setTtsId(ttsId);
+                ref.setStatus(1);
+                ref.setCreateBy("system");
+                ref.setUpdateBy("system");
+                companyVoiceCloneRefMapper.insertCompanyVoiceCloneRef(ref);
+            } else {
+                ref.setStatus(1);
+                ref.setUpdateBy("system");
+                companyVoiceCloneRefMapper.updateCompanyVoiceCloneRef(ref);
+            }
+            log.info("保存公司音色关联成功,companyId={}, ttsId={}", companyId, ttsId);
+        } catch (Exception e) {
+            log.error("保存公司音色关联失败,companyId={}, ttsId={}", companyId, ttsId, e);
+            throw new RuntimeException("保存公司音色关联失败", e);
+        }
+    }
 }

+ 68 - 0
fs-service/src/main/resources/mapper/company/CompanyVoiceCloneRefMapper.xml

@@ -0,0 +1,68 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper
+        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
+        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.fs.company.mapper.CompanyVoiceCloneRefMapper">
+
+    <resultMap id="CompanyVoiceCloneRefResult" type="com.fs.company.domain.CompanyVoiceCloneRef">
+        <id property="id" column="id"/>
+        <result property="companyId" column="company_id"/>
+        <result property="companyUserId" column="company_user_id"/>
+        <result property="ttsId" column="tts_id"/>
+        <result property="status" column="status"/>
+        <result property="createBy" column="create_by"/>
+        <result property="createTime" column="create_time"/>
+        <result property="updateBy" column="update_by"/>
+        <result property="updateTime" column="update_time"/>
+        <result property="remark" column="remark"/>
+    </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
+        from company_voice_clone_ref
+        where company_id = #{companyId}
+          and company_user_id = #{companyUserId}
+          and tts_id = #{ttsId}
+            limit 1
+    </select>
+
+    <insert id="insertCompanyVoiceCloneRef" parameterType="com.fs.company.domain.CompanyVoiceCloneRef"
+            useGeneratedKeys="true" keyProperty="id">
+        insert into company_voice_clone_ref
+        (
+            company_id,
+            company_user_id,
+            tts_id,
+            status,
+            create_by,
+            create_time,
+            update_by,
+            update_time,
+            remark
+        )
+        values
+            (
+                #{companyId},
+                #{companyUserId},
+                #{ttsId},
+                #{status},
+                #{createBy},
+                now(),
+                #{updateBy},
+                now(),
+                #{remark}
+            )
+    </insert>
+
+    <update id="updateCompanyVoiceCloneRef" parameterType="com.fs.company.domain.CompanyVoiceCloneRef">
+        update company_voice_clone_ref
+        <set>
+            <if test="status != null">status = #{status},</if>
+            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
+            <if test="remark != null">remark = #{remark},</if>
+            update_time = now()
+        </set>
+        where id = #{id}
+    </update>
+
+</mapper>