全部产品
Search
文档中心

Java SDK 2.0

更新时间: 2020-11-03

提示:

下载安装

可从maven 服务器下载最新版本SDK:

  1. <dependency>
  2. <groupId>com.alibaba.nls</groupId>
  3. <artifactId>nls-sdk-tts</artifactId>
  4. <version>2.1.0</version>
  5. </dependency>

使用方式参见下面代码示例。Demo 源码下载链接

demo 解压后,在pom 目录运行mvn package ,会在target目录生成可执行jarnls-example-tts-2.0.0-jar-with-dependencies.jar将此jar拷贝到目标服务器,可用于快速验证及压测服务。

服务验证

java -cp nls-example-tts-2.0.0-jar-with-dependencies.jar com.alibaba.nls.client.SpeechSynthesizerDemo并按提示提供相应参数,运行后在命令执行目录生成logs/nls.log

服务压测

java -jar nls-example-tts-2.0.0-jar-with-dependencies.jar并按提示提供相应参数,其中阿里云服务url参数为: wss://nls-gateway-ap-southeast-1.aliyuncs.com/ws/v1 ,并发数根据用户已购买并发谨慎选择。

温馨提示:自行压测超过2并发会产生费用。

关键接口

  • NlsClient:语音处理client,相当于所有语音相关处理类的factory,全局创建一个实例即可。线程安全。
  • SpeechSynthesizer:语音合成处理类,设置请求参数,发送请求。非线程安全。
  • SpeechSynthesizerListener:语音合成监听类,监听返回结果。非线程安全。有如下两个抽象方法需要实现:
    1. /**
    2. * 接收语音合成二进制数据
    3. */
    4. abstract public void onMessage(ByteBuffer message);
    5. /**
    6. * 语音合成结束事件通知
    7. *
    8. * @param response
    9. */
    10. abstract public void onComplete(SpeechSynthesizerResponse response);

更多介绍参见API文档链接: Java API接口说明

SDK 调用注意事项

  1. NlsClient对象创建一次可以重复使用,每次创建消耗性能。NlsClient使用了netty的框架,创建时比较消耗时间和资源,但创建之后可以重复利用。建议调用程序将NlsClient的创建和关闭与程序本身的生命周期结合。
  2. SpeechSynthesizer对象不能重复使用,一个语音合成任务对应一个SpeechSynthesizer对象。例如有N个文本需要语音合成,则要进行N次语音合成任务,创建N个SpeechSynthesizer对象。
  3. 实现的SpeechSynthesizerListener对象和SpeechSynthesizer对象是一一对应的,不能将一个SpeechSynthesizerListener对象设置到多个SpeechSynthesizer对象中,否则不能区分是哪个语音合成任务。
  4. Java SDK依赖了Netty网络库,版本需设置为4.1.17.Final及以上。如果您的应用中依赖了Netty,请确保版本符合要求。

代码示例

说明1:Demo显示了如何在在创建NlsClient对象的时候设置URL:

  1. client = new NlsClient("ws://nls-gateway-ap-southeast-1.aliyuncs.com/ws/v1", accessToken);

说明2:Demo中将合成的音频保存在了文件中,如果您需要播放音频且对实时性要求较高,建议使用流式播放,即边接收语音数据边播放,减少延时。

