录自己一段音频,后续根据文字生成自己音色的音频(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这个文件地址换成你要保存的地址。然后就可以运行后,就能得到一个复刻好的声音了,听一听吧。

相关推荐
机器学习之心26 分钟前
半经验模型引导的物理信息神经网络船舶速度损失预测方法,基于 SEM-PINN 的可复现实验与独立航次验证
人工智能·深度学习·神经网络·物理信息神经网络·半经验模型引导
掘金安东尼31 分钟前
大模型降本的三条开源路线:任务路由、指令优化与消耗量控制
人工智能·开源
严肃小白兔8 小时前
第 17/24 期:卷积神经网络的数学原理与计算过程
人工智能·神经网络·cnn
LabVIEW开发9 小时前
Sherborne 倾角仪 + LabVIEW 倾角怎么测?
人工智能·labview
stormzhangV9 小时前
一文说清楚 Neon 到底有多强?
人工智能·google·aigc
孟健10 小时前
拆解降噪站 Noise Reducer:定价页这样设计,月访问 49 万
人工智能·aigc
英辰朗迪AI获客10 小时前
英辰朗迪AI获客每日AI精选(2026.11.21)
人工智能
RSABLOCKCHAIN10 小时前
【AI应用】Agent三种实现范式的对比
人工智能
八月瓜科技10 小时前
专精特新企业如何搭建专利资产中台?三步落地法
大数据·数据库·人工智能·科技·深度学习·机器学习·oracle