utils/pianoroll-client.py
author ymh <ymh.work@gmail.com>
Thu, 09 Oct 2014 08:44:36 +0200
changeset 2 fc1ab0074e29
parent 0 e1d4d7a8255a
child 21 89d235bcbbf3
permissions -rw-r--r--
export config in config file + add static file

#!/usr/bin/env python
"""
Example of a UDP txosc sender with Twisted.

This example is in the public domain.
"""

import csv
import sys
import time

from twisted.internet import reactor
from txosc import osc
from txosc import dispatch
from txosc import async


class UDPSenderApplication(object):
    """
    Example that sends UDP messages.
    """
    def __init__(self, port, host, address, rows):
        self.port = port
        self.host = host
        self.client = async.DatagramClientProtocol()
        self._client_port = reactor.listenUDP(0, self.client)
        self.rows = rows
        self.address = address
        reactor.callLater(0, self.send_messages)

    def _send(self, element):
        # This method is defined only to simplify the example
        self.client.send(element, (self.host, self.port))
        print("Sent %s to %s:%d" % (element, self.host, self.port))

    def send_messages(self):
        tc = 0
        for row in self.rows:
            row_conv = [ osc.TimeTagArgument(float(row[0]))] + [osc.IntArgument(int(a)) for a in row[1:]]
            time.sleep((row_conv[1].value-tc)/10**3)
            tc = row_conv[1].value
            self._send(osc.Message(self.address,*row_conv))
        print("Goodbye.")
        reactor.callLater(0.1, reactor.stop)

if __name__ == "__main__":

    with open(sys.argv[1], 'rU') as datafile:
        reader = csv.reader(datafile, delimiter=' ')
        app = UDPSenderApplication(9090, "127.0.0.1", '/pianoroll/test/', list(reader))

    reactor.run()