-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgeneric_connector.py
executable file
·197 lines (171 loc) · 6.18 KB
/
generic_connector.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/env python
import getopt
import sys
import liblo
import math
import threading
import time
def db_to_coeff(db):
if db > -318.8:
return math.pow(10.0, db * 0.05)
else:
return 0.0
def coeff_to_db(coeff):
if coeff == 0.0:
return float('-inf')
else:
return 20.0 * math.log10(coeff)
def fader_to_db(fader):
if fader == 0.0:
return float('-inf')
log = math.log10(fader * 1.3422)
if fader > 0.745:
return 78.25 * log
else:
return 93.44 * log
def db_to_fader(db):
if db > -318.8:
if db > 0.0:
return 0.745 * math.pow(10.0, db*0.01277955272)
else:
return 0.745 * math.pow(10.0, db*0.01070205479)
else:
return 0.0
class VolumeOSCThread(threading.Thread):
def __init__(self, volumeosc, oscserver):
threading.Thread.__init__(self)
self.daemon = True
self.volumeosc = volumeosc
self.oscserver = oscserver
def run(self):
while self.volumeosc.server_active:
self.oscserver.recv(100)
class VolumeOSC():
def __init__(self, protocol, instance, host_a, port_a, local_port_a, host_b, port_b, local_port_b):
self.address_a = liblo.Address(host_a, port_a, protocol)
self.address_b = liblo.Address(host_b, port_b, protocol)
self.server_active = False
try:
searching_port = True
ntrials = 0
while searching_port:
try:
self.server_a = liblo.Server(local_port_a)
searching_port = False
except liblo.ServerError, err:
local_port_a = local_port_a + 1
ntrials = ntrials + 1
if ntrials > 1000:
raise liblo.ServerError(99, "stop searching free port", None)
searching_port = True
ntrials = 0
while searching_port:
try:
self.server_b = liblo.Server(local_port_b)
searching_port = False
except liblo.ServerError, err:
local_port_b = local_port_b + 1
ntrials = ntrials + 1
if ntrials > 1000:
raise liblo.ServerError(99, "stop searching free port", None)
self.server_a.add_method("/net/mhcloud/volume/" + instance + "/master", "f", self.callback_master_gain_a)
self.server_a.add_method("/net/mhcloud/volume/" + instance + "/master/mute", "i", self.callback_master_mute_a)
self.server_b.add_method("/net/mhcloud/volume/" + instance + "/master", "f", self.callback_master_gain_b)
self.server_b.add_method("/net/mhcloud/volume/" + instance + "/master/mute", "i", self.callback_master_mute_b)
for i in range(channels):
self.server_a.add_method("/net/mhcloud/volume/" + instance + "/" + str(i), "f", self.callback_channel_gain_a, i)
self.server_a.add_method("/net/mhcloud/volume/" + instance + "/" + str(i) + "/mute", "i", self.callback_channel_mute_a, i)
self.server_b.add_method("/net/mhcloud/volume/" + instance + "/" + str(i), "f", self.callback_channel_gain_b, i)
self.server_b.add_method("/net/mhcloud/volume/" + instance + "/" + str(i) + "/mute", "i", self.callback_channel_mute_b, i)
self.server_active = True
except liblo.ServerError, err:
sys.stderr.write("OSC server error occured:\n")
sys.stderr.write(str(err) + '\n')
t1 = VolumeOSCThread(self, self.server_a)
t1.start()
while self.server_active:
self.server_b.recv(100)
def callback_master_gain_a(self, path, args):
val = float(args[0])
val = max(0.0, val)
val = min(1.0, val)
coeff = db_to_coeff(fader_to_db(val))
liblo.send(self.address_b, "/net/mhcloud/volume/" + instance + "/master", coeff)
def callback_master_mute_a(self, path, args):
mute = int(args[0]!=0)
liblo.send(self.address_b, "/net/mhcloud/volume/" + instance + "/master/mute", mute)
def callback_channel_gain_a(self, path, args, types, src, data):
val = float(args[0])
val = max(0.0, val)
val = min(1.0, val)
coeff = db_to_coeff(fader_to_db(val))
channel = data
liblo.send(self.address_b, "/net/mhcloud/volume/" + instance + "/" + str(channel), coeff)
def callback_channel_mute_a(self, path, args, types, src, data):
mute = int(args[0]!=0)
channel = data
liblo.send(self.address_b, "/net/mhcloud/volume/" + instance + "/" + str(channel) + "/mute", int(mute))
def callback_master_gain_b(self, path, args):
val = float(args[0])
fader = db_to_fader(coeff_to_db(val))
liblo.send(self.address_a, "/net/mhcloud/volume/" + instance + "/master", fader)
def callback_master_mute_b(self, path, args):
mute = int(args[0]!=0)
liblo.send(self.address_a, "/net/mhcloud/volume/" + instance + "/master/mute", int(mute))
def callback_channel_gain_b(self, path, args, types, src, data):
val = float(args[0])
fader = db_to_fader(coeff_to_db(val))
channel = data
liblo.send(self.address_a, "/net/mhcloud/volume/" + instance + "/" + str(channel), fader)
def callback_channel_mute_b(self, path, args, types, src, data):
mute = int(args[0]!=0)
channel = data
liblo.send(self.address_a, "/net/mhcloud/volume/" + instance + "/" + str(channel) + "/mute", int(mute))
if __name__ == "__main__":
channels = 2
protocol = liblo.UDP
instance = "jack-volume"
host_a = "motog"
port_a = 7604
localport_a = 7603
host_b = "media"
port_b = 7601
localport_b = 7602
try:
opts, args = getopt.getopt(sys.argv[1:], "c:p:h:g:j:s:")
except getopt.GetoptError as err:
sys.stderr.write(str(err) + '\n')
sys.stderr.write("usage: " + sys.argv[0] + " [-c <nchannels>] [-p <jv-port>] [-h <host>] [-g <generic-host>] [-j <jack-volume_instance>] [-s <listening-port>]\n")
sys.stderr.write("options:\n")
sys.stderr.write("-c number of channels\n")
sys.stderr.write("-p udp port of jack-volume\n")
sys.stderr.write("-h jack-volume hostname\n")
sys.stderr.write("-g generic device hostname\n")
sys.stderr.write("-j name of the jack-volume instance\n")
sys.stderr.write("-s OSC UDP listening port\n")
sys.exit(2)
for k, v in opts:
if k == "-c":
channels = v
elif k == "-p":
port_b = int(v)
localport_b = port_b + 1
elif k == "-h":
host_b = v
elif k == "-g":
host_a = v
elif k == "-j":
instance = v
elif k == "-s":
localport_a = int(v)
port_a = localport_a + 1
channels = int(channels)
channels = min(channels, 32)
channels = max(channels, 1)
try:
osc = VolumeOSC(protocol, instance, host_a, port_a, localport_a, host_b, port_b, localport_b)
except IOError:
sys.stderr.write("IOError occured:\n")
sys.stderr.write(str(IOError) + '\n')
except KeyboardInterrupt:
sys.stdout.write("program quit\n")