Python SDK

更新时间:
复制 MD 格式

This topic describes how to use the Python software development kit (SDK) for Alibaba Cloud Voice Service. It also provides installation instructions and code examples.

Prerequisites

Before you use the SDK, read the API reference. For more information, see API reference.

Download and installation

Note
  • The SDK supports only Python 3.

  • Ensure that you have installed the Python package management tool setuptools. If it is not installed, run the following command in the terminal to install it:

    pip install setuptools
  1. Download the Python SDK.

    You can obtain the Python SDK from GitHub or download streamInputTts-github-python directly.

  2. Install the SDK dependencies.

    Go to the SDK root directory and run the following command to install the dependencies:

    python -m pip install -r requirements.txt
  3. Install the SDK.

    After the dependencies are installed, run the following command to install the SDK:

    python -m pip install .
  4. After installation, import the SDK using the following code:

    # -*- coding: utf-8 -*-
    import nls
Important

Run all the preceding commands in the SDK root directory.

Multi-threading and concurrency

In CPython, the global interpreter lock allows only one thread to execute Python code at a time, although some performance-oriented libraries can remove this limitation. To better utilize the compute resources of a multi-core computer, use multiprocessing or concurrent.futures.ProcessPoolExecutor. If you want to run multiple I/O-intensive tasks concurrently, multi-threading is still a suitable model.

If a single interpreter has too many threads, switching between them causes significant overhead and may lead to SDK errors. We recommend that you do not use more than 200 threads. Instead, use the multiprocessing technique or manually create multiple interpreters using a script.

Key interfaces

The NlsSpeechSynthesizer class is used for speech synthesis. Its core methods are as follows:

1. Initialization (__init__)

When you initialize the NlsSpeechSynthesizer class, you can set the endpoint (url), authentication parameters (appkey and token), and callback functions (on_metainfo, on_data, on_error, and on_close).

  • Parameters

    Parameter

    Type

    Description

    url

    String

    The WebSocket URL of the gateway. Default value: wss://nls-gateway-cn-shanghai.aliyuncs.com/ws/v1.

    appkey

    String

    The AppKey. For more information about how to obtain an AppKey, see Manage projects.

    long_tts

    bool

    The speech synthesis method. Valid values:

    • True: Use real-time long-text speech synthesis. For more information, see API reference.

    • False: Use real-time short-text synthesis. This is the default value.

    token

    String

    The access token. For more information, see Overview of obtaining a token.

    on_metainfo

    Function

    The callback for caption information. This callback is triggered if `enable_subtitle` is passed in the `ex` parameter of the `start` method. The callback parameters include the following:

    • A JSON-formatted string

    • A user-defined parameter

    The user-defined parameter is the content returned in the `callback_args` field below.

    on_data

    Function

    The callback for when synthesized data is available. The callback parameters include the following:

    • Binary audio data in the format specified by the `aformat` parameter of the `start` method

    • A user-defined parameter

    The user-defined parameter is the content returned in the `callback_args` field below.

    on_error

    Function

    The callback for when an error occurs in the SDK or on the server-side. The callback parameters include the following:

    • A JSON-formatted string

    • A user-defined parameter

    The user-defined parameter is the content returned in the `callback_args` field below.

    on_close

    Function

    The callback for when the connection to the cloud is disconnected. The callback parameter is the user-defined parameter returned in the `callback_args` field below.

    callback_args

    List

    A list of user-defined parameters. The content of the list is packaged into a List data structure and passed as the last parameter to each callback.

  • Return value: None

2. start

