utils/pianoroll-client.py
changeset 85 eff9460bd4f2
parent 73 2a6590aeac15
child 93 79ae42ad97d4
--- a/utils/pianoroll-client.py	Mon Jan 12 17:23:05 2015 +0100
+++ b/utils/pianoroll-client.py	Tue Jan 13 10:46:05 2015 +0100
@@ -7,12 +7,11 @@
 
 import argparse
 import csv
-import sys
 import time
 
+import ntplib
 from twisted.internet import reactor
 from txosc import osc
-from txosc import dispatch
 from txosc import async
 
 
@@ -20,13 +19,14 @@
     """
     Example that sends UDP messages.
     """
-    def __init__(self, port, host, address, rows):
+    def __init__(self, port, host, address, rows, shift):
         self.port = port
         self.host = host
         self.client = async.DatagramClientProtocol()
         self._client_port = reactor.listenUDP(0, self.client)
         self.rows = rows
         self.address = address
+        self.shift = shift
         reactor.callLater(0, self.send_messages)
 
     def _send(self, element):
@@ -35,8 +35,10 @@
         print("Sent %s to %s:%d" % (element, self.host, self.port))
 
     def send_messages(self):
-        tc = 0
+        t0 = time.time()
         for row in self.rows:
+            if self.shift:
+                row[0] = ntplib.system_to_ntp_time(t0 + float(row[1])/10**3)
             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)
             time.sleep(0.1)
@@ -50,11 +52,12 @@
     parser = argparse.ArgumentParser(description='Simulate an (osc) pianoroll client.')
     parser.add_argument('datafile', metavar='DATAFILE', help='The file containing the pianoroll data (CSV).')
     parser.add_argument('-e', '--event', dest='event', metavar='EVENT', required=True, help='the event code.')
+    parser.add_argument('-s', '--shift', dest='shift', action='store_true', required=False, help='Shift the data.', default=False)
 
     args = parser.parse_args()
 
     with open(args.datafile, 'rU') as datafile:
         reader = csv.reader(datafile, delimiter=' ')
-        app = UDPSenderApplication(9090, "127.0.0.1", "/pianoroll/%s/" % args.event, list(reader))
+        app = UDPSenderApplication(9090, "127.0.0.1", "/pianoroll/%s/" % args.event, list(reader), args.shift)
 
     reactor.run()