Kaynağa Gözat

讯飞实时听写接口,debug

Davidliu 1 ay önce
ebeveyn
işleme
7f44af7069
2 değiştirilmiş dosya ile 11 ekleme ve 10 silme
  1. 9 8
      src/core/voip/asr.py
  2. 2 2
      src/core/voip/bot.py

+ 9 - 8
src/core/voip/asr.py

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

+ 2 - 2
src/core/voip/bot.py

@@ -84,8 +84,8 @@ class MyAudioMediaPort(pj.AudioMediaPort):
         if self.asr:  # 如果ASR实例存在,则发送音频数据
             if self.first:
                 self.call.logger.warn("Received audio frame: %s, %s, %s", self.call.session_id,frame.buf,frame.size)
-            self.asr.send_audio(frame.buf, first=self.first)
-            self.first = False
+                self.first = False
+            self.asr.send_audio(frame.buf)
 
         try:
             asr_text = self.get_asr_text()