什么是语音合成?
将文本转换成自然流畅的语音,本篇文章将介绍“实时在线合成”(文本长度不得超过1024字节)
首先确认接口调用要求:
合成音频的格式:PCM
合成音频的采样率: 8000Hz,16000Hz
支持语言:中文(zh),英文(eng),粤语(cat),四川话(sch)
确认无误后,直接执行 2.2获取权限+2.3.4完整代码示例
java
2.1.1.登录
地址:https://ai.data-baker.com/#/?source=qaz123
(注:填写邀请码hi25d7,每日免费调用量还可以翻倍)
点击上方地址登录,支持短信、密码、微信三种登录方式。
2.1.2.创建应用
登录后,点击创建应用,填写相关信息(未实名认证只能创建一个应用)
(注:实名认证后可获得创建多个应用的权限)
进入应用,其中包含的技术产品有:语音识别、语音合成、声音复刻、声音转换
页面中功能主要包括:服务用量管理、购买服务量管理、开发者文档、授权管理、套餐管理
2.1.3.获取token
点击在线合成—>授权管理—>显示—>获取APISecret—>(获取访问令牌token)
注:右下角红框中是测试版提供的音色
2.3.1.获取token
/**
* 授权:需要在开放平台获取【https://ai.data-baker.com/#/?source=qaz123】
*/
private static final String clientId = "输入你的clientid";
private static final String clientSecret = "输入你的clientsecret";
/**
* 获取token的地址信息
*/
public static String tokenUrl = "https://openapi.data-baker.com/oauth/2.0/token?grant_type=client_credentials&client_secret=%s&client_id=%s";
public static String getAccessToken() {
String accessToken = "";
OkHttpClient client = new OkHttpClient();
// request 默认是get请求
String url = String.format(tokenUrl, clientSecret, clientId);
Request request = new Request.Builder().url(url).build();
JSONObject jsonObject;
try {
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
// 解析
String resultJson = response.body().string();
jsonObject = JSON.parseObject(resultJson);
accessToken = jsonObject.getString("access_token");
}
} catch (Exception e) {
e.printStackTrace();
}
return accessToken;
}
2.3.2.文本样例转码
public static void doSynthesis(String accessToken, String voiceName, String originText, Integer audioType, Double speed, Double volume, String filePath) {
//在非浏览器上操作,需要把合成文本转化为utf-8格式
try {
originText = URLEncoder.encode(originText, "utf-8");
String synthesisUrl = String.format(ttsUrl, accessToken, audioType, voiceName, speed, volume, originText);
fetchTtsResponse(synthesisUrl, filePath);
} catch (Exception e) {
e.printStackTrace();
}
}
2.3.3.获取音频流保存到本地
public static void fetchTtsResponse(String url, String filePath) throws IOException {
OkHttpClient client = new OkHttpClient();
//request 默认是get请求
Request request = new Request.Builder().url(url).build();
try {
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
if (response.body() != null
&& response.body().contentType().toString().startsWith("audio")) {
//写入文件
File targetFile = new File(filePath);
Files.write(targetFile.toPath(), response.body().bytes());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
2.3.4.完整代码示例
package ......
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.commons.lang3.StringUtils;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.file.Files;
public class JavaTtsRestDemo {
/**
* 合成使用的地址信息,rate、language等参数在本demo固定,开发者如需调整,参考https://www.data-baker.com/specs/file/tts_api_restful
*/
public static String ttsUrl = "https://openapi.data-baker.com/tts?access_token=%s&domain=1&rate=2&audiotype=%s&language=zh&voice_name=%s&speed=%s&volume=%s&text=%s";
/**
* 获取token的地址信息
*/
public static String tokenUrl = "https://openapi.data-baker.com/oauth/2.0/token?grant_type=client_credentials&client_secret=%s&client_id=%s";
public static String clientId = "YOUR_CLIENT_ID";
public static String clientSecret = "YOUR_CLIENT_SECRET";
/**
* 仅作为demo示例
* 失败重试、token过期重新获取、日志打印等优化工作需要开发者自行完成
**/
public static void main(String[] args) {
String accessToken = getAccessToken();
if (StringUtils.isNotEmpty(accessToken)) {
doSynthesis(accessToken, "Nannan", "测试文本", 6, 5.0, 5.0, "/home/tts/test.wav");
}
}
public static void doSynthesis(String accessToken, String voiceName, String originText, Integer audioType, Double speed, Double volume, String filePath) {
//在非浏览器上操作,需要把合成文本转化为utf-8格式
try {
originText = URLEncoder.encode(originText, "utf-8");
String synthesisUrl = String.format(ttsUrl, accessToken, audioType, voiceName, speed, volume, originText);
fetchTtsResponse(synthesisUrl, filePath);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 请求并获取音频流保存至本地文件:这里filePath为全路径
*
* @param url
* @param filePath
* @throws IOException
*/
public static void fetchTtsResponse(String url, String filePath) throws IOException {
OkHttpClient client = new OkHttpClient();
//request 默认是get请求
Request request = new Request.Builder().url(url).build();
try {
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
if (response.body() != null
&& response.body().contentType().toString().startsWith("audio")) {
//写入文件
File targetFile = new File(filePath);
Files.write(targetFile.toPath(), response.body().bytes());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static String getAccessToken() {
String accessToken = "";
OkHttpClient client = new OkHttpClient();
//request 默认是get请求
String url = String.format(tokenUrl, clientSecret, clientId);
Request request = new Request.Builder().url(url).build();
JSONObject jsonObject;
try {
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
//解析
String resultJson = response.body().string();
jsonObject = JSON.parseObject(resultJson);
accessToken = jsonObject.getString("access_token");
}
} catch (Exception e) {
e.printStackTrace();
}
return accessToken;
}
}
地址:https://ai.data-baker.com/#/?source=qaz123
(注:填写邀请码hi25d7,每日免费调用量还可以翻倍)