|
|
@ -22,6 +22,7 @@ from starlette.websockets import WebSocketState as WebSocketState
|
|
|
|
from paddlespeech.server.engine.engine_pool import get_engine_pool
|
|
|
|
from paddlespeech.server.engine.engine_pool import get_engine_pool
|
|
|
|
from paddlespeech.server.utils.buffer import ChunkBuffer
|
|
|
|
from paddlespeech.server.utils.buffer import ChunkBuffer
|
|
|
|
from paddlespeech.server.utils.vad import VADAudio
|
|
|
|
from paddlespeech.server.utils.vad import VADAudio
|
|
|
|
|
|
|
|
from paddlespeech.server.engine.asr.online.asr_engine import PaddleASRConnectionHanddler
|
|
|
|
|
|
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
|
|
|
|
|
@ -33,6 +34,7 @@ async def websocket_endpoint(websocket: WebSocket):
|
|
|
|
|
|
|
|
|
|
|
|
engine_pool = get_engine_pool()
|
|
|
|
engine_pool = get_engine_pool()
|
|
|
|
asr_engine = engine_pool['asr']
|
|
|
|
asr_engine = engine_pool['asr']
|
|
|
|
|
|
|
|
connection_handler = None
|
|
|
|
# init buffer
|
|
|
|
# init buffer
|
|
|
|
# each websocekt connection has its own chunk buffer
|
|
|
|
# each websocekt connection has its own chunk buffer
|
|
|
|
chunk_buffer_conf = asr_engine.config.chunk_buffer_conf
|
|
|
|
chunk_buffer_conf = asr_engine.config.chunk_buffer_conf
|
|
|
@ -67,13 +69,17 @@ async def websocket_endpoint(websocket: WebSocket):
|
|
|
|
if message['signal'] == 'start':
|
|
|
|
if message['signal'] == 'start':
|
|
|
|
resp = {"status": "ok", "signal": "server_ready"}
|
|
|
|
resp = {"status": "ok", "signal": "server_ready"}
|
|
|
|
# do something at begining here
|
|
|
|
# do something at begining here
|
|
|
|
|
|
|
|
# create the instance to process the audio
|
|
|
|
|
|
|
|
connection_handler = PaddleASRConnectionHanddler(asr_engine)
|
|
|
|
await websocket.send_json(resp)
|
|
|
|
await websocket.send_json(resp)
|
|
|
|
elif message['signal'] == 'end':
|
|
|
|
elif message['signal'] == 'end':
|
|
|
|
engine_pool = get_engine_pool()
|
|
|
|
engine_pool = get_engine_pool()
|
|
|
|
asr_engine = engine_pool['asr']
|
|
|
|
asr_engine = engine_pool['asr']
|
|
|
|
# reset single engine for an new connection
|
|
|
|
# reset single engine for an new connection
|
|
|
|
|
|
|
|
asr_results = connection_handler.decode(is_finished=True)
|
|
|
|
|
|
|
|
connection_handler.reset()
|
|
|
|
asr_engine.reset()
|
|
|
|
asr_engine.reset()
|
|
|
|
resp = {"status": "ok", "signal": "finished"}
|
|
|
|
resp = {"status": "ok", "signal": "finished", 'asr_results': asr_results}
|
|
|
|
await websocket.send_json(resp)
|
|
|
|
await websocket.send_json(resp)
|
|
|
|
break
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
else:
|
|
|
@ -81,23 +87,27 @@ async def websocket_endpoint(websocket: WebSocket):
|
|
|
|
await websocket.send_json(resp)
|
|
|
|
await websocket.send_json(resp)
|
|
|
|
elif "bytes" in message:
|
|
|
|
elif "bytes" in message:
|
|
|
|
message = message["bytes"]
|
|
|
|
message = message["bytes"]
|
|
|
|
|
|
|
|
|
|
|
|
engine_pool = get_engine_pool()
|
|
|
|
engine_pool = get_engine_pool()
|
|
|
|
asr_engine = engine_pool['asr']
|
|
|
|
asr_engine = engine_pool['asr']
|
|
|
|
asr_results = ""
|
|
|
|
asr_results = ""
|
|
|
|
frames = chunk_buffer.frame_generator(message)
|
|
|
|
connection_handler.extract_feat(message)
|
|
|
|
for frame in frames:
|
|
|
|
asr_results = connection_handler.decode(is_finished=False)
|
|
|
|
# get the pcm data from the bytes
|
|
|
|
# connection_handler.
|
|
|
|
samples = np.frombuffer(frame.bytes, dtype=np.int16)
|
|
|
|
# frames = chunk_buffer.frame_generator(message)
|
|
|
|
sample_rate = asr_engine.config.sample_rate
|
|
|
|
# for frame in frames:
|
|
|
|
x_chunk, x_chunk_lens = asr_engine.preprocess(samples,
|
|
|
|
# # get the pcm data from the bytes
|
|
|
|
sample_rate)
|
|
|
|
# samples = np.frombuffer(frame.bytes, dtype=np.int16)
|
|
|
|
asr_engine.run(x_chunk, x_chunk_lens)
|
|
|
|
# sample_rate = asr_engine.config.sample_rate
|
|
|
|
asr_results = asr_engine.postprocess()
|
|
|
|
# x_chunk, x_chunk_lens = asr_engine.preprocess(samples,
|
|
|
|
|
|
|
|
# sample_rate)
|
|
|
|
|
|
|
|
# asr_engine.run(x_chunk, x_chunk_lens)
|
|
|
|
|
|
|
|
# asr_results = asr_engine.postprocess()
|
|
|
|
|
|
|
|
|
|
|
|
asr_results = asr_engine.postprocess()
|
|
|
|
# # connection accept the sample data frame by frame
|
|
|
|
resp = {'asr_results': asr_results}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# asr_results = asr_engine.postprocess()
|
|
|
|
|
|
|
|
resp = {'asr_results': asr_results}
|
|
|
|
|
|
|
|
print("\n")
|
|
|
|
await websocket.send_json(resp)
|
|
|
|
await websocket.send_json(resp)
|
|
|
|
except WebSocketDisconnect:
|
|
|
|
except WebSocketDisconnect:
|
|
|
|
pass
|
|
|
|
pass
|
|
|
|