forked from snhirsch/katana-midi-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkatana_bridge_app
executable file
·318 lines (273 loc) · 9.59 KB
/
katana_bridge_app
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/usr/bin/python3
# -*-python-*-
import sys
import os
import mido
import time
import math
from panel_preset import PanelPreset
from katana import Katana
from range import Range
mido.set_backend('mido.backends.rtmidi')
preset_file = None
presets = dict()
active_preset = None
control_map = {
21: "booster",
22: "delay",
23: "reverb",
24: "mod",
25: "wah"
}
# NOTE: (3) CC#80 w/ value > 63 within a 2 second window arms
# us for preset capture. If the next message received is PC
# for value > 10 we capture the current amp state into that
# dictionary slot and persist the entire preset memory to
# disk file.
# State-machine for detection of preset capture command
class Trigger:
def __init__( self ):
self.armed = False
self.starttime = 0
self.count = 0
self.longpress = False
self.buttoncounts = {
21:0,
22:0,
23:0,
24:0,
25:0
}
def clear( self ):
self.armed = False
def is_armed( self ):
return self.armed
def detect( self, value ):
now = time.time()
if value > 63:
self.armed = False
if self.count == 0 or now - self.starttime > 2:
# State-machine init. One of these applies:
#
# - We were armed, but didn't follow up with a PC
# - First time we've seen the signal since starting
# - First time we've seen the signal since previous arming
# - Too much time elapsed since the last signal
self.count = 1
self.starttime = now
else:
# Within time window, increment counter
self.count += 1
if self.count == 3:
self.count = 0
self.armed = True
def detect_double(self, value):
now = time.time()
prevcount = self.buttoncounts[value]
if prevcount == 0 or now - self.starttime > 2:
# State-machine init. One of these applies:
#
# - We were armed, but didn't follow up with a PC
# - First time we've seen the signal since starting
# - First time we've seen the signal since previous arming
# - Too much time elapsed since the last signal
self.starttime = now
# reset all others to zero except current
for key in self.buttoncounts.keys():
if key == value:
self.buttoncounts[key] = 1
else:
self.buttoncounts[key] = 0
return False
else:
# Within time window and a double click, return true
for key in self.buttoncounts.keys():
self.buttoncounts[key] = 0
return True
def islongpress(self):
now = time.time()
return (now - self.starttime) > 1
def log_scale( value ):
if value == 0: return 0
frac = (value + 1) / 128
scale = (math.exp(frac)-1)/(math.e-1)
return int( value * scale )
def linear_scale(value, high=100):
frac = (value) / 127
return int(frac * high)
def array_value(value):
result = [0x00,0x00]
result[1] = value % 0x80
result[0] = (value // 0x80) % 0x80
print(str(value) + "=" + str(result[0]) +":"+ str(result[1]))
return result
def load_presets( presets ):
if os.path.isfile( preset_file ):
# Read all presets
with open(preset_file,'r') as fh:
for rec in PanelPreset.get_from_file( fh ):
presets[rec.id] = rec
# Rename existing data file and persist current
# live data to disk
def save_presets():
try:
if os.path.isfile( preset_file ):
os.rename( preset_file, preset_file + ".bak" )
with open(preset_file,'w') as outfh:
for rec in presets.values():
rec.serialize( outfh )
except OSError:
syslog.syslog( "Error saving presets: " + OSError )
# sys.exit( 1 )
# Capture and persist a new preset (overwrites existing)
def capture_preset( katana, program ):
# Read amp into rec using controller PC program value as id
rec = PanelPreset.read_from_amp( katana, program, rangeObj )
presets[ rec.id ] = rec
# Persist to disk
save_presets()
# Pulse volume for acknowledgement
katana.signal()
return rec
# Handle our presets
def handle_pc( trigger, katana, program ):
rec = None
if program > 9:
if trigger.is_armed():
trigger.clear()
rec = capture_preset( katana, program )
elif program in presets:
rec = presets[ program ]
rec.transmit( katana )
# Disarm at exit whether or not we did anything
trigger.clear()
return rec
def handle_cc( trigger, katana, control, value ):
print("got control " + str(control) + " value " + str(value))
if control == 80:
trigger.detect( value )
else:
trigger.clear()
# All our CC --> sysex mappings here
# First the toggle buttons
isdouble = False
# is it an SW or a parameter?
if control in control_map.keys():
if value > 0:
if(control >= 21 and control <= 24):
isdouble = trigger.detect_double(control)
# if double click, then change colour first
if isdouble:
katana.incrementcolour(control_map[control])
# since this is a momentary control, only toggle if it is an
# downpush
katana.toggle(control_map[control])
else:
longpress = trigger.islongpress()
print("Is this a long press? - " + str(longpress))
# TODO: do some long press stuff here. Example, toggle Fx?
elif control == 1:
scaled = linear_scale(value)
katana.prebass(scaled)
elif control == 2:
scaled = linear_scale(value)
katana.premid(scaled)
elif control == 3:
scaled = linear_scale(value)
katana.pretreble(scaled)
elif control == 4:
scaled = linear_scale(value,2000)
scaled = max(1, scaled)
arrayval = array_value(scaled)
katana.delaytime(arrayval)
elif control == 5:
scaled = linear_scale(value)
katana.delayfeedback(scaled)
elif control == 6:
scaled = linear_scale(value,120)
katana.delaylevel(scaled)
elif control == 7:
scaled = linear_scale(value)
scaled = max(1, scaled)
katana.reverbtime(scaled)
elif control == 8:
scaled = linear_scale(value)
katana.reverblevel(scaled)
elif control == 9:
scaled = linear_scale(value,120)
katana.pregain(scaled)
elif control == 10:
scaled = linear_scale(value,120)
katana.volume(scaled)
elif control == 11:
scaled = linear_scale(value)
katana.mastergain(scaled)
elif control == 12:
scaled = linear_scale(value)
katana.moddepth(scaled)
elif control == 13:
scaled = linear_scale(value)
katana.modintensity(scaled)
elif control == 14:
# Nothing here for now
scaled = linear_scale(value)
elif control == 15:
# Nothing here for now
scaled = linear_scale(value)
else:
# unmapped, just pass it on
print("warning: doing unmapped things")
katana.send_cc(control, value)
################################ (main) ##################################
args = sys.argv
if len(args) < 6:
print( "Usage: bridge.py <controller_port> <listen_channel> <amplifier_port> <amp_channel> <preset_file> [virt]\n" )
print( " Ex:\n" )
print( " bridge.py \"USB MS1x1 MIDI Interface:USB MS1x1 MIDI Interface MIDI 1 24:0\" 2 \\" )
print( " \"KATANA:KATANA MIDI 1 20:0\" 1 presetfile.txt\n" )
sys.exit( 1 )
# Load parameter metadata
scriptdir = os.environ.get('PYTHONPATH')
if scriptdir == None:
scriptdir = os.path.dirname(os.path.abspath(__file__))
datadir = scriptdir + '/parameters/'
rangeObj = Range( datadir + 'ranges.json' )
# Controller interface
interface = args[1]
# Controller MIDI port
try:
listen_ch = int( args[2] ) - 1
except ValueError:
print( "Arg2 must be numeric!\n" )
sys.exit( 1 )
# Amp interface
amp = args[3]
# Amp MIDI port
try:
amp_channel = int( args[4] ) - 1
except ValueError:
print( "Arg4 must be numeric!\n" )
sys.exit( 1 )
# Preset data
preset_file = args[5]
# Are we testing?
if len(args) == 7 and args[6] == 'virt':
virt = True
else:
virt = False
load_presets( presets )
katana = Katana( amp, amp_channel, clear_input=True )
trigger = Trigger()
# Main processing loop
with mido.open_input(interface, virtual=virt) as commands:
for msg in commands:
if msg.type == 'control_change' and msg.channel == listen_ch:
print( "%s: ch = %d, ctrl = %d, val = %d" % (msg.type, msg.channel, msg.control, msg.value) )
handle_cc( trigger, katana, msg.control, msg.value )
elif msg.type == 'program_change' and msg.channel == listen_ch:
print( "%s: ch = %d, prog = %d" % (msg.type, msg.channel, msg.program) )
if msg.program >= 0 and msg.program <= 4:
katana.send_pc( msg.program )
active_preset = None
else:
active_preset = handle_pc( trigger, katana, msg.program )