-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpypond.py
1354 lines (1232 loc) · 46.2 KB
/
pypond.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
#!/usr/bin/python3
"""An OOP approach to music scores with GNU lilypond text output"""
# MIDI notes:
# Recall: Middle C (C4) is key 60
# C1 = 24
# A1 = 21
# C0 = 12
# A0 = 9
# C-1 = 0
import sys
import circular
import re
DEBUG = True
LOGFILE = None
FILENAME = "pypond.py"
_LILYFLAT = 'es'
_LILYSHARP = 'is'
_LILYTIE = "~ "
_LILYMIDDLEOCTAVE = 3
_DEFAULT_OCTAVE = 4
_ENCODING_NOTES = {
'C' : 0,
'c' : 0,
'B#' : 0,
'b#' : 0,
'C#' : 1,
'c#' : 1,
'Db' : 1,
'db' : 1,
'D' : 2,
'd' : 2,
'Eb' : 3,
'eb' : 3,
'D#' : 3,
'd#' : 3,
'E' : 4,
'e' : 4,
'Fb' : 4,
'fb' : 4,
'F' : 5,
'f' : 5,
'E#' : 5,
'e#' : 5,
'Gb' : 6,
'gb' : 6,
'F#' : 6,
'f#' : 6,
'G' : 7,
'g' : 7,
'Ab' : 8,
'ab' : 8,
'G#' : 8,
'g#' : 8,
'A' : 9,
'a' : 9,
'Bb' : 10,
'bb' : 10,
'A#' : 10,
'a#' : 10,
'B' : 11,
'b' : 11,
}
#_ENCODING_ACCIDENTALS = {
# 'b' : -1,
# '#' : 1,
# "" : 0
#}
class LilySyntax():
kwClef = "\\clef"
kwKey = "\\key"
kwTimeSignature = "\\time"
kwKeyMajor = "\\major"
kwKeyMinor = "\\minor"
headerString = '\\version "2.20.0"\n{\n '
footerString = '\n \\bar "|."\n}'
lilyTie = "~ "
lilyDot = "."
lilyFlat = "es"
lilySharp = "is"
encodingAccidentals = {
lilyFlat : -1,
lilySharp : 1
}
_reFlat = re.compile(lilyFlat+ "+")
_reSharp = re.compile(lilySharp + "+")
defaultOctave = 4
octaveUp = "'"
octaveDown = ','
@classmethod
def _DecodeAccidentalString(cls, sAccidental):
"""Return the encoding (integer) corresponding to a particular
GNU Lilypond accidental. Handles up to infinity sharp and infinity flat."""
sAcc = sAccidental.strip()
# Strip non-accidental content
index = 0
for n in range(len(sAcc)):
if (sAcc[n] not in cls.lilyFlat) and (sAcc[n] not in cls.lilySharp):
index = n
break
sAcc = sAcc[:index]
encFlat = cls.encodingAccidentals.get(cls.lilyFlat, None)
encSharp = cls.encodingAccidentals.get(cls.lilySharp, None)
if cls._reFlat.match(sAcc):
return encFlat*len(sAcc)//2
elif cls._reSharp.match(sAcc):
return encSharp*len(sAcc)//2
else:
return 0
class Note(object):
octaveIndices = {'c' : 0, 'c#' : 1, 'db' : 1, 'd' : 2, 'd#' : 3, 'eb' : 3,
'e' : 4, 'f' : 5, 'f#' : 6, 'gb' : 6, 'g' : 7, 'g#' : 8,
'ab' : 8, 'a' : 9, 'a#' : 10, 'bb' : 10, 'b' : 11}
noteOrder = circular.Circular(['c','d','e','f','g','a','b'])
flatChar = 'b'
sharpChar = '#'
naturalChar = ""
encodingAccidentals = {
flatChar : -1,
sharpChar : 1,
naturalChar : 0
}
_reFlat = re.compile(flatChar + "+$")
_reSharp = re.compile(sharpChar + "+$")
_lilyTie = "~ "
def __init__(self, notestring, duration = None, isTied = False):
self.noteString = notestring
self.noteName = Note._NoteNameFromString(notestring)
self.accidental = Note._AccidentalFromString(notestring)
self.octave = Note._OctaveFromString(notestring)
self.duration = None
self.tempo = None # Use self.setTempo() to configure tempo for MIDI
self.beatDuration = None # the duration note that gets the beat (at self.tempo)
self.durationMs = None # self.setDuration()
self.isTied = isTied
self.measureNum = None
self.beatNum = None
self.dotted = False
# Requires configuration of self.setBeatDuration() and self.setTempo() first
if duration != None:
self.setDuration(duration)
if not self.checkValid():
_dbg("Warning! This Note object is no good: {}".format(self.__repr__()))
#self.noteString = self.getNoteLetter() + self.getAccidentalString() + str(self.getOctave())
#print("__init__ : self = {}".format(self))
def isRest(self):
return False
def getNoteName(self):
"""Returns the note name as a lower-case string"""
return self.noteName
def getNoteLetter(self):
"""Returns the note letter name without any accidentals"""
if self.noteName != None and self.noteName != "":
return self.noteName[0]
else:
return ""
def setAccidental(self, accidental):
"""Set the accidental for this note. 'accidental' should
be a signed integer with -1 representing flat, +1 representing
sharp, etc."""
acc = _int(accidental)
if acc != None:
self.accidental = acc
def getAccidental(self):
"""Returns -1 for flat, +1 for sharp, or 0 for natural."""
return self.accidental
def getAccidentalString(self, accidental = None):
"""Return a string representing the accidental as sharps or flats
if 'accidental' == None, uses self.accidental"""
if accidental == None:
accidental = self.getAccidental()
#print("accidental = {}".format(accidental))
accidental = _int(accidental)
sign = 0
self.naturalChar
if accidental > 0:
sign = 1
elif accidental < 0:
sign = -1
for (k, v) in self.encodingAccidentals.items():
if sign == v:
sAcc = k
break
return sAcc * abs(accidental)
def getOctave(self):
"""Returns the octave as a positive integer between 0 and 9"""
return self.octave
def setOctave(self, octave):
oct = _int(octave)
if oct != None:
self.octave = oct
def getOctaveIndex(self):
"""Get the index of the note within the octave. Octaves begin at 'C' and
thus C has octave index of 0. Increasing by a half-step increases the index
by 1 until B which has index of 11."""
notename = self.getNoteName().lower() # Most of the time this name should work
if notename not in self.octaveIndices.keys(): # If not, it probably has too many accidentals
simple = self.simplify() # Simplify
notename = simple.getNoteName().lower() # And try again
return self.octaveIndices.get(notename) # Return the index
def getNote(self):
"""Returns (noteName, accidental, octave)"""
return (self.noteName[0], self.accidental, self.octave)
def checkValid(self):
"""Check that Note constructor made a valid note object"""
if (self.noteName is not None) and (self.accidental is not None) and (self.octave is not None):
return True
else:
return False
def _getLilyAccidental(self):
"""Get the note accidental string in GNU lilypond syntax"""
s = ""
if self.accidental == 0:
return ""
elif self.accidental < 0:
s = _LILYFLAT
elif self.accidental > 0:
s = _LILYSHARP
return s * abs(self.accidental)
def _getLilyOctave(self):
"""Get the note octave string indicator in GNU lilypad syntax"""
o = self.getOctave()
if o == _LILYMIDDLEOCTAVE:
return ""
elif o > _LILYMIDDLEOCTAVE:
return "'" * (o - _LILYMIDDLEOCTAVE)
else:
return "," * (_LILYMIDDLEOCTAVE - o)
#@DEPRECATED!
def _getLilyDurationAligned(self, alignBeat):
"""First parse the alignBeat, then walk from LS-to-MS through the alignBeat parse
and 'fill in the holes' taking from the note duration. Once the beat has rounded
out to all zeros (an even multiple of whole notes), walk down MS-to-LS the remaining
note duration parse.
On the first pass (LS-to-MS), we don't need to worry about dotting notes."""
# Hmmm... didn't seem to get it right. Poke further into this
_dbg(" - - - - - - - - alignBeat = {}".format(alignBeat))
if alignBeat == None:
alignBeat = self.getBeatNum()
if alignBeat == None:
return self._getLilyDuration()
if self.duration == None:
return ""
duration = self.duration # Need a shallow copy since we'll be modifying this
nBeats = self.parseBeatLength(alignBeat)
_dbg("parsed beat = {}{}{}{}{}{}{}".format(*nBeats))
_dbg("old duration = {}".format(duration))
ll = []
tieSymbol = ""
for n in range(len(nBeats)): # Walk the beat parse
if nBeats[-(n+1)]: # (LS-to-MS)
index = len(nBeats) - (n + 1)
ddur = 2**(-index) # We need to borrow this delta-duration from the note if possible
if duration >= ddur:
duration -= ddur
ll.append("{}{}".format(tieSymbol, str(2**index)))
tieSymbol = self._lilyTie
alignBeat += ddur # Give that duration to the alignBeat
nBeats = self.parseBeatLength(alignBeat) # then parse the beat again
_dbg("After walk up: {}".format("".join(ll)))
_dbg("new duration = {}".format(duration))
# Now we walk down the parsed remainder of the note duration
nNotes = self.parseBeatLength(duration)
candot = False # Note dotting
for n in range(len(nNotes)): # Walk through the breakdown of the note duration, starting from whole notes
if nNotes[n] > 0: # If a duration exists,
if candot: # If the last duration exists, we can dot it!
candot = False # Then we turn off dotting so we don't end up with double-dots
ll.append('.') # (those are silly)
else:
candot = True # The next note can be a dot if present
durationInt = 2**n # 4 for quarter note, 8 for eighth note, etc...
ll.append("{}{}".format(tieSymbol, str(durationInt)))
tieSymbol = self._lilyTie # Once the first symbol has been added to lily list, set the tie symbol
else:
candot = False # The next note cannot be a dot
_dbg("After walk down: {}".format("".join(ll)))
return "".join(ll)
def _isBasisDuration(self):
"""Returns True if the note is a basis note (whole, half, quarter, eighth, 1/16, 1/32, 1/64)
Returns False if the note is a combination of basis notes."""
invdur = 1/self.getDurationNoDot()
if invdur % 1 > 0:
return False
else:
return True
@staticmethod
def _invLog2(x):
y = 0
inv = 1/x
while inv > 1:
inv /= 2
y += 1
return y
def _getLilyDuration(self, alignBeat = None):
"""Get the duration indication in GNU lilypond format.
For a power-of-two (i.e. quarter note, eighth note, whole note, etc.), we simply
return a string of the reciprocal.
For non-power-of-two (i.e. 11/16ths), we will end up with a combination of notes
tied together, or potentially a single dotted note.
We can use 'alignBeat' to try to suggest the best order of mixed-duration notes.
For example, if the duration of the note is 1/2 + 1/8 (half-note plus eighth note),
we could express it as a half note tied to an eighth note, or as an eighth tied to
a half. If we're at the beginning of a measure (alignBeat = 0), we bias toward
starting with the larger beat (half note in this example). If the current beat has
an odd number of eighth notes, it makes more sense to start with the eighth note
to round out the beat count, then continue with the largest-to-smallest pattern.
We first parse the note duration and the alignBeat into a binary number.
[b0, b1, b2, b3, b4, b5, b6]"""
# To tie notes, we can simply replace the duration with power-of-two
# durations (or dotted durations) with tildes (~) between them.
if self.duration == None:
return ""
if self._isBasisDuration():
return int(1/self.getDurationNoDot())
length = self.duration # non-reciprocal units
nNotes = self.parseBeatLength(length)
nNotes = [x for x in nNotes] # Need to convert tuple to a list to modify contents
#print("1: {}\n1/2: {}\n1/4: {}\n1/8: {}\n1/16: {}\n1/32: {}\n1/64: {}".format(
# nNotes[0], nNotes[1], nNotes[2], nNotes[3], nNotes[4], nNotes[5], nNotes[6]))
ll = []
candot = False
tieSymbol = "" # This will be populated later (this hack is to work around the dotted note)
if alignBeat != None: # If we're considering the alignment beat,
nBeats = self.parseBeatLength(alignBeat) # Parse the alignment beat count
shortestBeat = 0
for n in range(len(nBeats)): # Find the shortest non-zero beat duration
if nBeats[-(n+1)] > 0: # i.e. if we're on beat 2.5 of 4/4 (alignBeat = 1/4 + 1/4 + 1/8 = 1/2 + 1/8,
shortestBeat = len(nBeats) - (n + 1) # the shortest beat is 1/8
if nNotes[shortestBeat] > 0: # If the note duration includes this shortest beat, let's start with that one
ll.append(str(2**shortestBeat)) # Append it to the lily list
nNotes[shortestBeat] = 0 # Then blank it out of the loop
tieSymbol = self._lilyTie # Set the tie symbol for subsequent items in the list
for n in range(len(nNotes)): # Walk through the breakdown of the note duration, starting from whole notes
if nNotes[n] > 0: # If a duration exists,
if candot: # If the last duration exists, we can dot it!
candot = False # Then we turn off dotting so we don't end up with double-dots
ll.append('.') # (those are silly)
else:
candot = True # The next note can be a dot if present
durationInt = 2**n # 4 for quarter note, 8 for eighth note, etc...
ll.append("{}{}".format(tieSymbol, str(durationInt)))
tieSymbol = self._lilyTie # Once the first symbol has been added to lily list, set the tie symbol
else:
candot = False # The next note cannot be a dot
return "".join(ll)
@staticmethod
def parseBeatLength(length):
"""Parse a beat length into number of whole notes, half notes, quarter notes, etc...
The beat length should be 1/2 or a half note, 1/4 for a quarter note (non-reciprocal
units).
Returns [nWhole, nHalf, nQuarter, n8th, n16th, n32nd, n64th]
Here we're basically just representing a duration in a binary system with 1/64th as
the least-significant (LS) bit and a whole note as the most-significant (MS) bit.
This gives us a 7 digit number (0 to 127).
0 = No duration
1 = 1/64th note
2 = 1/32nd note
4 = 1/16th note
8 = 1/8th note
16 = 1/4 note
32 = 1/2 note
64 = whole note
Any other number is simply a combination of these."""
return [int(x) for x in "{:07b}".format(int(length*64))]
def _getLilyTie(self):
if self.getTie():
return LilySyntax.lilyTie
else:
return ""
def _getLilyDot(self):
if self.getDot():
return LilySyntax.lilyDot
else:
return ""
def asLily(self):
"""Return a string of the GNU lilypad representation of the note."""
n = self.getNoteName()[0].lower()
a = self._getLilyAccidental()
o = self._getLilyOctave()
d = self._getLilyDuration()
s = self._getLilyDot()
t = self._getLilyTie()
return "{}{}{}{}{}{}".format(n, a, o, d, s, t)
def asLilyNoteName(self):
n = self.getNoteLetter().lower()
a = self._getLilyAccidental()
return "{}{}".format(n, a)
def setBeatDuration(self, beatDuration):
dur = _float(beatDuration)
if dur == None:
raise Error_Float("Cannot parse {}".format(beatDuration))
return
self.beatDuration = dur
def getDurationDecomposed(self, reciprocal = True):
nBeats = self.parseBeatLength(self.getDuration())
s = []
for n in range(len(nBeats)):
if nBeats[n]:
m = 2**n
if not reciprocal:
s.append("1/{}".format(m))
else:
s.append(str(m))
return "+".join(s)
def setTempo(self, tempoBPM):
tempo = _float(tempoBPM)
if dur == None:
raise Error_Float("Cannot parse {}".format(tempoBPM))
return
self.tempo = tempoBPM
def durationToMs(self, duration):
if self.beatDuration == None or self.tempo == None:
#_dbg("Unconfigured tempo. beatDuration = {}\ttempo = {}".format(\
# self.beatDuration, self.tempo))
return None
if duration == 0:
_dbg("Invalid duration {}".format(duration))
return None
return (1000 * 60 * self.beatDuration * duration) / self.tempo
def getDuration(self):
"""Return the note duration as a float (i.e. 0.125 = 1/8 for an eighth note)
Includes effect of dotting."""
if self.getDot():
return self.duration*1.5
else:
return self.duration
def getDurationNoDot(self):
"""Return the note duration ignoring dotting."""
return self.duration
def getDurationReciprocal(self):
"""Return the note duration in reciprocol units
(i.e. 1 = whole note, 8 = 1/8th note, 16 = 1/16th note)"""
return 1/self.duration
def setDuration(self, duration):
"""Set the note duration in normal units, i.e. 0.125 = 1/8 for an eighth note.
Tuplets are undetermined as of yet."""
if duration == None:
return False
dur = _float(duration)
if dur == None:
raise Error_Float("Cannot interpret duration {}".format(duration))
return False
else:
self.duration = dur
self.durationMs = self.durationToMs(dur)
return True
def getDurationMs(self):
"""Return the note duration in milliseconds (ms)"""
return self.durationMs
def isEqualNote(self, notestring):
"""Returns true if 'notestring' represents the same note (could be
in a different octave) as self"""
if isinstance(notestring, Note):
noteEncoding = notestring.getEncoding()
else:
note = self.new(notestring)
simpleNote = note.simplify()
noteEncoding = simpleNote.getEncoding()
if noteEncoding == None:
return False
else:
if self.getEncoding() == noteEncoding:
return True
else:
return False
def isEqualOctave(self, notestring):
"""Returns True if 'notestring' represents a note in the same octave as self.
Note that this isn't all that useful outside of Note.isEqualPitch, since the
octave division point is somewhat arbitrary (between B and C)."""
if isinstance(notestring, Note):
octave = notestring.getOctave()
else:
octave = Note._OctaveFromString(notestring)
if octave == self.getOctave():
return True
else:
return False
def isEqualPitch(self, notestring):
"""Returns True only if 'notestring' represents the exact same pitch as self
(note name and octave)."""
if self.isEqualNote(notestring) and self.isEqualOctave(notestring):
return True
else:
return False
def getEncoding(self):
"""Get note name encoding according to class octaveIndex dict"""
noteSimple = self.simplify()
return self.octaveIndices.get(noteSimple.getNoteName().lower(), None)
def simplify(self, notestring = None, sharp = True):
"""Return a simplified enharmonic equivalent note.
I.e. if note = Dbb, note.simplify = C"""
if notestring != None:
note = self.new(notestring)
else:
note = self
b = note.getMIDIByte()
newnote = self.fromMIDIByte(b, sharp = sharp)
newnote.setDuration(note.getDuration())
newnote.setBeatNum(note.getBeatNum())
return newnote
def getInterval(self, note):
"""Return the smallest interval between self and note, ignoring octaves.
If note is higher than self, returns interval > 0"""
note = note.copy() # Shouldn't need this. I believe it passes a copy
note.setOctave(self.getOctave())
selfpitch = self.getMIDIByte()
notepitch = note.getMIDIByte()
interval = notepitch - selfpitch
if abs(interval) < 7:
return interval
else:
if interval < 0:
# Note is too low, go up an octave
sign = 1
else:
# Note is too high, go down an octave
sign = -1
while abs(interval) > 6:
interval += sign*12
return interval
def getNoteByInterval(self, interval, sharp = True):
"""Returns a new Note object that is 'interval' away from self.
'interval' is an integer number of half-steps (+/-).
If sharp == True, default to the sharp (#) enharmonic equivalent
if appropriate. Else, default to the flat (b) equivalent."""
interval = _int(interval)
if interval == None:
raise Error_Interval("Cannot interpret interval {}".format(interval))
return None
midibyte = self.getMIDIByte()
return self.fromMIDIByte(midibyte + interval, sharp = sharp)
@classmethod
def _NoteNameFromString(cls, notestring):
if notestring == None:
return ""
notestring = notestring.strip()
notenames = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
if (len(notestring) < 1):
_dbg("Empty notestring")
return None
if notestring[0].lower() not in notenames:
_dbg("Note name not valid: {}".format(notestring[0]))
return None
if len(notestring) > 1:
n = 1
while n < len(notestring):
t, c = cls._isAccidentalChar(notestring[n])
if not t:
# If we hit a character that's not an accidental char
break
n += 1
return notestring[0].upper() + notestring[1:n].lower()
else:
return notestring[0].upper()
@classmethod
def _NoteNameFromLilyString(cls, lilystring):
if lilystring == None:
return ""
lilystring = lilystring.strip()
notenames = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'r'] # Allow for rest too
if (len(lilystring) < 1):
_dbg("Empty lilystring")
return None
if lilystring[0].lower() not in notenames:
_dbg("Note name not valid: {}".format(lilystring[0]))
return None
if lilystring[0] == 'r':
return 'r'
if len(lilystring) > 1:
accidental = ''
t, c = cls._isAccidentalCharLily(lilystring[1:])
#print("_isAccidentalCharLily: {}, {}".format(t, c))
if t:
accidental = c
return lilystring[0].upper() + accidental
else:
return lilystring[0].upper()
@classmethod
def _DecodeLilyDuration(cls, lilystring):
nl = []
for c in lilystring:
if c.isdigit():
nl.append(c)
invdur = _int(''.join(nl))
if invdur == None:
return 1
return 1/invdur
@classmethod
def _DecodeLilyOctave(cls, lilystring):
octave = LilySyntax.defaultOctave
for c in lilystring:
if c == LilySyntax.octaveUp:
octave += 1
elif c == LilySyntax.octaveDown:
octave -= 1
return octave
@classmethod
def _DecodeLilyTie(cls, lilystring):
if not hasattr(lilystring, '__len__'):
return False
if lilystring == '':
return False
tied = False
for c in lilystring:
if c == LilySyntax.lilyTie[0]:
tied = True
return tied
@classmethod
def _AccidentalFromString(cls, notestring):
"""Parse the accidental from a notestring. E.g.
notestring Accidental
---------- ----------
Ab4 -1
G8 0
C#-1 1
Fbb10 -2
B##6 2
etc..."""
if notestring == None:
return ""
notestring = notestring.strip()
if (len(notestring) < 1):
_dbg("Empty notestring")
return None
if len(notestring) >= 2:
if _isDigit(notestring[1]):
# Accidental not specified; Default to natural
return 0
count = 0
n = 1
while n < len(notestring):
t, c = cls._isAccidentalChar(notestring[n].lower())
if t:
count += c
elif _isDigit(notestring[n]):
break
n += 1
return count
else:
# len(notestring) = 1, no accidental, no octave
return 0
@classmethod
def _OctaveFromString(cls, notestring):
"""Get the octave specified by a notestring.
Returns None on failure. Returns _DEFAULT_OCTAVE if no octave
is specified in notestring"""
if notestring == None:
return ""
notestring = notestring.strip()
index = -1
if len(notestring) <= 1:
# No octave specified
return _DEFAULT_OCTAVE
else:
n = 1
while n < len(notestring):
c = notestring[n]
if _isDigit(c):
break
n += 1
if n < len(notestring):
# A digit has been encountered
index = n
else:
# No octave specified
return _DEFAULT_OCTAVE
oct = _int(notestring[index:])
if oct == None:
_dbg("Cannot interpret octave from notestring {}".format(notestring))
return oct
@classmethod
def _DecodeAccidentalInt(cls, intAccidental):
"""Return the encoding (string) corresponding to a particular integer
accidental. Handles up to infinity sharp and infinity flat."""
intAccidental = _int(intAccidental) # Just in case
if intAccidental == None:
raise Error_Integer("{} cannot be interpreted as an integer".format(intAccidental))
for item in cls.encodingAccidentals.items():
if item[1] == -1:
encMinus = item[0]
elif item[1] == 0:
encZero = item[0]
elif item[1] == 1:
encPlus = item[0]
if intAccidental == 0:
s = encZero
elif intAccidental < 0:
s = encMinus
elif intAccidental > 0:
s = encPlus
return s*abs(intAccidental)
@classmethod
def _DecodeAccidentalString(cls, sAccidental):
"""Return the encoding (integer) corresponding to a particular
string accidental. Handles up to infinity sharp and infinity flat."""
sAcc = sAccidental.strip()
encFlat = cls.encodingAccidentals.get(cls.flatChar, None)
encSharp = cls.encodingAccidentals.get(cls.sharpChar, None)
encNatural = cls.encodingAccidentals.get(cls.naturalChar, None)
if sAcc == cls.naturalChar:
return encNatural
if cls._reFlat.match(sAcc):
return encFlat*len(sAcc)
if cls._reSharp.match(sAcc):
return encSharp*len(sAcc)
@classmethod
def _isAccidentalChar(cls, char):
char = char[0] # Just in case someone tries to pass more than one character
acc = cls.encodingAccidentals.get(char, None)
if acc == None:
f = False
else:
f = True
return (f, acc)
@classmethod
def _isAccidentalCharLily(cls, accstr):
if len(accstr) > 1:
chars = accstr[:2]
else:
return (False, None)
acc = LilySyntax.encodingAccidentals.get(chars, None)
if acc == None:
# No accidental
f = False
else:
# Accidental
accint = LilySyntax._DecodeAccidentalString(accstr) # Get the integer encoding
acc = cls._DecodeAccidentalInt(accint) # Decode as standard syntax (b/#)
f = True
return (f, acc)
@classmethod
def getPitchFromNoteString(cls, noteString):
"""Same as getMIDIByte(), but without creating an object to use the instance method.
Parse 'noteString' and return the integer pitch (based on MIDI standard) corresponding
to this note."""
noteName = cls._NoteNameFromString(noteString)
offset = cls.octaveIndices.get(noteName.lower(), None)
if offset == None:
_dbg("Note.getPitchFromNoteString() offset not found.")
return None
octave = cls._OctaveFromString(noteString)
accidental = cls._AccidentalFromString(noteString)
return 12*(octave + 1) + offset + accidental
def toInteger(self):
"""A synonym for 'getMIDIByte' which is more intuitive in most use cases."""
return self.getMIDIByte()
def getMIDIByte(self):
"""Get the corresponding MIDI number representing the pitch"""
offset = self.octaveIndices.get(self.getNoteName()[0].lower(), None)
if offset == None:
_dbg("Note.getMIDIByte() offset not found.")
return None
return 12*(self.getOctave() + 1) + offset + self.getAccidental()
@classmethod
def fromInteger(cls, integer, duration = None, sharp = True):
"""A synonym for 'fromMIDIByte' which is more intuitive in most use cases."""
return cls.fromMIDIByte(integer, duration, sharp)
@classmethod
def fromMIDIByte(cls, midibyte, duration = None, sharp = True):
"""Return a Note object represented by integer 'midibyte'
If sharp == True, default to the sharp (#) enharmonic equivalent
if appropriate. Else, default to the flat (b) equivalent."""
midibyte = _int(midibyte) # Convert to an integer, just in case
if midibyte == None:
raise Error_MIDI_Byte("Cannot interpret {}".format(midibyte))
octave = midibyte//12 - 1
offset = midibyte%12
noteMatch = None
nextHighest = None
nextLowest = None
accidental = None
accstring = None
for note in cls.noteOrder:
noffset = cls.octaveIndices[note]
if offset == noffset:
noteMatch = note
elif offset == noffset + 1:
nextLowest = note
elif offset == noffset - 1:
nextHighest = note
if noteMatch == None:
# We're looking at a black key
if sharp:
noteName = nextLowest # start with lower, then sharp it
accidental = 1
else:
noteName = nextHighest # start with higher, then flat it
accidental = -1
else:
noteName = noteMatch
accidental = 0
for key in cls.encodingAccidentals:
if accidental == cls.encodingAccidentals[key]:
accstring = key
notestring = "{}{}{}".format(noteName.upper(), accstring, str(octave))
return Note(notestring, duration)
def getEnharmonicEquivalent(self, steps):
"""Get an enharmonic equivalent note with base shifted by 'steps' steps.
Does not support multiple-octave offsets (e.g. abs(steps) > 7), so don't
be an asshole.
E.g.:
note steps result
---- ----- ------
C -2 A###
C +2 Ebbbb
Eb +1 Fbb
Eb -3 B####"""
noteBase, accidental, octave = self.getNote()
noteBase = noteBase.lower()
#print("org: {} {} {}".format(noteBase, accidental, octave))
baseIndex = self.noteOrder.index(noteBase)
baseOffset = self.octaveIndices.get(noteBase)
newBase = self.noteOrder[baseIndex + steps]
newOffset = self.octaveIndices.get(newBase)
#print("baseOffset = {}; newOffset = {}".format(baseOffset, newOffset))
#dSteps = min((newOffset - baseOffset)%12, (baseOffset - newOffset)%12)
doct = 0
if steps >= 0:
#dSteps = (baseOffset - newOffset)
if newOffset < baseOffset:
doct = 1
else:
#dSteps = (newOffset - baseOffset)
if newOffset > baseOffset:
doct = -1
dSteps = -(newOffset + 12*doct - baseOffset)
#print("dSteps = {}".format(dSteps))
newAccidental = accidental + dSteps
# How figure out octave?
newOctave = octave + doct
#print("new: {} {} {}".format(newBase, newAccidental, newOctave))
newnote = self.new(newBase)
newnote.setAccidental(newAccidental)
newnote.setOctave(newOctave)
newnote.copyRhythmParams(self)
return newnote
def getNoteString(self):
self.noteString = self.getNoteLetter() + self.getAccidentalString() + str(self.getOctave())
return self.noteString
def copy(self):
newnote = self.new(self.noteString, self.duration)
newnote.setOctave(self.getOctave())
return newnote
def copyRhythmParams(self, note):
"""Set this note's rhythmic parameters to those of 'note'.
These include:
duration
beatNum
tie
dot
"""
self.setDuration(note.getDurationNoDot())
self.setBeatNum(note.getBeatNum())
self.setTie(note.getTie())
self.setDot(note.getDot())
return
def alter(self, interval):
"""Similar to getNoteByInterval() except simply alters the note further.
I.e. if note = C#, note.getNoteByInterval(1) = D, note.alter(1) = C##
I.e. if note = Bb, note.getNoteByInterval(-2) = Ab, note.alter(-2) = Bbbb"""
acc = self.getAccidental()
sAcc = self.getAccidentalString(acc + _int(interval))
noteName = self.getNoteLetter()
return self.new(noteName + sAcc, self.getDuration())
def sharp(self):
"""Return a copy of the note sharped E.g.:
if note = C, note.sharp() = C#
if note = Eb, note.sharp() = E
if note = G#, note.sharp() = G##"""
return self.alter(1)
def flat(self):
"""Return a copy of the note flatted E.g.:
if note = C, note.flat() = Cb
if note = Eb, note.flat() = Ebb
if note = G#, note.flat() = G"""
return self.alter(-1)
def getTie(self):
"""Returns True if this note is tied to the subsequent note.
Only meaningful in sheet music."""
return self.isTied
def setTie(self, tie):
"""Set the 'isTied' flag to declare that this note is tied
to a subsequent note (only meaningful in sheet music)."""
if tie == None:
return
if tie:
self.isTied = True
else:
self.isTied = False
def setDot(self, dotted):
if dotted:
self.dotted = True
else:
self.dotted = False
def getDot(self):
return self.dotted
def setBeatNum(self, beatNum):
beatNum = _float(beatNum) # Sanitize input
if beatNum == None:
return False
self.beatNum = beatNum
return True
def getBeatNum(self):
return self.beatNum
def setMeasureNum(self, measureNum):
measureNum = _int(measureNum) # Sanitize input
if measureNum == None:
return False
self.measureNum = measureNum
return True
def getMeasureNum(self):
return self.measureNum
@classmethod
def new(cls, notestring, duration = None):