-
Notifications
You must be signed in to change notification settings - Fork 22
/
mimic3_client.py
executable file
·92 lines (87 loc) · 3 KB
/
mimic3_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/python
# -*- coding: utf-8 -*-
##
## Copyright 2024 Henry Kroll <[email protected]>
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
## MA 02110-1301, USA.
##
import gi
import sys
import urllib.parse
import logging
import time
# Initialize GStreamer
gi.require_version('Gst', '1.0')
from gi.repository import Gst
pipeline = None
Gst.init(None)
talk_process = None
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(lineno)d %(message)s",
handlers=[
# logging.FileHandler('/tmp/mimic_client.log'),
logging.StreamHandler()
]
)
def say(text, base_url="http://localhost:59125/api/tts"):
global pipeline
# Define the parameters for the TTS request
params = {'text': text, "voice": "en_US/vctk_low"}
query_string = "&".join(f"{k}={urllib.parse.quote_plus(v)}" for k, v in params.items())
# Define the GStreamer pipeline
pipeline_description = (
f" souphttpsrc name=soup location={base_url}?{query_string} "
"! wavparse "
"! audioconvert "
"! audioresample "
"! autoaudiosink"
)
pipeline = Gst.parse_launch(pipeline_description)
# Bus callback to handle EOS and ERROR
def on_message(bus, message):
global pipeline
mtype = message.type
if mtype == Gst.MessageType.EOS:
logging.debug("End-Of-Stream reached.")
pipeline.set_state(Gst.State.NULL)
elif mtype == Gst.MessageType.ERROR:
err, debug = message.parse_error()
logging.debug(f"Error received from element {message.src.get_name()}: {err.message}", file=sys.stderr)
if debug:
logging.debug(f"Debugging information: {debug}", file=sys.stderr)
return True
# Add a bus watch to the pipeline
bus = pipeline.get_bus()
bus.add_signal_watch()
bus.connect("message", on_message)
# Start the pipeline
pipeline.set_state(Gst.State.PLAYING)
def shutup():
global pipeline
for element in pipeline.children:
if isinstance(element, Gst.Element):
element.set_state(Gst.State.NULL)
time.sleep(0.2)
if pipeline is not None:
pipeline.send_event(Gst.Event.new_eos())
time.sleep(0.6)
pipeline.set_state(Gst.State.NULL)
# Example usage
if __name__ == "__main__":
say("Hello, this is a test of the text to speech system.")
time.sleep(1)
shutup()