失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 手把手教你用JAVA实现“语音合成”功能(文字转声音)标贝科技

手把手教你用JAVA实现“语音合成”功能(文字转声音)标贝科技

时间:2020-04-14 17:02:43

相关推荐

手把手教你用JAVA实现“语音合成”功能(文字转声音)标贝科技

手把手教你用JAVA实现“语音合成”功能(文字转声音)标贝科技


前言

什么是语音合成?

将文本转换成自然流畅的语音,本篇文章将介绍“实时在线合成”(文本长度不得超过1024字节)


一、内容太长不愿意看,直接使用系列

首先确认接口调用要求:

合成音频的格式:PCM

合成音频的采样率: 8000Hz,16000Hz

支持语言:中文(zh),英文(eng),粤语(cat),四川话(sch)

确认无误后,直接执行 2.2获取权限+2.3.4完整代码示例

二、用JAVA调用标贝科技“语音合成”接口使用流程

2.1.环境准备

java

2.2.获取权限

2.1.1.登录

地址:https://ai.data-/#/?source=qaz123

(注:填写邀请码hi25d7,每日免费调用量还可以翻倍)

​​​​​​

点击上方地址登录,支持短信、密码、微信三种登录方式。

2.1.2.创建应用

登录后,点击创建应用,填写相关信息(未实名认证只能创建一个应用)

(注:实名认证后可获得创建多个应用的权限)

进入应用,其中包含的技术产品有:语音识别、语音合成、声音复刻、声音转换

页面中功能主要包括:服务用量管理、购买服务量管理、开发者文档、授权管理、套餐管理

2.1.3.获取token

点击在线合成—>授权管理—>显示—>获取APISecret—>(获取访问令牌token)

注:右下角红框中是测试版提供的音色


2.3.代码实现

2.3.1.获取token

/*** 授权:需要在开放平台获取【https://ai.data-/#/?source=qaz123】*/private static final String clientId = "输入你的clientid";private static final String clientSecret = "输入你的clientsecret";/*** 获取token的地址信息*/public static String tokenUrl = "https://openapi.data-/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 mons.lang3.StringUtils;import java.io.File;import java.io.IOException;import .URLEncoder;import java.nio.file.Files;public class JavaTtsRestDemo {/*** 合成使用的地址信息,rate、language等参数在本demo固定,开发者如需调整,参考https://www.data-/specs/file/tts_api_restful*/public static String ttsUrl = "https://openapi.data-/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-/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-/#/?source=qaz123

(注:填写邀请码hi25d7,每日免费调用量还可以翻倍)

如果觉得《手把手教你用JAVA实现“语音合成”功能(文字转声音)标贝科技》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。