forked from Michael-F-Ellis/tbon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmidi_parser.py
1075 lines (965 loc) · 38.9 KB
/
midi_parser.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
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""
Description: Parser/processor for tbon notation language
Author: Mike Ellis
Copyright 2017 Ellis & Grant, Inc.
"""
#######################################################################
## Pylint Overrides
## pylint: disable=no-self-use, unused-argument
## pylint: disable=blacklisted-name
## pylint: disable=too-many-public-methods
## pylint: disable=too-many-instance-attributes
## pylint: disable=too-many-statements, invalid-name
## pylint: disable=too-many-lines
#######################################################################
import keysigs
from parsimonious.grammar import Grammar
#pylint: disable=anomalous-backslash-in-string
def parse(source):
"""Parse tbon Source"""
grammar = Grammar(
"""
score = wsc* music*
music = (partswitch* bar+)+ wsc*
partswitch = "P=" partnum
wsc = comment / ws+
comment = ws* ~r"/\*.*?\*/"s ws*
bar = (wsc* (meta / beat) wsc+)+ barline
meta = beatspec / key / tempo /
relativetempo / velocity /
de_emphasis / channel / instrument
beatspec = "B=" ("2." / "2" / "4." / "4" / "8." / "8")
key = "K=" keyname
keyname = ~r"[a-gA-G](@|#)?"
tempo = "T=" floatnum
relativetempo = "t=" floatnum
velocity = "V=" floatnum
de_emphasis = "D=" floatnum
channel = "C=" chnum
partnum = ~r"[1-9][0-9]*"i
instrument = "I=" inum
inum = ~r"[1-9][0-9]*"i
floatnum = ~r"\d*\.?\d+"i
chnum = ~r"\d*\.?\d+"i
beat = subbeat+
barline = "|" / ":"
extendable = chord / roll / ornament / pitch / rest
pitch = octave* alteration? pitchname
chord = chordstart chorditem chorditem* rparen
chordstart = "("
chorditem = chordpitch / chordhold / chordrest
chordpitch = octave* alteration? pitchname
chordhold = '-'
chordrest = "_" / "z"
rparen = ")"
roll = rollstart pitch pitch+ rparen
rollstart = "(:"
ornament = ornamentstart pitch pitch+ rparen
ornamentstart = "(~"
subbeat = extendable / hold
rest = "_" / "z"
hold = "-"
octave = octave_up / octave_down
alteration = doublesharp / sharp / doubleflat / flat / natural
doublesharp = "𝄪" / "##"
sharp = "♯" / "#"
doubleflat = "𝄫" / "@@"
flat = "♭" / "@"
natural = "♮" / "%"
octave_up = "^"
octave_down = "/"
pitchname = ~"[a-g1-7]"i
ws = ~r"\s*"i
"""
)
return grammar.parse(source)
#pylint: enable=anomalous-backslash-in-string
## Sub-beat tyoe constants
NOTE = 0
CHORD = 1
ROLL = 2
ORNAMENT = 3
## Dict used to compute new time signatures.
## Maps supported beatspecs to tuples of (mulitplier, denominator)
## Multiplier is applied to the beat count to compute the numerator,
## Thus, B=4. in a bar with 2 beats --> 6/8 time
TIMESIG_LUT = {
"2.":(3, 4),
"2":(1, 2),
"4.":(3, 8),
"4":(1, 4),
"8.":(3, 16),
"8":(1, 8),
}
def time_signature(beatspec, beatcount, index, part):
"""
Create a new time signature at index.
"""
multiplier, numerator = TIMESIG_LUT[beatspec]
return ('M', index, multiplier*beatcount, numerator, part)
class MidiPreEvaluator():
"""
Parses and evaluates a tbon source and produces a time-ordered list of
sub-beat durations for each beat.
"""
#pylint: disable=dangerous-default-value
def __init__(self):
self.first_tempo = 120
self.output = []
self.meta_output = []
self.beat_map = {1: []}
self.beat_lengths = []
self.subbeat_starts = []
self.subbeat_lengths = []
self.partstates = {0: self.new_part_state(0)}
self.processing_state = self.partstates[0]
self.current_part = 0
#pylint: enable=dangerous-default-value
def eval(self, source, verbosity=2):
"""Evaluate tbon source"""
node = parse(source) if isinstance(source, str) else source
method = getattr(self, node.expr_name, lambda node, children: children)
## Recursively evaluate subtree
method(node, [self.eval(n, verbosity) for n in node])
self.show_progress(node, verbosity)
return self.output
def show_progress(self, node, verbosity):
""" Call this *after* the node has been evaluated """
if verbosity <= 0:
return
if node.expr_name not in ('', 'ws', None):
print("Evaluated {}, '{}'".format(node.expr_name, node.text))
print("output={}".format(self.output))
if verbosity > 1:
print('state={}'.format(self.processing_state))
def score(self, node, children):
"""
Gather outputs for all parts
"""
if len(self.partstates) == 1:
## Unnested output
d = self.partstates[0]
self.subbeat_starts = d['subbeat_starts']
for n in d['subbeat_lengths']:
self.subbeat_lengths.append(n)
else:
for _, d in self.partstates.items():
self.subbeat_lengths.append(tuple(d['subbeat_lengths']))
self.subbeat_starts.append(d['subbeat_starts'])
def partswitch(self, node, children):
""" Switch to new part """
newpartnumber = int(node.children[1].text)
newpindex = newpartnumber - 1
try:
self.processing_state = self.partstates[newpindex]
except KeyError:
## Doesn't exist yet. Create it.
pstate = self.new_part_state(newpindex)
self.partstates[newpindex] = pstate
self.processing_state = self.partstates[newpindex]
self.current_part = newpindex
self.beat_map[newpartnumber] = []
def new_part_state(self, pindex):
""" Returns a new part state dict """
return dict(
basetempo=self.first_tempo,
tempo=self.first_tempo,
beat_index=0,
bar_beat_count=0,
channel=1,
in_chord=False,
chord_tone_count=0,
subbeats=0,
beatspec="4",
timesig=('M', 0.0, 4, 4, pindex),
subbeat_lengths=[],
subbeat_starts=[],
)
def channel(self, node, children):
""" Change the current channel """
state = self.processing_state
newchannel = int(node.children[1].text)
if 1 <= newchannel <= 16:
state['channel'] = newchannel
else:
msg = ("\nInvalid channel number, {}. "
"Must be between 1 and 16, inclusive.")
raise ValueError(msg.format(newchannel))
def instrument(self, node, children):
"""
Change the midi instrument on the current track and channel.
"""
state = self.processing_state
newinstrument = int(node.children[1].text)
if 1 <= newinstrument <= 128:
index = state['beat_index']
state['instrument'] = newinstrument
chan = state['channel']
part = self.current_part
self.meta_output.append(('I', index, newinstrument, part, chan))
else:
msg = ("\nInvalid instrument number, {}. "
"Must be between 1 and 128, inclusive.")
raise ValueError(msg.format(newinstrument))
def tempo(self, node, children):
""" Install a new tempo """
if self.current_part == 0:
state = self.processing_state
newtempo = int(round(float(node.children[1].text)))
if newtempo > 1:
state['basetempo'] = state['tempo'] = newtempo
else:
msg = ("\nInvalid Tempo, {}. "
"Tempo must be greater than 1.")
raise ValueError(msg.format(newtempo))
self.insert_tempo_meta(state)
else:
print(
"Ignoring tempo spec in part {}.".format(self.current_part))
def key(self, node, children):
""" Insert a key signature """
state = self.processing_state
keyname = node.children[1].text.strip()
if keyname in keysigs.KEYSIGS.keys():
self.processing_state['keyname'] = keyname
else:
msg = ("\n Invalid key name, '{}'. "
"Must be one of {}.")
validkeys = ', '.join(sorted(keysigs.KEYSIGS.keys()))
raise ValueError(msg.format(keyname, validkeys))
index = state['beat_index']
sig = keysigs.MIDISIGS[keyname]
part = self.current_part
self.meta_output.append(('K', index, sig, part))
def relativetempo(self, node, children):
""" Adjust the current tempo without altering the base tempo """
if self.current_part == 0:
state = self.processing_state
xtempo = float(node.children[1].text)
if xtempo > 0.0:
state['tempo'] = int(round(xtempo * state['basetempo']))
else:
msg = ("\nInvalid relative tempo, {}. "
"Must be greater than 0.0")
raise ValueError(msg.format(xtempo))
self.insert_tempo_meta(state)
else:
print(
"Ignoring tempo spec in part {}.".format(self.current_part))
def beatspec(self, node, children):
"""
Store the new beat division
"""
state = self.processing_state
oldbeatspec = state['beatspec']
newbeatspec = node.children[1].text
if oldbeatspec != newbeatspec:
state['beatspec'] = newbeatspec
def subbeat(self, node, children):
"""
Add 1 to subbeat count of current beat.
"""
state = self.processing_state
state['subbeats'] += 1
def beat(self, node, children):
"""
Compute subbeat duration for current beat in current tempo.
DESIGN NOTE: tbon will not support changing the tempo within
a beat.
"""
state = self.processing_state
mult, numer = TIMESIG_LUT[state['beatspec']]
#beat_length = 1
beat_length = 4 * mult / numer
subbeat_length = beat_length/state['subbeats']
subbeats = []
for n in range(state['subbeats']):
subbeats.append(state['beat_index'] + (n * subbeat_length))
#self.subbeat_starts.append(tuple(subbeats))
state['subbeat_starts'].append(tuple(subbeats))
state['subbeats'] = 0
state['beat_index'] += beat_length
state['bar_beat_count'] += 1
## if no tempo meta at end of first beat, insert the default.
if state['beat_index'] == beat_length:
for m in self.meta_output:
if m[0] == 'T':
break
else:
self.insert_tempo_meta(state, index=0)
state['subbeat_lengths'].append(subbeat_length)
self.beat_lengths.append(beat_length)
def barline(self, node, children):
""" Finish the bar. Add to beat map """
state = self.processing_state
partnum = self.current_part + 1
self.beat_map[partnum].append(state['bar_beat_count'])
mult, numer = TIMESIG_LUT[state['beatspec']]
beat_length = 4 * mult / numer
bar_index = state['beat_index'] - state['bar_beat_count'] * beat_length
timesig = time_signature(state['beatspec'],
state['bar_beat_count'],
bar_index, self.current_part)
if bar_index == 0 or state['timesig'][-3:] != timesig[-3:]:
self.meta_output.append(timesig)
state['timesig'] = timesig
state['bar_beat_count'] = 0
def insert_tempo_meta(self, state, index=None):
""" Append a tempo meta event """
if index is None:
index = state['beat_index']
self.meta_output.append(('T', index, state['tempo']))
class MidiEvaluator():
"""
Parses and evaluates a tbon source and produces a time-ordered list of
tuples representing midi note events in self.output. Each tuple consists of
(pitch, start, end, velocity, channel) where
* pitch = midi pitch number
* start = start time in quarter-note beats
* end = end time in quarter-note beats
* velocity = velocity as a number between 0.0 and 1.0,
* channel = MIDI channel number 1 through 16 inclusive.
Times are offsets from beginning of the music. Converting the list to
actual Midi NoteOn and NoteOff events and playing playing them is left up
to other software.
The constructor's "ignore_velocity" argument suppresses the inclusion of
both velocity and channel number. Its primary purpose is to simplify the
creation of test cases where those items are not needed.
The self.output list is actually a list of tuples of tuples. The extra
level is needed to handle compositions in multiple voices, so the actual
format is
[ ((p,s,e,v,c), ...), ((p,s,e,v,c), ...)), ... ]
The top-level tuples are ordered by part number.
Other outputs:
self.meta_output holds Tempo, Key and Time Signature events.
Tempo: Tuplets of the form ('T', start, bpm) where
start = start time of new tempo in quarter-note beats
bpm = new quarter-note tempo in beats per minute
Key: Tuplets of the form ('K', start, (sf, mode))
start = start time of new tempo in quarter-note beats
sf = Number of sharps (sf > 0) or flats (sf < 0)
mode = 0 if major, 1 if minor
Time Signature: Tuplets of the form ('M', start, numerator, denominator)
start = start time of new tempo in quarter-note beats
numerator = denominator notes per measure
denominator = one of [2, 4, 8, 16]
self.metronome_output. Tuples of metronome clicks in part 1, one per beat.
* same format as note events (p,s,e,v,c).
* channel is always 10,
* velocity follows the music
* downbeat pitch is high wood block (76)
* other beats are low wood block (77)
self.beat_map: List of number of beats in each measure
"""
def __init__(self,
pitch_order=tuple('cdefgab'),
ignore_velocity=False):
self.first_tempo = 120
self.pitch_order = pitch_order
self.ignore_velocity = ignore_velocity
self.pitch_midinumber = dict(zip(pitch_order, (0, 2, 4, 5, 7, 9, 11)))
self.output = []
self.metronome_output = []
self.meta_output = []
self.beat_map = ()
self.subbeat_starts = []
self.subbeat_lengths = None
self.beat_lengths = []
self.partstates = {}
self.processing_state = None
self.current_part = None
def new_part_state(self, newpartnumber):
""" Returns a new part state dict """
return dict(
notes=[],
basetempo=self.first_tempo,
tempo=self.first_tempo,
beat_index=0,
subbeats=0,
bar_beat_index=0,
bar_subbeats=0,
octave=5, ## middle C, midi number 60
alteration=0,
pitchname=self.pitch_order[0],
bar_accidentals={},
in_chord=NOTE,
chord_tone_count=0,
prior_chord_tone_count=0,
prior_chord_next_index=0,
keyname="C",
velocity=0.8,
de_emphasis=1.0,
channel=1,
output=[],
)
def eval(self, source, verbosity=2):
"""Evaluate tbon source"""
## Preprocess once only.
if self.subbeat_lengths is None:
mp = MidiPreEvaluator()
mp.eval(source, verbosity=0)
self.subbeat_lengths = mp.subbeat_lengths
self.subbeat_starts = mp.subbeat_starts
self.beat_lengths = mp.beat_lengths
self.meta_output = mp.meta_output
self.beat_map = {k: tuple(v) for k, v in mp.beat_map.items()}
## Update each partstate
pstates = self.partstates ## shorter name
for num, state in mp.partstates.items():
pstates[num] = self.new_part_state(num)
pstates[num]['subbeat_starts'] = state['subbeat_starts']
pstates[num]['subbeat_lengths'] = state['subbeat_lengths']
self.processing_state = pstates[0]
self.current_part = 0
#print("PreEval {}".format(mp.output))
node = parse(source) if isinstance(source, str) else source
method = getattr(self, node.expr_name, lambda node, children: children)
## Recursively evaluate subtree
method(node, [self.eval(n, verbosity) for n in node])
self.show_progress(node, verbosity)
return self.output
def show_progress(self, node, verbosity):
""" Call this in self.eval() *after* the node has been evaluated """
if verbosity <= 0:
return
if node.expr_name not in ('', 'ws', None):
print("Evaluated {}, '{}'".format(node.expr_name, node.text))
print("output={}".format(self.output))
print("subbeat_lengths={}".format(self.subbeat_lengths))
if verbosity > 1:
print('state={}'.format(self.processing_state))
def score(self, node, children):
"""
Add the last note or chord to the list,
Then convert output list items to tuples.
If we're ignoring velocity (and channel), the tuples are:
(pitch, start, end)
otherwise
(pitch, start, end, velocity, channel)
Finally, gather outputs for all parts.
"""
for _, state in self.partstates.items():
## Add the last note or chord to the list,
for note in state['notes']:
state['output'].append(note)
## sort output by start time
state['output'] = sorted(state['output'], key=lambda x: x[1])
## Convert the music output
converted = []
for item in state['output']:
pitch = item[0]
start = item[1]
end = item[2]
velocity = item[3]
channel = item[4]
if self.ignore_velocity:
converted.append((pitch, start, end))
else:
converted.append((pitch, start, end,
velocity, channel))
state['output'] = converted
## Convert the metronome output
converted = []
for item in self.metronome_output:
pitch = item[0]
start = item[1]
end = item[2]
velocity = item[3]
channel = 10 # MIDI Percussion channel
if self.ignore_velocity:
converted.append((pitch, start, end))
else:
converted.append((pitch, start, end, velocity, channel))
self.metronome_output = converted
## Gather outputs from all parts
for _, d in self.partstates.items():
self.output.append(tuple(d['output']))
def partswitch(self, node, children):
""" Switch to new part """
newpartnumber = int(node.children[1].text)
newpindex = newpartnumber - 1
try:
self.processing_state = self.partstates[newpindex]
except KeyError:
## Doesn't exist yet. Create it.
pstate = self.new_part_state(newpindex)
self.partstates[newpindex] = pstate
self.processing_state = self.partstates[newpindex]
self.current_part = newpindex
def tempo(self, node, children):
""" Install a new tempo """
if self.current_part == 0:
state = self.processing_state
newtempo = float(node.children[1].text)
if newtempo > 1:
state['basetempo'] = state['tempo'] = newtempo
else:
msg = ("\nInvalid Tempo, {}. "
"Tempo must be greater than 1.")
raise ValueError(msg.format(newtempo))
else:
print(
"Ignoring tempo spec in part {}.".format(self.current_part))
def relativetempo(self, node, children):
""" Adjust the current tempo without altering the base tempo """
if self.current_part == 0:
state = self.processing_state
newtempo = float(node.children[1].text)
assert newtempo != 0.0
state['tempo'] = newtempo * state['basetempo']
else:
print(
"Ignoring tempo spec in part {}.".format(self.current_part))
def velocity(self, node, children):
""" Change the current velocity """
state = self.processing_state
newvelocity = float(node.children[1].text)
if 0.0 <= newvelocity <= 1.0:
state['velocity'] = newvelocity
else:
msg = ("\nInvalid velocity, '{}'. "
"Must be between 0.0 and 1.0, inclusive")
raise ValueError(msg.format(newvelocity))
def channel(self, node, children):
""" Change the current channel """
state = self.processing_state
newchannel = int(node.children[1].text)
if 1 <= newchannel <= 16:
state['channel'] = newchannel
else:
msg = ("\nInvalid channel number, {}. "
"Must be between 1 and 16, inclusive.")
raise ValueError(msg.format(newchannel))
def de_emphasis(self, node, children):
""" Change the current de_emphasis """
state = self.processing_state
newde_emphasis = float(node.children[1].text)
if 0.0 <= newde_emphasis <= 1.0:
state['de_emphasis'] = 1.0 - newde_emphasis
else:
msg = ("\nInvalid De-emphasis, '{}'. "
"Must be between 0.0 and 1.0, inclusive.")
raise ValueError(msg.format(newde_emphasis))
def bar(self, node, children):
""" Clear any accidentals """
self.clear_bar_accidentals()
state = self.processing_state
state['bar_beat_index'] = 0
state['bar_subbeats'] = 0
def beat(self, node, children):
""" Update the beat indices and add to metronome_output"""
state = self.processing_state
## Metronome
velocity = state['velocity']
channel = 10
if state['bar_beat_index'] == 0:
pitchnumber = 76
else:
pitchnumber = 77
velocity *= state['de_emphasis']
bindex = state['beat_index']
start = state['subbeat_starts'][bindex][0]
end = start + self.beat_lengths[bindex]
self.metronome_output.append([pitchnumber, start, end,
velocity, channel])
## Update indices
state['subbeats'] = 0
state['beat_index'] += 1
state['bar_beat_index'] += 1
def chordstart(self, node, children):
"""
Close any pending accumulations
Initialize the state machine for chord tone counting.
"""
state = self.processing_state
if state['prior_chord_tone_count'] == 0:
for note in state['notes']:
if len(note) > 1:
state['output'].append(note)
state['notes'] = []
state['in_chord'] = CHORD
state['prior_chord_next_index'] = 0
def rollstart(self, node, children):
"""
Close any pending accumulations
Initialize the state machine for chord (roll) tone counting.
"""
state = self.processing_state
for note in state['notes']:
if len(note) > 1:
state['output'].append(note)
state['notes'] = []
state['in_chord'] = ROLL
state['chord_tone_count'] = 0
def ornamentstart(self, node, children):
"""
Init an ornament
Close any pending accumulations
Initialize the state machine for chord tone counting.
"""
state = self.processing_state
for note in state['notes']:
if len(note) > 1:
state['output'].append(note)
state['notes'] = []
state['in_chord'] = ORNAMENT
state['chord_tone_count'] = 0
def rparen(self, node, children):
"""
Finalize chord, roll, or ornament.
"""
state = self.processing_state
if state['in_chord'] == ROLL:
## Adjust starts and durations
## Before adjustments all durations are equal
## to the full subbeat duration.
count = state['chord_tone_count']
index = state['beat_index']
subduration = state['subbeat_lengths'][index]
subsub_duration = subduration/count
offset = 0
for i in range(1, count):
offset += subsub_duration
#print("i={},Adjusted duration = {}".format(i, duration))
state['notes'][i][1] += offset
state['subbeats'] += 1
state['bar_subbeats'] += 1
if state['in_chord'] == ORNAMENT:
## Adjust starts and durations
## Before adjustments all durations are equal
## to the full subbeat duration.
count = state['chord_tone_count']
index = state['beat_index']
subduration = state['subbeat_lengths'][index]
subsub_duration = subduration/count
offset = 0
for i in range(count):
state['notes'][i][1] += offset
state['notes'][i][2] = state['notes'][i][1] + subsub_duration
offset += subsub_duration
## Ornament tones (other than the last) do not sustain,
## so flush all but the last to the output list
for i in range(-count, -1):
state['output'].append(state['notes'][i])
## Keep on the last in the extendable note list
state['notes'] = [state['notes'][-1]]
state['subbeats'] += 1
state['bar_subbeats'] += 1
state['chord_tone_count'] = 1
elif state['in_chord'] == CHORD:
## push leftovers to output
nnotes = len(state['notes'])
nleftover = nnotes - state['chord_tone_count']
while nleftover > 0:
self.note2output(-nleftover, state)
nleftover -= 1
state['subbeats'] += 1
state['bar_subbeats'] += 1
state['in_chord'] = NOTE
state['prior_chord_tone_count'] = state['chord_tone_count']
state['chord_tone_count'] = 0
def octave_up(self, node, children):
"""
Octave up adds 1 to the octave shift count for next pitch.
"""
state = self.processing_state
state['octave'] += 1
def octave_down(self, node, children):
"""
Octave down subtracts 1 from the octave shift count for next pitch.
"""
state = self.processing_state
state['octave'] -= 1
def doublesharp(self, node, children):
""" Adds 1 to current pitch alteration count """
state = self.processing_state
state['alteration'] += 2
def sharp(self, node, children):
""" Adds 1 to current pitch alteration count """
state = self.processing_state
state['alteration'] += 1
def doubleflat(self, node, children):
""" Subtracts 1 from current pitch alteration count """
state = self.processing_state
state['alteration'] -= 2
def flat(self, node, children):
""" Subtracts 1 from current pitch alteration count """
state = self.processing_state
state['alteration'] -= 1
def natural(self, node, children):
"""
Sets alteration count to None (not 0) so other code will
know to cancel any prior accidentals in the bar for next pitch.
"""
state = self.processing_state
state['alteration'] = None
def rest(self, node, children):
""" rest = "z"
Encountering a rest triggers the following actions:
* Close the preceding notes
* Open a new one
* Insert None as the midi pitch
"""
state = self.processing_state
index = state['beat_index']
duration = state['subbeat_lengths'][index]
start = state['subbeat_starts'][index][state['subbeats']]
if not state['in_chord']:
for note in state['notes']:
if len(note) > 1:
note[2] = start ## which is the end :-)
state['output'].append(note)
end = start + duration
if not state['in_chord']:
state['notes'] = []
state['subbeats'] += 1
state['bar_subbeats'] += 1
state['chord_tone_count'] = 0
state['prior_chord_tone_count'] = 0
state['notes'].append([None, start, end,
state['velocity'],
state['channel'],
])
def pitchname(self, node, children):
""" pitchname = ~"[a-g]"i
Encountering a pitchname triggers the following actions:
* Close the preceding note
* Open a new one
* Look up the midi pitch
* Adjust relative to prior pitch
* Apply octave and alterations
* Insert new midi pitch into current note (the new one)
* Empty the current alteration and octave accumulators
TODO Relative pitch needs work.
"""
state = self.processing_state
pitchname = node.text.strip()
if pitchname not in self.pitch_order:
## User is trying to mix alpha and numeric pitches
msg = ("\nInvalid pitch character, '{}'. "
"(Can't mix numeric and alpha pitches in same file.)")
msg = msg.format(pitchname)
raise ValueError(msg)
state['octave'] += self.octave_change(state['pitchname'], pitchname)
if state['alteration'] != 0:
## Check for None which indicatas a natural sign.
if state['alteration'] is None:
state['alteration'] = 0
## Update the bar accidentals dict
self.set_bar_accidental(pitchname,
state['octave'],
state['alteration'])
alteration = self.get_bar_accidental(pitchname, state['octave'])
## For numeric pitchnames the 'alteration' above includes an
## offset that maps 1 to the tonic of the current key.
pitchnumber = self.pitch_midinumber[pitchname]
pitchnumber += alteration + 12 * state['octave']
## De-emphasize offbeats according to current de_emphasis value
if self.is_downbeat(state):
velocity = state['velocity'] ## downbeat gets full velocity
else:
velocity = state['velocity'] * state['de_emphasis']
state['alteration'] = 0
state['pitchname'] = pitchname
channel = state['channel']
state['pending_note'] = (pitchnumber,
velocity, channel)
def pitch(self, node, children):
"""
Deal with non-chord tones.
"""
state = self.processing_state
index = state['beat_index']
duration = state['subbeat_lengths'][index]
#start = state['subbeat_starts'][part][index][state['subbeats']]
start = state['subbeat_starts'][index][state['subbeats']]
if not state['in_chord']:
for note in state['notes']:
if len(note) > 1:
note[2] = start ## which is the end :-)
state['output'].append(note)
end = start + duration
pitchnumber, velocity, channel = state['pending_note']
if not state['in_chord']:
state['notes'] = []
state['subbeats'] += 1
state['bar_subbeats'] += 1
state['notes'].append([pitchnumber, start, end,
velocity, channel])
state['chord_tone_count'] = 0
state['prior_chord_tone_count'] = 1
elif state['in_chord'] in (ROLL, ORNAMENT):
## Rolls and Ornaments
state['notes'].append([pitchnumber, start, end,
velocity, channel])
state['chord_tone_count'] += 1
def chordpitch(self, node, children):
""" Deal with chord tones """
state = self.processing_state
index = state['beat_index']
duration = state['subbeat_lengths'][index]
pitchnumber, velocity, channel = state['pending_note']
start = state['subbeat_starts'][index][state['subbeats']]
end = start + duration
newnote = [pitchnumber, start, end,
velocity, channel]
pchindex = state['prior_chord_next_index']
try:
## Replace if possible, left to right
state['output'].append(state['notes'][pchindex])
state['notes'][pchindex] = newnote
state['prior_chord_next_index'] += 1
except IndexError:
## All prior chord notes already replaced
state['notes'].append(newnote)
state['prior_chord_next_index'] += 1
state['chord_tone_count'] += 1
def chordhold(self, node, children):
""" Extend corresponding pitch in prior chord """
state = self.processing_state
index = state['beat_index']
duration = state['subbeat_lengths'][index]
newend = state['subbeat_starts'][index][state['subbeats']] + duration
if state['in_chord'] in (CHORD,):
index = state['prior_chord_next_index']
state['notes'][index][2] = newend
state['prior_chord_next_index'] += 1
state['prior_chord_tone_count'] -= 1
state['chord_tone_count'] += 1
def hold(self, node, children):
"""
A hold triggers the following actions:
* Add 1 sub-beat to current note duration
* Add 1 to sub-beat count in current beat.
"""
state = self.processing_state
index = state['beat_index']
duration = state['subbeat_lengths'][index]
newend = state['subbeat_starts'][index][state['subbeats']] + duration
if state['in_chord'] in (CHORD,):
index = state['chord_tone_count']
state['notes'][index][2] = newend
state['chord_tone_count'] += 1
else:
for note in state['notes']:
note[2] = newend
state['subbeats'] += 1
state['bar_subbeats'] += 1
def chordrest(self, node, children):
"""
Drop a note from the notes list and append it to
the output, replacing it with a rest.
"""
state = self.processing_state
index = state['beat_index']
duration = state['subbeat_lengths'][index]
pitchnumber, velocity, channel = (None,
state['velocity'],
state['channel'])
start = state['subbeat_starts'][index][state['subbeats']]
end = start + duration
newnote = [pitchnumber, start, end,
velocity, channel]
pchindex = state['prior_chord_next_index']
try:
## Replace if possible, left to right
state['output'].append(state['notes'][pchindex])
state['notes'][pchindex] = newnote
state['prior_chord_next_index'] += 1
print("Replaced with rest.")
state['chord_tone_count'] += 1
except IndexError:
msg = "Not enough notes in prior chord."
print(msg)
raise
def pitchname_interval_ascending(self, pname0, pname1):
"""
Returns the musical interval number between the pitchnames assuming the
second pitch is the nearest higher from the first.
"""
order = self.pitch_order
return 1 + (order.index(pname1) - order.index(pname0)) % len(order)
def octave_change(self, pname0, pname1):
"""
Return -1, 0, or 1 depending on whether closest instance of pname1 is
above, within, or below the octave containing pname0.
"""
interval = self.pitchname_interval_ascending(pname0, pname1)
if interval == 1: ## Unison
return 0
higher = interval < 5
lower = not higher
order = self.pitch_order
index0 = order.index(pname0)
index1 = order.index(pname1)
if higher and index1 > index0:
result = 0
elif higher and index1 < index0:
result = 1