|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 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
-
-
-
- STATUS_FIRST_FRAME = 0
- STATUS_CONTINUE_FRAME = 1
- STATUS_LAST_FRAME = 2
-
-
-
- global outfile
-
- global wsParam
-
-
-
- class Ws_Param(object):
-
- def __init__(self, APPID, APIKey, APISecret,BusinessArgs,Text):
- self.APPID = APPID
- self.APIKey = APIKey
- self.APISecret = APISecret
- self.BusinessArgs = BusinessArgs
- self.Text = Text
-
-
- self.CommonArgs = {"app_id": self.APPID}
-
-
- self.Data = {"status": 2, "text": str(base64.b64encode(self.Text.encode('utf-8')), "UTF8")}
-
-
-
-
- def create_url(self):
- url = 'wss://tts-api.xfyun.cn/v2/tts'
-
- 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"
-
- 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):
-
- global outfile
- try:
- message =json.loads(message)
- code = message["code"]
- sid = message["sid"]
- audio = message["data"]["audio"]
- audio = base64.b64decode(audio)
- status = message["data"]["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(outfile, 'ab') as f:
- f.write(audio)
-
- except Exception as e:
- print("receive msg,but parse exception:", e)
-
-
-
-
- def on_open(ws):
- global outfile
- global wsParam
- def run(*args):
- d = {"common": wsParam.CommonArgs,
- "business": wsParam.BusinessArgs,
- "data": wsParam.Data,
- }
- d = json.dumps(d)
-
- ws.send(d)
- if os.path.exists(outfile):
- os.remove(outfile)
-
- thread.start_new_thread(run, ())
-
-
- def on_error(ws, error):
- print("### error:", error)
-
-
-
-
- def on_close(ws):
- print("### closed ###")
-
-
-
- def xunfei_tts(APPID, APIKey, APISecret,BusinessArgsTTS, Text, OutFile):
- global outfile
- global wsParam
- outfile = OutFile
- wsParam1 = Ws_Param(APPID,APIKey,APISecret,BusinessArgsTTS,Text)
- wsParam = wsParam1
- 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})
- return outfile
-
|