From 8b05b03a84044434b574e122d21a950e2bf13494 Mon Sep 17 00:00:00 2001
From: zhifu gao <zhifu.gzf@alibaba-inc.com>
Date: 星期四, 23 三月 2023 10:10:45 +0800
Subject: [PATCH] Merge pull request #284 from cgisky1980/main

---
 funasr/runtime/python/websocket/ASR_server.py |  143 +++++++++++++++++++++++++++++++++++
 funasr/runtime/python/websocket/ASR_client.py |   73 ++++++++++++++++++
 2 files changed, 216 insertions(+), 0 deletions(-)

diff --git a/funasr/runtime/python/websocket/ASR_client.py b/funasr/runtime/python/websocket/ASR_client.py
new file mode 100644
index 0000000..7dce880
--- /dev/null
+++ b/funasr/runtime/python/websocket/ASR_client.py
@@ -0,0 +1,73 @@
+import pyaudio
+import websocket #鍖哄埆鏈嶅姟绔繖閲屾槸 websocket-client搴�
+import time
+import websockets
+import asyncio
+from queue import Queue
+import threading
+voices = Queue()
+async def hello():
+    global ws # 瀹氫箟涓�涓叏灞�鍙橀噺ws锛岀敤浜庝繚瀛榳ebsocket杩炴帴瀵硅薄
+    uri = "ws://localhost:8899"
+    ws = await websockets.connect(uri, subprotocols=["binary"]) # 鍒涘缓涓�涓暱杩炴帴
+    ws.max_size = 1024 * 1024 * 20
+    print("connected ws server")
+async def send(data):
+    global ws # 寮曠敤鍏ㄥ眬鍙橀噺ws
+    try:
+        await ws.send(data) # 閫氳繃ws瀵硅薄鍙戦�佹暟鎹�
+    except Exception as e:
+        print('Exception occurred:', e)
+    
+
+
+asyncio.get_event_loop().run_until_complete(hello()) # 鍚姩鍗忕▼  
+
+
+# 鍏朵粬鍑芥暟鍙互閫氳繃璋冪敤send(data)鏉ュ彂閫佹暟鎹紝渚嬪锛�
+async def test():
+    #print("2")
+    global voices
+    FORMAT = pyaudio.paInt16
+    CHANNELS = 1
+    RATE = 16000
+    CHUNK = int(RATE / 1000 * 300)
+
+    p = pyaudio.PyAudio()
+
+    stream = p.open(format=FORMAT,
+                    channels=CHANNELS,
+                    rate=RATE,
+                    input=True,
+                    frames_per_buffer=CHUNK)
+
+    while True:
+
+        data = stream.read(CHUNK)
+        
+        voices.put(data)
+        #print(voices.qsize())
+        await asyncio.sleep(0.01)
+    
+      
+
+
+
+async def ws_send():
+    global voices
+    print("started to sending data!")
+    while True:
+        while not voices.empty():
+            data = voices.get()
+            voices.task_done()
+            await send(data)
+            await asyncio.sleep(0.01)
+        await asyncio.sleep(0.01)
+
+async def main():
+    task = asyncio.create_task(test()) # 鍒涘缓涓�涓悗鍙颁换鍔�
+    task2 = asyncio.create_task(ws_send()) # 鍒涘缓涓�涓悗鍙颁换鍔�
+     
+    await asyncio.gather(task, task2)
+
+asyncio.run(main())
\ No newline at end of file
diff --git a/funasr/runtime/python/websocket/ASR_server.py b/funasr/runtime/python/websocket/ASR_server.py
new file mode 100644
index 0000000..3627d3a
--- /dev/null
+++ b/funasr/runtime/python/websocket/ASR_server.py
@@ -0,0 +1,143 @@
+# server.py   娉ㄦ剰鏈緥浠呭鐞嗗崟涓猚lent鍙戦�佺殑璇煶鏁版嵁锛屽苟鏈澶歝lient杩炴帴杩涜鍒ゆ柇鍜屽鐞�
+from modelscope.pipelines import pipeline
+from modelscope.utils.constant import Tasks
+from modelscope.utils.logger import get_logger
+import logging
+
+logger = get_logger(log_level=logging.CRITICAL)
+logger.setLevel(logging.CRITICAL)
+import asyncio
+import websockets  #鍖哄埆瀹㈡埛绔繖閲屾槸 websockets搴�
+import time
+from queue import Queue
+import  threading
+
+print("model loading")
+voices = Queue()
+speek = Queue()
+# 鍒涘缓涓�涓猇AD瀵硅薄
+vad_pipline = pipeline(
+    task=Tasks.voice_activity_detection,
+    model="damo/speech_fsmn_vad_zh-cn-16k-common-pytorch",
+    model_revision="v1.2.0",
+    output_dir=None,
+    batch_size=1,
+)
+  
+# 鍒涘缓涓�涓狝SR瀵硅薄
+param_dict = dict()
+param_dict["hotword"] = "灏忎簲 灏忎簲鏈�"  # 璁剧疆鐑瘝锛岀敤绌烘牸闅斿紑
+inference_pipeline2 = pipeline(
+    task=Tasks.auto_speech_recognition,
+    model="damo/speech_paraformer-large-contextual_asr_nat-zh-cn-16k-common-vocab8404",
+    param_dict=param_dict,
+)
+print("model loaded")
+
+
+
+async def echo(websocket, path):
+    global voices
+    try:
+        async for message in websocket:
+            voices.put(message)
+            #print("put")
+    except websockets.exceptions.ConnectionClosedError as e:
+        print('Connection closed with exception:', e)
+    except Exception as e:
+        print('Exception occurred:', e)
+
+start_server = websockets.serve(echo, "localhost", 8899, subprotocols=["binary"],ping_interval=None)
+
+
+def vad(data):  # 鎺ㄧ悊
+    global vad_pipline
+    #print(type(data))
+    segments_result = vad_pipline(audio_in=data)
+    #print(segments_result)
+    if len(segments_result) == 0:
+        return False
+    else:
+        return True
+
+def asr():  # 鎺ㄧ悊
+    global inference_pipeline2
+    global speek
+    while True:
+        while not speek.empty():
+            audio_in = speek.get()
+            speek.task_done()
+            rec_result = inference_pipeline2(audio_in=audio_in)
+            print(rec_result)
+            time.sleep(0.1)
+        time.sleep(0.1)    
+
+
+def main():  # 鎺ㄧ悊
+    frames = []  # 瀛樺偍鎵�鏈夌殑甯ф暟鎹�
+    buffer = []  # 瀛樺偍缂撳瓨涓殑甯ф暟鎹紙鏈�澶氫袱涓墖娈碉級
+    silence_count = 0  # 缁熻杩炵画闈欓煶鐨勬鏁�
+    speech_detected = False  # 鏍囪鏄惁妫�娴嬪埌璇煶
+    RECORD_NUM = 0
+    global voices 
+    global speek
+    while True:
+        while not voices.empty():
+            
+            data = voices.get()
+            #print("闃熷垪鎺掗槦鏁�",voices.qsize())
+            voices.task_done()
+            buffer.append(data)
+            if len(buffer) > 2:
+                buffer.pop(0)  # 濡傛灉缂撳瓨瓒呰繃涓や釜鐗囨锛屽垯鍒犻櫎鏈�鏃╃殑涓�涓�
+            
+            if speech_detected:
+                frames.append(data)
+                RECORD_NUM += 1    
+            
+            if  vad(data):
+                if not speech_detected:
+                    print("妫�娴嬪埌浜哄0...")
+                    speech_detected = True  # 鏍囪涓烘娴嬪埌璇煶
+                    frames = []
+                    frames.extend(buffer)  # 鎶婁箣鍓�2涓闊虫暟鎹揩鍔犲叆
+                silence_count = 0  # 閲嶇疆闈欓煶娆℃暟
+            else:
+                silence_count += 1  # 澧炲姞闈欓煶娆℃暟
+
+                if speech_detected and (silence_count > 4 or RECORD_NUM > 50): #杩欓噷 50 鍙牴鎹渶姹傛敼涓哄悎閫傜殑鏁版嵁蹇暟閲�
+                    print("璇磋瘽缁撴潫鎴栬�呰秴杩囪缃渶闀挎椂闂�...")
+                    audio_in = b"".join(frames)
+                    #asrt = threading.Thread(target=asr,args=(audio_in,))
+                    #asrt.start()
+                    speek.put(audio_in)
+                    #rec_result = inference_pipeline2(audio_in=audio_in)  # ASR 妯″瀷閲岃窇涓�璺�
+                    frames = []  # 娓呯┖鎵�鏈夌殑甯ф暟鎹�
+                    buffer = []  # 娓呯┖缂撳瓨涓殑甯ф暟鎹紙鏈�澶氫袱涓墖娈碉級
+                    silence_count = 0  # 缁熻杩炵画闈欓煶鐨勬鏁版竻闆�
+                    speech_detected = False  # 鏍囪鏄惁妫�娴嬪埌璇煶
+                    RECORD_NUM = 0
+            time.sleep(0.01)
+        time.sleep(0.01)
+            
+
+
+s = threading.Thread(target=main)
+s.start()
+s = threading.Thread(target=asr)
+s.start()
+
+asyncio.get_event_loop().run_until_complete(start_server)
+asyncio.get_event_loop().run_forever()
+
+
+ 
+
+
+
+
+
+ 
+
+        
+

--
Gitblit v1.9.1