Merge pull request #1771 from lym0302/add_streaming_cli
[server] add streaming tts demospull/1776/head
commit
f256bb9c0e
@ -0,0 +1,88 @@
|
|||||||
|
# This is the parameter configuration file for PaddleSpeech Serving.
|
||||||
|
|
||||||
|
#################################################################################
|
||||||
|
# SERVER SETTING #
|
||||||
|
#################################################################################
|
||||||
|
host: 127.0.0.1
|
||||||
|
port: 8092
|
||||||
|
|
||||||
|
# The task format in the engin_list is: <speech task>_<engine type>
|
||||||
|
# engine_list choices = ['tts_online', 'tts_online-onnx']
|
||||||
|
# protocol = ['websocket', 'http'] (only one can be selected).
|
||||||
|
protocol: 'http'
|
||||||
|
engine_list: ['tts_online-onnx']
|
||||||
|
|
||||||
|
|
||||||
|
#################################################################################
|
||||||
|
# ENGINE CONFIG #
|
||||||
|
#################################################################################
|
||||||
|
|
||||||
|
################################### TTS #########################################
|
||||||
|
################### speech task: tts; engine_type: online #######################
|
||||||
|
tts_online:
|
||||||
|
# am (acoustic model) choices=['fastspeech2_csmsc', 'fastspeech2_cnndecoder_csmsc']
|
||||||
|
am: 'fastspeech2_csmsc'
|
||||||
|
am_config:
|
||||||
|
am_ckpt:
|
||||||
|
am_stat:
|
||||||
|
phones_dict:
|
||||||
|
tones_dict:
|
||||||
|
speaker_dict:
|
||||||
|
spk_id: 0
|
||||||
|
|
||||||
|
# voc (vocoder) choices=['mb_melgan_csmsc, hifigan_csmsc']
|
||||||
|
voc: 'mb_melgan_csmsc'
|
||||||
|
voc_config:
|
||||||
|
voc_ckpt:
|
||||||
|
voc_stat:
|
||||||
|
|
||||||
|
# others
|
||||||
|
lang: 'zh'
|
||||||
|
device: 'cpu' # set 'gpu:id' or 'cpu'
|
||||||
|
am_block: 42
|
||||||
|
am_pad: 12
|
||||||
|
voc_block: 14
|
||||||
|
voc_pad: 14
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#################################################################################
|
||||||
|
# ENGINE CONFIG #
|
||||||
|
#################################################################################
|
||||||
|
|
||||||
|
################################### TTS #########################################
|
||||||
|
################### speech task: tts; engine_type: online-onnx #######################
|
||||||
|
tts_online-onnx:
|
||||||
|
# am (acoustic model) choices=['fastspeech2_csmsc_onnx', 'fastspeech2_cnndecoder_csmsc_onnx']
|
||||||
|
am: 'fastspeech2_cnndecoder_csmsc_onnx'
|
||||||
|
# am_ckpt is a list, if am is fastspeech2_cnndecoder_csmsc_onnx, am_ckpt = [encoder model, decoder model, postnet model];
|
||||||
|
# if am is fastspeech2_csmsc_onnx, am_ckpt = [ckpt model];
|
||||||
|
am_ckpt: # list
|
||||||
|
am_stat:
|
||||||
|
phones_dict:
|
||||||
|
tones_dict:
|
||||||
|
speaker_dict:
|
||||||
|
spk_id: 0
|
||||||
|
am_sample_rate: 24000
|
||||||
|
am_sess_conf:
|
||||||
|
device: "cpu" # set 'gpu:id' or 'cpu'
|
||||||
|
use_trt: False
|
||||||
|
cpu_threads: 4
|
||||||
|
|
||||||
|
# voc (vocoder) choices=['mb_melgan_csmsc_onnx, hifigan_csmsc_onnx']
|
||||||
|
voc: 'hifigan_csmsc_onnx'
|
||||||
|
voc_ckpt:
|
||||||
|
voc_sample_rate: 24000
|
||||||
|
voc_sess_conf:
|
||||||
|
device: "cpu" # set 'gpu:id' or 'cpu'
|
||||||
|
use_trt: False
|
||||||
|
cpu_threads: 4
|
||||||
|
|
||||||
|
# others
|
||||||
|
lang: 'zh'
|
||||||
|
am_block: 42
|
||||||
|
am_pad: 12
|
||||||
|
voc_block: 14
|
||||||
|
voc_pad: 14
|
||||||
|
voc_upsample: 300
|
||||||
|
|
@ -0,0 +1,3 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# start server
|
||||||
|
paddlespeech_server start --config_file ./conf/tts_online_application.yaml
|
@ -0,0 +1,7 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# http client test
|
||||||
|
paddlespeech_client tts --server_ip 127.0.0.1 --port 8092 --protocol http --input "您好,欢迎使用百度飞桨语音合成服务。" --output output.wav
|
||||||
|
|
||||||
|
# websocket client test
|
||||||
|
#paddlespeech_client tts --server_ip 127.0.0.1 --port 8092 --protocol websocket --input "您好,欢迎使用百度飞桨语音合成服务。" --output output.wav
|
@ -1,112 +0,0 @@
|
|||||||
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
# you may not use this file except in compliance with the License.
|
|
||||||
# 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 argparse
|
|
||||||
import base64
|
|
||||||
import json
|
|
||||||
import threading
|
|
||||||
import time
|
|
||||||
|
|
||||||
import pyaudio
|
|
||||||
import requests
|
|
||||||
|
|
||||||
mutex = threading.Lock()
|
|
||||||
buffer = b''
|
|
||||||
p = pyaudio.PyAudio()
|
|
||||||
stream = p.open(
|
|
||||||
format=p.get_format_from_width(2), channels=1, rate=24000, output=True)
|
|
||||||
max_fail = 50
|
|
||||||
|
|
||||||
|
|
||||||
def play_audio():
|
|
||||||
global stream
|
|
||||||
global buffer
|
|
||||||
global max_fail
|
|
||||||
while True:
|
|
||||||
if not buffer:
|
|
||||||
max_fail -= 1
|
|
||||||
time.sleep(0.05)
|
|
||||||
if max_fail < 0:
|
|
||||||
break
|
|
||||||
mutex.acquire()
|
|
||||||
stream.write(buffer)
|
|
||||||
buffer = b''
|
|
||||||
mutex.release()
|
|
||||||
|
|
||||||
|
|
||||||
def test(args):
|
|
||||||
global mutex
|
|
||||||
global buffer
|
|
||||||
params = {
|
|
||||||
"text": args.text,
|
|
||||||
"spk_id": args.spk_id,
|
|
||||||
"speed": args.speed,
|
|
||||||
"volume": args.volume,
|
|
||||||
"sample_rate": args.sample_rate,
|
|
||||||
"save_path": ''
|
|
||||||
}
|
|
||||||
|
|
||||||
all_bytes = 0.0
|
|
||||||
t = threading.Thread(target=play_audio)
|
|
||||||
flag = 1
|
|
||||||
url = "http://" + str(args.server) + ":" + str(
|
|
||||||
args.port) + "/paddlespeech/streaming/tts"
|
|
||||||
st = time.time()
|
|
||||||
html = requests.post(url, json.dumps(params), stream=True)
|
|
||||||
for chunk in html.iter_content(chunk_size=1024):
|
|
||||||
mutex.acquire()
|
|
||||||
chunk = base64.b64decode(chunk) # bytes
|
|
||||||
buffer += chunk
|
|
||||||
mutex.release()
|
|
||||||
if flag:
|
|
||||||
first_response = time.time() - st
|
|
||||||
print(f"首包响应:{first_response} s")
|
|
||||||
flag = 0
|
|
||||||
t.start()
|
|
||||||
all_bytes += len(chunk)
|
|
||||||
|
|
||||||
final_response = time.time() - st
|
|
||||||
duration = all_bytes / 2 / 24000
|
|
||||||
|
|
||||||
print(f"尾包响应:{final_response} s")
|
|
||||||
print(f"音频时长:{duration} s")
|
|
||||||
print(f"RTF: {final_response / duration}")
|
|
||||||
|
|
||||||
t.join()
|
|
||||||
stream.stop_stream()
|
|
||||||
stream.close()
|
|
||||||
p.terminate()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
parser = argparse.ArgumentParser()
|
|
||||||
parser.add_argument(
|
|
||||||
'--text',
|
|
||||||
type=str,
|
|
||||||
default="您好,欢迎使用语音合成服务。",
|
|
||||||
help='A sentence to be synthesized')
|
|
||||||
parser.add_argument('--spk_id', type=int, default=0, help='Speaker id')
|
|
||||||
parser.add_argument('--speed', type=float, default=1.0, help='Audio speed')
|
|
||||||
parser.add_argument(
|
|
||||||
'--volume', type=float, default=1.0, help='Audio volume')
|
|
||||||
parser.add_argument(
|
|
||||||
'--sample_rate',
|
|
||||||
type=int,
|
|
||||||
default=0,
|
|
||||||
help='Sampling rate, the default is the same as the model')
|
|
||||||
parser.add_argument(
|
|
||||||
"--server", type=str, help="server ip", default="127.0.0.1")
|
|
||||||
parser.add_argument("--port", type=int, help="server port", default=8092)
|
|
||||||
|
|
||||||
args = parser.parse_args()
|
|
||||||
test(args)
|
|
@ -1,160 +0,0 @@
|
|||||||
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
# you may not use this file except in compliance with the License.
|
|
||||||
# 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 _thread as thread
|
|
||||||
import argparse
|
|
||||||
import base64
|
|
||||||
import json
|
|
||||||
import ssl
|
|
||||||
import threading
|
|
||||||
import time
|
|
||||||
|
|
||||||
import pyaudio
|
|
||||||
import websocket
|
|
||||||
|
|
||||||
mutex = threading.Lock()
|
|
||||||
buffer = b''
|
|
||||||
p = pyaudio.PyAudio()
|
|
||||||
stream = p.open(
|
|
||||||
format=p.get_format_from_width(2), channels=1, rate=24000, output=True)
|
|
||||||
flag = 1
|
|
||||||
st = 0.0
|
|
||||||
all_bytes = 0.0
|
|
||||||
|
|
||||||
|
|
||||||
class WsParam(object):
|
|
||||||
# 初始化
|
|
||||||
def __init__(self, text, server="127.0.0.1", port=8090):
|
|
||||||
self.server = server
|
|
||||||
self.port = port
|
|
||||||
self.url = "ws://" + self.server + ":" + str(self.port) + "/ws/tts"
|
|
||||||
self.text = text
|
|
||||||
|
|
||||||
# 生成url
|
|
||||||
def create_url(self):
|
|
||||||
return self.url
|
|
||||||
|
|
||||||
|
|
||||||
def play_audio():
|
|
||||||
global stream
|
|
||||||
global buffer
|
|
||||||
while True:
|
|
||||||
time.sleep(0.05)
|
|
||||||
if not buffer: # buffer 为空
|
|
||||||
break
|
|
||||||
mutex.acquire()
|
|
||||||
stream.write(buffer)
|
|
||||||
buffer = b''
|
|
||||||
mutex.release()
|
|
||||||
|
|
||||||
|
|
||||||
t = threading.Thread(target=play_audio)
|
|
||||||
|
|
||||||
|
|
||||||
def on_message(ws, message):
|
|
||||||
global flag
|
|
||||||
global t
|
|
||||||
global buffer
|
|
||||||
global st
|
|
||||||
global all_bytes
|
|
||||||
|
|
||||||
try:
|
|
||||||
message = json.loads(message)
|
|
||||||
audio = message["audio"]
|
|
||||||
audio = base64.b64decode(audio) # bytes
|
|
||||||
status = message["status"]
|
|
||||||
all_bytes += len(audio)
|
|
||||||
|
|
||||||
if status == 0:
|
|
||||||
print("create successfully.")
|
|
||||||
elif status == 1:
|
|
||||||
mutex.acquire()
|
|
||||||
buffer += audio
|
|
||||||
mutex.release()
|
|
||||||
if flag:
|
|
||||||
print(f"首包响应:{time.time() - st} s")
|
|
||||||
flag = 0
|
|
||||||
print("Start playing audio")
|
|
||||||
t.start()
|
|
||||||
elif status == 2:
|
|
||||||
final_response = time.time() - st
|
|
||||||
duration = all_bytes / 2 / 24000
|
|
||||||
print(f"尾包响应:{final_response} s")
|
|
||||||
print(f"音频时长:{duration} s")
|
|
||||||
print(f"RTF: {final_response / duration}")
|
|
||||||
print("ws is closed")
|
|
||||||
ws.close()
|
|
||||||
else:
|
|
||||||
print("infer error")
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
print("receive msg,but parse exception:", e)
|
|
||||||
|
|
||||||
|
|
||||||
# 收到websocket错误的处理
|
|
||||||
def on_error(ws, error):
|
|
||||||
print("### error:", error)
|
|
||||||
|
|
||||||
|
|
||||||
# 收到websocket关闭的处理
|
|
||||||
def on_close(ws):
|
|
||||||
print("### closed ###")
|
|
||||||
|
|
||||||
|
|
||||||
# 收到websocket连接建立的处理
|
|
||||||
def on_open(ws):
|
|
||||||
def run(*args):
|
|
||||||
global st
|
|
||||||
text_base64 = str(
|
|
||||||
base64.b64encode((wsParam.text).encode('utf-8')), "UTF8")
|
|
||||||
d = {"text": text_base64}
|
|
||||||
d = json.dumps(d)
|
|
||||||
print("Start sending text data")
|
|
||||||
st = time.time()
|
|
||||||
ws.send(d)
|
|
||||||
|
|
||||||
thread.start_new_thread(run, ())
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
parser = argparse.ArgumentParser()
|
|
||||||
parser.add_argument(
|
|
||||||
"--text",
|
|
||||||
type=str,
|
|
||||||
help="A sentence to be synthesized",
|
|
||||||
default="您好,欢迎使用语音合成服务。")
|
|
||||||
parser.add_argument(
|
|
||||||
"--server", type=str, help="server ip", default="127.0.0.1")
|
|
||||||
parser.add_argument("--port", type=int, help="server port", default=8092)
|
|
||||||
args = parser.parse_args()
|
|
||||||
|
|
||||||
print("***************************************")
|
|
||||||
print("Server ip: ", args.server)
|
|
||||||
print("Server port: ", args.port)
|
|
||||||
print("Sentence to be synthesized: ", args.text)
|
|
||||||
print("***************************************")
|
|
||||||
|
|
||||||
wsParam = WsParam(text=args.text, server=args.server, port=args.port)
|
|
||||||
|
|
||||||
websocket.enableTrace(False)
|
|
||||||
wsUrl = wsParam.create_url()
|
|
||||||
ws = websocket.WebSocketApp(
|
|
||||||
wsUrl, on_message=on_message, on_error=on_error, on_close=on_close)
|
|
||||||
ws.on_open = on_open
|
|
||||||
ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE})
|
|
||||||
|
|
||||||
t.join()
|
|
||||||
print("End of playing audio")
|
|
||||||
stream.stop_stream()
|
|
||||||
stream.close()
|
|
||||||
p.terminate()
|
|
Loading…
Reference in new issue