Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
DeepSpeech
提交
13a37b48
D
DeepSpeech
项目概览
PaddlePaddle
/
DeepSpeech
大约 2 年 前同步成功
通知
210
Star
8425
Fork
1598
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
245
列表
看板
标记
里程碑
合并请求
3
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
DeepSpeech
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
245
Issue
245
列表
看板
标记
里程碑
合并请求
3
合并请求
3
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
13a37b48
编写于
4月 23, 2022
作者:
X
xiongxinlei
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update the online protocal note, test=doc
上级
2f2cb7ea
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
28 addition
and
20 deletion
+28
-20
paddlespeech/server/ws/asr_socket.py
paddlespeech/server/ws/asr_socket.py
+28
-20
未找到文件。
paddlespeech/server/ws/asr_socket.py
浏览文件 @
13a37b48
...
@@ -20,50 +20,52 @@ from starlette.websockets import WebSocketState as WebSocketState
...
@@ -20,50 +20,52 @@ from starlette.websockets import WebSocketState as WebSocketState
from
paddlespeech.server.engine.asr.online.asr_engine
import
PaddleASRConnectionHanddler
from
paddlespeech.server.engine.asr.online.asr_engine
import
PaddleASRConnectionHanddler
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.vad
import
VADAudio
router
=
APIRouter
()
router
=
APIRouter
()
@
router
.
websocket
(
'/ws/asr'
)
@
router
.
websocket
(
'/ws/asr'
)
async
def
websocket_endpoint
(
websocket
:
WebSocket
):
async
def
websocket_endpoint
(
websocket
:
WebSocket
):
"""PaddleSpeech Online ASR Server api
Args:
websocket (WebSocket): the websocket instance
"""
#1. the interface wait to accept the websocket protocal header
# and only we receive the header, it establish the connection with specific thread
await
websocket
.
accept
()
await
websocket
.
accept
()
#2. if we accept the websocket headers, we will get the online asr engine instance
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
# each websocekt connection has its own chunk buffer
chunk_buffer_conf
=
asr_engine
.
config
.
chunk_buffer_conf
chunk_buffer
=
ChunkBuffer
(
window_n
=
chunk_buffer_conf
.
window_n
,
shift_n
=
chunk_buffer_conf
.
shift_n
,
window_ms
=
chunk_buffer_conf
.
window_ms
,
shift_ms
=
chunk_buffer_conf
.
shift_ms
,
sample_rate
=
chunk_buffer_conf
.
sample_rate
,
sample_width
=
chunk_buffer_conf
.
sample_width
)
# init vad
#3. each websocket connection, we will create an PaddleASRConnectionHanddler to process such audio
vad_conf
=
asr_engine
.
config
.
get
(
'vad_conf'
,
None
)
# and each connection has its own connection instance to process the request
if
vad_conf
:
# and only if client send the start signal, we create the PaddleASRConnectionHanddler instance
vad
=
VADAudio
(
connection_handler
=
None
aggressiveness
=
vad_conf
[
'aggressiveness'
],
rate
=
vad_conf
[
'sample_rate'
],
frame_duration_ms
=
vad_conf
[
'frame_duration_ms'
])
try
:
try
:
#4. we do a loop to process the audio package by package according the protocal
# and only if the client send finished signal, we will break the loop
while
True
:
while
True
:
# careful here, changed the source code from starlette.websockets
# careful here, changed the source code from starlette.websockets
# 4.1 we wait for the client signal for the specific action
assert
websocket
.
application_state
==
WebSocketState
.
CONNECTED
assert
websocket
.
application_state
==
WebSocketState
.
CONNECTED
message
=
await
websocket
.
receive
()
message
=
await
websocket
.
receive
()
websocket
.
_raise_on_disconnect
(
message
)
websocket
.
_raise_on_disconnect
(
message
)
#4.2 text for the action command and bytes for pcm data
if
"text"
in
message
:
if
"text"
in
message
:
# we first parse the specific command
message
=
json
.
loads
(
message
[
"text"
])
message
=
json
.
loads
(
message
[
"text"
])
if
'signal'
not
in
message
:
if
'signal'
not
in
message
:
resp
=
{
"status"
:
"ok"
,
"message"
:
"no valid json data"
}
resp
=
{
"status"
:
"ok"
,
"message"
:
"no valid json data"
}
await
websocket
.
send_json
(
resp
)
await
websocket
.
send_json
(
resp
)
# start command, we create the PaddleASRConnectionHanddler instance to process the audio data
# end command, we process the all the last audio pcm and return the final result
# and we break the loop
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
...
@@ -72,6 +74,7 @@ async def websocket_endpoint(websocket: WebSocket):
...
@@ -72,6 +74,7 @@ async def websocket_endpoint(websocket: WebSocket):
await
websocket
.
send_json
(
resp
)
await
websocket
.
send_json
(
resp
)
elif
message
[
'signal'
]
==
'end'
:
elif
message
[
'signal'
]
==
'end'
:
# reset single engine for an new connection
# reset single engine for an new connection
# and we will destroy the connection
connection_handler
.
decode
(
is_finished
=
True
)
connection_handler
.
decode
(
is_finished
=
True
)
connection_handler
.
rescoring
()
connection_handler
.
rescoring
()
asr_results
=
connection_handler
.
get_result
()
asr_results
=
connection_handler
.
get_result
()
...
@@ -88,12 +91,17 @@ async def websocket_endpoint(websocket: WebSocket):
...
@@ -88,12 +91,17 @@ async def websocket_endpoint(websocket: WebSocket):
resp
=
{
"status"
:
"ok"
,
"message"
:
"no valid json data"
}
resp
=
{
"status"
:
"ok"
,
"message"
:
"no valid json data"
}
await
websocket
.
send_json
(
resp
)
await
websocket
.
send_json
(
resp
)
elif
"bytes"
in
message
:
elif
"bytes"
in
message
:
# bytes for the pcm data
message
=
message
[
"bytes"
]
message
=
message
[
"bytes"
]
# we extract the remained audio pcm
# and decode for the result in this package data
connection_handler
.
extract_feat
(
message
)
connection_handler
.
extract_feat
(
message
)
connection_handler
.
decode
(
is_finished
=
False
)
connection_handler
.
decode
(
is_finished
=
False
)
asr_results
=
connection_handler
.
get_result
()
asr_results
=
connection_handler
.
get_result
()
# return the current period result
# if the engine create the vad instance, this connection will have many period results
resp
=
{
'asr_results'
:
asr_results
}
resp
=
{
'asr_results'
:
asr_results
}
await
websocket
.
send_json
(
resp
)
await
websocket
.
send_json
(
resp
)
except
WebSocketDisconnect
:
except
WebSocketDisconnect
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录