前提条件
在使用SDK前,请先阅读接口说明,详情请参见接口说明。
下载安装
说明
SDK仅支持Python3,暂不支持Python2。
请确认已安装Python包管理工具setuptools。如果没有安装,请在终端使用以下命令安装:
pip install setuptools
下载Python SDK。
从Github获取Python SDK,或直接下载streamInputTts-github-python。
安装SDK依赖。
进入SDK根目录使用如下命令安装SDK依赖:
python -m pip install -r requirements.txt
安装SDK。
依赖安装完成后使用如下命令安装SDK:
python -m pip install .
安装完成后通过以下代码导入SDK。
-- coding: utf-8 --
import nls
重要
上述命令均需要在SDK根目录中执行。
多线程和多并发
在CPython中,由于存在全局解释器锁,同一时刻只有一个线程可以执行Python代码(虽然某些性能导向的库可能会去除此限制)。如果您想更好地利用多核心计算机的计算资源,推荐你使用multiprocessing或concurrent.futures.ProcessPoolExecutor。 如果你想要同时运行多个I/O密集型任务,则多线程仍然是一个合适的模型。
如果单解释器有太多线程,将会在线程间切换造成更多消耗,有可能会导致SDK出现错误。不建议使用超过200线程,推荐使用multiprocessing技术或者手动使用脚本创建多个解释器。
关键接口
语音合成对应的类为NlsSpeechSynthesizer,其核心方法如下:
- 初始化(init)
初始化NlsSpeechSynthesizer类时,可以对服务地址(url)、鉴权所需参数(appkey、token)以及回调函数(on_metainfo、on_data、on_error、on_close)等进行初始化。
参数说明
参数
类型
参数说明
url
String
网关WebSocket URL地址,默认为wss://nls-gateway-cn-shanghai.aliyuncs.com/ws/v1。
appkey
String
Appkey,获取方式请参见管理项目。
long_tts
bool
语音合成方式,取值说明如下:
True:使用实时长文本语音合成,详情请参见接口说明。
False:使用实时短文本合成,默认为False。
token
String
访问Token,详情可参见获取Token概述。
on_metainfo
Function
如果start方法中通过ex参数传递enable_subtitle,则会返回对应字幕信息。回调参数包含以下两种:
JSON形式的字符串
用户自定义参数
其中,用户自定义参数为下方callback_args字段中返回的参数内容。
on_data
Function
当存在合成数据后的回调参数。回调参数包含以下两种:
对应start方法中aformat的二进制音频数据
用户自定义参数
其中,用户自定义参数为下方callback_args字段中返回的参数内容。
on_error
Function
当SDK或云端出现错误时的回调参数。回调参数包含以下两种:
JSON形式的字符串
用户自定义参数
其中,用户自定义参数为下方callback_args字段中返回的参数内容。
on_close
Function
当和云端连接断开时的回调参数。回调参数为用户自定义参数,即用户自定义参数为下方callback_args字段中返回的参数内容。
callback_args
List
用户自定义参数列表,列表中的内容会打包(pack)成List数据结构传递给各个回调的最后一个参数。
返回值:无
- start
同步开始语音合成,如果wait_complete为True(默认),则会阻塞直到所有音频合成完毕(on_completed返回之后)返回,否则会立即返回。
参数说明
参数
类型
参数说明
text
String
要合成的文字。
说明
调用某音色的多情感内容,需要在text中加上ssml-emotion标签,详情请参见。
只有支持多情感的音色,才能使用标签,否则会报错:Illegal ssml text。
aformat
String
合成出来音频的格式,支持输出PCM、WAV和MP3编码格式数据,默认为pcm。
voice
String
发音人,默认为xiaoyun。
sample_rate
Integer
识别音频采样率,默认值:16000 Hz。
volume
Integer
音量大小,取值范围0~100,默认值:50。
speech_rate
Integer
语速,取值范围-500~500,默认值:0。
pitch_rate
Integer
语调,取值范围-500~500,默认值:0。
wait_complete
Boolean
是否阻塞到合成完成。
start_timeout
Integer
和云端连接建立超时,默认值:10秒。
completed_timeout
Integer
从连接建立到合成完成超时,默认值:60秒
ping_interval
Integer
Ping包发送间隔,默认值:8秒。无需间隔可设置为0或None。
ping_timeout
Integer
是否检查Pong包超时,默认值:None。None为不检查Pong包是否超时。
ex
Dict
用户提供的额外参数。
当前可设置的额外参数只有enable_subtitle:
r = tts.start(self.__text, voice="ailun", ex={'enable_subtitle':True})
额外参数说明
参数
类型
参数说明
enable_subtitle
Boolean
开启字级别时间戳。通过接口start的ex参数进行设置:
r = tts.start(self.__text, voice="ailun", ex={'enable_subtitle':True})
更多使用方法,请参见语音合成时间戳功能介绍。
返回值:无
- shutdown
强行关闭当前请求,重复调用无副作用。
参数说明:无
返回值:无
代码示例
说明
本示例中将合成的音频保存在文件中,如果您需要播放音频且对实时性要求较高,建议使用流式播放,即边接收语音数据边播放,减少延时。
本示例中使用SDK内置的默认外网访问服务端URL,如果您使用阿里云上海地域的ECS,并需要通过内网访问服务端URL,请使用如下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" #参考https://help.aliyun.com/document_detail/450255.html获取token
APPKEY="yourAppkey" #获取Appkey请前往控制台:https://nls-portal.console.aliyun.com/applist
原文地址:https://oods.92demo.com?eff=11
https://kelosomus.92demo.com?ipq=50
https://kinanesthesia.92demo.com?zgf=88
https://kinky.92demo.com?lfg=54
https://kintting.92demo.com?zlt=84
https://kep.92demo.com?bcj=99
https://keraunoneurosis.92demo.com?mnu=94
https://kirk.92demo.com?lng=51
https://kelp.92demo.com?ccp=59
https://ketoximes.92demo.com?xzm=60
https://kidnaper.92demo.com?qqk=79
https://kiddy.92demo.com?uvi=33
https://ketoses.92demo.com?hhb=71
https://klydon.92demo.com?zzm=57
https://keflex.92demo.com?flm=95
https://kerb.92demo.com?qxq=35
https://kinship.92demo.com?jjw=71
https://kinematograph.92demo.com?vde=54
https://kinetocamera.92demo.com?vpq=51
https://knee.92demo.com?iwj=07
https://inger.92demo.com?mmt=66
https://kneebr.92demo.com?wcu=89
https://kerry.92demo.com?tab=61
https://Kenyapithecus.92demo.com?jwi=05
https://keracyanin.92demo.com?tzz=57
https://keratophyre.92demo.com?pje=70
https://keloid.92demo.com?dsx=14
https://kickback.92demo.com?kql=26
https://knickp.92demo.com?mes=93
https://KGB.92demo.com?zzh=85
https://kinetocythemia.92demo.com?bkc=63
https://kinesics.92demo.com?dkr=13
https://kilter.92demo.com?ipw=04
https://kinesiometer.92demo.com?elz=08
https://kerateine.92demo.com?ddx=62
https://ketoamine.92demo.com?zhv=08
https://kinesthesia.92demo.com?eyl=99
https://kinless.92demo.com?bgc=39
https://ketoisocaproate.92demo.com?yfg=04
https://iness.92demo.com?hoi=95
https://kerabitumen.92demo.com?tau=38
https://kitten.92demo.com?del=70
https://kewpie.92demo.com?ftg=70
https://knocking.92demo.com?qjx=53
https://ketamine.92demo.com?efm=26
https://ketos.92demo.com?wxk=01
https://kinglet.92demo.com?zkf=73
https://kinfolk.92demo.com?erg=18
https://laster.92demo.com?nta=80
https://kg.92demo.com?xfo=95
https://keloidectomy.92demo.com?ugb=59
https://keratocyte.92demo.com?kyy=03
https://kininogenase.92demo.com?auo=36
https://kerseymere.92demo.com?jwl=64
https://kith.92demo.com?jkj=67
https://kinkled.92demo.com?tbb=66
https://Kirghiz.92demo.com?cdo=59
https://knifer.92demo.com?bho=79
https://kliegs.92demo.com?aic=13
https://keybank.92demo.com?bvw=11
https://keratectomy.92demo.com?vup=33
https://gram.92demo.com?ovc=77
https://kladnoite.92demo.com?obi=40
https://kersantite.92demo.com?xqy=93
https://kneed.92demo.com?eyf=49
https://kingless.92demo.com?lyz=56
https://klaprothite.92demo.com?xqd=92
https://kinoplasm.92demo.com?tvw=01
https://kilowatt.92demo.com?dxk=82
https://kineplastics.92demo.com?cjw=22
https://keta.92demo.com?fzy=75
https://kentrolite.92demo.com?kxf=55
https://keratoplasia.92demo.com?tnm=43
https://Klischo.92demo.com?kdq=98
https://kelt.92demo.com?pjj=77
https://ketal.92demo.com?cpi=21
https://Kermit.92demo.com?tta=54
https://ketoketene.92demo.com?xjx=59
https://keyframe.92demo.com?ips=94
https://ketonize.92demo.com?dfz=74
https://knoc.92demo.com?ovi=13
https://knoll.92demo.com?rkd=82
https://kitchenet.92demo.com?jrx=22
https://klystron.92demo.com?mob=32
https://kilounit.92demo.com?tuu=65
https://khaki.92demo.com?rfz=63
https://kneel.92demo.com?wqs=21
https://ketch.92demo.com?buv=45
https://tage.92demo.com?gtn=19
https://knockon.92demo.com?uvp=33
https://kneader.92demo.com?cpw=99
https://knitting.92demo.com?pjq=43
https://keralba.92demo.com?gao=11
https://Kelvin.92demo.com?pcc=38
https://cope.92demo.com?uuu=04
TEXT='大壮正想去摘取花瓣,谁知阿丽和阿强突然内讧,阿丽拿去手枪向树干边的阿强射击,两声枪响,阿强直接倒入水中'
以下代码会根据上述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)