-
Notifications
You must be signed in to change notification settings - Fork 0
/
livefrontend.py
executable file
·147 lines (119 loc) · 5.17 KB
/
livefrontend.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
#!/usr/bin/env python
# Copyright (c) 2008 Carnegie Mellon University.
#
# You may modify and redistribute this file under the same terms as
# the CMU Sphinx system. See
# http://cmusphinx.sourceforge.net/html/LICENSE for more information.
import gobject
import pygst
pygst.require('0.10')
gobject.threads_init()
import gst
from MythTV import Frontend
class DemoApp(object):
"""GStreamer/PocketSphinx Demo Application"""
def __init__(self):
"""Initialize a DemoApp object"""
self.init_gst()
self.init_tivo()
def init_gst(self):
"""Initialize the speech components"""
self.pipeline = gst.Pipeline('player')
source = gst.element_factory_make('pulsesrc')
convert = gst.element_factory_make('audioconvert')
resample = gst.element_factory_make('audioresample')
vader = gst.element_factory_make('vader')
vader.set_property('name', 'vad')
vader.set_property('auto-threshold', 'true')
sphinx = gst.element_factory_make('pocketsphinx')
sphinx.set_property('name', 'asr')
sink = gst.element_factory_make('fakesink')
self.pipeline.add(source, convert, resample, vader, sphinx, sink)
gst.element_link_many(source, convert, resample, vader, sphinx, sink)
asr = self.pipeline.get_by_name('asr')
asr.connect('partial_result', self.asr_partial_result)
asr.connect('result', self.asr_result)
asr.set_property('configured', True)
asr.set_property('lm', 'language_model.lm')
asr.set_property('dict', 'dictionary.dic')
bus = self.pipeline.get_bus()
bus.add_signal_watch()
bus.connect('message::application', self.application_message)
self.pipeline.set_state(gst.STATE_PLAYING)
def init_tivo(self):
"""Initialize the MythTV Frontend connection"""
self.frontend = Frontend('localhost', '6546')
def asr_partial_result(self, asr, text, uttid):
"""Forward partial result signals on the bus to the main thread."""
struct = gst.Structure('partial_result')
struct.set_value('hyp', text)
struct.set_value('uttid', uttid)
asr.post_message(gst.message_new_application(asr, struct))
def asr_result(self, asr, text, uttid):
"""Forward result signals on the bus to the main thread."""
struct = gst.Structure('result')
struct.set_value('hyp', text)
struct.set_value('uttid', uttid)
asr.post_message(gst.message_new_application(asr, struct))
def application_message(self, bus, msg):
"""Receive application messages from the bus."""
msgtype = msg.structure.get_name()
if msgtype == 'result':
res = msg.structure['hyp']
if res: print res
self.dispatch_command(res)
def dispatch_command(self, command):
play_commands = {
"TIVO PLAY": "speed normal",
"TIVO PAUSE": "speed pause",
"TIVO SKIP": "seek forward",
"TIVO SKIP FORWARD": "seek forward",
"TIVO SKIP BACK": "seek backward",
}
key_commands = {
"TIVO STOP": "escape",
"TIVO ESCAPE": "escape",
"TIVO PAGE UP": "pageup",
"TIVO PAGE DOWN": "pagedown",
"TIVO UP": "up",
"TIVO DOWN": "down",
"TIVO ALTO": "up",
"TIVO BAHO": "down",
"TIVO LEFT": "left",
"TIVO RIGHT": "right",
"TIVO ENTER": "enter",
"TIVO SELECT": "enter",
"TIVO OK": "enter",
}
jump_commands = {
"TIVO STOP": "playbackbox",
}
if command in play_commands.keys():
print "** received %s, sending 'play %s'" % (command, play_commands[command])
self.frontend.sendPlay(play_commands[command])
elif command in key_commands.keys():
print "** received %s, sending 'key %s'" % (command, key_commands[command])
try:
self.frontend.sendKey(key_commands[command])
except AttributeError:
self.frontend.key[key_commands[command]]
elif command in jump_commands.keys():
print "** received %s, sending 'jump %s'" % (command, jump_commands[command])
try:
self.frontend.sendJump(jump_commands[command])
except AttributeError:
self.frontend.jump[jump_commands[command]]
elif command == "TIVO PLAY DAILY SHOW":
print "PLAYing latest daily show"
daily_shows = [x for x in self.frontend.sendQuery('recordings').split('\r\n') if x.find('Daily Show') >= 0]
latest_ds = sorted(daily_shows)[-1].split()
self.frontend.sendPlay('program %s %s' % (latest_ds[0], latest_ds[1]))
elif command == "TIVO PLAY COLBERT":
print "PLAYing latest colbert"
daily_shows = [x for x in self.frontend.sendQuery('recordings').split('\r\n') if x.find('Colbert Report') >= 0]
latest_ds = sorted(daily_shows)[-1].split()
self.frontend.sendPlay('program %s %s' % (latest_ds[0], latest_ds[1]))
app = DemoApp()
loop = gobject.MainLoop()
gobject.threads_init()
loop.run()