-
Notifications
You must be signed in to change notification settings - Fork 1
/
scarlett_speaker.py
213 lines (176 loc) · 6.16 KB
/
scarlett_speaker.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# import pprint
# import dbus
# import dbus.service
# import dbus.mainloop.glib
# from dbus.mainloop.glib import threads_init
# import gobject
# gobject.threads_init()
# threads_init()
# import pygst
# pygst.require('0.10')
# import gst
import dbus
import dbus.service
from dbus.mainloop.glib import DBusGMainLoop
from dbus.mainloop.glib import threads_init
import argparse
import pprint
pp = pprint.PrettyPrinter(indent=4)
import gi
gi.require_version('Gst', '1.0')
from gi.repository import GObject
from gi.repository import Gst
from gi.repository import GLib
from gi.repository import Gio
import threading
GObject.threads_init()
Gst.init(None)
threads_init()
DBusGMainLoop(set_as_default=True)
print '********************************************************'
print 'GObject: '
pp.pprint(GObject.pygobject_version)
print ''
print 'Gst: '
pp.pprint(Gst.version_string())
print '********************************************************'
Gst.debug_set_active(True)
Gst.debug_set_default_threshold(3)
import StringIO
import os
import sys
import re
import ConfigParser
import signal
from IPython.core.debugger import Tracer
from IPython.core import ultratb
from gettext import gettext as _
sys.excepthook = ultratb.FormattedTB(mode='Verbose',
color_scheme='Linux',
call_pdb=True,
ostream=sys.__stdout__)
from colorlog import ColoredFormatter
import logging
def setup_logger():
"""Return a logger with a default ColoredFormatter."""
formatter = ColoredFormatter(
"%(asctime)s.%(msecs)03d (%(threadName)-9s) %(filename)s %(funcName)s %(module)s %(processName)s %(log_color)s%(levelname)-8s%(reset)s %(message_log_color)s%(message)s",
datefmt='%Y-%m-%d,%H:%M:%S',
reset=True,
log_colors={
'DEBUG': 'cyan',
'INFO': 'green',
'WARNING': 'yellow',
'ERROR': 'red',
'CRITICAL': 'red',
},
secondary_log_colors={
'message': {
'ERROR': 'red',
'CRITICAL': 'red',
'DEBUG': 'yellow'
}
},
style='%'
)
logger = logging.getLogger(__name__)
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
return logger
# Create a player
logger = setup_logger()
import threading
import time
import scarlett_gstutils
class ScarlettSpeaker():
def __init__(self, cmd):
global logger
self._loop = gobject.MainLoop()
self.debug = False
# Element playbin automatic plays any cmd
espeak_pipeline = 'espeak name=source ! autoaudiosink'
self.player = gst.parse_launch(espeak_pipeline)
self.end_cond = threading.Condition(threading.Lock())
#######################################################################
# all writable properties(including text) make sense only at start playing;
# to apply new values you need to stop pipe.set_state(gst.STATE_NULL) pipe and
# start it again with new properties pipe.set_state(gst.STATE_PLAYING).
# source: http://wiki.sugarlabs.org/go/Activity_Team/gst-plugins-espeak
#######################################################################
# Set the uri to the cmd
source = self.player.get_by_name("source")
source.props.pitch = 50
source.props.rate = 100
source.props.voice = "en+f3"
source.props.text = _(f'{cmd}')
self.text = source.props.text
# Enable message bus to check for errors in the pipeline
bus = self.player.get_bus()
bus.add_signal_watch()
# bus.connect("message", self.on_message)
bus.connect("message", self._on_message_cb)
logger.debug("ScarlettSpeaker __init__ finished")
self.mainloopthread = scarlett_gstutils.MainloopThread(self._loop)
self.mainloopthread.start()
# start pipeline
self.player.set_state(gst.STATE_PLAYING)
def run(self):
logger.debug(f"ScarlettSpeaker text: {self.self.text}")
# self.player.set_state(gst.STATE_PLAYING)
self._loop.run()
def on_message(self, bus, message):
t = message.type
if t == gst.MESSAGE_EOS:
self.player.set_state(gst.STATE_NULL)
self._loop.quit()
self.quit()
elif t == gst.MESSAGE_ERROR:
self.player.set_state(gst.STATE_NULL)
err, debug = message.parse_error()
print "Error: %s" % err, debug
self._loop.quit()
self.quit()
def _on_message_cb(self, bus, message):
if self.debug:
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(bus)
pp.pprint(message)
t = message.type
if t == gst.MESSAGE_EOS:
logger.debug("OKAY, MESSAGE_EOS: ".format(gst.MESSAGE_EOS))
self.end_cond.acquire()
self.player.set_state(gst.STATE_NULL)
self._loop.quit()
self.end_reached = True
self.end_cond.notify()
self.end_cond.release()
self.quit()
elif t == gst.MESSAGE_ERROR:
logger.debug("OKAY, MESSAGE_ERROR: ".format(gst.MESSAGE_ERROR))
self.end_cond.acquire()
self.player.set_state(gst.STATE_NULL)
self._loop.quit()
self.end_reached = True
err, debug = message.parse_error()
self.error_msg = f"Error: {err}", debug
self.end_cond.notify()
self.end_cond.release()
self.quit()
def finish_request(self):
self.player.set_state(gst.STATE_NULL)
self._loop.quit()
self.quit()
time.sleep(2)
return
def on_finish(self, bus, message):
logger.debug("OKAY, on_finish. Setting state to STATE_NULL")
self.finish_request()
def on_error(self, bus, message):
logger.debug("OKAY, on_error. Setting state to STATE_NULL")
self.finish_request()
def quit(self):
logger.debug(" shutting down ScarlettSpeaker")