getOSC.py :  » Media-Sound-Audio » athenaCL » athenaCL » libATH » osc » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Media Sound Audio » athenaCL 
athenaCL » athenaCL » libATH » osc » getOSC.py
#!/usr/bin/python
"""Waits on port 4950, ready to decode any OSC packets that come its
way."""

import sys, time, socket
from athenaCL.libATH.osc import OSC
from athenaCL.tools.util.superCollider.scListenerSingleton import Future

def decodeOSC(data):
    """Converts a typetagged OSC message to a Python list."""
    table = {"i":OSC.readInt, "f":OSC.readFloat, "s":OSC.readString, "b":OSC.readBlob}
    decoded = []
    address,  rest = OSC.readString(data)
    typetags = ""

    if address == "#bundle":
        time, rest = OSC.readLong(rest)
        decoded.append(address)
        decoded.append(time)
        while len(rest)>0:
            length, rest = OSC.readInt(rest)
            decoded.append(decodeOSC(rest[:length]))
            rest = rest[length:]

    elif len(rest)>0:
        typetags, rest = OSC.readString(rest)
        decoded.append(address)
        decoded.append(typetags)
        if(typetags[0] == ","):
            for tag in typetags[1:]:
                value, rest = table[tag](rest)                
                decoded.append(value)
        else:
            print "Oops, typetag lacks the magic ,"

    # return only the data
    return decoded


class getOSC:
    def __init__(self, port):
        self.s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.s.bind(('', port))

        self.stringIn = 1
        Future(self.stringKill)
        time.sleep(.5)
        print ""
        while self.stringIn == 1:
            self.s.settimeout(.5)
            try:
                data, address = self.s.recvfrom(2**12)
                print address
                OSC.hexDump(data)
                print decodeOSC(data)
                self.printLines()
            except socket.timeout:
                print "No Data coming from port %s; (any key quits)" %port
                
    def printLines(self):
        print "_____________________"
       
    def stringKill(self):
        self.stringIn = raw_input("ENTER KEY QUITS")
        print self.stringIn


def run():
    port = 57110
    helpArguments = ('--help', '-h', '-help')

    help = """%s

Listens for OSC packets, decoding and printing
them as they arrive.

Usage:
    %s [port]
    (port defaults to %d)\n""" % (sys.argv[0], sys.argv[0], port)
    
    if len(sys.argv) >= 2:
        if sys.argv[1] in helpArguments:
            print help
            sys.exit(0)
            
        try:
            port = int(sys.argv[1])
        except:
            print help
            sys.exit(0)

    x = getOSC(port)
    
if __name__ == '__main__':
   run()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.