|
@@ -320,11 +320,13 @@ class XunfeiAsr:
|
|
STATUS_LAST_FRAME = 2 # 最后一帧的标识
|
|
STATUS_LAST_FRAME = 2 # 最后一帧的标识
|
|
|
|
|
|
def __init__(self, tid, logger, message_receiver=None):
|
|
def __init__(self, tid, logger, message_receiver=None):
|
|
- self.connected = False
|
|
|
|
self.__id = tid
|
|
self.__id = tid
|
|
self.logger = logger
|
|
self.logger = logger
|
|
self.message_receiver = message_receiver
|
|
self.message_receiver = message_receiver
|
|
|
|
|
|
|
|
+ self.connected = False
|
|
|
|
+ self.status = XunfeiAsr.STATUS_FIRST_FRAME # 音频的状态信息,标识音频是第一帧,还是中间帧、最后一帧
|
|
|
|
+
|
|
self.APPID = '1ec1097b'
|
|
self.APPID = '1ec1097b'
|
|
self.APIKey = '7237c29d862daa6fd805f788ed70c409'
|
|
self.APIKey = '7237c29d862daa6fd805f788ed70c409'
|
|
self.APISecret = 'YTk1YzAyMmQwYjQ3ZDJkYWQyZGQwMDFm'
|
|
self.APISecret = 'YTk1YzAyMmQwYjQ3ZDJkYWQyZGQwMDFm'
|
|
@@ -403,37 +405,36 @@ class XunfeiAsr:
|
|
def start(self):
|
|
def start(self):
|
|
self.__th.start()
|
|
self.__th.start()
|
|
|
|
|
|
- def send_audio(self, audio_data=None, first=False):
|
|
|
|
|
|
+ def send_audio(self, audio_data=None):
|
|
if not self.connected:
|
|
if not self.connected:
|
|
self.logger.info('xunfei.Asr.send_audio:ws_is_None: call_id: %s, chunk:%s, %s', self.__id, len(audio_data), audio_data)
|
|
self.logger.info('xunfei.Asr.send_audio:ws_is_None: call_id: %s, chunk:%s, %s', self.__id, len(audio_data), audio_data)
|
|
return
|
|
return
|
|
|
|
|
|
- status = XunfeiAsr.STATUS_FIRST_FRAME if first else XunfeiAsr.STATUS_CONTINUE_FRAME # 音频的状态信息,标识音频是第一帧,还是中间帧、最后一帧
|
|
|
|
if not audio_data:
|
|
if not audio_data:
|
|
- status = XunfeiAsr.STATUS_LAST_FRAME
|
|
|
|
|
|
+ self.status = XunfeiAsr.STATUS_LAST_FRAME
|
|
buf = bytes(audio_data) if audio_data else bytes()
|
|
buf = bytes(audio_data) if audio_data else bytes()
|
|
# self.logger.info('xunfei.Asr.send_audio: call_id: %s, status:%s, chunk:%s, %s', self.__id, status, (len(buf) if buf else 0) , buf)
|
|
# self.logger.info('xunfei.Asr.send_audio: call_id: %s, status:%s, chunk:%s, %s', self.__id, status, (len(buf) if buf else 0) , buf)
|
|
|
|
|
|
# 第一帧处理
|
|
# 第一帧处理
|
|
# 发送第一帧音频,带business 参数
|
|
# 发送第一帧音频,带business 参数
|
|
# appid 必须带上,只需第一帧发送
|
|
# appid 必须带上,只需第一帧发送
|
|
- if status == XunfeiAsr.STATUS_FIRST_FRAME:
|
|
|
|
|
|
+ if self.status == XunfeiAsr.STATUS_FIRST_FRAME:
|
|
d = {"common": self.CommonArgs,
|
|
d = {"common": self.CommonArgs,
|
|
"business": self.BusinessArgs,
|
|
"business": self.BusinessArgs,
|
|
"data": {"status": 0, "format": "audio/L16;rate=16000",
|
|
"data": {"status": 0, "format": "audio/L16;rate=16000",
|
|
"audio": str(base64.b64encode(buf), 'utf-8'),
|
|
"audio": str(base64.b64encode(buf), 'utf-8'),
|
|
"encoding": "raw"}}
|
|
"encoding": "raw"}}
|
|
d = json.dumps(d)
|
|
d = json.dumps(d)
|
|
-
|
|
|
|
self.ws.send(d)
|
|
self.ws.send(d)
|
|
|
|
+ self.status = XunfeiAsr.STATUS_CONTINUE_FRAME
|
|
# 中间帧处理
|
|
# 中间帧处理
|
|
- elif status == XunfeiAsr.STATUS_CONTINUE_FRAME:
|
|
|
|
|
|
+ elif self.status == XunfeiAsr.STATUS_CONTINUE_FRAME:
|
|
d = {"data": {"status": 1, "format": "audio/L16;rate=16000",
|
|
d = {"data": {"status": 1, "format": "audio/L16;rate=16000",
|
|
"audio": str(base64.b64encode(buf), 'utf-8'),
|
|
"audio": str(base64.b64encode(buf), 'utf-8'),
|
|
"encoding": "raw"}}
|
|
"encoding": "raw"}}
|
|
self.ws.send(json.dumps(d))
|
|
self.ws.send(json.dumps(d))
|
|
# 最后一帧处理
|
|
# 最后一帧处理
|
|
- elif status == XunfeiAsr.STATUS_LAST_FRAME:
|
|
|
|
|
|
+ elif self.status == XunfeiAsr.STATUS_LAST_FRAME:
|
|
d = {"data": {"status": 2, "format": "audio/L16;rate=16000",
|
|
d = {"data": {"status": 2, "format": "audio/L16;rate=16000",
|
|
"audio": str(base64.b64encode(buf), 'utf-8'),
|
|
"audio": str(base64.b64encode(buf), 'utf-8'),
|
|
"encoding": "raw"}}
|
|
"encoding": "raw"}}
|