package com.alibaba.idst.nls.demo;
import com.alibaba.idst.nls.protocol.NlsResponse;
import com.alibaba.idst.nls.NlsClient;
import com.alibaba.idst.nls.NlsFuture;
import com.alibaba.idst.nls.event.NlsEvent;
import com.alibaba.idst.nls.event.NlsListener;
import com.alibaba.idst.nls.protocol.NlsRequest;
import java.io.File;
import java.io.FileOutputStream;
public class TTSDemo implements NlsListener {
//文字合成语音
private NlsClient client = new NlsClient();
private String akId;
private String akSecret;
private String tts_text = "周冬雨是我女神,周冬雨是我女神,周冬雨是我女神,周冬雨是我女神,周冬雨是我女神,周冬雨是我女神,周冬雨是我女神,周冬雨是我女神,周冬雨是我女神,周冬雨是我女神,周冬雨是我女神,周冬雨是我女神";
public TTSDemo(String akId, String akSecret) {
System.out.println("init Nls client...");
this.akId = akId;
this.akSecret = akSecret;
// 初始化NlsClient
client.init();
}
public void shutDown() {
System.out.println("close NLS client");
// 关闭客户端并释放资源
client.close();
System.out.println("demo done");
}
public void startTTS() {
File file = new File("tts.wav");
if(!file.exists()) {
try {
file.createNewFile();
} catch (Exception e) {
e.printStackTrace();
}
}
NlsRequest req = new NlsRequest();
String appkey = "nls-service";//社交聊天
req.setAppKey(appkey); // 设置语音文件格式
req.setTtsRequest(tts_text); //传入测试文本,返回语音结果
req.setTtsEncodeType("wav");//返回语音数据格式,支持pcm,wav.alaw
req.setTtsVolume(30); //音量大小默认50,阈值0-100
req.setTtsSpeechRate(0); //语速,阈值-500~500
req.setTtsBackgroundMusic(1,0);//背景音乐编号,偏移量
req.authorize(akId, akSecret); // 请替换为用户申请到的Access Key ID和Access Key Secret
try {
FileOutputStream fileOutputStream = new FileOutputStream(file);
NlsFuture future = client.createNlsFuture(req, this); // 实例化请求,传入请求和监听器
int total_len = 0;
byte[] data ;
while((data = future.read()) != null) {
fileOutputStream.write(data, 0, data.length);
total_len += data.length;
}
fileOutputStream.close();
System.out.println("tts audio file size is :" + total_len);
future.await(10000); // 设置服务端结果返回的超时时间
} catch (Exception e) {
e.printStackTrace();
}
}
// @Override
public void onMessageReceived(NlsEvent e) {
NlsResponse response = e.getResponse();
String result = "";
int statusCode = response.getStatus_code();
if (response.getTts_ret() != null) {
result += "\nget tts result: statusCode=[" + statusCode + "], " + response.getTts_ret();
}
if (result != null) {
System.out.println(result);
}
else {
System.out.println(response.jsonResults.toString());
}
}
//@Override
public void onOperationFailed(NlsEvent e) {
//识别失败的回调
System.out.print("on operation failed: ");
System.out.println(e.getErrorMessage());
}
//@Override
public void onChannelClosed(NlsEvent e) {
//socket 连接关闭的回调
System.out.println("on websocket closed.");
}
/**
* @param args
*/
public static void main(String[] args) {
String akId ="LTAI4yGBbd7rJNVi";//请替换为用户申请到的Access Key ID和Access Key
String akSecret = "GvQsZ3x2Wg8uYAO8tGz9CMwt0QKPJv";
TTSDemo ttsDemo = new TTSDemo(akId, akSecret);
ttsDemo.startTTS();
ttsDemo.shutDown();
}
}
package com.alibaba.idst.nls.demo;
import java.io.InputStream;
import com.alibaba.idst.nls.NlsClient;
import com.alibaba.idst.nls.NlsFuture;
import com.alibaba.idst.nls.event.NlsEvent;
import com.alibaba.idst.nls.event.NlsListener;
import com.alibaba.idst.nls.protocol.NlsRequest;
import com.alibaba.idst.nls.protocol.NlsResponse;
public class NluDemo implements NlsListener {//其他
private NlsClient client = new NlsClient();
private String akId;
private String akSecret;
public NluDemo(String akId, String akSecret) {
System.out.println("init Nls client...");
this.akId = akId;
this.akSecret = akSecret;
// 初始化NlsClient
client.init();
}
public void shutDown() {
System.out.println("close NLS client");
// 关闭客户端并释放资源
client.close();
System.out.println("demo done");
}
public void startNLU() {
System.out.println("open audio file...");
InputStream fis = null;
try {
fis = this.getClass().getClassLoader().getResourceAsStream("sample.pcm");
} catch (Exception e) {
e.printStackTrace();
}
if (fis != null) {
System.out.println("create NLS future");
try {
NlsRequest req = new NlsRequest();
req.setAppKey("nls-service"); // appkey请从 "快速开始" 帮助页面的appkey列表中获取
req.setAsrFormat("pcm"); // 设置语音文件格式为pcm,我们支持16k 16bit 的无头的pcm文件。
/*热词相关配置*/
//req.setAsrVocabularyId("热词词表id");//热词词表id
/*热词相关配置*/
/*或者单独使用nlu*/
//req.setAsrFake("北京天气"); //使用文本请求nlu结果,此模式下不需要发送语音数据
/*或者单独使用nlu*/
req.enableNLUResult(); // 设置nlu请求
req.authorize(akId, akSecret); // 请替换为用户申请到的Access Key ID和Access Key Secret
NlsFuture future = client.createNlsFuture(req, this);
System.out.println("call NLS service");
System.out.println(req.toJson());
byte[] b = new byte[1024];
int len = 0;
if (req.getAsrFake() == null) {
while ((len = fis.read(b)) > 0) {
future.sendVoice(b, 0, len); // 发送语音数据
Thread.sleep(50);
}
future.sendFinishSignal(); // 语音识别结束时,发送结束符
}
System.out.println("main thread enter waiting for less than 10s.");
future.await(10000); // 设置服务端结果返回的超时时间
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("calling NLS service end");
}
}
// @Override
public void onMessageReceived(NlsEvent e) {
NlsResponse response = e.getResponse();
String result = "";
int statusCode = response.getStatus_code();
System.out.println(statusCode);
if (response.getDs_ret() != null) {
result = "get ds result: statusCode=[" + statusCode + "], " + response.getDs_ret();
}
if (response.getAsr_ret() != null) {
result += "\nget asr result: statusCode=[" + statusCode + "], " + response.getAsr_ret();
}
System.out.println(result);
}
// @Override
public void onOperationFailed(NlsEvent e) {
//识别失败的回调
String result = "";
result += "on operation failed: statusCode=[" + e.getResponse().getStatus_code() + "], " + e.getErrorMessage();
System.out.println(result);
}
// @Override
public void onChannelClosed(NlsEvent e) {
System.out.println("on websocket closed.");
}
/**
* @param args
*/
public static void main(String[] args) {
String akId ="LTAI4yGBbd7rJNVi";//请替换为用户申请到的Access Key ID和Access Key
String akSecret = "GvQsZ3x2Wg8uYAO8tGz9CMwt0QKPJv";
NluDemo nluDemo = new NluDemo(akId, akSecret);
nluDemo.startNLU();
nluDemo.shutDown();
}
}