-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjMap.py
901 lines (708 loc) · 22.9 KB
/
jMap.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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
# Build keyboard map for samples
#
# Builds soundfont key map given a set of sample files
# and a little config data.
#
# Sample files must be named according to a convention:
#
# xxxx_<mnote>_xxxx_<layer>.wav
#
# where
# <mnote> is the MIDI note number
# <layer> is arbitrary text identifying the layer (see layer config below)
#
# UPDATE:
# Set VEL_LOC and NOTE_LOC to specify format
# IN PROGRESS: build .sfz file (!)
import sys
import warnings
import glob
import jmidi
import jtime
import jtrans
# index constants
LNAME = 0
LVEL = 1
LATTEN = 2
LLEVEL = 3
LPREVLEV = 4
# initializations
# entry structure - list of:
# lname name
# lvel velocity
# latten sf attenuation for layer (obsolete?)
# llevel amount layer was boosted by when normalized.
# If zero, velocity curve adjustment not done.
# lprevlevel amount previous layer was boosted by.
#
# Layer entries must be ordered from softest to loudest.
LAYER = []
############################################################################
#
# Configuration (should be read in)
#
# How to find layer name and note from file name:
# DELIMS are the characters treated as delimiters to split the
# file name.
# After splitting a line into parts separated by the delimiter,
# LOC is the location of the note or layer name, where 0 is the
# first part, 1 is the second, etc. -1 is the last, -2 is the
# second to last, etc.
DELIMS = " `~!@#$%^&*()_+-={}|[]:\";'<>?,.\t"
# LAYER_LOC = -1
# NOTE_LOC = 2
# defaults for mapping parameters
MAX_LAYER_SHIFT = 2 # don't jump layers more than this
MAX_NOTE_SHIFT = 7 # don't stretch pitch more than this
NOTE_SHIFT_COST = 2 # relative cost of shifing notes (vs. layers)
LAYER_SHIFT_COST = 1 # relative cost of shifing layers (vs. notes)
EXTEND_LAYER_UP = True # whether better to map higher vel sample than lower
EXTEND_NOTE_UP = True # whether better to map higher note sample than lower
LOWEST_LEVEL = 640 # attenuation for vel=1 notes (when using "level"), in cB
# default range for whole keyboard map
LO_KEY = jmidi.notenum("C1") # lowest C on piano, lowest key I use
HI_KEY = jmidi.notenum("G7") # highest key on MR76
###############################################################################
class Globals:
def __init__(self):
pass
gl = Globals
gl.grid = []
gl.samps = {}
gl.layernum = {}
gl.lnamelen = 0
class Samp:
def __init__(self):
pass
def build_grid(grid):
for layer in range(0, len(LAYER)):
grid.append([])
for key in range(0, HI_KEY+MAX_NOTE_SHIFT+1):
grid[layer].append(None)
gl.layernum[LAYER[layer][LNAME]] = layer
def build_sampchars():
# omit ANSI and DOS unprintables (7f only one for DOS)
# unprintables = [0x7f, 0x81, 0x8d, 0x8f, 0x90, 0x9d,]
chars = ""
# for ichar in range(ord('"'), 0xff):
for ichar in range(ord('"'), 0x7f):
# if ichar not in unprintables:
chars += chr(ichar)
return chars
def load_filenames(args):
global gl
warnings = []
errors = []
for arg in args:
#{
for sampfname in glob.glob(arg):
if True:
try:
sampf = file(sampfname, "rb")
except IOError, msg:
errors.append("Can't open sample file: " + str(msg))
continue
else:
sampf.close()
samp = Samp()
# strip directory
basename = sampfname.replace("\\", "/")
basename = basename.split("/")[-1]
# strip ".wav" extension
basename = basename.split(".")
basename = ".".join(basename[0:-1])
basename = jtrans.tr(basename, DELIMS, " ")
# get layer name
parts = basename.split(" ")
if len(parts) <= abs(LAYER_LOC):
loc = LAYER_LOC
if loc >= 0:
loc += 1
print >>sys.stderr, (
"After splitting filename '%s' delimiters,"
% (basename))
print >>sys.stderr, (
"there aren't enough parts to find part number %d." % loc)
sys.exit(1)
layername = parts[LAYER_LOC]
# get note: might be MIDI number or note name
if len(parts) <= abs(NOTE_LOC):
loc = NOTE_LOC
if loc >= 0:
loc += 1
print >>sys.stderr, (
"After splitting filename '%s' at delimiters, "
% (sampfname))
print >>sys.stderr, (
"there aren't enough parts to find part number %d." % loc)
sys.exit(1)
notespec = parts[NOTE_LOC]
mnote = jmidi.notenum(notespec)
if mnote == None:
print >>sys.stderr, (
"Invalid MIDI note designation '%s' in '%s'"
% (notespec, basename))
print >>sys.stderr, "Parts are:", parts
sys.exit(1)
# print sampfname, mnote, layername, jmidi.mnote_name(mnote)[0]
samp.fname = sampfname
samp.mnote = mnote
samp.notename = jmidi.mnote_name(mnote, pad=None)
samp.layername = layername
if layername not in gl.layernum:
warnings.append("Sample for unconfigured layer '%s': %s"
% (samp.layername, samp.fname))
continue
samp.layer = gl.layernum[layername]
if samp.layer == None:
warnings.append("Sample for missing layer '%s': %s"
% (samp.layername, samp.fname))
continue
x = LO_KEY - MAX_NOTE_SHIFT
if (samp.mnote < max(0, LO_KEY - MAX_NOTE_SHIFT)
or samp.mnote > HI_KEY + MAX_NOTE_SHIFT):
warnings.append("Sample outside useful note range (%s): %s"
% (samp.notename, samp.fname))
continue
samp.char = None
gl.samps[sampfname] = samp
gl.grid[samp.layer][mnote] = samp
#}
if errors:
for msg in errors:
print >>sys.stderr, msg
sys.exit(1)
if warnings:
for msg in warnings:
print >>sys.stderr, "Warning:", msg
# print the samples, along with a character to assigned for
# showing the key map in showmap().
print >>gl.ofile, "# Samples:"
sampnum = 0
sampchars = build_sampchars()
for layer in range(len(LAYER)-1, -1, -1):
print >>gl.ofile, (
"# Layer %*s vel %3d: "
% (gl.lnamelen, LAYER[layer][LNAME], LAYER[layer][LVEL])),
for mnote in range(LO_KEY, HI_KEY+1):
samp = gl.grid[layer][mnote]
if samp:
if samp.char == None:
samp.char = sampchars[sampnum]
sampnum += 1
if sampnum >= len(sampchars):
sampnum = 0
print >>gl.ofile, "%3s=%c" % (samp.notename, samp.char),
print >>gl.ofile
print >>gl.ofile
def window(row, col, bounds):
row_min = bounds[0]
row_max = bounds[1]
col_min = bounds[2]
col_max = bounds[3]
row_lo = max(row - MAX_LAYER_SHIFT, row_min)
row_hi = min(row + MAX_LAYER_SHIFT, row_max)
col_lo = max(col - MAX_NOTE_SHIFT, col_min)
col_hi = min(col + MAX_NOTE_SHIFT, col_max)
return (row_lo, row_hi, col_lo, col_hi)
def distance(torow, tocol, fromrow, fromcol):
row_dist = (abs(torow - fromrow) * LAYER_SHIFT_COST) * 2
col_dist = (abs(tocol - fromcol) * NOTE_SHIFT_COST) * 2
if torow > fromrow and EXTEND_LAYER_UP:
row_dist += 1
elif torow < fromrow and not EXTEND_LAYER_UP:
row_dist += 1
if tocol > fromcol and EXTEND_NOTE_UP:
col_dist += 1
elif tocol < fromcol and not EXTEND_NOTE_UP:
col_dist += 1
# print "(", fromrow, fromcol, ")",
# print torow, fromrow, "=>", row_dist, "|", tocol, fromcol, "=>", col_dist
return col_dist + row_dist
def sname(samp):
return "%s-%s" % (samp.layername, samp.notename)
def assign_keys(map):
global gl
# fix bounds of search
row_min = 0
row_max = len(LAYER)-1
col_min = LO_KEY
col_max = HI_KEY
bounds = (row_min, row_max, col_min - MAX_NOTE_SHIFT, col_max + MAX_NOTE_SHIFT)
keymap = []
build_grid(keymap)
for row in range(row_min, row_max+1):
#{
# set initial window for this row pass
win = window(row, col_min, bounds)
# load initial neighbors. Omit rightmost column (they're added below)
neighbors = [] # (samp, row, col)
col = 0
for rr in range(win[0], win[1]+1):
for cc in range(win[2], win[3]):
nbr = gl.grid[rr][cc]
if nbr:
# print "init:", sname(nbr)
neighbors.append((nbr, rr, cc))
for col in range(col_min, col_max+1):
#{
# set new window
win = window(row, col, bounds)
# add new neighbors at right of window
cc = win[3]
for rr in range(win[0], win[1]+1):
nbr = gl.grid[rr][cc]
if nbr:
# print "new: ", rr, cc, sname(nbr)
neighbors.append((nbr, rr, cc))
# Visit neighbors.
# Discard any neighbors in old left column.
# Go backwards to make deletion easy
# Calculate distance and find closest neighbor.
bestdist = 9999
bestnbr = None
# print jmidi.mnote_name(col), len(neighbors), col, win
if len(neighbors) == 0:
### print >>sys.stderr, " -- No neighbors for layer", row, "key", col
pass
for nbrix in range(len(neighbors)-1, -1, -1):
(nbr, rr, cc) = neighbors[nbrix]
if cc < win[2]:
del neighbors[nbrix]
continue
dist = distance(rr, cc, row, col)
# print " ", row, col, rr, cc, ":", dist,
if dist < bestdist:
bestnbr = nbr
bestdist = dist
keymap[row][col] = bestnbr
#}
#}
gl.grid = keymap
def showmap(grid, layerdata):
# Generate heading showing "piano keyboard" in three lines:
# first line is octave number
# second line is key name, omitting sharp or flat
# third line is "b" for flats and " " for naturals
#
# Example:
# 111111111111222222222222333333333333444444444444...
# CDDEEFGGAABBCDDEEFGGAABBCDDEEFGGAABBCDDEEFGGAABB...
# b b b b b b b b b b b b b b b b b b b b ...
line1 = line2 = line3 = ""
for col in range(LO_KEY, HI_KEY+1):
notename = jmidi.mnote_name(col, pad=None)
line2 += notename[0]
if notename[1] == "b":
line3 += "b"
octave = notename[2]
else:
line3 += " "
octave = notename[1]
line1 += octave
# print "keyboard"
print >>gl.ofile, "#", line1
print >>gl.ofile, "#", line2
print >>gl.ofile, "#", line3
print >>gl.ofile, "#"
# print key assignments
for row in range(len(layerdata)-1, -1, -1):
line = ""
for col in range(LO_KEY, HI_KEY+1):
samp = gl.grid[row][col]
if samp:
if samp.layer == row and samp.mnote == col:
line += " "
else:
line += samp.char
else:
line += "!"
print >>gl.ofile, (
"# %s Layer %-6s v=%03d"
% (line, layerdata[row][LNAME], layerdata[row][LVEL]))
print >>gl.ofile, "#"
print >>gl.ofile, "# Key:"
print >>gl.ofile, "# space = unity-mapped key"
print >>gl.ofile, "# ! = unmapped key"
print >>gl.ofile, "# anything else: see sample layer list above"
def emit_keymap(samp, keyLo, keyHi):
print >>gl.ofile, " SAMP:%s:%d:%d:%d:\t(%3s - %3s)" % (
samp.fname, keyLo, keyHi, samp.mnote,
jmidi.mnote_name(keyLo, None),
jmidi.mnote_name(keyHi, None))
print >>gl.sfzf
print >>gl.sfzf, "<region> sample=%s" % samp.fname
print >>gl.sfzf, "lokey=%s" % jmidi.mnote_name(keyLo, None)
print >>gl.sfzf, "hikey=%s" % jmidi.mnote_name(keyHi, None)
print >>gl.sfzf, "pitch_keycenter=%s" %jmidi.mnote_name(samp.mnote, None)
def cB2scalefactor(cb):
return (pow(10.0, cb/200.0))
def emit_map(grid, layerdata):
print >>gl.ofile
print >>gl.ofile, "BANKNAME:%s" % BANKNAME
print >>gl.ofile, "DESIGNER:%s" % DESIGNER
print >>gl.ofile, "COPYRIGHT:%s" % COPYRIGHT
print >>gl.ofile, "COMMENT:%s" % COMMENT
print >>gl.ofile
print >>gl.ofile, "PRESET:%s" % PRESETNAME
print >>gl.ofile
print >>gl.ofile, "RELEASE:%s" % RELEASE
loVel = 1
# LOWEST_LEVEL = 640 # centibels %%% SHOULD BE CONFIGURED
lprevl = LOWEST_LEVEL
for row in range(0, len(layerdata)):
#{
hiVel = layerdata[row][LVEL]
latten = layerdata[row][LATTEN]
llevel = layerdata[row][LLEVEL]
print >>gl.ofile
print >>gl.ofile, "VLAYER:%s:%3d:%3d:%2d" % (
layerdata[row][LNAME], loVel, hiVel, latten) # %%% llevel
print >>gl.sfzf
print >>gl.sfzf, ("<group> lovel=%d hivel=%d" % (loVel, hiVel))
print >>gl.sfzf, "ampeg_release=%f" % RELEASE
if (latten != 0):
print >>gl.sfzf, "volume=%f" % (-latten/10.0)
if (llevel != 0):
midVel = (hiVel + loVel) / 2
midLev = (llevel + lprevl) / 2
print >>gl.sfzf, "amp_velcurve_%d %f" % (loVel, cB2scalefactor(-lprevl))
print >>gl.sfzf, "amp_velcurve_%d %f" % (midVel, cB2scalefactor(-midLev))
print >>gl.sfzf, "amp_velcurve_%d %f" % (hiVel, cB2scalefactor(-llevel))
print ("row %s velocity %3d level %3d cB, scale %f"
% (layerdata[row][LNAME], loVel, lprevl, cB2scalefactor(-lprevl)))
print ("row %s velocity %3d level %3d cB, scale %f"
% (layerdata[row][LNAME], midVel, midLev, cB2scalefactor(-midLev)))
print ("row %s velocity %3d level %3d cB, scale %f"
% (layerdata[row][LNAME], hiVel, llevel, cB2scalefactor(-llevel)))
lprevl = llevel
lastSamp = None
for col in range(LO_KEY, HI_KEY+1):
#{
samp = gl.grid[row][col]
if samp != lastSamp:
if lastSamp != None:
emit_keymap(lastSamp, firstKey, col-1)
lastSamp = samp
firstKey = col
#}
# handle last unfinished keymap
if lastSamp != None:
emit_keymap(lastSamp, firstKey, col)
loVel = hiVel + 1
#}
def convert_int(val, lineno):
try:
ival = int(val)
except:
print >>sys.stderr, (
"Line %d: expecting integer, got '%s'"
% (lineno, val))
sys.exit(1)
return (ival)
def kwval(group, lineno):
try:
(kw, val) = group.split("=")
except Exception, msg:
print >>sys.stderr, (
"Line %d: expecting 'keyword=value' format, got '%s'"
% (lineno, group))
print >>sys.stderr, msg
raise Exception
sys.exit(1)
return (kw, val)
def process_cfg(cfg_fname, sfname):
#{
global LAYER_LOC, NOTE_LOC
global BANKNAME, DESIGNER, COPYRIGHT, COMMENT
global LO_KEY, HI_KEY
global MAX_LAYER_SHIFT, LAYER_SHIFT_COST, EXTEND_LAYER_UP
global MAX_NOTE_SHIFT, NOTE_SHIFT_COST, EXTEND_NOTE_UP
global PRESETNAME, RELEASE, LAYER
global LOWEST_LEVEL
global gl
try:
cfgf = file(cfg_fname, "r")
except Exception, msg:
print >>sys.stderr, msg
sys.exit(1)
# Defaults
COMMENT = ""
BANKNAME = sfname
PRESETNAME = BANKNAME
DESIGNER = ""
COPYRIGHT = ""
RELEASE = 0.1
# entry structure - list of:
# lname name
# lvel velocity
# lrange velocity range
# latten sf attenuation for layer (obsolete?)
# llevel amount layer was boosted by when normalized.
# If zero, velocity curve adjustment not done.
layers = []
lvmode = None
lineno = 0
for iline in cfgf.readlines():
#{
line = jtrans.tr(iline.strip(), "\t", " ")
lineno += 1
# skip blank line or comment
if len(line) == 0 or line[0] == "#":
continue
# print >>sys.stderr, line
groups = line.split(" ")
cmd = groups[0]
for ix in range(len(groups)-1, -1, -1):
if len(groups[ix]) == 0:
del groups[ix]
if cmd == "bankname":
BANKNAME = " ".join(groups[1:])
PRESETNAME = BANKNAME # default
continue
if cmd == "designer":
DESIGNER = " ".join(groups[1:])
continue
if cmd == "copyright":
COPYRIGHT = " ".join(groups[1:])
continue
if cmd == "comment":
COMMENT += " ".join(groups[1:])
continue
if cmd == "preset":
if len(groups) >= 2:
PRESETNAME = " ".join(groups[1:])
continue
if cmd == "release":
if len(groups) < 2:
print >>sys.stderr, (
"Line %d: expecting release value."
% (lineno))
sys.exit(1)
val = groups[1]
try:
RELEASE = float(val)
except:
print >>sys.stderr, (
"Line %d: expecting float value for release time, got '%s'."
% (lineno, val))
sys.exit(1)
continue
if cmd == "layer-opts":
for group in groups[1:]:
if len(group) == 0:
continue
(kw, val) = kwval(group, lineno)
if kw == "max-shift":
MAX_LAYER_SHIFT = convert_int(val, lineno)
elif kw == "shift-cost":
LAYER_SHIFT_COST = convert_int(val, lineno)
elif kw == "extend-up":
if val.upper() == "Y":
EXTEND_LAYER_UP = True
else:
EXTEND_LAYER_UP = False continue
if cmd == "note-opts":
for group in groups[1:]:
if len(group) == 0:
continue
(kw, val) = kwval(group, lineno)
if kw == "max-shift":
MAX_NOTE_SHIFT = convert_int(val, lineno)
elif kw == "shift-cost":
NOTE_SHIFT_COST = convert_int(val, lineno)
elif kw == "extend-up":
if val.upper() == "Y":
EXTEND_NOTE_UP = True
else:
EXTEND_NOTE_UP = False
continue
if cmd == "keyboard-range":
for group in groups[1:]:
if len(group) == 0:
continue
(kw, val) = kwval(group, lineno)
if kw == "low-key":
key = jmidi.notenum(val)
if key == None:
print >>sys.stderr, (
"Line %d: expecting note name, got '%s'."
% (lineno, val))
sys.exit(1)
LO_KEY = key
# print >>sys.stderr, "LO_KEY", key
elif kw == "high-key":
key = jmidi.notenum(val)
if key == None:
print >>sys.stderr, (
"Line %d: expecting note name, got '%s'."
% (lineno, val))
sys.exit(1)
HI_KEY = key
continue
# sample filename format (the parts we need to know)
if cmd == "format":
for group in groups[1:]:
(kw, val) = kwval(group, lineno)
if kw == "layer-loc":
ival = convert_int(val, lineno)
if ival > 0:
ival -= 1
LAYER_LOC = ival
elif kw == "note-loc":
ival = convert_int(val, lineno)
if ival > 0:
ival -= 1
NOTE_LOC = ival
continue
if cmd == "lowest-level":
LOWEST_LEVEL = convert_int(groups[1], lineno)
if (LOWEST_LEVEL < 0):
print >>sys.stderr, ("Line %d: lowest-level must not be negative."
% (lineno, groups[1:]))
print "LOWEST_LEVEL", LOWEST_LEVEL
continue
if cmd == "layer":
if len(groups) < 2:
print >>sys.stderr, (
"Line %d: expecting layer name."
% (lineno))
sys.exit(1)
lname = groups[1]
latten = 0
llevel = 0 # no adjustment
lvel = -1
lrange = -1
gl.lnamelen = max(gl.lnamelen, len(lname))
for group in groups[2:]:
if len(group) == 0:
continue
(kw, val) = kwval(group, lineno)
if kw == "vel":
if lvmode and lvmode != "vel":
print >>sys.stderr, (
"Line %d: can't mix 'vel' and 'vel-range' in same preset."
% (lineno))
sys.exit(1)
lvel = convert_int(val, lineno)
lvmode = "vel"
elif kw == "vel-range":
if lvmode and lvmode != "range":
print >>sys.stderr, (
"Line %d: can't mix 'vel' and 'vel-range' in same preset."
% (lineno))
sys.exit(1)
lrange = convert_int(val, lineno)
lvmode = "range"
elif kw == "atten":
latten = convert_int(val, lineno)
elif kw == "level":
llevel = convert_int(val, lineno)
layers.append((lname, lvel, lrange, latten, llevel))
continue
#}
# Are we assigning velocities?
if lvmode == "vel":
# No. Check the assignments.
last_lvel = -1
for (lname, lvel, lrange, latten, llevel) in layers:
if lvel == -1:
print >>sys.stderr, (
"No velocity assigned for layer '%s'"
% lname)
sys.exit(1)
if lvel <= last_lvel:
print >>sys.stderr, (
"Velocity for layer '%s' must be higher than previous layer"
% lname)
sys.exit(1)
last_lvel = lvel
if last_lvel != 127:
print >>sys.stderr, (
"Warning: velocity for top layer '%s' should be 127"
% lname)
else:
#{
# We're assigning velocities.
# Find how many layers need ranges assigned,
# and how much room is left
count = 0
unused = 127
for (lname, lvel, lrange, latten, llevel) in layers:
if lrange == -1:
count += 1
else:
unused -= lrange
if unused < 0:
print >>sys.stderr, "Total of velocity ranges must not exceed 127"
sys.exit(1)
# allocate unused velocity range to layers without vel-range specs
ix = 0
for (lname, lvel, lrange, latten, llevel) in layers:
if lrange == -1:
lrange = unused / count
count -= 1
unused -= lrange
layers[ix] = (lname, lvel, lrange, latten, llevel)
ix += 1
# assign specific max velocities to each
ix = 0
last_lvel = 0
for (lname, lvel, lrange, latten, llevel) in layers:
last_lvel += lrange
layers[ix] = (lname, last_lvel, lrange, latten, llevel)
print >>sys.stderr, (
"Layer %*s: velocity %3d, range %3d, level %3d"
% (gl.lnamelen, lname, last_lvel, lrange, llevel))
ix += 1
#}
# build LAYER array
for (lname, lvel, lrange, latten, llevel) in layers:
LAYER.append((lname, lvel, latten, llevel))
#}
# Module initialization
def usage(prog):
print >>sys.stderr
print >>sys.stderr, "%s: create keyboard map for building a soundfont" % prog
print >>sys.stderr
print >>sys.stderr, "usage: %s <sfname> {sampfile}" % prog
print >>sys.stderr
print >>sys.stderr, " where:"
print >>sys.stderr, " <sfname> specifies input and output:"
print >>sys.stderr, " <sfname>.sfc is the input (config),"
print >>sys.stderr, " <sfname>.sfk is the output (keymap)."
print >>sys.stderr, " {sampfile} is any number of sample filenames, with UNIX wildcards"
print >>sys.stderr, " mapfile is the output file name"
print >>sys.stderr
print >>sys.stderr, " Output is ASCII text, and includes a char-graphic keyboard map layout"
print >>sys.stderr
sys.exit(1)
# Main
if __name__ == "__main__":
args = sys.argv
prog = args[0].split("\\")[-1]
del args[0]
if len(args) < 2:
usage(prog)
sys.exit(1)
sfname = args[0]
del args[0]
ofname = sfname + ".sfk"
cfname = sfname + ".sfc"
zfname = sfname + ".sfz"
try:
gl.ofile = file(ofname, "w")
gl.sfzf = file(zfname, "w")
except Exception, msg:
print >>sys.stderr, msg
sys.exit(1)
print >>sys.stderr, "Input (control) file:", cfname
print >>sys.stderr, "Output (keymap) file:", ofname
print >>sys.stderr, "Output (sfz) file:", zfname
process_cfg(cfname, sfname)
build_grid(gl.grid)
load_filenames(args)
assign_keys(map)
showmap(gl.grid, LAYER)
emit_map(gl.grid, LAYER)