API reference

Updated at:

The mobile NUI SDK converts text to speech. You can select a voice and audio format, and adjust the speech rate, pitch, and volume.

Description

The NUI SDK provides end-to-end speech capabilities and can also be used for speech synthesis alone. Speech synthesis supports PCM, WAV, and MP3 audio output. The SDK delivers synthesized audio and events through callbacks.

For supported voices, languages, sample rates, and timestamp capabilities, see the voice list. In the mobile SDK, set font_name to the corresponding voice value in the list.

Limitations

  • Input text must be UTF-8 encoded.

  • Short-text synthesis processes up to 300 characters per request. Text beyond this limit is truncated.

    To synthesize text that exceeds 300 characters, set tts_version to "1" to use long-text synthesis.

Service endpoints

Set url to the service endpoint when you initialize the SDK.

Access method

Region

URL

Internet

Shanghai (default)

wss://nls-gateway-cn-shanghai.aliyuncs.com/ws/v1

Beijing

wss://nls-gateway-cn-beijing.aliyuncs.com/ws/v1

Shenzhen

wss://nls-gateway-cn-shenzhen.aliyuncs.com/ws/v1

ECS internal network

Shanghai

ws://nls-gateway-cn-shanghai-internal.aliyuncs.com:80/ws/v1

Beijing

ws://nls-gateway-cn-beijing-internal.aliyuncs.com:80/ws/v1

Shenzhen

ws://nls-gateway-cn-shenzhen-internal.aliyuncs.com:80/ws/v1

Note

ECS instances in China (Shanghai), China (Beijing), or China (Shenzhen) can access the internal endpoint in the same region through a virtual private cloud (VPC). This access does not incur Internet traffic fees for the ECS instance. Classic networks do not support access to speech services. Use a VPC instead.

Interaction process

image

Authenticate and initialize

The client uses an NLS token for authentication when establishing a WebSocket connection. For instructions, see Obtain a token.

Pass the following parameters during initialization. In the Android SDK, call tts_initialize with a JSON string that contains these parameters.

Parameter

Type

Description

app_key (required)

String

The Appkey of the project created in the console.

token (required)

String

A valid NLS token. Set it during initialization or update it through the parameter-setting method.

device_id (required)

String

A unique device identifier, such as a MAC address, SN, or UniquePsuedoID.

mode_type (required)

String

The online speech synthesis mode. Set this parameter to the string "2".

url

String

The service endpoint. Use the endpoint that corresponds to the project and token.

workspace (required)

String

The working directory from which the SDK reads configuration files. This parameter can be omitted for online-only synthesis in Android SDK 2.7.4.

Start synthesis

After initialization, configure synthesis parameters by calling setparamTts, and then start a speech synthesis request.

Parameter

Type

Description

app_key

String

The project Appkey. The value set during initialization remains available.

token

String

Set this parameter when the authentication token needs to be updated.

font_name

String

The voice. The default is xiaoyun. For valid values, see the voice values in the voice list.

encode_type

String

The audio encoding format. Valid values: pcm, wav, and mp3. The default is pcm.

sample_rate

String

The audio sample rate in Hz. The default is 16000.

volume

String

The volume. Valid range: 0 to 2. The default is 1.0.

speed_level

String

The speech rate. Valid range: 0 to 2. The default is 1.0. A larger value produces faster speech.

pitch_level

String

The pitch. Valid range: -500 to 500. The default is 0. A larger value produces a higher pitch.

enable_subtitle

String

The timestamp switch. Set it to "1" to enable timestamps or "0" to disable them. This parameter applies only to voices that support timestamps.

tts_version

String

The synthesis mode. "0" specifies short-text synthesis. You can omit this parameter for short-text synthesis. "1" specifies long-text synthesis, which can process text longer than 300 characters.

Additional parameter

Type

Description

direct_host

String

Allows the client to resolve the service domain name and pass an IP address to access the service.

custom_params

String

Sets parameters supported by the interaction protocol but not listed above. Use custom_params as the key and a JSON string containing the additional parameters as the value.

Receive synthesized audio

The server returns synthesized audio as binary data. The SDK provides the audio data through callbacks.

Complete synthesis

When synthesis is complete, the server sends a synthesis completion event.

Error codes

If an error occurs during synthesis, the SDK reports a TTS_EVENT_ERROR event with error details.

For error descriptions and solutions, see Error codes.