Starts speech synthesis synchronously. If `wait_complete` is `True` (default), the method blocks until all audio is synthesized (after `on_completed` returns). Otherwise, it returns immediately.

  • Parameters

    Parameter

    Type

    Description

    text

    String

    The text to synthesize.

    Note

    To call the multi-emotion feature of a voice, add the ssml-emotion tag to the text. For more information, see <emotion>.

    If you use the <emotion> tag for a voice that does not support multiple emotions, the `Illegal ssml text` error is reported.

    aformat

    String

    The format of the synthesized audio. Supported output formats are PCM, WAV, and MP3. The default value is `pcm`.

    voice

    String

    The voice. The default value is `xiaoyun`.

    sample_rate

    Integer

    The audio sampling rate. Default value: 16000 Hz.

    volume

    Integer

    The volume. The value ranges from 0 to 100. Default value: 50.

    speech_rate

    Integer

    The speech rate. The value ranges from -500 to 500. Default value: 0.

    pitch_rate

    Integer

    The pitch. The value ranges from -500 to 500. Default value: 0.

    wait_complete

    Boolean

    Specifies whether to block until synthesis is complete.

    start_timeout

    Integer

    The timeout for establishing a connection with the cloud. Default value: 10 seconds.

    completed_timeout

    Integer

    The timeout from connection establishment to synthesis completion. Default value: 60 seconds.

    ping_interval

    Integer

    The interval for sending ping packets. Default value: 8 seconds. Set to 0 or None to disable.

    ping_timeout

    Integer

    Specifies whether to check for a pong packet timeout. Default value: None. `None` means no check is performed.

    ex

    Dict

    Extra parameters provided by the user.

    The only extra parameter that can be set is `enable_subtitle`:

    r = tts.start(self.__text, voice="ailun", ex={'enable_subtitle':True})
  • Extra parameters

    Parameter

    Type

    Description

    enable_subtitle

    Boolean

    Enables word-level timestamps. Set this using the `ex` parameter of the `start` method:

    r = tts.start(self.__text, voice="ailun", ex={'enable_subtitle':True})

    For more information about how to use this feature, see Introduction to the speech synthesis timestamp feature.

  • Return value: None

3. shutdown

Forcibly closes the current request. Repeated calls have no side effects.

  • Parameters: None

  • Return value: None

Code examples

Note
  • This example saves the synthesized audio to a file. For real-time playback, which requires low latency, use streaming playback. This means you play the audio data as you receive it.

  • This example uses the default public endpoint URL built into the SDK. If you use an Alibaba Cloud ECS instance in the China (Shanghai) region and need to access the service over the internal network, use the following URL: URL="wss://nls-gateway-cn-shanghai.aliyuncs.com/ws/v1".

import time
import threading
import sys

import nls

URL="wss://nls-gateway-cn-shanghai.aliyuncs.com/ws/v1"
TOKEN="yourToken"  # For more information about how to obtain a token, see https://help.aliyun.com/document_detail/450255.html
APPKEY="yourAppkey"       # To obtain an AppKey, go to the console: https://nls-portal.console.aliyun.com/applist



TEXT='Da Zhuang was about to pick the petals, but Ali and A Qiang suddenly started fighting. Ali took a pistol and shot at A Qiang by the tree trunk. After two gunshots, A Qiang fell directly into the water.'

# The following code repeatedly performs speech synthesis based on the preceding TEXT.
class TestTts:
    def __init__(self, tid, test_file):
        self.__th = threading.Thread(target=self.__test_run)
        self.__id = tid
        self.__test_file = test_file
   
    def start(self, text):
        self.__text = text
        self.__f = open(self.__test_file, "wb")
        self.__th.start()
    
    def test_on_metainfo(self, message, *args):
        print("on_metainfo message=>{}".format(message))  

    def test_on_error(self, message, *args):
        print("on_error args=>{}".format(args))

    def test_on_close(self, *args):
        print("on_close: args=>{}".format(args))
        try:
            self.__f.close()
        except Exception as e:
            print("close file failed since:", e)

    def test_on_data(self, data, *args):
        try:
            self.__f.write(data)
        except Exception as e:
            print("write data failed:", e)

    def test_on_completed(self, message, *args):
        print("on_completed:args=>{} message=>{}".format(args, message))


    def __test_run(self):
      	print("thread:{} start..".format(self.__id))
      	tts = nls.NlsSpeechSynthesizer(url=URL,
      	      	      	      	       token=TOKEN,
      	      	      	      	       appkey=APPKEY,
      	      	      	      	       on_metainfo=self.test_on_metainfo,
      	      	      	      	       on_data=self.test_on_data,
      	      	      	      	       on_completed=self.test_on_completed,
      	      	      	      	       on_error=self.test_on_error,
      	      	      	      	       on_close=self.test_on_close,
      	      	      	      	       callback_args=[self.__id])
      	print("{}: session start".format(self.__id))
      	r = tts.start(self.__text, voice="ailun")
      	print("{}: tts done with result:{}".format(self.__id, r))

def multiruntest(num=500):
    for i in range(0, num):
        name = "thread" + str(i)
        t = TestTts(name, "tests/test_tts.pcm")
        t.start(TEXT)

nls.enableTrace(True)
multiruntest(1)