-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathkatana_bridge_app
executable file
·195 lines (159 loc) · 5.54 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
#!/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
# NOTE: (3) CC#3 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
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 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 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 ):
if control == 3:
trigger.detect( value )
else:
trigger.clear()
# All our CC --> sysex mappings here
if control == 70:
scaled = log_scale(value)
katana.volume( scaled )
################################ (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) )
if msg.control >= 16 and msg.control <= 19:
katana.send_cc( msg.control, msg.value )
else:
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 )