All Products
Search
Document Center

Java SDK 2.0

Last Updated: Jun 02, 2020

Notes:

Download and installation

You can download the latest version of the SDK from the Maven repository:

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

For more information about how to use the Java SDK, see the sample code below. Download the Java SDK demo.

Decompress the demo package. Run the mvn package command from the pom directory. An executable JAR packagejar nls-example-tts-2.0.0-jar-with-dependencies.jar is generated in the target directory. Copy this JAR package to the target server. You can use it for quick service validation and stress testing.

Service validation

Run the java -cp nls-example-tts-2.0.0-jar-with-dependencies.jar com.alibaba.nls.client.SpeechSynthesizerDemo command.Set parameters as required. Then, the logs/nls.log file is generated in the directory where the command is run.

Stress testing

Run the java -jar nls-example-tts-2.0.0-jar-with-dependencies.jar command.Set parameters as required. The parameter for Alibaba Cloud URL is wss://nls-gateway-ap-southeast-1.aliyuncs.com/ws/v1. Set the maximum number of concurrent calls based on your purchased service.

Note: Stress testing on more than two concurrent calls will generate fees.

Key objects

  • NlsClient: the speech processing client, which is equivalent to a factory for all speech processing classes. You can globally create an NlsClient instance. This object is thread-safe.
  • SpeechSynthesizer: the speech synthesis object. You can use this object to set request parameters and send a request. This object is not thread-safe.
  • SpeechSynthesizerListener: the speech synthesis result listener, which listens to synthesis results. This object is not thread-safe. It implements the following two abstract methods:
  1. /**
  2. * Method to receive the speech synthesis binary data
  3. */
  4. abstract public void onMessage(ByteBuffer message);
  5. /**
  6. * Method to notify a speech synthesis completion event
  7. *
  8. * @param response
  9. */
  10. abstract public void onComplete(SpeechSynthesizerResponse response);

For more information, see Java API Reference.

Notes on SDK calls

  1. You can globally create an NlsClient object and reuse it if necessary. Based on Netty, the creation of an NlsClient object consumes time and resources, but the created NlsClient object can be reused. We recommend that you create and close an NlsClient object based on the lifecycle of your application.
  2. The SpeechSynthesizer object cannot be reused. You must create a SpeechSynthesizer object for each speech synthesis task. For example, to process N audio files, you must create N SpeechSynthesizer objects to complete N speech synthesis tasks.
  3. A SpeechSynthesizerListener object corresponds to a SpeechSynthesizer object. You cannot use a SpeechSynthesizerListener object for multiple SpeechSynthesizer objects. Otherwise, you may fail to distinguish speech synthesis tasks.
  4. The Java SDK is dependent on Netty. The version of Netty must be 4.1.17.Final or later. If your application is dependent on Netty, ensure that the version of Netty is appropriate.

Sample code

Note 1: The demo shows how to use the specific URL when you create the NlsClient object.

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

Note 2: In the demo, the synthesized audio is stored in a file. If you need to play the audio in real time, we recommend that you use stream playback to receive audio data while playing. This reduces the latency.

Example:

  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. //Close connection
  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. }