The RESTful API of the speech synthesis service allows you to send HTTPS GET or POST requests to upload text to the server for speech synthesis. The server synthesizes speech from the text based on your parameter settings. You must make sure that the connection with the server is not interrupted before the speech synthesis result is returned.
Features
The RESTful API supports the following features:
Converts input text to audio files in pulse-code modulation (PCM), WAV, and MP3 formats.
Allows you to set the sampling rates of the synthesized audio files to 8,000 Hz or 16,000 Hz.
Provides multiple voice types.
Allows you to set the speed, intonation, and volume of the voice.
Increasingly complex algorithms are used to ensure constant improvement of the text-to-speech (TTS) synthesis effect. This may increase the synthesis time. To resolve this issue, we recommend that you use stream synthesis. For more information, see the sample code in this topic and the demo project that is provided in the SDK.
The input text in a single service request can contain up to 300 characters. If the text contains more than 300 characters, excess characters are truncated. For more information about how to synthesize speech from long text that contains more than 300 characters, see the sample code that the SDK provides to segment and splice long text.
Download the nls-restful-java-demo.zip package.
The demo for Java is added to demonstrate how to call the RESTful API to send a speech synthesis request. In addition, the demo programs that are used to enable HTTP chunked transfer encoding and divide long text into segments for synthesis are provided.
Prerequisites
A project is created in the Intelligent Speech Interaction console and the appkey of the project is obtained. For more information, see Create a project.
An access token that is used to call Intelligent Speech Interaction services is obtained. For more information, see Obtain an access token.
Endpoints
Access type | Description | URL | Host |
External access from the Internet | This endpoint allows you to access the speech synthesis service from any host over the Internet. | nls-gateway-ap-southeast-1.aliyuncs.com/stream/v1/tts | nls-gateway-ap-southeast-1.aliyuncs.com |
Internal access from an Elastic Compute Service (ECS) instance in the China (Shanghai) region | This endpoint allows you to access the speech synthesis service from an ECS instance in the China (Shanghai) region over an internal network. | nls-gateway.ap-southeast-1-internal.aliyuncs.com/stream/v1/tts | nls-gateway.ap-southeast-1-internal.aliyuncs.com |
In the following example, the speech synthesis service is accessed over the Internet. To use an ECS instance in the China (Shanghai) region to access the speech synthesis service over an internal network, use HTTP instead of HTTPS. In addition, replace the URL and host for Internet access with those for internal access. In the following example, all demos except the demo for Python support both HTTP and HTTPS. Read the relevant notes before you use the demo for Python.
Interaction process
The client sends an HTTPS GET or POST request that contains the input text to the server. Then, the server returns an HTTPS response that contains the synthesized speech data.
The server adds the task_id parameter to the response message for all responses to indicate the ID of the synthesis task. You must record the value of this parameter. If an error occurs, join the DingTalk group (ID: 23050005920) to report the task ID and error message for technical support.
Request parameters
The following table describes the request parameters for speech synthesis.
If you send an HTTPS GET request, you must specify the request parameters in the HTTPS URL.
If you send an HTTPS POST request, you must specify the request parameters in the HTTPS request body.
Parameter | Type | Required | Description |
appkey | String | Yes | The appkey of your project. |
text | String | Yes | The text from which you want to synthesize speech. The text must be |
token | String | No | The token that is used for service authentication. If you do not set this parameter, you must set the |
format | String | No | The audio coding format. Valid formats: PCM, WAV, and MP3. Default value: PCM. |
sample_rate | Integer | No | The audio sampling rate. Unit: Hz. Valid values: 16000 and 8000. Default value: 16000. |
voice | String | No | The voice type. Default value: xiaoyun. For more information about available voice types, see Overview of speech synthesis. |
volume | Integer | No | The volume of the voice. Valid values: 0 to 100. Default value: 50. |
speech_rate | Integer | No | The speed of the voice. Valid values: -500 to 500. Default value: 0. |
pitch_rate | Integer | No | The intonation of the voice. Valid values: -500 to 500. Default value: 0. |
Upload text by using the HTTPS GET method
An HTTPS GET request of the speech synthesis RESTful API contains the following elements:
URL
Protocol
URL
Method
HTTPS
nls-gateway-ap-southeast-1.aliyuncs.com/stream/v1/tts
GET
Request parameters
For more information, see the Request parameters section of this topic. The following example shows a request link that is composed of the preceding URL and request parameters. You can open this link in a browser to obtain the speech synthesis result.
# text: "Today is Monday. It is a fine day." https://nls-gateway-ap-southeast-1.aliyuncs.com/stream/v1/tts?appkey=${Your appkey}&token=${Your token}&text=%E4%BB%8A%E5%A4%A9%E6%98%AF%E5%91%A8%E4%B8%80%EF%BC%8C%E5%A4%A9%E6%B0%94%E6%8C%BA%E5%A5%BD%E7%9A%84%E3%80%82&format=wav&sample_rate=16000
HTTP GET request header
Header
Type
Required
Description
X-NLS-Token
String
No
The token that is used for service authentication.
You can set the token by using one of the following methods:
(Recommended) Set the
token
parameter in the request parameters.Set the
X-NLS-Token
header in the HTTPS request.
The
text
parameter must beUTF-8
encoded. You must encode the URL based on the RFC 3986 standard. For example, you must encode a plus sign (+
) as%2B
, an asterisk (*
) as%2A
, and a tilde (~
) as%7E
.
Upload text by using the HTTPS POST method
An HTTPS POST request of the speech synthesis RESTful API contains the following elements:
URL
Protocol
URL
Method
HTTPS
nls-gateway-ap-southeast-1.aliyuncs.com/stream/v1/tts
POST
HTTPS POST request headers
Header
Type
Required
Description
Content-Type
String
Yes
The content type of the HTTPS request body. Set the value to application/json. This indicates that the content of the HTTPS request body must be a JSON string.
X-NLS-Token
String
No
The token that is used for service authentication.
Content-Length
long
No
The content length of the HTTPS request body.
HTTPS POST request body
The HTTPS POST request body is a JSON string that is composed of request parameters. You must set the Content-Type header in the HTTPS POST request to application/json. Example:
{ "appkey":"31f932fb", "text": "Today is Monday. It is a fine day." "token":"450343c793aaaaaa****", "format":"wav" }
You can set the token by using one of the following methods:
(Recommended) Set the
token
parameter in the request body.Set the
X-NLS-Token
header in the HTTPS request.
If you send an HTTPS POST request, the
text
parameter in the request body must beUTF-8
encoded. However, you do not need to encode the URL based on the RFC 3986 standard.
Responses
The responses to an HTTPS GET request and an HTTPS POST request are the same. The responses are included in the HTTPS response body. Whether a request succeeds or fails is specified by the value of the Content-Type
header in the HTTPS response.
Success response
If the synthesis is successful, the value of the
Content-Type
header in the HTTPS response isaudio/mpeg
. The synthesized audio data is included in the response body.The value of the
X-NLS-RequestId
header in the HTTPS response is the ID of the synthesis task.The content of the response body is the synthesized audio data in binary format.
Error response
If the synthesis fails, the HTTPS response does not contain the
Content-Type
header or the value of theContent-Type
header isapplication/json
. The response body includes the error message.The value of the
X-NLS-RequestId
header in the HTTPS response is the ID of the synthesis task.The response body includes the error message. The error message is a JSON string. Example:
{ "task_id":"8f95d0b9b6e948bc98e8d0ce64b0****", "result":"", "status":40000000, "message":"Gateway:CLIENT_ERROR:in post data, json format illegal" }
The following table describes the parameters in the error message.
Parameter
Type
Description
task_id
String
The task ID that consists of 32 characters. Record this value for troubleshooting.
result
String
The service result.
status
Integer
The service status code.
message
String
The description of the service status.
Service status codes
Service status code | Service status description | Solution |
20000000 | The request is successful. | N/A |
40000000 | The error message returned because a client error has occurred. This is the default client error code. | View error messages or join the DingTalk group (ID: 23050005920) for technical support. |
40000001 | The error message returned because the client fails authentication. | Check whether the token that is used by the client is correct and valid. |
40000002 | The error message returned because the request is invalid. | Check whether the request that is sent by the client meets the requirements. |
40000003 | The error message returned because one or more parameters are invalid. | Check whether the parameter value is correct. |
40000004 | The error message returned because the idle status of the client times out. | Check whether the client does not send data to the server for a long time. |
40000005 | The error message returned because the number of requests exceeds the upper limit. | Check whether the number of concurrent connections or queries per second (QPS) exceeds the upper limit. |
50000000 | The error message returned because a server error has occurred. This is the default server error code. | If the error code is occasionally returned, ignore the error code. If the error code is repeatedly returned, join the DingTalk group (ID: 23050005920) for technical support. |
50000001 | The error message returned because an internal gRPC call error has occurred. | If the error code is occasionally returned, ignore the error code. If the error code is repeatedly returned, join the DingTalk group (ID: 23050005920) for technical support. |
Demo for Java
Dependencies:
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.9.1</version>
</dependency>
<!-- http://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>
<dependency>
<groupId>org.asynchttpclient</groupId>
<artifactId>async-http-client</artifactId>
<version>2.5.4</version>
</dependency>
Sample code:
import java.io.File;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import com.alibaba.fastjson.JSONObject;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class SpeechSynthesizerRestfulDemo {
private String accessToken;
private String appkey;
public SpeechSynthesizerRestfulDemo(String appkey, String token) {
this.appkey = appkey;
this.accessToken = token;
}
/**
* HTTPS GET request
*/
public void processGETRequet(String text, String audioSaveFile, String format, int sampleRate, String voice) {
/**
* Set the HTTPS GET request.
* 1. Protocol: HTTPS.
* 2. Endpoint: nls-gateway-ap-southeast-1.aliyuncs.com.
* 3. Request path: /stream/v1/tts.
* 4. Required parameters: appkey, token, text, format, and sample_rate.
* 5. Optional parameters: voice, volume, speech_rate, and pitch_rate.
*/
String url = "https://nls-gateway-ap-southeast-1.aliyuncs.com/stream/v1/tts";
url = url + "?appkey=" + appkey;
url = url + "&token=" + accessToken;
url = url + "&text=" + text;
url = url + "&format=" + format;
url = url + "&voice=" + voice;
url = url + "&sample_rate=" + String.valueOf(sampleRate);
// voice: optional. The voice type. Default value: xiaoyun.
// url = url + "&voice=" + "xiaoyun";
// volume: optional. The volume of the voice. Valid values: 0 to 100. Default value: 50.
// url = url + "&volume=" + String.valueOf(50);
// speech_rate: optional. The speed of the voice. Valid values: -500 to 500. Default value: 0.
// url = url + "&speech_rate=" + String.valueOf(0);
// pitch_rate: optional. The intonation of the voice. Valid values: -500 to 500. Default value: 0.
// url = url + "&pitch_rate=" + String.valueOf(0);
System.out.println("URL: " + url);
/**
* Send the HTTPS GET request and process the response from the server.
*/
Request request = new Request.Builder().url(url).get().build();
try {
long start = System.currentTimeMillis();
OkHttpClient client = new OkHttpClient();
Response response = client.newCall(request).execute();
System.out.println("total latency :" + (System.currentTimeMillis() - start) + " ms");
System.out.println(response.headers().toString());
String contentType = response.header("Content-Type");
if ("audio/mpeg".equals(contentType)) {
File f = new File(audioSaveFile);
FileOutputStream fout = new FileOutputStream(f);
fout.write(response.body().bytes());
fout.close();
System.out.println("The GET request succeed!");
}
else {
// The value of the ContentType parameter is null or application/json.
String errorMessage = response.body().string();
System.out.println("The GET request failed: " + errorMessage);
}
response.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* HTTPS POST request
*/
public void processPOSTRequest(String text, String audioSaveFile, String format, int sampleRate, String voice) {
/**
* Set the HTTPS POST request.
* 1. Protocol: HTTPS.
* 2. Endpoint: nls-gateway-ap-southeast-1.aliyuncs.com.
* 3. Request path: /stream/v1/tts.
* 4. Required parameters: appkey, token, text, format, and sample_rate.
* 5. Optional parameters: voice, volume, speech_rate, and pitch_rate.
*/
String url = "https://nls-gateway-ap-southeast-1.aliyuncs.com/stream/v1/tts";
JSONObject taskObject = new JSONObject();
taskObject.put("appkey", appkey);
taskObject.put("token", accessToken);
taskObject.put("text", text);
taskObject.put("format", format);
taskObject.put("voice", voice);
taskObject.put("sample_rate", sampleRate);
// voice: optional. The voice type. Default value: xiaoyun.
// taskObject.put("voice", "xiaoyun");
// volume: optional. The volume of the voice. Valid values: 0 to 100. Default value: 50.
// taskObject.put("volume", 50);
// speech_rate: optional. The speed of the voice. Valid values: -500 to 500. Default value: 0.
// taskObject.put("speech_rate", 0);
// pitch_rate: optional. The intonation of the voice. Valid values: -500 to 500. Default value: 0.
// taskObject.put("pitch_rate", 0);
String bodyContent = taskObject.toJSONString();
System.out.println("POST Body Content: " + bodyContent);
RequestBody reqBody = RequestBody.create(MediaType.parse("application/json"), bodyContent);
Request request = new Request.Builder()
.url(url)
.header("Content-Type", "application/json")
.post(reqBody)
.build();
try {
OkHttpClient client = new OkHttpClient();
Response response = client.newCall(request).execute();
String contentType = response.header("Content-Type");
if ("audio/mpeg".equals(contentType)) {
File f = new File(audioSaveFile);
FileOutputStream fout = new FileOutputStream(f);
fout.write(response.body().bytes());
fout.close();
System.out.println("The POST request succeed!");
}
else {
// The value of the ContentType parameter is null or application/json.
String errorMessage = response.body().string();
System.out.println("The POST request failed: " + errorMessage);
}
response.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
if (args.length < 2) {
System.err.println("SpeechSynthesizerRestfulDemo need params: <token> <app-key>");
System.exit(-1);
}
String token = args[0];
String appkey = args[1];
SpeechSynthesizerRestfulDemo demo = new SpeechSynthesizerRestfulDemo(appkey, token);
String text = "Today is Monday. It is a fine day."
// Encode the URL based on the RFC 3986 standard.
String textUrlEncode = text;
try {
textUrlEncode = URLEncoder.encode(textUrlEncode, "UTF-8")
.replace("+", "%20")
.replace("*", "%2A")
.replace("%7E", "~");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println(textUrlEncode);
String audioSaveFile = "syAudio.wav";
String format = "wav";
int sampleRate = 16000;
demo.processGETRequet(textUrlEncode, audioSaveFile, format, sampleRate, "siyue");
//demo.processPOSTRequest(text, audioSaveFile, format, sampleRate, "siyue");
System.out.println("### Game Over ###");
}
}
Java sample code for stream synthesis:
import java.io.File;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.concurrent.CountDownLatch;
import io.netty.handler.codec.http.HttpHeaders;
import org.asynchttpclient.AsyncHandler;
import org.asynchttpclient.AsyncHttpClient;
import org.asynchttpclient.AsyncHttpClientConfig;
import org.asynchttpclient.DefaultAsyncHttpClient;
import org.asynchttpclient.DefaultAsyncHttpClientConfig;
import org.asynchttpclient.HttpResponseBodyPart;
import org.asynchttpclient.HttpResponseStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* This demo demonstrates the following two operations:
* 1. Call the RESTful API operation for speech synthesis.
* 2. Enable HTTP chunked transfer encoding.
*/
public class SpeechSynthesizerRestfulChunkedDemo {
private static Logger logger = LoggerFactory.getLogger(SpeechSynthesizerRestfulChunkedDemo.class);
private String accessToken;
private String appkey;
public SpeechSynthesizerRestfulChunkedDemo(String appkey, String token) {
this.appkey = appkey;
this.accessToken = token;
}
public void processGETRequet(String text, String audioSaveFile, String format, int sampleRate, String voice, boolean chunked) {
/**
* Set the HTTPS GET request.
* 1. Protocol: HTTPS.
* 2. Endpoint: nls-gateway-ap-southeast-1.aliyuncs.com.
* 3. Request path: /stream/v1/tts.
* 4. Required parameters: appkey, token, text, format, and sample_rate.
* 5. Optional parameters: voice, volume, speech_rate, and pitch_rate.
* 6. chunk: specifies whether to enable HTTP chunked transfer encoding.
*/
String url = "https://nls-gateway-ap-southeast-1.aliyuncs.com/stream/v1/tts";
url = url + "?appkey=" + appkey;
url = url + "&token=" + accessToken;
url = url + "&text=" + text;
url = url + "&format=" + format;
url = url + "&voice=" + voice;
url = url + "&sample_rate=" + String.valueOf(sampleRate);
url = url + "&chunk=" + String.valueOf(chunked);
System.out.println("URL: " + url);
try {
AsyncHttpClientConfig config = new DefaultAsyncHttpClientConfig.Builder()
.setConnectTimeout(3000)
.setKeepAlive(true)
.setReadTimeout(10000)
.setRequestTimeout(50000)
.setMaxConnections(1000)
.setMaxConnectionsPerHost(200)
.setPooledConnectionIdleTimeout(-1)
.build();
AsyncHttpClient httpClient = new DefaultAsyncHttpClient(config);
CountDownLatch latch = new CountDownLatch(1);
AsyncHandler<org.asynchttpclient.Response> handler = new AsyncHandler<org.asynchttpclient.Response>() {
FileOutputStream outs;
boolean firstRecvBinary = true;
long startTime = System.currentTimeMillis();
int httpCode = 200;
@Override
public State onStatusReceived(HttpResponseStatus httpResponseStatus) throws Exception {
logger.info("onStatusReceived status {}", httpResponseStatus);
httpCode = httpResponseStatus.getStatusCode();
if (httpResponseStatus.getStatusCode() != 200) {
logger.error("request error " + httpResponseStatus.toString());
}
return null;
}
@Override
public State onHeadersReceived(HttpHeaders httpHeaders) throws Exception {
outs = new FileOutputStream(new File("tts.wav"));
return null;
}
@Override
public State onBodyPartReceived(HttpResponseBodyPart httpResponseBodyPart) throws Exception {
// Note: When a data stream is received, the server can immediately play the stream to the user or process the stream to improve the response speed.
// Note: Do not perform time-consuming operations in this callback. You can push binary voice streams asynchronously or in queue mode to another thread.
logger.info("onBodyPartReceived " + httpResponseBodyPart.getBodyPartBytes().toString());
if(httpCode != 200) {
System.err.write(httpResponseBodyPart.getBodyPartBytes());
}
if (firstRecvBinary) {
firstRecvBinary = false;
// Calculate the first packet latency. The synthesized speech can be played or sent to the caller immediately after the first packet is received. Note: The first packet latency includes the time that is consumed for establishing a network connection.
logger.info("tts first latency " + (System.currentTimeMillis() - startTime) + " ms");
}
// In this example, the voice stream is saved to a file.
outs.write(httpResponseBodyPart.getBodyPartBytes());
return null;
}
@Override
public void onThrowable(Throwable throwable) {
logger.error("throwable {}", throwable);
latch.countDown();
}
@Override
public org.asynchttpclient.Response onCompleted() throws Exception {
logger.info("completed");
logger.info("tts total latency " + (System.currentTimeMillis() - startTime) + " ms");
outs.close();
latch.countDown();
return null;
}
};
httpClient.prepareGet(url).execute(handler);
// Wait until the synthesis is complete.
latch.await();
httpClient.close();
}catch (Exception e) {
}
}
public static void main(String[] args) {
if (args.length < 2) {
System.err.println("SpeechSynthesizerRestfulDemo need params: <token> <app-key>");
System.exit(-1);
}
String token = args[0];
String appkey = args[1];
SpeechSynthesizerRestfulChunkedDemo demo = new SpeechSynthesizerRestfulChunkedDemo(appkey, token);
String text = "Behind our house was a great garden known in our family as Hundred-Plant Garden. It has long since been sold, together with the house, to the descendants of Zhu Xi; and the last time I saw it, already seven or eight years ago. I am pretty sure there were only weeds growing there. But in my childhood it was my paradise."
// Encode the URL based on the RFC 3986 standard.
String textUrlEncode = text;
try {
textUrlEncode = URLEncoder.encode(textUrlEncode, "UTF-8")
.replace("+", "%20")
.replace("*", "%2A")
.replace("%7E", "~");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println(textUrlEncode);
String audioSaveFile = "syAudio.wav";
String format = "wav";
int sampleRate = 16000;
// A value of true for the last parameter indicates that HTTP chunked transfer encoding is enabled.
demo.processGETRequet(textUrlEncode, audioSaveFile, format, sampleRate, "aixia", true);
System.out.println("### Game Over ###");
}
}
Demo for C++
The demo for C++ uses the third-party library cURL to process HTTPS requests and responses, and uses JsonCpp to generate the JSON string in the HTTPS POST request body.
Minimum runtime environment for Linux: Glibc 2.5 or later, and GCC 4 or GCC 5.
In Windows, decompress the windows.zip package in the lib directory for compilation.
The demo package contains the following content:
CMakeLists.txt: the CMakeList file of the demo project.
demo: the demo file.
File
Description
restfulTtsDemo.cpp
The demo of using the RESTful API for speech synthesis.
include
Directory
Description
curl
The header file directory of the cURL library.
json
The header file directory of the JsonCpp library.
lib: the library, which contains the cURL dynamic library and JsonCpp dynamic library.
Based on the operating system, you can select the following versions:
Linux: Glibc 2.5 or later, and GCC 4 or GCC 5
Windows: Visual Studio 2013 or Visual Studio 2015
readme.txt: the description.
release.log: the release notes.
version: the version number.
build.sh: the demo compilation script.
Procedure for compiling and running the demo:
Assume that the demo is decompressed to the path/to
directory. Perform the following operations on a Linux terminal to compile and run the program.
If the operating system supports CMake:
Make sure that CMake 2.4 or later is installed in the local operating system.
Run the
cd path/to/sdk/lib
command.Run the
tar -zxvpf linux.tar.gz
command.Run the
cd path/to/sdk
command.Run the
./build.sh
command.Run the
cd path/to/sdk/demo
command.Run the
./restfulTtsDemo <your-token> <your-appkey>
command.
If the operating system does not support CMake, run the following commands in sequence:
cd path/to/sdk/lib
tar -zxvpf linux.tar.gz
cd path/to/sdk/demo
g++ -o restfulTtsDemo restfulTtsDemo.cpp -I path/to/sdk/include -L path/to/sdk/lib/linux -ljsoncpp -lssl -lcrypto -lcurl -D_GLIBCXX_USE_CXX11_ABI=0
export LD_LIBRARY_PATH=path/to/sdk/lib/linux/
./restfulTtsDemo <your-token> <your-appkey>
Sample code:
#ifdef _WIN32
#include <Windows.h>
#endif
#include <iostream>
#include <string>
#include <map>
#include <fstream>
#include <sstream>
#include "curl/curl.h"
#include "json/json.h"
using namespace std;
#ifdef _WIN32
string GBKToUTF8(const string &strGBK) {
string strOutUTF8 = "";
WCHAR * str1;
int n = MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, NULL, 0);
str1 = new WCHAR[n];
MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, str1, n);
n = WideCharToMultiByte(CP_UTF8, 0, str1, -1, NULL, 0, NULL, NULL);
char * str2 = new char[n];
WideCharToMultiByte(CP_UTF8, 0, str1, -1, str2, n, NULL, NULL);
strOutUTF8 = str2;
delete[] str1;
str1 = NULL;
delete[] str2;
str2 = NULL;
return strOutUTF8;
}
#endif
void stringReplace(string& src, const string& s1, const string& s2) {
string::size_type pos = 0;
while ((pos = src.find(s1, pos)) != string::npos) {
src.replace(pos, s1.length(), s2);
pos += s2.length();
}
}
string urlEncode(const string& src) {
CURL* curl = curl_easy_init();
char* output = curl_easy_escape(curl, src.c_str(), src.size());
string result(output);
curl_free(output);
curl_easy_cleanup(curl);
return result;
}
size_t responseHeadersCallback(void* ptr, size_t size, size_t nmemb, void* userdata)
{
map<string, string> *headers = (map<string, string>*)userdata;
string line((char*)ptr);
string::size_type pos = line.find(':');
if (pos != line.npos)
{
string name = line.substr(0, pos);
string value = line.substr(pos + 2);
size_t p = 0;
if ((p = value.rfind('\r')) != value.npos) {
value = value.substr(0, p);
}
headers->insert(make_pair(name, value));
}
return size * nmemb;
}
size_t responseBodyCallback(void* ptr, size_t size, size_t nmemb, void* userData) {
size_t len = size * nmemb;
char* pBuf = (char*)ptr;
string* bodyContent = (string*)userData;
(*bodyContent).append(string(pBuf, pBuf + len));
return len;
}
int processGETRequest(string appKey, string token, string text,
string audioSaveFile, string format, int sampleRate) {
CURL* curl = NULL;
CURLcode res;
curl = curl_easy_init();
if (curl == NULL) {
return -1;
}
string url = "https://nls-gateway-ap-southeast-1.aliyuncs.com/stream/v1/tts";
/**
* Set the request parameters in the HTTPS URL.
*/
ostringstream oss;
oss << url;
oss << "?appkey=" << appKey;
oss << "&token=" << token;
oss << "&text=" << text;
oss << "&format=" << format;
oss << "&sample_rate=" << sampleRate;
// voice: optional. The voice type. Default value: xiaoyun.
// oss << "&voice=" << "xiaoyun";
// volume: optional. The volume of the voice. Valid values: 0 to 100. Default value: 50.
// oss << "&volume=" << 50;
// speech_rate: optional. The speed of the voice. Valid values: -500 to 500. Default value: 0.
// oss << "&speech_rate=" << 0;
// pitch_rate: optional. The intonation of the voice. Valid values: -500 to 500. Default value: 0.
// oss << "&pitch_rate=" << 0;
string request = oss.str();
cout << request << endl;
curl_easy_setopt(curl, CURLOPT_URL, request.c_str());
/**
* Set the callback function for obtaining the headers in the HTTPS response.
*/
map<string, string> responseHeaders;
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, responseHeadersCallback);
curl_easy_setopt(curl, CURLOPT_HEADERDATA, &responseHeaders);
/**
* Set the callback function for obtain the body of the HTTPS response.
*/
string bodyContent = "";
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, responseBodyCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &bodyContent);
/**
* Send the HTTPS GET request.
*/
res = curl_easy_perform(curl);
/**
* Release resources.
*/
curl_easy_cleanup(curl);
if (res != CURLE_OK) {
cerr << "curl_easy_perform failed: " << curl_easy_strerror(res) << endl;
return -1;
}
/**
* Process the response from the server.
*/
map<string, string>::iterator it = responseHeaders.find("Content-Type");
if (it != responseHeaders.end() && it->second.compare("audio/mpeg") == 0) {
ofstream fs;
fs.open(audioSaveFile.c_str(), ios::out | ios::binary);
if (!fs.is_open()) {
cout << "The audio save file can not open!";
return -1;
}
fs.write(bodyContent.c_str(), bodyContent.size());
fs.close();
cout << "The GET request succeed!" << endl;
}
else {
cout << "The GET request failed: " + bodyContent << endl;
return -1;
}
return 0;
}
int processPOSTRequest(string appKey, string token, string text,
string audioSaveFile, string format, int sampleRate) {
CURL* curl = NULL;
CURLcode res;
curl = curl_easy_init();
if (curl == NULL) {
return -1;
}
string url = "https://nls-gateway-ap-southeast-1.aliyuncs.com/stream/v1/tts";
/**
* Set the URL of the HTTPS POST request.
*/
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_POST, 1L);
/**
* Set the headers in the HTTPS POST request.
*/
struct curl_slist* headers = NULL;
// Content-Type
headers = curl_slist_append(headers, "Content-Type:application/json");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
/**
* Set the body of the HTTPS POST request.
*/
Json::Value root;
Json::FastWriter writer;
root["appkey"] = appKey;
root["token"] = token;
root["text"] = text;
root["format"] = format;
root["sample_rate"] = sampleRate;
// voice: optional. The voice type. Default value: xiaoyun.
// root["voice"] = "xiaoyun";
// volume: optional. The volume of the voice. Valid values: 0 to 100. Default value: 50.
// root["volume"] = 50;
// speech_rate: optional. The speed of the voice. Valid values: -500 to 500. Default value: 0.
// root["speech_rate"] = 0;
// pitch_rate: optional. The intonation of the voice. Valid values: -500 to 500. Default value: 0.
// root["pitch_rate"] = 0;
string task = writer.write(root);
cout << "POST request Body: " << task << endl;
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, task.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, task.length());
/**
* Set the callback function for obtaining the headers in the HTTPS response.
*/
map<string, string> responseHeaders;
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, responseHeadersCallback);
curl_easy_setopt(curl, CURLOPT_HEADERDATA, &responseHeaders);
/**
* Set the callback function for obtaining the body of the HTTPS response.
*/
string bodyContent = "";
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, responseBodyCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &bodyContent);
/**
* Send the HTTPS POST request.
*/
res = curl_easy_perform(curl);
/**
* Release resources.
*/
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
if (res != CURLE_OK) {
cerr << "curl_easy_perform failed: " << curl_easy_strerror(res) << endl;
return -1;
}
/**
* Process the response from the server.
*/
map<string, string>::iterator it = responseHeaders.find("Content-Type");
if (it != responseHeaders.end() && it->second.compare("audio/mpeg") == 0) {
ofstream fs;
fs.open(audioSaveFile.c_str(), ios::out | ios::binary);
if (!fs.is_open()) {
cout << "The audio save file can not open!";
return -1;
}
fs.write(bodyContent.c_str(), bodyContent.size());
fs.close();
cout << "The POST request succeed!" << endl;
}
else {
cout << "The POST request failed: " + bodyContent << endl;
return -1;
}
return 0;
}
int main(int argc, char* argv[]) {
if (argc < 3) {
cerr << "params is not valid. Usage: ./demo your_token your_appkey" << endl;
return -1;
}
string token = argv[1];
string appKey = argv[2];
string text = "Today is Monday. It is a fine day."
#ifdef _WIN32
text = GBKToUTF8(text);
#endif
string textUrlEncode = urlEncode(text);
stringReplace(textUrlEncode, "+", "%20");
stringReplace(textUrlEncode, "*", "%2A");
stringReplace(textUrlEncode, "%7E", "~");
string audioSaveFile = "syAudio.wav";
string format = "wav";
int sampleRate = 16000;
// The demo is globally initialized only once.
curl_global_init(CURL_GLOBAL_ALL);
processGETRequest(appKey, token, textUrlEncode, audioSaveFile, format, sampleRate);
//processPOSTRequest(appKey, token, text, audioSaveFile, format, sampleRate);
curl_global_cleanup();
return 0;
}
Demo for Python
Use the httplib module for Python 2.x. Use the http.client module for Python 3.x.
Encode the URL based on the RFC 3986 standard. Use urllib.quote of the urllib module for Python 2.x. Use urllib.parse.quote_plus of the urllib.parse module for Python 3.x.
If you access the service by using an internal access URL, you must use HTTP. In this case, replace
HTTPSConnection
withHTTPConnection
.# Use the httplib module for Python 2.x. # conn = httplib.HTTPConnection(host) # Use the http.client module for Python 3.x. conn = http.client.HTTPConnection(host)
# -*- coding: UTF-8 -*-
# Import the httplib module for Python 2.x.
# import httplib
# Import the http.client module for Python 3.x.
import http.client
# Import the urllib module for Python 2.x.
# import urllib
# Import the urllib.parse module for Python 3.x.
import urllib.parse
import json
def processGETRequest(appKey, token, text, audioSaveFile, format, sampleRate) :
host = 'nls-gateway-ap-southeast-1.aliyuncs.com'
url = 'https://' + host + '/stream/v1/tts'
# Set the request parameters in the URL.
url = url + '?appkey=' + appKey
url = url + '&token=' + token
url = url + '&text=' + text
url = url + '&format=' + format
url = url + '&sample_rate=' + str(sampleRate)
# voice: optional. The voice type. Default value: xiaoyun.
# url = url + '&voice=' + 'xiaoyun'
# volume: optional. The volume of the voice. Valid values: 0 to 100. Default value: 50.
# url = url + '&volume=' + str(50)
# speech_rate: optional. The speed of the voice. Valid values: -500 to 500. Default value: 0.
# url = url + '&speech_rate=' + str(0)
# pitch_rate: optional. The intonation of the voice. Valid values: -500 to 500. Default value: 0.
# url = url + '&pitch_rate=' + str(0)
print(url)
# Use the httplib module for Python 2.x.
# conn = httplib.HTTPSConnection(host)
# Use the http.client module for Python 3.x.
conn = http.client.HTTPSConnection(host)
conn.request(method='GET', url=url)
# Process the response from the server.
response = conn.getresponse()
print('Response status and response reason:')
print(response.status ,response.reason)
contentType = response.getheader('Content-Type')
print(contentType)
body = response.read()
if 'audio/mpeg' == contentType :
with open(audioSaveFile, mode='wb') as f:
f.write(body)
print('The GET request succeed!')
else :
print('The GET request failed: ' + str(body))
conn.close()
def processPOSTRequest(appKey, token, text, audioSaveFile, format, sampleRate) :
host = 'nls-gateway-ap-southeast-1.aliyuncs.com'
url = 'https://' + host + '/stream/v1/tts'
# Set the HTTPS headers.
httpHeaders = {
'Content-Type': 'application/json'
}
# Set the HTTPS body.
body = {'appkey': appKey, 'token': token, 'text': text, 'format': format, 'sample_rate': sampleRate}
body = json.dumps(body)
print('The POST request body content: ' + body)
# Use the httplib module for Python 2.x.
# conn = httplib.HTTPSConnection(host)
# Use the http.client module for Python 3.x.
conn = http.client.HTTPSConnection(host)
conn.request(method='POST', url=url, body=body, headers=httpHeaders)
# Process the response from the server.
response = conn.getresponse()
print('Response status and response reason:')
print(response.status ,response.reason)
contentType = response.getheader('Content-Type')
print(contentType)
body = response.read()
if 'audio/mpeg' == contentType :
with open(audioSaveFile, mode='wb') as f:
f.write(body)
print('The POST request succeed!')
else :
print('The POST request failed: ' + str(body))
conn.close()
appKey = 'Your appkey'
token = 'Your token'
text = 'Today is Monday. It is a fine day.' '
# Encode the URL based on the RFC 3986 standard.
textUrlencode = text
# Use urllib.quote for Python 2.x.
# textUrlencode = urllib.quote(textUrlencode, '')
# Use urllib.parse.quote_plus for Python 3.x.
textUrlencode = urllib.parse.quote_plus(textUrlencode)
textUrlencode = textUrlencode.replace("+", "%20")
textUrlencode = textUrlencode.replace("*", "%2A")
textUrlencode = textUrlencode.replace("%7E", "~")
print('text: ' + textUrlencode)
audioSaveFile = 'syAudio.wav'
format = 'wav'
sampleRate = 16000
# GET request
processGETRequest(appKey, token, textUrlencode, audioSaveFile, format, sampleRate)
# POST request
# processPOSTRequest(appKey, token, text, audioSaveFile, format, sampleRate)
Demo for PHP
The demo for PHP uses cURL functions. Make sure that you have installed PHP 4.0.2 or later and cURL extensions.
<?php
function processGETRequest($appkey, $token, $text, $audioSaveFile, $format, $sampleRate) {
$url = "https://nls-gateway-ap-southeast-1.aliyuncs.com/stream/v1/tts";
$url = $url . "?appkey=" . $appkey;
$url = $url . "&token=" . $token;
$url = $url . "&text=" . $text;
$url = $url . "&format=" . $format;
$url = $url . "&sample_rate=" . strval($sampleRate);
// voice: optional. The voice type. Default value: xiaoyun.
// $url = $url . "&voice=" . "xiaoyun";
// volume: optional. The volume of the voice. Valid values: 0 to 100. Default value: 50.
// $url = $url . "&volume=" . strval(50);
// speech_rate: optional. The speed of the voice. Valid values: -500 to 500. Default value: 0.
// $url = $url . "&speech_rate=" . strval(0);
// pitch_rate: optional. The intonation of the voice. Valid values: -500 to 500. Default value: 0.
// $url = $url . "&pitch_rate=" . strval(0);
print $url . "\n";
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
/**
* Set the HTTPS GET URL.
*/
curl_setopt($curl, CURLOPT_URL, $url);
/**
* Set the headers in the HTTPS response.
*/
curl_setopt($curl, CURLOPT_HEADER, TRUE);
/**
* Send the HTTPS GET request.
*/
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
$response = curl_exec($curl);
if ($response == FALSE) {
print "curl_exec failed!\n";
curl_close($curl);
return ;
}
/**
* Process the response from the server.
*/
$headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$headers = substr($response, 0, $headerSize);
$bodyContent = substr($response, $headerSize);
curl_close($curl);
if (stripos($headers, "Content-Type: audio/mpeg") != FALSE || stripos($headers, "Content-Type:audio/mpeg") != FALSE) {
file_put_contents($audioSaveFile, $bodyContent);
print "The GET request succeed!\n";
}
else {
print "The GET request failed: " . $bodyContent . "\n";
}
}
function processPOSTRequest($appkey, $token, $text, $audioSaveFile, $format, $sampleRate) {
$url = "https://nls-gateway-ap-southeast-1.aliyuncs.com/stream/v1/tts";
/**
* Set request parameters in the JSON format in the body of the HTTPS POST request.
*/
$taskArr = array(
"appkey" => $appkey,
"token" => $token,
"text" => $text,
"format" => $format,
"sample_rate" => $sampleRate
// voice: optional. The voice type. Default value: xiaoyun.
// "voice" => "xiaoyun",
// volume: optional. The volume of the voice. Valid values: 0 to 100. Default value: 50.
// "volume" => 50,
// speech_rate: optional. The speed of the voice. Valid values: -500 to 500. Default value: 0.
// "speech_rate" => 0,
// pitch_rate: optional. The intonation of the voice. Valid values: -500 to 500. Default value: 0.
// "pitch_rate" => 0
);
$body = json_encode($taskArr);
print "The POST request body content: " . $body . "\n";
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
/**
* Set the HTTPS POST URL.
*/
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, TRUE);
/**
* Set the headers in the HTTPS POST request.
* */
$httpHeaders = array(
"Content-Type: application/json"
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $httpHeaders);
/**
* Set the body of the HTTPS POST request.
*/
curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
/**
* Set the header in the HTTPS response.
*/
curl_setopt($curl, CURLOPT_HEADER, TRUE);
/**
* Send the HTTPS POST request.
*/
$response = curl_exec($curl);
if ($response == FALSE) {
print "curl_exec failed!\n";
curl_close($curl);
return ;
}
/**
* Process the response from the server.
*/
$headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$headers = substr($response, 0, $headerSize);
$bodyContent = substr($response, $headerSize);
curl_close($curl);
if (stripos($headers, "Content-Type: audio/mpeg") != FALSE || stripos($headers, "Content-Type:audio/mpeg") != FALSE) {
file_put_contents($audioSaveFile, $bodyContent);
print "The POST request succeed!\n";
}
else {
print "The POST request failed: " . $bodyContent . "\n";
}
}
$appkey = "Your appkey";
$token = "Your token";
$text = "Today is Monday. It is a fine day."
$textUrlEncode = urlencode($text);
$textUrlEncode = preg_replace('/\+/', '%20', $textUrlEncode);
$textUrlEncode = preg_replace('/\*/', '%2A', $textUrlEncode);
$textUrlEncode = preg_replace('/%7E/', '~', $textUrlEncode);
$audioSaveFile = "syAudio.wav";
$format = "wav";
$sampleRate = 16000;
processGETRequest($appkey, $token, $textUrlEncode, $audioSaveFile, $format, $sampleRate);
// processPOSTRequest($appkey, $token, $text, $audioSaveFile, $format, $sampleRate);
?>
Demo for Node.js
To install the request dependencies, run the following command in the directory of your demo:
npm install request --save
const request = require('request');
const fs = require('fs');
function processGETRequest(appkey, token, text, audioSaveFile, format, sampleRate) {
var url = 'https://nls-gateway-ap-southeast-1.aliyuncs.com/stream/v1/tts';
/**
* Set the request parameters in the URL.
*/
url = url + '?appkey=' + appkey;
url = url + '&token=' + token;
url = url + '&text=' + text;
url = url + '&format=' + format;
url = url + '&sample_rate=' + sampleRate;
// voice: optional. The voice type. Default value: xiaoyun.
// url = url + "&voice=" + "xiaoyun";
// volume: optional. The volume of the voice. Valid values: 0 to 100. Default value: 50.
// url = url + "&volume=" + 50;
// speech_rate: optional. The speed of the voice. Valid values: -500 to 500. Default value: 0.
// url = url + "&speech_rate=" + 0;
// pitch_rate: optional. The intonation of the voice. Valid values: -500 to 500. Default value: 0.
// url = url + "&pitch_rate=" + 0;
console.log(url);
/**
* Set the HTTPS GET request.
* Set the encoding parameter to null, and set the HTTPS response body to the binary buffer type.
*/
var options = {
url: url,
method: 'GET',
encoding: null
};
request(options, function (error, response, body) {
/**
* Process the response from the server.
*/
if (error != null) {
console.log(error);
}
else {
var contentType = response.headers['content-type'];
if (contentType === undefined || contentType != 'audio/mpeg') {
console.log(body.toString());
console.log('The GET request failed!');
}
else {
fs.writeFileSync(audioSaveFile, body);
console.log('The GET request is succeed!');
}
}
});
}
function processPOSTRequest(appkeyValue, tokenValue, textValue, audioSaveFile, formatValue, sampleRateValue) {
var url = 'https://nls-gateway-ap-southeast-1.aliyuncs.com/stream/v1/tts';
/**
* Set request parameters in the JSON format in the body of the HTTPS POST request.
*/
var task = {
appkey : appkeyValue,
token : tokenValue,
text : textValue,
format : formatValue,
sample_rate : sampleRateValue
// voice: optional. The voice type. Default value: xiaoyun.
// voice : 'xiaoyun',
// volume: optional. The volume of the voice. Valid values: 0 to 100. Default value: 50.
// volume : 50,
// speech_rate: optional. The speed of the voice. Valid values: -500 to 500. Default value: 0.
// speech_rate : 0,
// pitch_rate: optional. The intonation of the voice. Valid values: -500 to 500. Default value: 0.
// pitch_rate : 0
};
var bodyContent = JSON.stringify(task);
console.log('The POST request body content: ' + bodyContent);
/**
* Set the headers in the HTTPS POST request.
*/
var httpHeaders = {
'Content-type' : 'application/json'
}
/**
* Set the HTTPS POST request.
* Set the encoding parameter to null, and set the HTTPS response body to the binary buffer type.
*/
var options = {
url: url,
method: 'POST',
headers: httpHeaders,
body: bodyContent,
encoding: null
};
request(options, function (error, response, body) {
/**
* Process the response from the server.
*/
if (error != null) {
console.log(error);
}
else {
var contentType = response.headers['content-type'];
if (contentType === undefined || contentType != 'audio/mpeg') {
console.log(body.toString());
console.log('The POST request failed!');
}
else {
fs.writeFileSync(audioSaveFile, body);
console.log('The POST request is succeed!');
}
}
});
}
var appkey='Your appkey';
var token='Your token';
var text='Today is Monday. It is a fine day.' ';
var textUrlEncode = encodeURIComponent(text)
.replace(/[!'()*]/g, function(c) {
return '%' + c.charCodeAt(0).toString(16);
});
console.log(textUrlEncode);
var audioSaveFile = 'syAudio.wav';
var format = 'wav';
var sampleRate = 16000;
processGETRequest(appkey, token, textUrlEncode, audioSaveFile, format, sampleRate);
// processPOSTRequest(appkey, token, text, audioSaveFile, format, sampleRate);
Demo for .NET
The demo for .NET depends on System.Net.Http, System.Web, and Newtonsoft.Json.Linq.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net.Http;
using System.Web;
using Newtonsoft.Json.Linq;
namespace RESTfulAPI
{
class SpeechSynthesizerRESTfulDemo
{
private string appkey;
private string token;
public SpeechSynthesizerRESTfulDemo(string appkey, string token)
{
this.appkey = appkey;
this.token = token;
}
public void processGETRequest(string text, string audioSaveFile, string format, int sampleRate)
{
/**
* Set the HTTPS GET request.
* 1. Protocol: HTTPS.
* 2. Endpoint: nls-gateway-ap-southeast-1.aliyuncs.com.
* 3. Request path: /stream/v1/tts.
* 4. Required parameters: appkey, token, text, format, and sample_rate.
* 5. Optional parameters: voice, volume, speech_rate, and pitch_rate.
*/
string url = "http://nls-gateway.aliyuncs.com/stream/v1/tts";
url = url + "?appkey=" + appkey;
url = url + "&token=" + token;
url = url + "&text=" + text;
url = url + "&format=" + format;
url = url + "&sample_rate=" + sampleRate.ToString();
// voice: optional. The voice type. Default value: xiaoyun.
// url = url + "&voice=" + "xiaoyun";
// volume: optional. The volume of the voice. Valid values: 0 to 100. Default value: 50.
// url = url + "&volume=" + 50;
// speech_rate: optional. The speed of the voice. Valid values: -500 to 500. Default value: 0.
// url = url + "&speech_rate=" + 0;
// pitch_rate: optional. The intonation of the voice. Valid values: -500 to 500. Default value: 0.
// url = url + "&pitch_rate=" + 0;
System.Console.WriteLine(url);
/**
* Send the HTTPS GET request and process the response from the server.
*/
HttpClient client = new HttpClient();
HttpResponseMessage response = null;
response = client.GetAsync(url).Result;
string contentType = null;
if (response.IsSuccessStatusCode)
{
string[] typesArray = response.Content.Headers.GetValues("Content-Type").ToArray();
if (typesArray.Length > 0)
{
contentType = typesArray.First();
}
}
if ("audio/mpeg".Equals(contentType))
{
byte[] audioBuff = response.Content.ReadAsByteArrayAsync().Result;
FileStream fs = new FileStream(audioSaveFile, FileMode.Create);
fs.Write(audioBuff, 0, audioBuff.Length);
fs.Flush();
fs.Close();
System.Console.WriteLine("The GET request succeed!");
}
else
{
// The value of the ContentType parameter is null or application/json.
System.Console.WriteLine("Response status code and reason phrase: " +
response.StatusCode + " " + response.ReasonPhrase);
string responseBodyAsText = response.Content.ReadAsStringAsync().Result;
System.Console.WriteLine("The GET request failed: " + responseBodyAsText);
}
}
public void processPOSTRequest(string text, string audioSaveFile, string format, int sampleRate)
{
/**
* Set the HTTPS POST request.
* 1. Protocol: HTTPS.
* 2. Endpoint: nls-gateway-ap-southeast-1.aliyuncs.com.
* 3. Request path: /stream/v1/tts.
* 4. Required parameters: appkey, token, text, format, and sample_rate.
* 5. Optional parameters: voice, volume, speech_rate, and pitch_rate.
*/
string url = "https://nls-gateway-ap-southeast-1.aliyuncs.com/stream/v1/tts";
JObject obj = new JObject();
obj["appkey"] = appkey;
obj["token"] = token;
obj["text"] = text;
obj["format"] = format;
obj["sample_rate"] = sampleRate;
// voice: optional. The voice type. Default value: xiaoyun.
// obj["voice"] = "xiaoyun";
// volume: optional. The volume of the voice. Valid values: 0 to 100. Default value: 50.
// obj["volume"] = 50;
// speech_rate: optional. The speed of the voice. Valid values: -500 to 500. Default value: 0.
// obj["speech_rate"] = 0;
// pitch_rate: optional. The intonation of the voice. Valid values: -500 to 500. Default value: 0.
// obj["pitch_rate"] = 0;
String bodyContent = obj.ToString();
StringContent content = new StringContent(bodyContent, Encoding.UTF8, "application/json");
/**
* Send the HTTPS POST request and process the response from the server.
*/
HttpClient client = new HttpClient();
HttpResponseMessage response = client.PostAsync(url, content).Result;
string contentType = null;
if (response.IsSuccessStatusCode)
{
string[] typesArray = response.Content.Headers.GetValues("Content-Type").ToArray();
if (typesArray.Length > 0)
{
contentType = typesArray.First();
}
}
if ("audio/mpeg".Equals(contentType))
{
byte[] audioBuff = response.Content.ReadAsByteArrayAsync().Result;
FileStream fs = new FileStream(audioSaveFile, FileMode.Create);
fs.Write(audioBuff, 0, audioBuff.Length);
fs.Flush();
fs.Close();
System.Console.WriteLine("The POST request succeed!");
}
else
{
System.Console.WriteLine("Response status code and reason phrase: " +
response.StatusCode + " " + response.ReasonPhrase);
string responseBodyAsText = response.Content.ReadAsStringAsync().Result;
System.Console.WriteLine("The POST request failed: " + responseBodyAsText);
}
}
static void Main(string[] args)
{
if (args.Length < 2)
{
System.Console.WriteLine("SpeechSynthesizerRESTfulDemo need params: <token> <app-key>");
return;
}
string token = args[0];
string appkey = args[1];
SpeechSynthesizerRESTfulDemo demo = new SpeechSynthesizerRESTfulDemo(appkey, token);
string text = "Today is Monday. It is a fine day."
// Encode the URL based on the RFC 3986 standard.
string textUrlEncode = text;
textUrlEncode = HttpUtility.UrlEncode(textUrlEncode, Encoding.UTF8)
.Replace("+", "%20")
.Replace("*", "%2A")
.Replace("%7E", "~");
System.Console.WriteLine(textUrlEncode);
string audioSaveFile = "syAudio.wav";
string format = "wav";
int sampleRate = 16000;
demo.processGETRequest(textUrlEncode, audioSaveFile, format, sampleRate);
//demo.processPOSTRequest(text, audioSaveFile, format, sampleRate);
}
}
}
Demo for Go
package main
import (
"fmt"
"net/url"
"net/http"
"io/ioutil"
"encoding/json"
"strconv"
"os"
"bytes"
"strings"
)
func processGETRequest(appkey string, token string, text string, audioSaveFile string, format string, sampleRate int) {
/**
* Set the HTTPS GET request.
* 1. Protocol: HTTPS.
* 2. Endpoint: nls-gateway-ap-southeast-1.aliyuncs.com.
* 3. Request path: /stream/v1/tts.
* 4. Required parameters: appkey, token, text, format, and sample_rate.
* 5. Optional parameters: voice, volume, speech_rate, and pitch_rate.
*/
var url string = "https://nls-gateway-ap-southeast-1.aliyuncs.com/stream/v1/tts"
url = url + "?appkey=" + appkey
url = url + "&token=" + token
url = url + "&text=" + text
url = url + "&format=" + format
url = url + "&sample_rate=" + strconv.Itoa(sampleRate)
// voice: optional. The voice type. Default value: xiaoyun.
// url = url + "&voice=" + "xiaoyun"
// volume: optional. The volume of the voice. Valid values: 0 to 100. Default value: 50.
// url = url + "&volume=" + strconv.Itoa(50)
// speech_rate: optional. The speed of the voice. Valid values: -500 to 500. Default value: 0.
// url = url + "&speech_rate=" + strconv.Itoa(0)
// pitch_rate: optional. The intonation of the voice. Valid values: -500 to 500. Default value: 0.
// url = url + "&pitch_rate=" + strconv.Itoa(0)
fmt.Println(url)
/**
* Send the HTTPS GET request and process the response from the server.
*/
response, err := http.Get(url)
if err != nil {
fmt.Println("The GET request failed!")
panic(err)
}
defer response.Body.Close()
contentType := response.Header.Get("Content-Type")
body, _ := ioutil.ReadAll(response.Body)
if ("audio/mpeg" == contentType) {
file, _ := os.Create(audioSaveFile)
defer file.Close()
file.Write([]byte(body))
fmt.Println("The GET request succeed!")
} else {
// The value of the ContentType parameter is null or application/json.
statusCode := response.StatusCode
fmt.Println("The HTTP statusCode: " + strconv.Itoa(statusCode))
fmt.Println("The GET request failed: " + string(body))
}
}
func processPOSTRequest(appkey string, token string, text string, audioSaveFile string, format string, sampleRate int) {
/**
* Set the HTTPS POST request.
* 1. Protocol: HTTPS.
* 2. Endpoint: nls-gateway-ap-southeast-1.aliyuncs.com.
* 3. Request path: /stream/v1/tts.
* 4. Required parameters: appkey, token, text, format, and sample_rate.
* 5. Optional parameters: voice, volume, speech_rate, and pitch_rate.
*/
var url string = "https://nls-gateway-ap-southeast-1.aliyuncs.com/stream/v1/tts"
bodyContent := make(map[string]interface{})
bodyContent["appkey"] = appkey
bodyContent["text"] = text
bodyContent["token"] = token
bodyContent["format"] = format
bodyContent["sample_rate"] = sampleRate
// voice: optional. The voice type. Default value: xiaoyun.
// bodyContent["voice"] = "xiaoyun"
// volume: optional. The volume of the voice. Valid values: 0 to 100. Default value: 50.
// bodyContent["volume"] = 50
// speech_rate: optional. The speed of the voice. Valid values: -500 to 500. Default value: 0.
// bodyContent["speech_rate"] = 0
// pitch_rate: optional. The intonation of the voice. Valid values: -500 to 500. Default value: 0.
// bodyContent["pitch_rate"] = 0
bodyJson, err := json.Marshal(bodyContent)
if err != nil {
panic(nil)
}
fmt.Println(string(bodyJson))
/**
* Send the HTTPS POST request and process the response from the server.
*/
response, err := http.Post(url, "application/json;charset=utf-8", bytes.NewBuffer([]byte(bodyJson)))
if err != nil {
panic(err)
}
defer response.Body.Close()
contentType := response.Header.Get("Content-Type")
body, _ := ioutil.ReadAll(response.Body)
if ("audio/mpeg" == contentType) {
file, _ := os.Create(audioSaveFile)
defer file.Close()
file.Write([]byte(body))
fmt.Println("The POST request succeed!")
} else {
// The value of the ContentType parameter is null or application/json.
statusCode := response.StatusCode
fmt.Println("The HTTP statusCode: " + strconv.Itoa(statusCode))
fmt.Println("The POST request failed: " + string(body))
}
}
func main() {
var appkey string = "Your appkey"
var token string = "Your token"
var text string = "Today is Monday. It is a fine day."
var textUrlEncode = text
textUrlEncode = url.QueryEscape(textUrlEncode)
textUrlEncode = strings.Replace(textUrlEncode, "+", "%20", -1)
textUrlEncode = strings.Replace(textUrlEncode, "*", "%2A", -1)
textUrlEncode = strings.Replace(textUrlEncode, "%7E", "~", -1)
fmt.Println(textUrlEncode)
var audioSaveFile string = "syAudio.wav"
var format string = "wav"
var sampleRate int = 16000
processGETRequest(appkey, token, textUrlEncode, audioSaveFile, format, sampleRate)
// processPOSTRequest(appkey, token, text, audioSaveFile, format, sampleRate)
}