录自己一段音频,后续根据文字生成自己音色的音频(java实现)

目前自定义TTS(文字转语音)技术已经比较成熟了,在很多场景都有运用。比较常见的多是一些系统预制好的音色,然后提供文字,即可出来这个音色出来的音频。

随着AI技术的不断发展,以及大家对自定义音色的产品诉求有所增加(譬如导航用自己的声音、用女神的声音、录别人一段音然后假装是这个人说话),现在也能比较容易的实现自己录一段音频,然后以此为模板,后续生成该音色的音频,即语音复刻功能。

下面我们用java实现一个语音复刻功能,其实比较简单,刻出来的音色也还算可以,听起来和本人相似度8成,好在是免费的。想要更相似的程度,就需要花钱了。

第一步 准备自己的录音文件

自行准备一个音频文件,可以用手机自己录个导出来,格式:WAV、MP3、M4A。时长就15秒左右即可。

然后将音频上传到阿里云oss中,得到一个oss地址,主要目的是获得一个可以公开访问的音频地址。也可以上传到别处,如果音频直接就是一个网络地址也可以。具体文档地址是:简介与SDK代码示例_智能语音交互(ISI)-阿里云帮助中心

然后开通阿里云智能语音服务,https://common-buy.aliyun.com/?spm=a2c4g.11186623.0.0.767c21f5AUyKdW&commodityCode=nlsService

开通之后你就可以复刻自己的音频了。

第二步 复刻自己音色的模型

复制代码
<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-core</artifactId>
    <version>4.6.4</version>
</dependency>

package org.example;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.profile.DefaultProfile;

public class CosyVoiceDemo {
    //域名
    private static final String DOMAIN = "nls-slp.cn-shanghai.aliyuncs.com";
    // API版本
    private static final String API_VERSION = "2019-08-19";

    private static final IAcsClient client;

    static {
        // 创建DefaultAcsClient实例并初始化
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-shanghai",
                System.getenv("AK_ID"),
                System.getenv("AK_SECRET"));
        client = new DefaultAcsClient(profile);
    }

    public static void main(String[] args) throws InterruptedException {
        String voicePrefix = "your-voice-prefix";
        String url = "your-file-url";
        cosyClone(voicePrefix, url);
        //cosyList(voicePrefix);
    }

    private static void cosyList(String voicePrefix) {
        CommonRequest request = buildRequest("ListCosyVoice");
        request.putBodyParameter("VoicePrefix", voicePrefix);
        String response = sendRequest(request);
        System.out.println(response);
    }

    private static void cosyClone(String voicePrefix, String url) {
        CommonRequest cloneRequest = buildRequest("CosyVoiceClone");
        cloneRequest.putBodyParameter("VoicePrefix", voicePrefix);
        cloneRequest.putBodyParameter("Url", url);
        // 设定等待超时时间为15s
        cloneRequest.setSysReadTimeout(15000);
        long startTime = System.currentTimeMillis();
        String response = sendRequest(cloneRequest);
        long endTime = System.currentTimeMillis();
        System.out.println(response);
        System.out.println("cost: "+ (endTime - startTime) + " 毫秒");
    }

    private static CommonRequest buildRequest(String popApiName) {
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain(DOMAIN);
        request.setVersion(API_VERSION);
        request.setAction(popApiName);
        request.setProtocol(ProtocolType.HTTPS);
        return request;
    }

    private static String sendRequest(CommonRequest request) {
        try {
            CommonResponse response = client.getCommonResponse(request);
            return response.getData();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }
}

填入你在智能语音交互那里得到的AK、Secret,填入音频地址,然后调用cosyClone方法,该方法执行完毕后会得到一个你设置的前缀开通的模型名称,保存这个模型名,后续用TTS生成音频时就用这个模型名称。

第三步 根据模型生成任意文字的音频

复刻出自己音色的模型后,你就可以使用这个模型去生成任意文字的音频了。

文档地址是流式文本语音合成Java SDK_智能语音交互(ISI)-阿里云帮助中心

见最下面的代码FlowingSpeechSynthesizerDemo,里面修改synthesizer.setVoice("siyue")这里修改为你上一步复刻出来的模型名称,flowingTts.wav这个文件地址换成你要保存的地址。然后就可以运行后,就能得到一个复刻好的声音了,听一听吧。

相关推荐
ygyqinghuan19 分钟前
读懂目标检测
人工智能·目标检测·目标跟踪
华东数交21 分钟前
企业与国有数据资产:入表全流程管理及资产化闭环理论解析
大数据·人工智能
newxtc3 小时前
【昆明市不动产登记中心-注册安全分析报告】
人工智能·安全
techdashen3 小时前
圆桌讨论:Coding Agent or AI IDE 的现状和未来发展
ide·人工智能
CV实验室4 小时前
TIP 2025 | 哈工大&哈佛等提出 TripleMixer:攻克雨雪雾干扰的3D点云去噪网络!
人工智能·计算机视觉·3d·论文
余俊晖5 小时前
一套针对金融领域多模态问答的自适应多层级RAG框架-VeritasFi
人工智能·金融·rag
码农阿树5 小时前
视频解析转换耗时—OpenCV优化摸索路
人工智能·opencv·音视频
伏小白白白6 小时前
【论文精度-2】求解车辆路径问题的神经组合优化算法:综合展望(Yubin Xiao,2025)
人工智能·算法·机器学习
应用市场6 小时前
OpenCV编程入门:从零开始的计算机视觉之旅
人工智能·opencv·计算机视觉
星域智链6 小时前
宠物智能用品:当毛孩子遇上 AI,是便利还是过度?
人工智能·科技·学习·宠物