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 DEBUG = False
28
29 import gobject
30 gobject.threads_init()
31 import pygst
32 pygst.require("0.10")
33 import gst
34 import os, sys
35 import time
36 import urlparse, urllib
37 from threading import Thread
38
39
41 return urlparse.urljoin(
42 'file:', urllib.pathname2url(path))
43
44
46
48 Thread.__init__(self)
49 import liblo
50 self.port = port
51 try:
52 self.server = liblo.Server(self.port)
53 except liblo.ServerError, err:
54 print str(err)
55
58
60 while True:
61 self.server.recv(100)
62
63
65
72
74 self.server.setup(channel, self.server.IN, pull_up_down=self.method)
75 self.server.add_event_detect(channel, self.method, callback=callback, bouncetime=bouncetime)
76
79
80
81
83
84 osc_port = 12345
85 gpio_channel_play = 22
86 gpio_channel_stop = 24
87 playing = False
88 looping = False
89 auto_next = False
90 alsa_device = 'hw:0'
91 gpio_parasite_filter_time = 0.02
92
94
95 self.play_dir = play_dir
96 self. playlist = []
97 self.set_playlist()
98
99
100 self.osc_controller = OSCController(self.osc_port)
101 self.osc_controller.add_method('/play', 'i', self.osc_play_pause)
102 self.osc_controller.add_method('/stop', 'i', self.osc_stop)
103 self.osc_controller.start()
104
105
106 self.gpio_controller = GPIOController()
107 self.gpio_controller.add_channel_callback(self.gpio_channel_play, self.gpio_play, 3000)
108 self.gpio_controller.add_channel_callback(self.gpio_channel_stop, self.gpio_stop, 1000)
109 self.gpio_controller.start()
110
111
112 self.pipeline = gst.Pipeline()
113
114
115 self.bus = self.pipeline.get_bus()
116 self.bus.add_signal_watch()
117 self.bus.connect('message::eos', self.on_eos)
118
119 self.bus.connect('message::error', self.on_error)
120
121
122 self.srcdec = gst.element_factory_make('uridecodebin')
123 self.conv = gst.element_factory_make('audioconvert')
124 self.rsmpl = gst.element_factory_make('audioresample')
125 self.sink = gst.element_factory_make('alsasink')
126
127
128
129 self.play_id = 0
130 self.uri = self.playlist[self.play_id]
131 self.srcdec.set_property('uri', self.uri)
132
133
134 self.srcdec.connect('pad-added', self.on_pad_added)
135
136
137
138
139
140
141 self.sink.set_property('device', self.alsa_device)
142
143
144 self.pipeline.add(self.srcdec, self.conv, self.rsmpl, self.sink)
145
146
147
148 gst.element_link_many(self.conv, self.rsmpl, self.sink)
149
150
151 self.apad = self.conv.get_pad('sink')
152
153
154 self.mainloop = gobject.MainLoop()
155
156 if self.playing:
157 self.play()
158
160 caps = pad.get_caps()
161 name = caps[0].get_name()
162
163 if name == 'audio/x-raw-float' or name == 'audio/x-raw-int':
164 if not self.apad.is_linked():
165 pad.link(self.apad)
166
172
174 taglist = msg.parse_tag()
175 print 'on_tag:'
176 for key in taglist.keys():
177 print '\t%s = %s' % (key, taglist[key])
178
180 error = msg.parse_error()
181 print 'on_error:', error[1]
182 self.mainloop.quit()
183
185 for root, dirs, files in os.walk(self.play_dir):
186 for filename in files:
187 path = root + os.sep + filename
188 self.playlist.append(path2url(path))
189 self.playlist.sort()
190
192 self.play_id += 1
193 if self.play_id >= len(self.playlist):
194 self.play_id = 0
195 self.uri = self.playlist[self.play_id]
196 self.pipeline.set_state(gst.STATE_NULL)
197 self.srcdec.set_property('uri', self.uri)
198 self.pipeline.set_state(gst.STATE_PLAYING)
199 if DEBUG:
200 print self.play_id, self.uri
201
205
207 if not self.playing:
208 self.pipeline.set_state(gst.STATE_PLAYING)
209 self.playing = True
210 elif self.auto_next:
211 self.next()
212
214 if self.playing:
215 self.pipeline.set_state(gst.STATE_NULL)
216 self.playing = False
217
219 if self.playing:
220 self.pipeline.set_state(gst.STATE_PAUSED)
221 self.playing = False
222
224 value = value[0]
225 if value and not self.playing:
226 self.play()
227 else:
228 self.pause()
229
231 value = value[0]
232 if value and self.playing:
233 self.stop()
234
238
242
245
248