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
|
# gst_simple_tts.py
# Copyright (C) 2010 Aleksey Lim
#
# 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 St, Fifth Floor, Boston, MA 02110-1301 USA
import gtk
import gst
import pango
window = gtk.Window()
window.connect('destroy',
lambda sender: gtk.main_quit())
workspace = gtk.VBox()
window.add(workspace)
# text widget
scrolled = gtk.ScrolledWindow()
workspace.pack_start(scrolled)
text = gtk.TextView()
text.set_left_margin(50)
text.set_right_margin(50)
text.set_wrap_mode(gtk.WRAP_WORD)
scrolled.add(text)
buffer = text.props.buffer
buffer.props.text = file("testtts.txt").read()
tag = buffer.create_tag()
tag.props.weight = pango.WEIGHT_BOLD
# play controls
toolbar = gtk.HBox()
workspace.pack_end(toolbar, False)
play = gtk.Button('Play/Resume')
play.connect('clicked',
lambda sender: pipe.set_state(gst.STATE_PLAYING))
toolbar.add(play)
pause = gtk.Button('Pause')
pause.connect('clicked',
lambda sender: pipe.set_state(gst.STATE_PAUSED))
toolbar.add(pause)
stop = gtk.Button('Stop')
stop.connect('clicked',
lambda sender: pipe.set_state(gst.STATE_NULL))
toolbar.add(stop)
# gst code
pipe = gst.parse_launch('espeak name=src ! autoaudiosink')
src = pipe.get_by_name('src')
src.props.text = buffer.props.text
src.props.track = 1 # track for words
def tts_cb(bus, message):
if message.structure.get_name() != 'espeak-word':
return
offset = message.structure['offset']
len = message.structure['len']
buffer.remove_tag(tag, buffer.get_start_iter(), buffer.get_end_iter())
start = buffer.get_iter_at_offset(offset)
end = buffer.get_iter_at_offset(offset + len)
buffer.apply_tag(tag, start, end)
bus = pipe.get_bus()
bus.add_signal_watch()
bus.connect('message::element', tts_cb)
# gtk start
window.show_all()
gtk.main()
|