OSC Python
From
Voorbeeld python output OSC 'device' met pyOSC (alle platformen)
Download File:PyOSC.tar.gz, pak het gecomprimeerde bestand ergens uit en typ (linux):
sudo python setup.py install
of (windows):
python setup.py install
Nu maakt pyOSC deel uit van je python installatie
Voor onderstaande joystick client moet pygame geinstalleerd zijn, dat beschikbaar is voor Linux, Windows en Macintosh.
#!/usr/bin/env python """Eventlist is a sloppy style of pygame, but is a handy tool for learning about pygame events and input. At the top of the screen are the state of several device values, and a scrolling list of events are displayed on the bottom. This is not quality 'ui' code at all, but you can see how to implement very non-interactive status displays, or even a crude text output control. """ import OSC import sys from pygame import * ImgOnOff = [] Font = None LastKey = None def showtext(win, pos, text, color, bgcolor): textimg = Font.render(text, 1, color, bgcolor) win.blit(textimg, pos) return pos[0] + textimg.get_width() + 5, pos[1] def drawstatus(win): bgcolor = 50, 50, 50 win.fill(bgcolor, (0, 0, 640, 120)) win.blit(Font.render('Status Area', 1, (155, 155, 155), bgcolor), (2, 2)) pos = showtext(win, (10, 30), 'Mouse Focus', (255, 255, 255), bgcolor) win.blit(ImgOnOff[mouse.get_focused()], pos) pos = showtext(win, (330, 30), 'Keyboard Focus', (255, 255, 255), bgcolor) win.blit(ImgOnOff[key.get_focused()], pos) pos = showtext(win, (10, 60), 'Mouse Position', (255, 255, 255), bgcolor) p = '%s, %s' % mouse.get_pos() pos = showtext(win, pos, p, bgcolor, (255, 255, 55)) pos = showtext(win, (330, 60), 'Last Keypress', (255, 255, 255), bgcolor) if LastKey: p = '%d, %s' % (LastKey, key.name(LastKey)) else: p = 'None' pos = showtext(win, pos, p, bgcolor, (255, 255, 55)) pos = showtext(win, (10, 90), 'Input Grabbed', (255, 255, 255), bgcolor) win.blit(ImgOnOff[event.get_grab()], pos) def drawhistory(win, history): win.blit(Font.render('Event History Area', 1, (155, 155, 155), (0,0,0)), (2, 132)) ypos = 450 h = list(history) h.reverse() for line in h: r = win.blit(line, (10, ypos)) win.fill(0, (r.right, r.top, 620, r.height)) ypos -= Font.get_height() def main(): if len(sys.argv) < 3: print "usage: " + sys.argv[0] + " <IP address> <port> [OSC name]" return init() win = display.set_mode((640, 480), RESIZABLE) display.set_caption("Mouse Focus Workout") global Font Font = font.Font(None, 26) global ImgOnOff ImgOnOff.append(Font.render("Off", 1, (0, 0, 0), (255, 50, 50))) ImgOnOff.append(Font.render("On", 1, (0, 0, 0), (50, 255, 50))) history = [] #let's turn on the joysticks just so we can play with em for x in range(joystick.get_count()): j = joystick.Joystick(x) j.init() txt = 'Enabled joystick: ' + j.get_name() img = Font.render(txt, 1, (50, 200, 50), (0, 0, 0)) history.append(img) if not joystick.get_count(): img = Font.render('No Joysticks to Initialize', 1, (50, 200, 50), (0, 0, 0)) history.append(img) client = OSC.OSCClient() client.connect((sys.argv[1], int(sys.argv[2]))) # client.connect(("2001:610:600:6db::2", 8000, 0, 0)) # aankondigen output ports name = sys.argv[3] if len(sys.argv) > 3 else "pyjoy" message = OSC.OSCMessage("/" + name + "/out/outputs") message.append("axis0,axis1,axis2,axis3") client.send(message) going = True while going: for e in event.get(): if e.type == QUIT: going = False if e.type == KEYDOWN: if e.key == K_ESCAPE: going = False else: global LastKey LastKey = e.key if e.type == MOUSEBUTTONDOWN: event.set_grab(1) elif e.type == MOUSEBUTTONUP: event.set_grab(0) if e.type == VIDEORESIZE: win = display.set_mode(e.size, RESIZABLE) if e.type != MOUSEMOTION: txt = '%s: %s' % (event.event_name(e.type), e.dict) if e.type == JOYAXISMOTION: value = (e.value + 1.0) / 2.0 message = OSC.OSCMessage("/" + name + "/out/axis" + str(e.axis)) message.append(value) client.send(message) img = Font.render(txt, 1, (50, 200, 50), (0, 0, 0)) history.append(img) history = history[-13:] drawstatus(win) drawhistory(win, history) display.flip() time.wait(10) quit() if __name__ == '__main__': main()
Demo that sends on a programmable number of channels.
#!/usr/bin/env python import sys import time import random import OSC def main(): if len(sys.argv) < 3: print "usage: " + sys.argv[0] + " <IP address> <port> [OSC name]" return client = OSC.OSCClient() client.connect((sys.argv[1], int(sys.argv[2]))) #client.connect(("servertkkrlab", 8000, 0, 0)) name = sys.argv[3] if len(sys.argv) > 3 else "channels" channels = 8 channel = [0] * channels direction = [0.01] * channels cstr = "" for c in xrange(channels): if c > 0: cstr += "," cstr += "ch" + str(c+1) message = OSC.OSCMessage("/" + name + "/out/outputs") message.append(cstr) client.send(message) while True: for c in xrange(channels): if channel[c] >= 1.0: channel[c] = 1.0 direction[c] = -0.002 * random.randint(1, 3) if channel[c] <= 0.0: channel[c] = 0.0 direction[c] = 0.002 * random.randint(1, 3) message = OSC.OSCMessage("/" + name + "/out/ch" + str(c+1)) message.append(channel[c]) client.send(message) channel[c] += direction[c] time.sleep(0.02) if __name__ == '__main__': main()
Demo reading midi controller data.
#!/usr/bin/env python import OSC import sys from pygame import * import pygame.midi # midi programming examples # https://audiodestrukt.wordpress.com/2013/06/23/midi-programming-in-python/ # https://bitbucket.org/pygame/pygame/src/25e3f2cee879/examples/midi.py def main(): init() pygame.midi.init() argc = len(sys.argv) if argc < 4: print "usage: " + sys.argv[0] + " <IP address> <port> <midi dev_no> [OSC name]" # list all midi output devices for x in range( 0, pygame.midi.get_count() ): dev_info = pygame.midi.get_device_info(x) if dev_info[2] == 1: print x,dev_info return client = OSC.OSCClient() client.connect((sys.argv[1], int(sys.argv[2]))) name = sys.argv[4] if argc > 4 else "pymidi" # aankondigen output ports message = OSC.OSCMessage("/" + name + "/out/outputs") message.append("ch0,ch1,ch2,ch3,ch4,ch5,ch6,ch7,ch8,ch9,ch10,ch11,ch12,ch13,ch14,ch15") client.send(message) midi_input = pygame.midi.Input(int(sys.argv[3])) try: while True: if midi_input.poll(): # no way to find number of messages in queue # so we just specify a high max value evlist = midi_input.read(1000) for x in range( 0, len(evlist)): ev = evlist[x][0] status,data1,data2,data3 = ev; print str(status) + ", " + str(data1) + ", " + str(data2) + ", " + str(data3) if (status == 176): value = data2 / 128.0 message = OSC.OSCMessage("/" + name + "/out/ch" + str(data1)) message.append(value) client.send(message) time.wait(10) finally: del midi_input pygame.midi.quit() quit() if __name__ == '__main__': main()
Voorbeeld python output OSC 'device' met python-liblo (alleen Linux)
Installeer de python OSC library.
sudo apt-get install python-liblo
import liblo, sys, time # 192.168.100.250 is IP adres tkkrlab server # 8000 Heinze's protocol default poort try: target = liblo.Address("192.168.100.250", 8000) except liblo.AddressError, err: print str(err) sys.exit() # aankondigen output ports (vervang python door je eigen naam) liblo.send(target, "/python/out/outputs", "a,b,c,d") x = 0.0 while x < 1.0: # zend output waarde liblo.send(target, "/python/out/a", x) time.sleep(0.01) x += 0.001
More complete demo that sends on a programmable number of channels.
import liblo, sys, time import random # 192.168.100.250 is IP adres tkkrlab server # 8000 Heinze's protocol default poort try: target = liblo.Address("192.168.100.250", 8000) except liblo.AddressError, err: print str(err) sys.exit() # aankondigen output ports (vervang python door je eigen naam) channels = 8 channel = [0] * channels direction = [0.01] * channels cstr = "" for c in xrange(channels): if c > 0: cstr += "," cstr += "ch" + str(c+1) liblo.send(target, "/channels/out/outputs", cstr) while True: for c in xrange(channels): if channel[c] >= 1.0: channel[c] = 1.0 direction[c] = -0.002 * random.randint(1, 3) if channel[c] <= 0.0: channel[c] = 0.0 direction[c] = 0.002 * random.randint(1, 3) liblo.send(target, "/channels/out/ch" + str(c+1), channel[c]) channel[c] += direction[c] time.sleep(0.02)
Heinze Havinga's datadirigent geimplementeerd in Twisted:
Deze wordt gehost op Github