author | ymh <ymh.work@gmail.com> |
Tue, 13 Jan 2015 10:46:05 +0100 | |
changeset 85 | eff9460bd4f2 |
parent 73 | 2a6590aeac15 |
child 93 | 79ae42ad97d4 |
permissions | -rw-r--r-- |
0 | 1 |
#!/usr/bin/env python |
2 |
""" |
|
3 |
Example of a UDP txosc sender with Twisted. |
|
4 |
||
5 |
This example is in the public domain. |
|
6 |
""" |
|
7 |
||
73
2a6590aeac15
Add argument for events on pianoroll client
ymh <ymh.work@gmail.com>
parents:
68
diff
changeset
|
8 |
import argparse |
0 | 9 |
import csv |
10 |
import time |
|
11 |
||
85
eff9460bd4f2
add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
73
diff
changeset
|
12 |
import ntplib |
0 | 13 |
from twisted.internet import reactor |
14 |
from txosc import osc |
|
15 |
from txosc import async |
|
16 |
||
17 |
||
18 |
class UDPSenderApplication(object): |
|
19 |
""" |
|
20 |
Example that sends UDP messages. |
|
21 |
""" |
|
85
eff9460bd4f2
add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
73
diff
changeset
|
22 |
def __init__(self, port, host, address, rows, shift): |
0 | 23 |
self.port = port |
24 |
self.host = host |
|
25 |
self.client = async.DatagramClientProtocol() |
|
26 |
self._client_port = reactor.listenUDP(0, self.client) |
|
27 |
self.rows = rows |
|
28 |
self.address = address |
|
85
eff9460bd4f2
add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
73
diff
changeset
|
29 |
self.shift = shift |
0 | 30 |
reactor.callLater(0, self.send_messages) |
31 |
||
32 |
def _send(self, element): |
|
33 |
# This method is defined only to simplify the example |
|
34 |
self.client.send(element, (self.host, self.port)) |
|
35 |
print("Sent %s to %s:%d" % (element, self.host, self.port)) |
|
36 |
||
37 |
def send_messages(self): |
|
85
eff9460bd4f2
add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
73
diff
changeset
|
38 |
t0 = time.time() |
0 | 39 |
for row in self.rows: |
85
eff9460bd4f2
add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
73
diff
changeset
|
40 |
if self.shift: |
eff9460bd4f2
add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
73
diff
changeset
|
41 |
row[0] = ntplib.system_to_ntp_time(t0 + float(row[1])/10**3) |
0 | 42 |
row_conv = [ osc.TimeTagArgument(float(row[0]))] + [osc.IntArgument(int(a)) for a in row[1:]] |
21 | 43 |
#time.sleep((row_conv[1].value-tc)/10**3) |
44 |
time.sleep(0.1) |
|
48
a7abfcfd7959
real time with piano roll and new category management
cavaliet
parents:
21
diff
changeset
|
45 |
#tc = row_conv[1].value |
0 | 46 |
self._send(osc.Message(self.address,*row_conv)) |
47 |
print("Goodbye.") |
|
48 |
reactor.callLater(0.1, reactor.stop) |
|
49 |
||
50 |
if __name__ == "__main__": |
|
51 |
||
73
2a6590aeac15
Add argument for events on pianoroll client
ymh <ymh.work@gmail.com>
parents:
68
diff
changeset
|
52 |
parser = argparse.ArgumentParser(description='Simulate an (osc) pianoroll client.') |
2a6590aeac15
Add argument for events on pianoroll client
ymh <ymh.work@gmail.com>
parents:
68
diff
changeset
|
53 |
parser.add_argument('datafile', metavar='DATAFILE', help='The file containing the pianoroll data (CSV).') |
2a6590aeac15
Add argument for events on pianoroll client
ymh <ymh.work@gmail.com>
parents:
68
diff
changeset
|
54 |
parser.add_argument('-e', '--event', dest='event', metavar='EVENT', required=True, help='the event code.') |
85
eff9460bd4f2
add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
73
diff
changeset
|
55 |
parser.add_argument('-s', '--shift', dest='shift', action='store_true', required=False, help='Shift the data.', default=False) |
73
2a6590aeac15
Add argument for events on pianoroll client
ymh <ymh.work@gmail.com>
parents:
68
diff
changeset
|
56 |
|
2a6590aeac15
Add argument for events on pianoroll client
ymh <ymh.work@gmail.com>
parents:
68
diff
changeset
|
57 |
args = parser.parse_args() |
2a6590aeac15
Add argument for events on pianoroll client
ymh <ymh.work@gmail.com>
parents:
68
diff
changeset
|
58 |
|
2a6590aeac15
Add argument for events on pianoroll client
ymh <ymh.work@gmail.com>
parents:
68
diff
changeset
|
59 |
with open(args.datafile, 'rU') as datafile: |
0 | 60 |
reader = csv.reader(datafile, delimiter=' ') |
85
eff9460bd4f2
add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
73
diff
changeset
|
61 |
app = UDPSenderApplication(9090, "127.0.0.1", "/pianoroll/%s/" % args.event, list(reader), args.shift) |
0 | 62 |
|
63 |
reactor.run() |