http_client.py 3.0 KB
Newer Older
L
lym0302 已提交
1
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
L
lym0302 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15
#
# 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

L
lym0302 已提交
16
from paddlespeech.server.utils.audio_handler import TTSHttpHandler
L
lym0302 已提交
17
from paddlespeech.server.utils.util import compute_delay
L
lym0302 已提交
18 19 20 21

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument(
L
lym0302 已提交
22
        "--text",
L
lym0302 已提交
23
        type=str,
L
lym0302 已提交
24 25 26 27 28
        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)
L
lym0302 已提交
29 30 31 32 33 34 35 36
    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,
L
lym0302 已提交
37
        choices=[0, 8000, 16000],
L
lym0302 已提交
38 39
        help='Sampling rate, the default is the same as the model')
    parser.add_argument(
L
lym0302 已提交
40
        "--output", type=str, help="save audio path", default=None)
L
lym0302 已提交
41
    parser.add_argument(
L
lym0302 已提交
42
        "--play", type=bool, help="whether to play audio", default=False)
L
lym0302 已提交
43
    args = parser.parse_args()
L
lym0302 已提交
44 45 46

    print("tts http client start")
    handler = TTSHttpHandler(args.server, args.port, args.play)
L
lym0302 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    first_response, final_response, duration, save_audio_success, receive_time_list, chunk_duration_list = handler.run(
        args.text, args.spk_id, args.speed, args.volume, args.sample_rate,
        args.output)
    delay_time_list = compute_delay(receive_time_list, chunk_duration_list)

    print(f"sentence: {args.text}")
    print(f"duration: {duration} s")
    print(f"first response: {first_response} s")
    print(f"final response: {final_response} s")
    print(f"RTF: {final_response/duration}")
    if args.output is not None:
        if save_audio_success:
            print(f"Audio successfully saved in {args.output}")
        else:
            print("Audio save failed.")

    if delay_time_list != []:
        print(
            f"Delay situation: total number of packages: {len(receive_time_list)}, the number of delayed packets: {len(delay_time_list)}, minimum delay time: {min(delay_time_list)} s, maximum delay time: {max(delay_time_list)} s, average delay time: {sum(delay_time_list)/len(delay_time_list)} s, delay rate:{len(delay_time_list)/len(receive_time_list)}"
        )
    else:
        print("The sentence has no delay in streaming synthesis.")