基于百度AI的语音合成
来源:互联网 发布:西安seo技术 编辑:程序博客网 时间:2024/05/16 10:18
1.该功能基于度娘语音合成接口
2.下面代码直接粘贴到开发工具即可使用
3.APP_ID,API_KEY,SECRET_KEY这些需要你自己去度娘开发者中心申请。
public class SpeechSynthesis {
// 设置APPID/AK/SKpublic static final String APP_ID = "xxxx";// "你的 App ID"
public static final String API_KEY = "xxxxxx";// "你的 Api ID"
public static final String SECRET_KEY = "xxxxxx";// "你的 Secret Key"
public static void main(String[] args) {
AipSpeech client = initCilent();
HashMap<String, Object> options = initOptions();
StringBuffer text = new StringBuffer();
text.append(嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻");
String textStr = text.toString();
int textLength = textStr.length();
int makeCount = textLength % 500 == 0 ? textLength / 500 : textLength / 500 + 1;
int flag = 0;
int count = 1;
byte[] totalData = null;
if (makeCount == 1) {
totalData = getByteData(client, options, textStr);
} else {
while (count < makeCount) {
String newText = textStr.substring(flag, flag + 500);
System.out.println("Info:" + count + "次,本次合成内容:" + newText);
if (count == 1) {
totalData = getByteData(client, options, newText);
} else {
byte[] newData = getByteData(client, options, newText);
totalData = byteMerger(totalData, newData);
}
flag += 500;
count++;
}
if (count == makeCount) {
String newText = textStr.substring(flag);
byte[] lastData = getByteData(client, options, newText);
totalData = byteMerger(totalData, lastData);
System.out.println("Info:" + count + "次,本次合成内容:" + newText);
}
}
MakeVoice("Voice.mp3", totalData);
}
// 设置语音合成参数
private static HashMap<String, Object> initOptions() {
HashMap<String, Object> options = new HashMap<String, Object>();
options.put("spd", "5");
options.put("pit", "5");
options.put("vol", "5");
options.put("per", "3");
return options;
}
// 初始化一个FaceClient
private static AipSpeech initCilent() {
AipSpeech client = new AipSpeech(APP_ID, API_KEY, SECRET_KEY);
// 可选:设置网络连接参数
client.setConnectionTimeoutInMillis(2000);
client.setSocketTimeoutInMillis(60000);
return client;
}
// 发送文字,得到返回的语音字节码
private static byte[] getByteData(AipSpeech client,
HashMap<String, Object> options, String textStr) {
TtsResponse res = client.synthesis(textStr, "zh", 1, options);
byte[] data = res.getData();
return data;
}
// 语音合成mp3
private static void MakeVoice(String fileName, byte[] data) {
File file = new File("/d:" + File.separator + fileName);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
fos.write(data);
} catch (Exception e) {
e.printStackTrace();
}
if (fos != null) {
try {
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 字节码流合成
public static byte[] byteMerger(byte[] data1, byte[] data2) {
byte[] data3 = new byte[data1.length + data2.length];
System.arraycopy(data1, 0, data3, 0, data1.length);
System.arraycopy(data2, 0, data3, data1.length, data2.length);
return data3;
}
}
阅读全文
0 0
- 基于百度AI的语音合成
- 百度语音合成的使用
- 百度的语音合成功能
- 集成百度语音合成的一些问题
- iOS-百度语音合成的使用
- c# 百度语音合成
- 百度语音合成
- 百度语音合成学习
- 百度语音离线合成
- 百度语音合成
- 百度离线语音合成
- 百度语音合成api
- 调用百度语音合成接口
- 百度语音合成案例分享
- 百度语音识别和合成
- 百度语音合成快速集成
- ReactNative集成百度语音合成
- Android百度语音合成Demo
- 解决Warning: World-writable config file ‘/etc/my.cnf’ is ignored
- 多布局
- Android数据存储(三)---File存储
- 数塔(dp)
- command模式
- 基于百度AI的语音合成
- POJ—1019 Number Sequence
- Python网络爬虫与信息提取-Day4-网络爬虫及Robots协议
- c++中如何写参数不定的函数
- 使用CSDN-markdown编辑器编辑csdn博客
- Jenkins 的部署(Windows)
- EasyUI DataGrid 时间格式化、字符串长度截取
- 图论总结(5)2—SAT问题
- 文章标题