示例:

  1. package com.alibaba.nls.client;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.nio.ByteBuffer;
  6. import com.alibaba.nls.client.protocol.NlsClient;
  7. import com.alibaba.nls.client.protocol.OutputFormatEnum;
  8. import com.alibaba.nls.client.protocol.SampleRateEnum;
  9. import com.alibaba.nls.client.protocol.tts.SpeechSynthesizer;
  10. import com.alibaba.nls.client.protocol.tts.SpeechSynthesizerListener;
  11. import com.alibaba.nls.client.protocol.tts.SpeechSynthesizerResponse;
  12. import org.slf4j.Logger;
  13. import org.slf4j.LoggerFactory;
  14. /**
  15. * The demo of speech synthesis.
  16. * (for demo show only)
  17. */
  18. public class SpeechSynthesizerDemo {
  19. private static final Logger logger = LoggerFactory.getLogger(SpeechSynthesizerDemo.class);
  20. private static long startTime;
  21. private String appKey;
  22. NlsClient client;
  23. public SpeechSynthesizerDemo(String appKey, String token) {
  24. this.appKey = appKey;
  25. // Create an NlsClient object. You can globally create an NlsClient object and specify the endpoint.
  26. client = new NlsClient("wss://nls-gateway-ap-southeast-1.aliyuncs.com/ws/v1", token);
  27. }
  28. private static SpeechSynthesizerListener getSynthesizerListener() {
  29. SpeechSynthesizerListener listener = null;
  30. try {
  31. listener = new SpeechSynthesizerListener() {
  32. File f=new File("tts_test.wav");
  33. FileOutputStream fout = new FileOutputStream(f);
  34. private boolean firstRecvBinary = true;
  35. // Speech synthesis is completed.
  36. @Override
  37. public void onComplete(SpeechSynthesizerResponse response) {
  38. System.out.println("name: " + response.getName() + ", status: " + response.getStatus()+", output file :"+f.getAbsolutePath());
  39. }
  40. // The speech binary data of speech synthesis.
  41. @Override
  42. public void onMessage(ByteBuffer message) {
  43. try {
  44. if(firstRecvBinary) {
  45. // the latency of first binary
  46. firstRecvBinary = false;
  47. long now = System.currentTimeMillis();
  48. logger.info("tts first latency : " + (now - SpeechSynthesizerDemo.startTime) + " ms");
  49. }
  50. byte[] bytesArray = new byte[message.remaining()];
  51. message.get(bytesArray, 0, bytesArray.length);
  52. //System.out.println("write array:" + bytesArray.length);
  53. fout.write(bytesArray);
  54. } catch (IOException e) {
  55. e.printStackTrace();
  56. }
  57. }
  58. @Override
  59. public void onFail(SpeechSynthesizerResponse response){
  60. System.out.println(
  61. "task_id: " + response.getTaskId() +
  62. // the code 20000000 indicates that the request is successful.
  63. ", status: " + response.getStatus() +
  64. // error message
  65. ", status_text: " + response.getStatusText());
  66. }
  67. };
  68. } catch (Exception e) {
  69. e.printStackTrace();
  70. }
  71. return listener;
  72. }
  73. public void process() {
  74. SpeechSynthesizer synthesizer = null;
  75. try {
  76. //Create an object and establish a connection.
  77. synthesizer = new SpeechSynthesizer(client, getSynthesizerListener());
  78. synthesizer.setAppKey(appKey);
  79. //Specify the audio coding format of the returned audio file.
  80. synthesizer.setFormat(OutputFormatEnum.WAV);
  81. //Specify the audio sampling rate of the returned audio file.
  82. synthesizer.setSampleRate(SampleRateEnum.SAMPLE_RATE_16K);
  83. //The speaker.
  84. synthesizer.setVoice("siyue");
  85. //Optional. The intonation. Value range: -500 to 500. Default value: 0.
  86. synthesizer.setPitchRate(100);
  87. //The speed. Value range: -500 to 500. Default value: 0.
  88. synthesizer.setSpeechRate(100);
  89. //Set the text to be synthesized.
  90. synthesizer.setText("hello world!");
  91. //Serialize preceding parameters to the JSON format and send them to the server for confirmation.
  92. long start = System.currentTimeMillis();
  93. synthesizer.start();
  94. logger.info("tts start latency " + (System.currentTimeMillis() - start) + " ms");
  95. SpeechSynthesizerDemo.startTime = System.currentTimeMillis();
  96. //Wait until the speech synthesis is completed.
  97. synthesizer.waitForComplete();
  98. logger.info("tts stop latency " + (System.currentTimeMillis() - start) + " ms");
  99. } catch (Exception e) {
  100. e.printStackTrace();
  101. } finally {
  102. //关闭连接
  103. if (null != synthesizer) {
  104. synthesizer.close();
  105. }
  106. }
  107. }
  108. public void shutdown() {
  109. client.shutdown();
  110. }
  111. public static void main(String[] args) throws Exception {
  112. String appKey = "your appkey";
  113. String token = "your token";
  114. if (args.length == 2) {
  115. appKey = args[0];
  116. token = args[1];
  117. } else {
  118. System.err.println("run error, need params: " + "<app-key> <token>");
  119. System.exit(-1);
  120. }
  121. SpeechSynthesizerDemo demo = new SpeechSynthesizerDemo(appKey, token);
  122. demo.process();
  123. demo.shutdown();
  124. }
  125. }