Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
DeepSpeech
提交
2f2cb7ea
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看板
提交
2f2cb7ea
编写于
4月 23, 2022
作者:
X
xiongxinlei
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update the audio handler note, test=doc
上级
7aa5a3df
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
48 addition
and
15 deletion
+48
-15
paddlespeech/server/utils/audio_handler.py
paddlespeech/server/utils/audio_handler.py
+48
-15
未找到文件。
paddlespeech/server/utils/audio_handler.py
浏览文件 @
2f2cb7ea
import
numpy
as
np
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
import
logging
#
import
argparse
# Licensed under the Apache License, Version 2.0 (the "License");
import
asyncio
# you may not use this file except in compliance with the License.
import
codecs
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import
json
import
json
import
logging
import
logging
import
os
import
numpy
as
np
import
numpy
as
np
import
soundfile
import
soundfile
import
websockets
import
websockets
from
paddlespeech.cli.log
import
logger
from
paddlespeech.cli.log
import
logger
class
ASRAudioHandler
:
class
ASRAudioHandler
:
def
__init__
(
self
,
url
=
"127.0.0.1"
,
port
=
8090
):
def
__init__
(
self
,
url
=
"127.0.0.1"
,
port
=
8090
):
"""PaddleSpeech Online ASR Server Client audio handler
Online asr server use the websocket protocal
Args:
url (str, optional): the server ip. Defaults to "127.0.0.1".
port (int, optional): the server port. Defaults to 8090.
"""
self
.
url
=
url
self
.
url
=
url
self
.
port
=
port
self
.
port
=
port
self
.
url
=
"ws://"
+
self
.
url
+
":"
+
str
(
self
.
port
)
+
"/ws/asr"
self
.
url
=
"ws://"
+
self
.
url
+
":"
+
str
(
self
.
port
)
+
"/ws/asr"
def
read_wave
(
self
,
wavfile_path
:
str
):
def
read_wave
(
self
,
wavfile_path
:
str
):
"""read the audio file from specific wavfile path
Args:
wavfile_path (str): the audio wavfile,
we assume that audio sample rate matches the model
Yields:
numpy.array: the samall package audio pcm data
"""
samples
,
sample_rate
=
soundfile
.
read
(
wavfile_path
,
dtype
=
'int16'
)
samples
,
sample_rate
=
soundfile
.
read
(
wavfile_path
,
dtype
=
'int16'
)
x_len
=
len
(
samples
)
x_len
=
len
(
samples
)
chunk_size
=
85
*
16
#80ms, sample_rate = 16kHz
chunk_size
=
85
*
16
#80ms, sample_rate = 16kHz
if
x_len
%
chunk_size
!=
0
:
if
x_len
%
chunk_size
!=
0
:
padding_len_x
=
chunk_size
-
x_len
%
chunk_size
padding_len_x
=
chunk_size
-
x_len
%
chunk_size
else
:
else
:
padding_len_x
=
0
padding_len_x
=
0
...
@@ -40,11 +65,19 @@ class ASRAudioHandler:
...
@@ -40,11 +65,19 @@ class ASRAudioHandler:
yield
x_chunk
yield
x_chunk
async
def
run
(
self
,
wavfile_path
:
str
):
async
def
run
(
self
,
wavfile_path
:
str
):
"""Send a audio file to online server
Args:
wavfile_path (str): audio path
Returns:
str: the final asr result
"""
logging
.
info
(
"send a message to the server"
)
logging
.
info
(
"send a message to the server"
)
# self.read_wave()
# send websocket handshake protocal
#
1.
send websocket handshake protocal
async
with
websockets
.
connect
(
self
.
url
)
as
ws
:
async
with
websockets
.
connect
(
self
.
url
)
as
ws
:
# server has already received handshake protocal
#
2.
server has already received handshake protocal
# client start to send the command
# client start to send the command
audio_info
=
json
.
dumps
(
audio_info
=
json
.
dumps
(
{
{
...
@@ -59,14 +92,14 @@ class ASRAudioHandler:
...
@@ -59,14 +92,14 @@ class ASRAudioHandler:
msg
=
await
ws
.
recv
()
msg
=
await
ws
.
recv
()
logger
.
info
(
"receive msg={}"
.
format
(
msg
))
logger
.
info
(
"receive msg={}"
.
format
(
msg
))
# send chunk audio data to engine
#
3.
send chunk audio data to engine
for
chunk_data
in
self
.
read_wave
(
wavfile_path
):
for
chunk_data
in
self
.
read_wave
(
wavfile_path
):
await
ws
.
send
(
chunk_data
.
tobytes
())
await
ws
.
send
(
chunk_data
.
tobytes
())
msg
=
await
ws
.
recv
()
msg
=
await
ws
.
recv
()
msg
=
json
.
loads
(
msg
)
msg
=
json
.
loads
(
msg
)
logger
.
info
(
"receive msg={}"
.
format
(
msg
))
logger
.
info
(
"receive msg={}"
.
format
(
msg
))
#
finished
#
4. we must send finished signal to the server
audio_info
=
json
.
dumps
(
audio_info
=
json
.
dumps
(
{
{
"name"
:
"test.wav"
,
"name"
:
"test.wav"
,
...
@@ -78,9 +111,9 @@ class ASRAudioHandler:
...
@@ -78,9 +111,9 @@ class ASRAudioHandler:
separators
=
(
','
,
': '
))
separators
=
(
','
,
': '
))
await
ws
.
send
(
audio_info
)
await
ws
.
send
(
audio_info
)
msg
=
await
ws
.
recv
()
msg
=
await
ws
.
recv
()
# decode the bytes to str
#
5.
decode the bytes to str
msg
=
json
.
loads
(
msg
)
msg
=
json
.
loads
(
msg
)
logger
.
info
(
"final receive msg={}"
.
format
(
msg
))
logger
.
info
(
"final receive msg={}"
.
format
(
msg
))
result
=
msg
result
=
msg
return
result
return
result
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录