|
@@ -10,7 +10,6 @@ from wsgiref.handlers import format_date_time
|
|
|
from datetime import datetime
|
|
|
from time import mktime
|
|
|
import _thread as thread
|
|
|
-import os
|
|
|
|
|
|
|
|
|
"""
|
|
@@ -18,16 +17,16 @@ import os
|
|
|
"""
|
|
|
|
|
|
class WsParamSynthesize:
|
|
|
- def __init__(self, APPID, APIKey, APISecret, Text, output_file='demo.pcm'):
|
|
|
+ def __init__(self, APPID, APIKey, APISecret, Text):
|
|
|
self.APPID = APPID
|
|
|
self.APIKey = APIKey
|
|
|
self.APISecret = APISecret
|
|
|
self.Text = Text
|
|
|
- self.output_file = output_file
|
|
|
+ self.audio_data = ""
|
|
|
|
|
|
# 初始化其他需要的属性
|
|
|
self.CommonArgs = {"app_id": self.APPID}
|
|
|
- self.BusinessArgs = {"aue": "raw", "auf": "audio/L16;rate=16000", "vcn": "xiaoyan", "tte": "utf8"}
|
|
|
+ self.BusinessArgs = {"aue": "lame", "auf": "audio/L16;rate=16000", "vcn": "xiaoyan", "tte": "utf8", "sfl": 1}
|
|
|
self.Data = {"status": 2, "text": str(base64.b64encode(self.Text.encode('utf-8')), "UTF8")}
|
|
|
|
|
|
def create_url(self):
|
|
@@ -64,24 +63,21 @@ class WsParamSynthesize:
|
|
|
|
|
|
def on_message(self, ws, message):
|
|
|
try:
|
|
|
- print("Received message:", message) # 打印接收到的消息
|
|
|
message = json.loads(message)
|
|
|
code = message["code"]
|
|
|
sid = message["sid"]
|
|
|
- audio = message["data"]["audio"]
|
|
|
- audio = base64.b64decode(audio)
|
|
|
status = message["data"]["status"]
|
|
|
- print(message)
|
|
|
if status == 2:
|
|
|
- print("WebSocket connection is closed.")
|
|
|
ws.close()
|
|
|
if code != 0:
|
|
|
errMsg = message["message"]
|
|
|
print(f"sid:{sid} call error:{errMsg} code is:{code}")
|
|
|
else:
|
|
|
- print("Writing audio data to file.")
|
|
|
- with open(self.output_file, 'ab') as f:
|
|
|
- f.write(audio)
|
|
|
+ audio = message["data"]["audio"]
|
|
|
+ self.audio_data = audio
|
|
|
+ if status == 2: # 最后一帧
|
|
|
+ print("WebSocket connection is closed.")
|
|
|
+ ws.close()
|
|
|
except Exception as e:
|
|
|
print("Receive message, but parse exception:", e)
|
|
|
|
|
@@ -95,15 +91,10 @@ class WsParamSynthesize:
|
|
|
|
|
|
def on_open(self, ws):
|
|
|
def run(*args):
|
|
|
- print("WebSocket connection is open.")
|
|
|
d = {"common": self.CommonArgs, "business": self.BusinessArgs, "data": self.Data}
|
|
|
d = json.dumps(d)
|
|
|
- print("Sending text data...")
|
|
|
ws.send(d)
|
|
|
- if os.path.exists(self.output_file):
|
|
|
- os.remove(self.output_file)
|
|
|
|
|
|
- print("Starting new thread for sending data...")
|
|
|
thread.start_new_thread(run, ())
|
|
|
|
|
|
def start(self):
|
|
@@ -113,4 +104,5 @@ class WsParamSynthesize:
|
|
|
on_error=lambda msg: self.on_error(msg),
|
|
|
on_close=self.on_close,
|
|
|
on_open=lambda ws: self.on_open(ws)) # 使用 lambda 来确保 ws 参数传递
|
|
|
- self.ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE})
|
|
|
+ self.ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE})
|
|
|
+ return self.audio_data
|