乐趣区

关于android:虚拟偶像的歌声原来是这样生成的

HMS Core 音频编辑服务(Audio Editor Kit)6.6.0 版本上线,新增歌声合成能力。通过歌词和曲调,联合不同的曲风让机器也能生成真实度极高的歌声。反对字级别输出歌词进行音素转换,生成对应歌词的歌声,可灵便调整音高、滑音、呼吸音、颤音等细节参数,让歌声更实在。

歌声合成服务可广泛应用于 音视频创意制作、影音娱乐、音乐教育、虚构偶像 等畛域。例如,在音乐创作或短视频创意编辑时,歌声合成服务能够助力用户自在创作合成歌曲,使创作更加丰富多彩。在虚构偶像畛域,通过歌声合成,能够让虚拟人领有特定音色歌唱能力,使其形象更活泼。在音乐游戏或者歌唱教育中,歌声合成能够迅速生成规范参考声音,进步音频制作效率节俭人力老本。

歌声合成成果

听到了歌声合成媲美真人的歌唱成果,是否急不可待想上手应用了呢,以下是歌声合成的具体集成办法。快来亲自集成试试吧!

1. 开发筹备

1.1 注册成为开发者

在开发利用前须要在华为开发者联盟网站上注册成为开发者并实现实名认证,具体方法请参见帐号注册认证。

1.2 创立我的项目及利用

参见创立我的项目,而后在我的项目下创立利用实现利用的创立,非凡配置如下:

抉择平台:抉择“Web”。

1.3 关上相干服务

应用 Audio Editor Kit 服务须要您在 AppGallery Connect 上关上 Audio Editor Kit 服务开关,具体操作步骤请参见关上服务开关。

2. 歌声合成性能集成

2.1 同步接口(流式)

2.1.1 获取 access_token 鉴权信息

应用开发者联盟界面取得的客户端 ID 以及对应密钥,发送 HTTPS POST 申请,获取查问 access_token。获取形式请参见客户端模式(Client Credentials)。

2.1.2 依据 access_token 调用同步接口(流式)

通过以上步骤获取的 access_token 信息,发送 HTTPS POST 调用同步接口(流式)。

示例代码(Java)如下所示:

其中 requestUrl = “https://audioeditor-api-drcn.cloud.huawei.com/v1/audioeditor/gateway/ai/ttsing/sync”。

/**
     * 调用同步接口(流式)* @param accessToken 依据 clientId 和密钥获取的 token
     * @throws Exception IO 异样
     */
    private static void syncTask(String accessToken) throws Exception {
        // 设置申请 header
        PostMethod postMethod = new PostMethod(requestUrl);
        postMethod.setRequestHeader("Content-Type","application/json;charset=utf-8");
        postMethod.setRequestHeader("X-Request-ID","9af1aeda-531b-407a-80b4-65b40ef77bd6");
        postMethod.setRequestHeader("X-Package-Name","com.huawei.demo");
        postMethod.setRequestHeader("X-Country-Code","cn");
        postMethod.setRequestHeader("HMS-APPLICATION-ID","123456");
        postMethod.setRequestHeader("certFingerprint","xxxxx");
        postMethod.setRequestHeader("Authorization","Bearer" + accessToken);
        // 设置申请 body
        Map<String, Object> bodyMap = new HashMap<>();
        Map<String, Object> dataMap = new HashMap<>();
        Map<String, Object> configMap = new HashMap<>();
        // filePath 是 MusicXML 文件门路(含文件名、后缀)String lyricFilePath = "filePath";
        dataMap.put("lyric", FileUtils.readFileToString(new File(lyricFilePath), "UTF-8"));
        dataMap.put("language", "chinese");
        configMap.put("type", 1);
        configMap.put("outputEncoderFormat", 0);
        bodyMap.put("data", dataMap);
        bodyMap.put("config", configMap);
        RequestEntity requestEntity = new StringRequestEntity(JSONObject.toJSONString(bodyMap),"application/json" ,"UTF-8");
        postMethod.setRequestEntity(requestEntity);

        HttpClient httpClient = new HttpClient();
        int ret = httpClient.executeMethod(postMethod);
        if (ret == 200) {Header responseHeader = postMethod.getResponseHeader("content-type");
            if ("application/octet-stream".equals(responseHeader.getValue())) {InputStream rpsContent = postMethod.getResponseBodyAsStream();
                // filePath 是要保留文件的门路(含文件名、后缀)String filePath = "filePath";
                FileUtils.copyInputStreamToFile(rpsContent, new File(filePath));
            } else {String errorString = postMethod.getResponseBodyAsString();
                System.out.println(errorString);
            }
        } else {System.out.println("callApi failed: ret =" + ret + "rsp=" + postMethod.getResponseBodyAsString());
        }
    }

2.2 异步接口

2.2.1 创立异步工作

