utils/pianoroll-client.py
changeset 0 e1d4d7a8255a
child 2 fc1ab0074e29
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/utils/pianoroll-client.py	Wed Oct 08 15:14:58 2014 +0200
@@ -0,0 +1,53 @@
+#!/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))
+            break
+        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()