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 |
|
|
8 |
import csv |
|
9 |
import sys |
|
10 |
import time |
|
11 |
|
|
12 |
from twisted.internet import reactor |
|
13 |
from txosc import osc |
|
14 |
from txosc import dispatch |
|
15 |
from txosc import async |
|
16 |
|
|
17 |
|
|
18 |
class UDPSenderApplication(object): |
|
19 |
""" |
|
20 |
Example that sends UDP messages. |
|
21 |
""" |
|
22 |
def __init__(self, port, host, address, rows): |
|
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 |
|
29 |
reactor.callLater(0, self.send_messages) |
|
30 |
|
|
31 |
def _send(self, element): |
|
32 |
# This method is defined only to simplify the example |
|
33 |
self.client.send(element, (self.host, self.port)) |
|
34 |
print("Sent %s to %s:%d" % (element, self.host, self.port)) |
|
35 |
|
|
36 |
def send_messages(self): |
|
37 |
tc = 0 |
|
38 |
for row in self.rows: |
|
39 |
row_conv = [ osc.TimeTagArgument(float(row[0]))] + [osc.IntArgument(int(a)) for a in row[1:]] |
|
40 |
time.sleep((row_conv[1].value-tc)/10**3) |
|
41 |
tc = row_conv[1].value |
|
42 |
self._send(osc.Message(self.address,*row_conv)) |
|
43 |
break |
|
44 |
print("Goodbye.") |
|
45 |
reactor.callLater(0.1, reactor.stop) |
|
46 |
|
|
47 |
if __name__ == "__main__": |
|
48 |
|
|
49 |
with open(sys.argv[1], 'rU') as datafile: |
|
50 |
reader = csv.reader(datafile, delimiter=' ') |
|
51 |
app = UDPSenderApplication(9090, "127.0.0.1", '/pianoroll/test/', list(reader)) |
|
52 |
|
|
53 |
reactor.run() |