Package toon :: Module opensoundcontrol
[hide private]
[frames] | no frames]

Source Code for Module toon.opensoundcontrol

  1  #!/usr/bin/env python 
  2  # 
  3  # Toonloop for Python 
  4  # 
  5  # Copyright 2008 Alexandre Quessy & Tristan Matthews 
  6  # <alexandre@quessy.net> & <le.businessman@gmail.com> 
  7  # http://www.toonloop.com 
  8  # 
  9  # Original idea by Alexandre Quessy 
 10  # http://alexandre.quessy.net 
 11  # 
 12  # Toonloop is free software: you can redistribute it and/or modify 
 13  # it under the terms of the GNU General Public License as published by 
 14  # the Free Software Foundation, either version 3 of the License, or 
 15  # (at your option) any later version. 
 16  # 
 17  # Toonloop is distributed in the hope that it will be useful, 
 18  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 19  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 20  # GNU General Public License for more details. 
 21  # 
 22  # You should have received a copy of the gnu general public license 
 23  # along with Toonloop.  If not, see <http://www.gnu.org/licenses/>. 
 24  # 
 25  """ 
 26  OSC controls for Toonloop 
 27  """ 
 28  from rats import osc 
 29   
30 -def _print(text):
31 """ 32 Simply prints to the console debug messages 33 """ 34 print(text)
35
36 -class ToonOsc(object):
37 """ 38 OSC callbacks and sends for Toonloop 39 40 == Sends: == 41 * /toon/frame <i> 42 * /toon/sequence <i> 43 * /toon/framerate <i> 44 * /toon/writehead <i> 45 * /sampler/play/start <i> 46 * /sampler/play/stop <i> 47 * /sampler/record/start <i> 48 * /sampler/record/stop <i> 49 50 == Receives: == 51 * /toon/frame/remove 52 * /toon/frame/add 53 * /toon/osc/subscribe <s> <i> 54 * /toon/clip/select <i> 55 """ 56 #TODO: 57 #Receives: 58 #/toon/framerate/set <i> 59 #/toon/framerate/increase 60 #/toon/framerate/decrease 61 #/toon/auto/enable <i> 62 #/toon/auto/rate <i> 63 #/toon/reset 64 #/toon/playhead <i> 65 #/toon/writehead <i> 66 #/toon/clip/reset <i> 67 68 # TODO: UDP broadcasting 69 # could be to 255.255.255.255 or 192.168.0.255 or so.
70 - def __init__(self, toonloop, listen_port=7772, send_port=7770, send_host="localhost"):
71 self.toonloop = toonloop 72 self.listen_port = listen_port 73 self.send_port = send_port 74 self.send_host = send_host 75 self.osc_server = None 76 self.osc_sender = None 77 self.verbose = self.toonloop.config.osc_verbose 78 # register callbacks 79 self._setup()
80
81 - def __del__(self):
82 del self.osc_server 83 del self.osc_sender
84
85 - def _setup(self):
86 """ 87 Starts the server and registers the callbacks. 88 """ 89 self.osc_server = osc.OscServer(self.listen_port) 90 self.osc_server.add_callback("/toon/frame/add", None, self._r_frame_add) 91 self.osc_server.add_callback("/toon/frame/remove", None, self._r_frame_remove) 92 self.osc_server.add_callback("/toon/osc/subscribe", "si", self._r_osc_subscribe) # host, port. Only one subscriber for now. 93 self.osc_server.add_callback("/toon/clip/select", "i", self._r_clip_select) 94 self.osc_server.add_callback("/toon/clip/reset", None, self._r_clip_reset) # XXX dangerous 95 self.osc_server.add_callback("/toon/clip/save", None, self._r_clip_save) 96 self.osc_server.add_callback("/toon/pause", None, self._r_pause) 97 self.osc_server.add_callback("/toon/quit", None, self._r_quit) # XXX dangerous 98 self.osc_server.add_callback("/toon/config/set", "ss", self._r_config_set) # XXX dangerous 99 self.osc_server.add_callback("/toon/print/stats", None, self._r_print_stats) 100 self.osc_server.add_callback("/toon/framerate/increase", "i", self._r_framerate_increase) 101 102 self.osc_server.add_callback("/ping", None, self._r_ping) 103 self.osc_server.add_callback("/pong", None, self._r_pong) 104 self.osc_server.add_callback(None, None, self._r_fallback) 105 print("Will now start listening OSC server...") 106 self.osc_server.start() # this hangs on Ubuntu 8.10. Update liblo-dev to latest tarball. 107 print("OSC Server started. ") 108 # the sender 109 client_addr = "osc.udp://%s:%d" % (self.send_host, self.send_port) 110 self.osc_sender = osc.OscClient(client_addr) 111 # would be nice to have a push/subscribe mechanism, with many senders. 112 #c.send_message("/ping", ('i', 123), ('f', 3.13159), ('s', "hello")) 113 114 self.toonloop.signal_playhead.connect(self._slot_playhead)# int index 115 self.toonloop.signal_writehead.connect(self._slot_writehead)# int index 116 self.toonloop.signal_framerate.connect(self._slot_framerate) # int fps 117 self.toonloop.signal_clip.connect(self._slot_clip) # int clip id
118 #self.toonloop.signal_sampler_record.connect(self._slot_sampler_record)# bool start/stop 119 120 # slots for toonloop's signals:
121 - def _slot_playhead(self, index):
122 """ 123 Slot which listens or a Toonloop's signal 124 :param index: int frame index 125 """ 126 #if self.verbose: 127 # print("playhead %s" % (index)) 128 self._s("/toon/playhead", index)
129
130 - def _slot_writehead(self, index):
131 """ 132 Slot which listens for a Toonloop's signal 133 :param index: int frame index 134 """ 135 if self.verbose: 136 print("writehead %s" % (index)) 137 self._s("/toon/writehead", index)
138
139 - def _slot_framerate(self, fps):
140 """ 141 Slot which listens for a Toonloop's signal 142 :param fps: int fps 143 """ 144 if self.verbose: 145 print("fps %s" % (fps)) 146 self._s("/toon/framerate", fps)
147
148 - def _slot_clip(self, index):
149 """ 150 Slot which listens for a Toonloop's signal 151 :param index: int clip number 152 """ 153 if self.verbose: 154 print("clip %s" % (index)) 155 self._s("/toon/clip/index", index)
156 157 #def _slot_sampler_record(self, starting): 158 # """ 159 # Slot which listens for a Toonloop's signal 160 # :param starting: bool start/stop 161 # """ 162 # if self.verbose: 163 # print("sampler record %s" % (starting)) 164 # #TODO: send_record_start 165 # #TODO: send_record_stop 166
167 - def send_sampler_record_start(self, buffer_index):
168 self._s("/sampler/record/start", buffer_index)
169
170 - def send_sampler_record_stop(self, index):
171 self._s("/sampler/record/stop", index)
172
173 - def send_sampler_clear(self, index):
174 self._s("/sampler/clear", index)
175
176 - def send_sampler_play_start(self, player_id, buffer_id):
177 self.osc_sender.send_message("/sampler/play/start", ('i', player_id), ('i', buffer_id))
178
179 - def send_sampler_play_stop(self, index):
180 self._s("/sampler/play/stop", index)
181
182 - def _s(self, path, index):
183 """ 184 wrapper for sending a osc message with an int arg. 185 """ 186 self.osc_sender.send_message(path, ('i', index))
187
188 - def _r_frame_add(self, path, args):
189 print("Got /toon/frame/add") 190 self.toonloop.frame_add()
191
192 - def _r_pause(self, path, args):
193 print("Got /toon/pause") 194 self.toonloop.pause()
195
196 - def _r_config_set(self, path, args):
197 key = args[0] 198 value = args[1] 199 print("Got /toon/config/set %s %s" % (key, value)) 200 self.toonloop.config_set(key, value)
201
202 - def _r_clip_select(self, path, args):
203 print("Got /toon/clip/select") 204 self.toonloop.clip_select(args[0])
205
206 - def _r_clip_reset(self, path, args):
207 # FIXME : dangerous !!! 208 print("Got /toon/clip/reset") 209 self.toonloop.clip_reset()
210
211 - def _r_framerate_increase(self, path, args):
212 dir = args[0] 213 print("got /toon/framerate/increase %d" % (dir)) 214 self.toonloop.framerate_increase(dir)
215
216 - def _r_clip_save(self, path, args):
217 print("Got /toon/clip/save") 218 self.toonloop.clip_save()
219
220 - def _r_quit(self, path, args):
221 print("Got /toon/quit") 222 self.toonloop.quit()
223
224 - def _r_print_stats(self, path, args):
225 print("Got /toon/print/stats") 226 self.toonloop.print_stats()
227
228 - def _r_frame_remove(self, path, args):
229 print("Got /toon/frame/remove") 230 self.toonloop.frame_remove()
231
232 - def _r_osc_subscribe(self, path, args):
233 print("Got /toon/osc/subscribe") 234 try: 235 self.send_host = args[0] 236 self.send_port = args[1] 237 except IndexError, e: 238 print(e.message) 239 else: 240 del self.osc_sender 241 client_addr = "osc.udp://%s:%d" % (self.send_host, self.send_port) 242 print("Now sending to %s" % (client_addr)) 243 self.osc_sender = osc.OscClient(client_addr)
244
245 - def _r_ping(self, path, args):
246 print("Got /ping. Sending /pong.") 247 self.osc_sender.send_message("/pong")
248
249 - def _r_pong(self, path, args):
250 print("Got /pong.")
251
252 - def _r_fallback(self, path, args, types, src):
253 print("got unknown OSC message '%s' from '%s'" % (path, src.get_url())) 254 for a, t in zip(args, types): 255 print "argument of type '%s': %s" % (t, a)
256