一.申请阿里云的key和appkey。
因为我已经申请完了,所以直接从网上找个流程,粘贴到这里。如下
语音试听地址:/nls/tts
当然也可点击文章头部的语音,看看是否你需要的类型。
注册阿里云这些步骤,我就省略了啊。
阿里云官网:/
第一步
1、注册之后点击链接进入控制台:
https://nls-portal./overview
会提醒你开通语音合成,点击即可
2、进入控制台创建一个项目——点击总览或全部项目——【创建项目】
3、创建之后记得保存——appkey至文本,后面会用到
二.安装python SDK
按照下面页面指示进行。
/document_detail/120699.html?spm=a2c4g.11186623.6.594.22a42bc0iltYNk
在python setup.py install的时候,会遇到下面的错误。
命令行上面内容省略。。。
Installed c:\users\administrator\appdata\local\programs\python\python36\lib\site-packages\alibabacloud_nls_java_sdk-2.0.0-py3.6.egg
Processing dependencies for alibabacloud-nls-java-sdk==2.0.0
Searching for websocket-client
Reading /simple/websocket-client/
Download error on /simple/websocket-client/: timed out -- Some packages may not be found!
Couldn't find index page for 'websocket-client' (maybe misspelled?)
Scanning index of all packages (this may take a while)
Reading /simple/
Download error on /simple/: timed out -- Some packages may not be found!
No local packages or working download links found for websocket-client
error: Could not find suitable distribution for Requirement.parse('websocket-client')
解决办法是运行
pip install incremental==17.5.0
再运行就没有错误了。成功安装。
像下面这样是安装成功了。
修改示例代码,在文章的最下面。
运行出现错误。
G:\vscode_python\语音合成>C:/Users/Administrator/AppData/Local/Programs/Python/Python36/python.exe g:/vscode_python/语音合成/make_audio.py
-06-29 18:24:39,921 alispeech-WARNING _on_error:123 retry start: [WinError 10042] 在 getsockopt 或 setsockopt 调用中指定的一个未知的、无
效的或不受支持的选项或层次。
MyCallback.OnRecognitionChannelClosed
-06-29 18:24:50,268 alispeech-WARNING _on_error:123 retry start: [WinError 10042] 在 getsockopt 或 setsockopt 调用中指定的一个未知的、无
效的或不受支持的选项或层次。
MyCallback.OnRecognitionChannelClosed
-06-29 18:25:00,575 alispeech-ERROR _on_error:115 [WinError 10042] 在 getsockopt 或 setsockopt 调用中指定的一个未知的、无效的或不受支持
的选项或层次。
MyCallback.OnRecognitionTaskFailed-task_id:0, status_text:[WinError 10042] 在 getsockopt 或 setsockopt 调用中指定的一个未知的、无效的或不受支
持的选项或层次。
MyCallback.OnRecognitionChannelClosed
-06-29 18:25:00,584 alispeech-ERROR start:156 start failed, status: 6
查了很久,发现是python版本的问题。
/questions/47962516/python-error-10042-pusher-websocket
I saw the same error using a different library that uses websockets. I can see from your description (and link) that Pysher uses websockets.
I found (yet another) websocket client for Python that reported an issue with websockets, specifically with Python 3.6.4: [/websocket-client/websocket-client/issues/370]
It references the bug in Python tracker as well [/issue32394]
Upgrading to Python 3.6.5 worked for me. Alternatively, they suggest that upgrading to Windows 10 1703+ should work too (just for completeness; I have not verified this).
所以,不要用python3.6.4,后来我下载安装了python3.8.就可以正常运行了。
demo代码如下:
# -*- coding: utf-8 -*-import threadingimport ali_speechfrom ali_speech.callbacks import SpeechSynthesizerCallbackfrom ali_speech.constant import TTSFormatfrom ali_speech.constant import TTSSampleRateclass MyCallback(SpeechSynthesizerCallback):# 参数name用于指定保存音频的文件def __init__(self, name):self._name = nameself._fout = open(name, 'wb')def on_binary_data_received(self, raw):print('MyCallback.on_binary_data_received: %s' % len(raw))self._fout.write(raw)def on_completed(self, message):print('MyCallback.OnRecognitionCompleted: %s' % message)self._fout.close()def on_task_failed(self, message):print('MyCallback.OnRecognitionTaskFailed-task_id:%s, status_text:%s' % (message['header']['task_id'], message['header']['status_text']))self._fout.close()def on_channel_closed(self):print('MyCallback.OnRecognitionChannelClosed')def process(client, appkey, token, text, audio_name, voice):callback = MyCallback(audio_name)synthesizer = client.create_synthesizer(callback)synthesizer.set_appkey(appkey)synthesizer.set_token(token)synthesizer.set_voice(voice)synthesizer.set_text(text)synthesizer.set_format(TTSFormat.WAV)synthesizer.set_sample_rate(TTSSampleRate.SAMPLE_RATE_16K)synthesizer.set_volume(50)synthesizer.set_speech_rate(-200)synthesizer.set_pitch_rate(0)try:ret = synthesizer.start()if ret < 0:return retsynthesizer.wait_completed()except Exception as e:print(e)finally:synthesizer.close()def process_multithread(client, appkey, token, number):thread_list = []for i in range(0, number):text = "这是线程" + str(i) + "的合成。"audio_name = "sy_audio_" + str(i) + ".wav"thread = threading.Thread(target=process, args=(client, appkey, token, text, audio_name, voice))thread_list.append(thread)thread.start()for thread in thread_list:thread.join()if __name__ == "__main__":client = ali_speech.NlsClient()# 设置输出日志信息的级别:DEBUG、INFO、WARNING、ERRORclient.set_log_level('INFO')voice = 'AiDa'appkey = '你自己的appkey'token = '你自己的token'text = "这里是测试的文字。"audio_name = 'sy_audio.wav'process(client, appkey, token, text, audio_name, voice)# 多线程示例# process_multithread(client, appkey, token, 2)
还有一个需要补充的是,帮助里面说java sdk是可以获取生成的语音的时间戳的。但是没有提Python。
自己找也没有找到相应的函数。
其他扩展资料:
网上高手用pyqt5写的语音合成:
/NH4L/voiceAssistant/tree/6dfcba16a644e7e1f9f67e7ce3eddeafd565c7dc
如果觉得《阿里云语音合成使用流程完全记录》对你有帮助,请点赞、收藏,并留下你的观点哦!