- Anaconda
- Python 3.7
- ビジュアルスタジオコード
- をコンピューター
- APPID
- APISecret
- APIKey
pip install websocket-client pip install playsound次に、4 つの関数を含むクラス プレイを定義します
class play: def __init__(self): #初始化函数 def play_sound(self):#播放音频函数 def select_vcn(self,*arg):#选择下拉框设置发音人 def xfyun_tts(self):#进行语音合成ここでは、iFlytek オープン プラットフォーム コンソールから取得した appid、appkey、appsecret を入力する必要があります
def __init__(self): self.APP_ID = 'xxx' #请填上自己的appid self.API_KEY = 'xxx'#请填上自己的appkey self.SECRET_KEY = 'xxx' #请填上自己的appsecret self.root=tk.Tk() #初始化窗口 self.root.title("语音合成系统") #窗口名称 self.root.geometry("600x550") #设置窗口大小 self.root.resizable(0,0) #self.root.resizable(width=True,height=True)#设置窗口是否可变,宽不可变,高可变,默认为True self.lb=tk.Label(self.root,text='请选择语音发音人')#标签 self.tt=tk.Text(self.root,width=77,height=30) #多行文本框 self.cb=ttk.Combobox(self.root, width=12)#下拉列表框 #设置下拉列表框的内容 self.cb['values']=("甜美女声-小燕","亲切男声-许久","知性女声-小萍", "可爱童声-许小宝","亲切女声-小婧") self.cb.current(0)#将当前选择状态置为0,也就是第一项 self.cb.bind("<<ComboboxSelected>>", self.select_vcn) self.tk_tts_file=tk.Label(self.root,text='生成文件名') self.b1=tk.Button(self.root, text='进行语音合成', width=10,height=1,command=self.xfyun_tts) #按钮 self.tk_play=tk.Button(self.root, text='播放', width=10,height=1,command=self.play_sound) #按钮 #各个组件的位置 self.tk_tts_file.place(x=30,y=500) self.b1.place(x=300,y=500) self.tk_play.place(x=400,y=500) self.lb.place(x=30,y=30) self.cb.place(x=154,y=30) self.tt.place(x=30,y=60) self.root.mainloop()ドロップダウン リストが選択されたら、対応する発音記号を設定します
def select_vcn(self,*arg): if self.cb.get()=='甜美女声-小燕': self.vcn="xiaoyan" elif self.cb.get()=='亲切男声-许久': self.vcn="aisjiuxu" elif self.cb.get()=='知性女声-小萍': self.vcn="aisxping" elif self.cb.get()=='可爱童声-许小宝': self.vcn="aisbabyxu" elif self.cb.get()=='亲切女声-小婧': self.vcn="aisjinger" print(self.vcn)次に、iFlytek に付属の Python デモを変更して、より使いやすくします。さらに、パブリック アカウント プログラマー Xiaole のバックエンドを検索し、「お金を稼ぐ」と返信すると、サプライズ ギフト パッケージが届きます。
# -*- coding:utf-8 -*- # # author: iflytek # #本demo测试时运行的环境为:Windows + Python3.7 #本demo测试成功运行时所安装的第三方库及其版本如下: # cffi==1.12.3 # gevent==1.4.0 # greenlet==0.4.15 # pycparser==2.19 # six==1.12.0 # websocket==0.2.1 # websocket-client==0.56.0 # 合成小语种需要传输小语种文本、使用小语种发音人vcn、tte=unicode以及修改文本编码方式 #错误码链接:https://www.xfyun.cn/document/error-code (code返回错误码时必看) # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # import websocket import datetime import hashlib import base64 import hmac import json from urllib.parse import urlencode import time import ssl from wsgiref.handlers import format_date_time from datetime import datetime from time import mktime import _thread as thread import os import wave STATUS_FIRST_FRAME = 0# 第一帧的标识 STATUS_CONTINUE_FRAME = 1# 中间帧标识 STATUS_LAST_FRAME = 2# 最后一帧的标识 PCM_PATH = "./demo.pcm" class Ws_Param(object): # 初始化 def __init__(self): pass def set_tts_params(self, text, vcn): if text != "": self.Text = text if vcn != "": self.vcn = vcn # 业务参数(business),更多个性化参数可在官网查看 self.BusinessArgs = {"bgs":1,"aue": "raw", "auf": "audio/L16;rate=16000", "vcn": self.vcn, "tte": "utf8"} #使用小语种须使用以下方式,此处的unicode指的是 utf16小端的编码方式,即"UTF-16LE"” #self.Data = {"status": 2, "text": str(base64.b64encode(self.Text.encode('utf-16')), "UTF8")} self.Data = {"status": 2, "text": str(base64.b64encode(self.Text.encode('utf-8')), "UTF8")} def set_params(self, appid, apiSecret, apiKey): if appid != "": self.APPID = appid # 公共参数(common) self.CommonArgs = {"app_id": self.APPID} if apiKey != "": self.APIKey = apiKey if apiSecret != "": self.APISecret = apiSecret # 生成url def create_url(self): url = 'wss://tts-api.xfyun.cn/v2/tts' # 生成RFC1123格式的时间戳 now = datetime.now() date = format_date_time(mktime(now.timetuple())) # 拼接字符串 signature_origin = "host: " + "ws-api.xfyun.cn" + "n" signature_origin += "date: " + date + "n" signature_origin += "GET " + "/v2/tts " + "HTTP/1.1" # 进行hmac-sha256进行加密 signature_sha = hmac.new(self.APISecret.encode('utf-8'), signature_origin.encode('utf-8'), digestmod=hashlib.sha256).digest() signature_sha = base64.b64encode(signature_sha).decode(encoding='utf-8') authorization_origin = "api_key="%s", algorithm="%s", headers="%s", signature="%s"" % ( self.APIKey, "hmac-sha256", "host date request-line", signature_sha) authorization = base64.b64encode(authorization_origin.encode('utf-8')).decode(encoding='utf-8') # 将请求的鉴权参数组合为字典 v = { "authorization": authorization, "date": date, "host": "ws-api.xfyun.cn" } url = url + '?' + urlencode(v) return url def on_message(ws, message): try: #print(message) try: message =json.loads(message) except Exception as e: print("111",e) code = message["code"] sid = message["sid"] audio = message["data"]["audio"] audio = base64.b64decode(audio) status = message["data"]["status"] print(code, sid, status) if status == 2: print("ws is closed") ws.close() if code != 0: errMsg = message["message"] print("sid:%s call error:%s code is:%s" % (sid, errMsg, code)) else: with open(PCM_PATH, 'ab') as f: f.write(audio) except Exception as e: print("receive msg,but parse exception:", e) # 收到websocket错误的处理 def on_error(ws, error): print("### error:", error) # 收到websocket关闭的处理 def on_close(ws): print("### closed ###") # 收到websocket连接建立的处理 def on_open(ws): def run(*args): d = {"common": wsParam.CommonArgs, "business": wsParam.BusinessArgs, "data": wsParam.Data, } d = json.dumps(d) print("------>开始发送文本数据") ws.send(d) if os.path.exists(PCM_PATH): os.remove(PCM_PATH) thread.start_new_thread(run, ()) def text2pcm(appid, apiSecret, apiKey, text, vcn, fname): wsParam.set_params(appid, apiSecret, apiKey) wsParam.set_tts_params(text, vcn) websocket.enableTrace(False) wsUrl = wsParam.create_url() ws = websocket.WebSocketApp(wsUrl, on_message=on_message, on_error=on_error, on_close=on_close) ws.on_open = on_open ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE}) pcm2wav(PCM_PATH, fname) def pcm2wav(fname, dstname): with open(fname, 'rb') as pcmfile: pcmdata = pcmfile.read() print(len(pcmdata)) with wave.open(dstname, "wb") as wavfile: wavfile.setparams((1, 2, 16000, 0, 'NONE', 'NONE')) wavfile.writeframes(pcmdata) wsParam = Ws_Param()最終的な音声合成システムはこのようにして実装されました。
以上がPythonを使って音声合成システムを構築するの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

