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

相关推荐
AI码农小姐姐3 小时前
AI漫剧推文短视频推理加速:LCM-LoRA与少步采样实践
人工智能·音视频·ai工具·ai漫剧
宸津-代码粉碎机3 小时前
微服务线上踩坑复盘:接口超时、负载倾斜隐形问题根治方案(生产级配置)
java·大数据·人工智能·python·spring
阿里云大数据AI技术3 小时前
阿里云PAI推出InferX:Agent时代重塑企业专属的高保障SLO推理服务
人工智能·agent
Raas1003 小时前
MAI Gateway(魔芋企业级AI网关)能力解析:AI网关支持OpenAI吗?AI网关核心功能详解
人工智能·网关·ai网关·mai gateway·企业级产品·独立团队
多多鼠3 小时前
System Prompt 的“版本漂移”问题:从变更管理到 A/B 测试体系
开发语言·网络·人工智能·python·langchain
LuTshoes4 小时前
spring ai 实战RAG(4)-模块化RAG
java·人工智能·spring
Thom5804 小时前
【迅投 QMT】QMT如何实现布林带突破策略?Python指标与交易信号示例
人工智能·经验分享·量化交易·量化编程
2601_962380764 小时前
考研数学公式推导和解题步骤怎么做成动画讲解视频
音视频
m0_734571764 小时前
深入理解人工智能 chatGPT 基础设施与数据层 (Infrastructure & Data Layer)
人工智能
魔众4 小时前
写歌、翻唱、可编辑乐谱,YuE2-3B 在 AIGCPanel 一键跑通
人工智能·开源