author | ymh <ymh.work@gmail.com> |
Fri, 16 Jan 2015 03:16:19 +0100 | |
changeset 93 | 79ae42ad97d4 |
parent 85 | eff9460bd4f2 |
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 |
93
79ae42ad97d4
optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents:
85
diff
changeset
|
10 |
import signal |
0 | 11 |
import time |
12 |
||
85
eff9460bd4f2
add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
73
diff
changeset
|
13 |
import ntplib |
0 | 14 |
from twisted.internet import reactor |
15 |
from txosc import osc |
|
16 |
from txosc import async |
|
17 |
||
18 |
||
19 |
class UDPSenderApplication(object): |
|
20 |
""" |
|
21 |
Example that sends UDP messages. |
|
22 |
""" |
|
93
79ae42ad97d4
optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents:
85
diff
changeset
|
23 |
def __init__(self, port, host, address, rows, shift, token): |
0 | 24 |
self.port = port |
25 |
self.host = host |
|
26 |
self.client = async.DatagramClientProtocol() |
|
27 |
self._client_port = reactor.listenUDP(0, self.client) |
|
28 |
self.rows = rows |
|
29 |
self.address = address |
|
85
eff9460bd4f2
add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
73
diff
changeset
|
30 |
self.shift = shift |
93
79ae42ad97d4
optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents:
85
diff
changeset
|
31 |
self.token = token |
0 | 32 |
reactor.callLater(0, self.send_messages) |
33 |
||
34 |
def _send(self, element): |
|
35 |
# This method is defined only to simplify the example |
|
36 |
self.client.send(element, (self.host, self.port)) |
|
37 |
print("Sent %s to %s:%d" % (element, self.host, self.port)) |
|
38 |
||
39 |
def send_messages(self): |
|
85
eff9460bd4f2
add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
73
diff
changeset
|
40 |
t0 = time.time() |
93
79ae42ad97d4
optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents:
85
diff
changeset
|
41 |
#tc = 0 |
0 | 42 |
for row in self.rows: |
93
79ae42ad97d4
optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents:
85
diff
changeset
|
43 |
if not self.token.running: |
79ae42ad97d4
optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents:
85
diff
changeset
|
44 |
break |
85
eff9460bd4f2
add new visualization + small corrections
ymh <ymh.work@gmail.com>
parents:
73
diff
changeset
|
45 |
if self.shift: |
93
79ae42ad97d4
optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents:
85
diff
changeset
|
46 |
row[0] = ntplib.system_to_ntp_time(t0 + float(row[1])/1000.0) |
0 | 47 |
row_conv = [ osc.TimeTagArgument(float(row[0]))] + [osc.IntArgument(int(a)) for a in row[1:]] |
93
79ae42ad97d4
optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents:
85
diff
changeset
|
48 |
#time.sleep((row_conv[1].value-tc)/1000.0) |
79ae42ad97d4
optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents:
85
diff
changeset
|
49 |
sleep_time = t0+float(row[1])/1000.0-time.time() |
79ae42ad97d4
optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents:
85
diff
changeset
|
50 |
if sleep_time > 0: |
79ae42ad97d4
optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents:
85
diff
changeset
|
51 |
time.sleep(sleep_time) |
79ae42ad97d4
optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents:
85
diff
changeset
|
52 |
#time.sleep(0.1) |
79ae42ad97d4
optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents:
85
diff
changeset
|
53 |
tc = row_conv[1].value |
0 | 54 |
self._send(osc.Message(self.address,*row_conv)) |
55 |
print("Goodbye.") |
|
56 |
reactor.callLater(0.1, reactor.stop) |
|
57 |
||
93
79ae42ad97d4
optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents:
85
diff
changeset
|
58 |
class Token(object): |
79ae42ad97d4
optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents:
85
diff
changeset
|
59 |
def __init__(self): |
79ae42ad97d4
optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents:
85
diff
changeset
|
60 |
self.running = True |
79ae42ad97d4
optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents:
85
diff
changeset
|
61 |
|
0 | 62 |
if __name__ == "__main__": |
63 |
||
93
79ae42ad97d4
optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents:
85
diff
changeset
|
64 |
token = Token() |
79ae42ad97d4
optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents:
85
diff
changeset
|
65 |
|
73
2a6590aeac15
Add argument for events on pianoroll client
ymh <ymh.work@gmail.com>
parents:
68
diff
changeset
|
66 |
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
|
67 |
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
|
68 |
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
|
69 |
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
|
70 |
|
2a6590aeac15
Add argument for events on pianoroll client
ymh <ymh.work@gmail.com>
parents:
68
diff
changeset
|
71 |
args = parser.parse_args() |
2a6590aeac15
Add argument for events on pianoroll client
ymh <ymh.work@gmail.com>
parents:
68
diff
changeset
|
72 |
|
93
79ae42ad97d4
optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents:
85
diff
changeset
|
73 |
def customHandler(signum, _): |
79ae42ad97d4
optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents:
85
diff
changeset
|
74 |
print("Got signal: %s" % signum) |
79ae42ad97d4
optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents:
85
diff
changeset
|
75 |
token.running = False |
79ae42ad97d4
optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents:
85
diff
changeset
|
76 |
if reactor.running: |
79ae42ad97d4
optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents:
85
diff
changeset
|
77 |
reactor.callFromThread(reactor.stop) # to stop twisted code when in the reactor loop |
79ae42ad97d4
optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents:
85
diff
changeset
|
78 |
signal.signal(signal.SIGINT, customHandler) |
79ae42ad97d4
optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents:
85
diff
changeset
|
79 |
|
79ae42ad97d4
optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents:
85
diff
changeset
|
80 |
|
73
2a6590aeac15
Add argument for events on pianoroll client
ymh <ymh.work@gmail.com>
parents:
68
diff
changeset
|
81 |
with open(args.datafile, 'rU') as datafile: |
0 | 82 |
reader = csv.reader(datafile, delimiter=' ') |
93
79ae42ad97d4
optimize and refactor pianoroll component
ymh <ymh.work@gmail.com>
parents:
85
diff
changeset
|
83 |
app = UDPSenderApplication(9090, "127.0.0.1", "/pianoroll/%s/" % args.event, list(reader), args.shift, token) |
0 | 84 |
|
85 |
reactor.run() |