通过 access_token 信息,发送 HTTPS POST 创立歌声合成异步工作。

  /**
     * 调用创立异步工作接口
     * @param accessToken 依据 clientId 和密钥获取的 token
     * @throws Exception IO 异样
     */
    private static void creatAsyncTask(String accessToken) throws Exception {
        // 设置申请 header
        PostMethod postMethod = new PostMethod(requestUrl);
        postMethod.setRequestHeader("Content-Type","application/json;charset=utf-8");
        postMethod.setRequestHeader("X-Request-ID","9af1aeda-531b-407a-80b4-65b40ef77bd6");
        postMethod.setRequestHeader("X-Package-Name","com.huawei.demo");
        postMethod.setRequestHeader("X-Country-Code","cn");
        postMethod.setRequestHeader("HMS-APPLICATION-ID","123456");
        postMethod.setRequestHeader("certFingerprint","xxxxx");
        postMethod.setRequestHeader("Authorization","Bearer" + accessToken);
        // 设置申请 body
        Map<String, Object> bodyMap = new HashMap<>();
        Map<String, Object> dataMap = new HashMap<>();
        Map<String, Object> configMap = new HashMap<>();
        // filePath 是 MusicXML 文件门路(含文件名、后缀)String lyricFilePath = "filePath";
        dataMap.put("lyric", FileUtils.readFileToString(new File(lyricFilePath), "UTF-8"));
        dataMap.put("language", "chinese");
        configMap.put("type", 1);
        configMap.put("outputEncoderFormat", 0);
        bodyMap.put("data", dataMap);
        bodyMap.put("config", configMap);
        RequestEntity requestEntity = new StringRequestEntity(JSONObject.toJSONString(bodyMap),"application/json" ,"UTF-8");
        postMethod.setRequestEntity(requestEntity);

        HttpClient httpClient = new HttpClient();
        int ret = httpClient.executeMethod(postMethod);
        String rpsContent = postMethod.getResponseBodyAsString();
        if (ret == 200) {System.out.println(rpsContent);
        } else {System.out.println("callApi failed: ret =" + ret + "rsp=" + rpsContent);
        }
    }

2.2.2 查问异步工作状态

通过 access_token 信息,发送 HTTPS POST 查问歌声合成异步工作状态。

  /**
     * 调用查问异步工作状态接口
     * @param accessToken 依据 clientId 和密钥获取的 token
     * @throws Exception IO 异样
     */
    private static void queryAsyncTaskInfo(String accessToken) throws Exception {
        // 设置申请 header
        PostMethod postMethod = new PostMethod(requestUrl);
        postMethod.setRequestHeader("Content-Type","application/json;charset=utf-8");
        postMethod.setRequestHeader("X-Request-ID","9af1aeda-531b-407a-80b4-65b40ef77bd6");
        postMethod.setRequestHeader("X-Package-Name","com.huawei.demo");
        postMethod.setRequestHeader("X-Country-Code","cn");
        postMethod.setRequestHeader("HMS-APPLICATION-ID","123456");
        postMethod.setRequestHeader("certFingerprint","xxxxx");
        postMethod.setRequestHeader("Authorization","Bearer" + accessToken);
        // 设置申请 body
        Map<String, Object> bodyMap = new HashMap<>();
        // taskId 对应的值是创立异步工作时返回的工作 ID(taskId)bodyMap.put("taskId", "taskId");
        RequestEntity requestEntity = new StringRequestEntity(JSONObject.toJSONString(bodyMap),"application/json" ,"UTF-8");
        postMethod.setRequestEntity(requestEntity);

        HttpClient httpClient = new HttpClient();
        int ret = httpClient.executeMethod(postMethod);
        String rpsContent = postMethod.getResponseBodyAsString();
        if (ret == 200) {System.out.println(rpsContent);
        } else {System.out.println("callApi failed: ret =" + ret + "rsp=" + rpsContent);
        }
    }

2.2.3 勾销异步工作

通过 access_token 信息,发送 HTTPS POST 勾销异步工作。

  /**
     * 调用勾销异步工作接口
     * @param accessToken 依据 clientId 和密钥获取的 token
     * @throws Exception IO 异样
     */
    private static void cancelAsuncTask(String accessToken) throws Exception {
        // 设置申请 header
        PostMethod postMethod = new PostMethod(requestUrl);
        postMethod.setRequestHeader("Content-Type","application/json;charset=utf-8");
        postMethod.setRequestHeader("X-Request-ID","9af1aeda-531b-407a-80b4-65b40ef77bd6");
        postMethod.setRequestHeader("X-Package-Name","com.huawei.demo");
        postMethod.setRequestHeader("X-Country-Code","cn");
        postMethod.setRequestHeader("HMS-APPLICATION-ID","123456");
        postMethod.setRequestHeader("certFingerprint","xxxxx");
        postMethod.setRequestHeader("Authorization","Bearer" + accessToken);
        // 设置申请 body
        Map<String, Object> bodyMap = new HashMap<>();
        // taskId 对应的值是创立异步工作时返回的工作 ID(taskId)bodyMap.put("taskId", "taskId");
        RequestEntity requestEntity = new StringRequestEntity(JSONObject.toJSONString(bodyMap),"application/json" ,"UTF-8");
        postMethod.setRequestEntity(requestEntity);

        HttpClient httpClient = new HttpClient();
        int ret = httpClient.executeMethod(postMethod);
        String rpsContent = postMethod.getResponseBodyAsString();
        if (ret == 200) {System.out.println(rpsContent);
        } else {System.out.println("callApi failed: ret =" + ret + "rsp=" + rpsContent);
        }
    }

除了歌声合成能力,音频编辑服务还提供音频根底编辑、AI 配音伴奏提取、空间渲染、变声降噪等丰盛的音频解决能力,为寰球开发者提供性能优异、简略易用、开放性强的接口,帮忙开发者轻松高效构建利用音频编辑能力。

理解更多详情 >>

拜访华为开发者联盟官网
获取开发领导文档
华为挪动服务开源仓库地址:GitHub、Gitee

关注咱们,第一工夫理解 HMS Core 最新技术资讯~

退出移动版