-
Notifications
You must be signed in to change notification settings - Fork 11
/
panel_preset_old.py
287 lines (231 loc) · 9.48 KB
/
panel_preset_old.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
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
#!/usr/bin/python3
#
# Represent a preset
#
#
import sys
import re
from time import sleep
from globals import *
from pprint import pprint
class ParmRecOld:
def __init__( self, addr=None, data=None, memo="" ):
self.addr = addr
self.data = data
self.memo = memo
def to_string( self ):
print( "Memo: ", self.memo )
print( "Addr: ", self.addr )
print( "Data: ", self.data )
class PanelPresetOld:
# Track object state
Start, SawId, SawAddr, SawData, Done = range( 5 )
# Static generator to create multiple PanelPresetOld objects by parsing a
# text file input stream
#
@staticmethod
def get_from_file( infh ):
lineNum = 0
obj = PanelPresetOld(None, None, None)
for line in infh:
lineNum += 1
line = line.strip()
if len( line ) == 0: continue
if re.match( r'^#', line ):
# If we're in a preset stanza gather comments
if obj.curr_rec != None:
obj.curr_rec.memo += line
continue
try:
type, value = line.split( ' ', 1 )
except ValueError:
print( "Parse error at line %d. Expected more than one token." % lineNum )
sys.exit( 1 )
# First token in input line names the handler.
handler = getattr( obj, type, None )
if handler == None:
print( "Parse error at line %d. Line type %s unknown" % (lineNum, type) )
sys.exit( 1 )
else:
handler( value, lineNum )
if obj.state == obj.Done:
# Deal with legacy data
# a = obj.get_data(AMP_VOLUME_ADDR, 0, 1)
# if a == None:
# a = obj.get_data(AMP_TYPE_ADDR, 2, 1)
# obj.volume_midi_scale = a[0] / 128
yield obj
obj = PanelPresetOld(None, None, None)
if obj.state != obj.Start:
print( "Parse error at line %d." % lineNum )
# Static factory method to create a single PanelPresetOld object by reading
# current amplifier state
#
@staticmethod
def read_from_amp( katana, preset_id, colorObj, simpleObj, complexObj ):
obj = PanelPresetOld( colorObj, simpleObj, complexObj )
obj.state = obj.Done
obj.id = preset_id
# Store zero-volume command
parm = ParmRecOld( AMP_VOLUME_ADDR, (0,), "Mute amplifier" )
obj.parms.append( parm )
# NOTE: This code can hang or blow up with invalid list index if
# remnants from an earlier reply are sitting in the input
# buffer.
# Read active DSP deep parms
for rec in colorObj.read_color_assign( katana ):
# Get the appropriate group handler
handler = obj.dsp[ rec['group'] ]
# Lookup parm blocks
coords = handler.get_coords( rec['category'], rec['type'] )
# And read from amp
for block in coords['blocks']:
addr, length = block
addr, data = katana.query_sysex_data( addr, length )
parm = ParmRecOld( addr[0], data[0], "Category: %s, Type: %s" % (rec['category'], coords['name']) )
obj.parms.append( parm )
# Read chain, color assign and color button state
addr, data = katana.query_sysex_data( COLOR_ASSIGN_ADDR, COLOR_ASSIGN_LEN )
parm = ParmRecOld( addr[0], data[0], "Color assign" )
obj.parms.append( parm )
# Read noise gate state
addr, data = katana.query_sysex_data( NS_ADDR, NS_LEN )
parm = ParmRecOld( addr[0], data[0], "Noise Gate" )
obj.parms.append( parm )
# Read amplifier panel block
addr, data = katana.query_sysex_data( PANEL_STATE_ADDR, PANEL_STATE_LEN )
# Create a list so we can modify
data_list_0 = list( data[0] )
# Grab stored volume setting
volume = data_list_0[2]
# Patch in a zero to keep it muted
data_list_0[2] = 0
# Store patched block
parm = ParmRecOld( addr[0], data_list_0, "Amp Panel" )
obj.parms.append( parm )
# Store a delay record to prevent "pop" at patch change.
# Data is a single byte specifying delay in ms.
parm = ParmRecOld( (0xff,), (50,), "Delay" )
obj.parms.append( parm )
# Restore actual volume
parm = ParmRecOld( AMP_VOLUME_ADDR, (volume,), "Restore amp volume" )
obj.parms.append( parm )
# Keep this around so we can properly scale controller
# pedal input
obj.volume_midi_scale = volume / 128
return obj
def __init__( self, colorObj, simpleObj, complexObj ):
self.state = self.Start
self.id = -1
self.by_addr = {}
self.curr_rec = None
self.parms = []
self.colorObj = colorObj
self.dsp = {}
self.dsp['simple'] = simpleObj
self.dsp['complex'] = complexObj
# State machine handlers for parsing data file:
def _preset( self, value, lineNum ):
if self.state != self.Start:
print( "Phase error at line %d. Expected Start, but was %d." % (lineNum, self.state) )
sys.exit( 1 )
try:
self.id = int( value )
self.curr_rec = ParmRecOld()
self.state = self.SawId
except ValueError:
print( "Parse error at line %d. Expecting single integer." )
sys.exit( 1 )
def _addr( self, value, lineNum ):
if self.state != self.SawId and self.state != self.SawData:
print( "Phase error at line %d. Expected SawId or SawData, but was %d." % (lineNum, self.state) )
sys.exit( 1 )
address_bytes = []
for hex in value.split():
address_bytes.append( int(hex,16) )
self.curr_rec.addr = tuple( address_bytes )
self.state = self.SawAddr
def _data( self, value, lineNum ):
if self.state != self.SawAddr:
print( "Phase error at line %d. Expected SawAddr, but was %d." % (lineNum, self.state) )
sys.exit( 1 )
data = []
for hex in value.split():
data.append( int(hex,16) )
self.curr_rec.data = tuple( data )
self.parms.append( self.curr_rec )
self.by_addr[ self.curr_rec.addr ] = self.curr_rec
self.curr_rec = ParmRecOld()
self.state = self.SawData
def _endPreset( self, value, lineNum ):
if self.state != self.SawData:
print( "Phase error at line %d. Expected SawData, but was %d." % (lineNum, self.state) )
sys.exit( 1 )
try:
endId = int( value )
except ValueError:
print( "Parse error at line %d. Expecting single integer." % lineNum )
sys.exit( 1 )
if endId != self.id:
print( "Parse error at line %d. Preset number mismatch. Expected %d, but saw %d." % (lineNum, self.id, endId) )
sys.exit( 1 )
self.state = self.Done
# Send current data set to amplifier
def transmit( self, katanaObj ):
for parm in self.parms:
if parm.addr[0] == 0xff:
sleep( parm.data[0]/1000 )
continue
katanaObj.send_sysex_data( parm.addr, parm.data )
# Print current data set to passed filehandle.
def serialize( self, outfh ):
outfh.write( "_preset %d\n" % self.id )
i = 0
for parm in self.parms:
if len(parm.memo): outfh.write( "# %s\n" % parm.memo )
hexstr = ' '.join( "%02x" % i for i in parm.addr )
outfh.write( "_addr %s\n" % hexstr )
hexstr = ' '.join( "%02x" % i for i in parm.data )
outfh.write( "_data %s\n" % hexstr )
i += 1
outfh.write( "_endPreset %d\n" % self.id )
# Controller input 0..127. Scale this so it maxes out at the captured
# volume.
def scale_volume_to_amp( self, controller_value ):
if controller_value > 0:
controller_value += 1
return int( controller_value * self.volume_midi_scale )
# Full range scaling when using built-in preset.
# FIXME: No reason why we cannot read actual volume from
# built-in.
@staticmethod
def scale_volume_to_amp_default( controller_value ):
if controller_value > 0:
controller_value += 1
return int( controller_value * (100/128) )
# Return array of bytes from current preset data. Call with
# base address, offset and count.
def get_data( self, address, offset, count ):
if address in self.by_addr:
parm = self.by_addr[ address ]
bytes = parm.data
if offset + count > len( bytes ):
count = len( bytes ) - offset
return bytes[ offset : offset + count ]
else:
return None
if __name__ == '__main__':
args = sys.argv
file = sys.stdout
infh = open( args[1], 'r' )
objs = []
for obj in PanelPresetOld.get_from_file( infh ):
objs.append( obj )
for obj in objs:
for parm in obj.parms:
print( "-----" )
parm.to_string()
print( "---" )
obj.serialize( file )
test_data = obj.get_data( PANEL_STATE_ADDR, 0, 1 )
print( "Test data = " + str(test_data).strip('[]') )