限られた時間でPythonの学習効率を最大化するには、PythonのDateTime、時間、およびスケジュールモジュールを使用できます。 1. DateTimeモジュールは、学習時間を記録および計画するために使用されます。 2。時間モジュールは、勉強と休息の時間を設定するのに役立ちます。 3.スケジュールモジュールは、毎週の学習タスクを自動的に配置します。

PythonはゲームとGUI開発に優れています。 1)ゲーム開発は、2Dゲームの作成に適した図面、オーディオ、その他の機能を提供し、Pygameを使用します。 2)GUI開発は、TKINTERまたはPYQTを選択できます。 TKINTERはシンプルで使いやすく、PYQTは豊富な機能を備えており、専門能力開発に適しています。

Pythonは、データサイエンス、Web開発、自動化タスクに適していますが、Cはシステムプログラミング、ゲーム開発、組み込みシステムに適しています。 Pythonは、そのシンプルさと強力なエコシステムで知られていますが、Cは高性能および基礎となる制御機能で知られています。

2時間以内にPythonの基本的なプログラミングの概念とスキルを学ぶことができます。 1.変数とデータ型、2。マスターコントロールフロー(条件付きステートメントとループ)、3。機能の定義と使用を理解する4。

Pythonは、Web開発、データサイエンス、機械学習、自動化、スクリプトの分野で広く使用されています。 1)Web開発では、DjangoおよびFlask Frameworksが開発プロセスを簡素化します。 2)データサイエンスと機械学習の分野では、Numpy、Pandas、Scikit-Learn、Tensorflowライブラリが強力なサポートを提供します。 3)自動化とスクリプトの観点から、Pythonは自動テストやシステム管理などのタスクに適しています。

2時間以内にPythonの基本を学ぶことができます。 1。変数とデータ型を学習します。2。ステートメントやループの場合などのマスター制御構造、3。関数の定義と使用を理解します。これらは、簡単なPythonプログラムの作成を開始するのに役立ちます。

10時間以内にコンピューター初心者プログラミングの基本を教える方法は?コンピューター初心者にプログラミングの知識を教えるのに10時間しかない場合、何を教えることを選びますか...

fiddlereveryversings for the-middleの測定値を使用するときに検出されないようにする方法


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

ドリームウィーバー CS6
ビジュアル Web 開発ツール

Safe Exam Browser
Safe Exam Browser は、オンライン試験を安全に受験するための安全なブラウザ環境です。このソフトウェアは、あらゆるコンピュータを安全なワークステーションに変えます。あらゆるユーティリティへのアクセスを制御し、学生が無許可のリソースを使用するのを防ぎます。

SublimeText3 Linux 新バージョン
SublimeText3 Linux 最新バージョン

MantisBT
Mantis は、製品の欠陥追跡を支援するために設計された、導入が簡単な Web ベースの欠陥追跡ツールです。 PHP、MySQL、Web サーバーが必要です。デモおよびホスティング サービスをチェックしてください。

WebStorm Mac版
便利なJavaScript開発ツール
