Skip to content

Commit 8b7cf63

Browse files
authored
Merge pull request #1842 from cuthbertLab/tuple-int-fix
ReduceChords -- work on notes in streams
2 parents 62516b2 + 591d16e commit 8b7cf63

File tree

5 files changed

+41
-63
lines changed

5 files changed

+41
-63
lines changed

music21/analysis/reduceChords.py

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -276,11 +276,11 @@ def collapseArpeggios(self, scoreTree):
276276
elif one.measureNumber != two.measureNumber:
277277
continue
278278

279-
# is this used?
280-
bothPitches = set()
281-
bothPitches.update([x.nameWithOctave for x in onePitches])
282-
bothPitches.update([x.nameWithOctave for x in twoPitches])
283-
bothPitches = sorted([pitch.Pitch(x) for x in bothPitches])
279+
# # is this used?
280+
# bothPitches = set()
281+
# bothPitches.update([x.nameWithOctave for x in onePitches])
282+
# bothPitches.update([x.nameWithOctave for x in twoPitches])
283+
# bothPitches = sorted([pitch.Pitch(x) for x in bothPitches])
284284

285285
# if not timespanStream.Verticality.pitchesAreConsonant(bothPitches):
286286
# intervalClasses = self._getIntervalClassSet(bothPitches)
@@ -359,10 +359,7 @@ def computeMeasureChordWeights(
359359
self.numberOfElementsInMeasure = len(measureObject)
360360
for i, c in enumerate(measureObject):
361361
self.positionInMeasure = i
362-
if c.isNote:
363-
p = tuple(c.pitch.pitchClass)
364-
else:
365-
p = tuple({x.pitchClass for x in c.pitches})
362+
p = tuple({x.pitchClass for x in c.pitches})
366363
if p not in presentPCs:
367364
presentPCs[p] = 0.0
368365
presentPCs[p] += weightAlgorithm(c)
@@ -582,13 +579,8 @@ def reduceMeasureToNChords(
582579
currentGreedyChord = None
583580
currentGreedyChordPCs = None
584581
currentGreedyChordNewLength = 0.0
585-
for c in measureObject:
586-
if isinstance(c, note.Note):
587-
p = tuple(c.pitch.pitchClass)
588-
elif isinstance(c, chord.Chord):
589-
p = tuple({x.pitchClass for x in c.pitches})
590-
else:
591-
continue
582+
for c in measureObject.getElementsByClass(note.NotRest):
583+
p = tuple({x.pitchClass for x in c.pitches})
592584
if p in trimmedMaxChords and p != currentGreedyChordPCs:
593585
# keep this chord
594586
if currentGreedyChord is None and c.offset != 0.0:
@@ -609,7 +601,7 @@ def reduceMeasureToNChords(
609601
measureObject.remove(c)
610602
if currentGreedyChord is not None:
611603
currentGreedyChord.quarterLength = currentGreedyChordNewLength
612-
# not read later - no need to cleanup.
604+
# not read later - no need to clean up.
613605
# currentGreedyChordNewLength = 0.0
614606
# even chord lengths
615607
for i in range(1, len(measureObject)):

music21/analysis/reduceChordsOld.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def reduceMeasureToNChords(self,
6363
weightAlgorithm=None,
6464
trimBelow=0.25):
6565
'''
66+
Takes a measure and reduces it to only one chord or `numChords` chords.
6667
6768
>>> s = analysis.reduceChordsOld.testMeasureStream1()
6869
>>> cr = analysis.reduceChordsOld.ChordReducer()
@@ -110,10 +111,7 @@ def reduceMeasureToNChords(self,
110111
currentGreedyChordPCs = None
111112
currentGreedyChordNewLength = 0.0
112113
for c in mObj:
113-
if c.isNote:
114-
p = tuple(c.pitch.pitchClass)
115-
else:
116-
p = tuple({x.pitchClass for x in c.pitches})
114+
p = tuple({x.pitchClass for x in c.pitches})
117115
if p in trimmedMaxChords and p != currentGreedyChordPCs:
118116
# keep this chord
119117
if currentGreedyChord is None and c.offset != 0.0:
@@ -200,10 +198,7 @@ def computeMeasureChordWeights(self, measureObj, weightAlgorithm=None):
200198

201199
for i, c in enumerate(measureObj):
202200
self.positionInMeasure = i
203-
if c.isNote:
204-
p = tuple(c.pitch.pitchClass)
205-
else:
206-
p = tuple({x.pitchClass for x in c.pitches})
201+
p = tuple({x.pitchClass for x in c.pitches})
207202
if p not in presentPCs:
208203
presentPCs[p] = 0.0
209204
presentPCs[p] += weightAlgorithm(c)
@@ -317,7 +312,7 @@ def reduceThisMeasure(self, mI, measureIndex, maxChords, closedPosition, forceOc
317312
for pitchI in range(len(self._lastPitchedObject)):
318313
if self._lastPitchedObject.pitches[pitchI] != firstPitched.pitches[pitchI]:
319314
allSame = False
320-
if allSame is True:
315+
if allSame:
321316
self._lastPitchedObject.tie = tie.Tie('start')
322317
self._lastPitchedObject = m[-1]
323318

music21/clef.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -967,27 +967,21 @@ def findHeight(pInner):
967967
return height
968968
# environLocal.printDebug(['calling bestClef()'])
969969

970-
totalNotes = 0
970+
totalPitches = 0
971971
totalHeight = 0
972972

973973
sIter = streamObj.recurse() if recurse else streamObj.iter()
974974

975975
notes = sIter.notesAndRests
976976

977977
for n in notes:
978-
if n.isRest:
979-
pass
980-
elif n.isNote:
981-
totalNotes += 1
982-
totalHeight += findHeight(n.pitch)
983-
elif n.isChord:
984-
for p in n.pitches:
985-
totalNotes += 1
986-
totalHeight += findHeight(p)
987-
if totalNotes == 0:
978+
for p in n.pitches:
979+
totalPitches += 1
980+
totalHeight += findHeight(p)
981+
if totalPitches == 0:
988982
averageHeight = 29.0
989983
else:
990-
averageHeight = totalHeight / totalNotes
984+
averageHeight = totalHeight / totalPitches
991985

992986
# environLocal.printDebug(['average height', averageHeight])
993987
if averageHeight > 49: # value found with experimentation; revise

music21/omr/correctors.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -761,11 +761,10 @@ def getHashString(self):
761761
hashString += self.hashGrace(n)
762762
elif n.isNote:
763763
hashString += self.hashNote(n)
764-
elif not n.isNote:
765-
if n.isRest:
766-
hashString += self.hashRest(n)
767-
elif n.isChord:
768-
hashString += self.hashNote(n)
764+
elif n.isRest:
765+
hashString += self.hashRest(n)
766+
elif n.isChord:
767+
hashString += self.hashNote(n)
769768
self.hashString = hashString
770769
return hashString
771770

music21/search/base.py

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ def translateStreamToString(inputStreamOrIterator, returnMeasures=False):
698698
b += translateNoteWithDurationToBytes(n)
699699
if returnMeasures:
700700
measures.append(n.measureNumber)
701-
if returnMeasures is False:
701+
if not returnMeasures:
702702
return b
703703
else:
704704
return (b, measures)
@@ -750,7 +750,7 @@ def translateDiatonicStreamToString(inputStreamOrIterator, returnMeasures=False)
750750
mNum = n.measureNumber
751751

752752
if n.isRest:
753-
if previousRest is True:
753+
if previousRest:
754754
continue
755755
else:
756756
previousRest = True
@@ -759,7 +759,7 @@ def translateDiatonicStreamToString(inputStreamOrIterator, returnMeasures=False)
759759
continue
760760
else:
761761
previousRest = False
762-
if previousTie is True:
762+
if previousTie:
763763
if n.tie is None or n.tie.type == 'stop':
764764
previousTie = False
765765
continue
@@ -778,7 +778,7 @@ def translateDiatonicStreamToString(inputStreamOrIterator, returnMeasures=False)
778778
b.append(newName)
779779

780780
joined = ''.join(b)
781-
if returnMeasures is False:
781+
if not returnMeasures:
782782
return joined
783783
else:
784784
return (joined, measures)
@@ -822,21 +822,20 @@ def translateIntervalsAndSpeed(inputStream, returnMeasures=False):
822822
b = []
823823
measures = []
824824

825-
previousRest = False
826-
previousTie = False
825+
previousRest = False # was the previous a Rest
826+
previousTie = False # was the previous a Tie?
827827
previousQL = None
828828
previousMidi = 60
829-
for n in inputStream:
830-
if n.isNote:
831-
previousMidi = n.pitches[0].midi
832-
break
829+
for n in inputStream.getElementsByClass(note.Note):
830+
previousMidi = n.pitches[0].midi
831+
break
833832

834833
for n in inputStream:
835834
mNum = None
836835
if returnMeasures:
837836
mNum = n.measureNumber
838837
if n.isRest:
839-
if previousRest is True:
838+
if previousRest:
840839
continue
841840
else:
842841
previousRest = True
@@ -845,7 +844,7 @@ def translateIntervalsAndSpeed(inputStream, returnMeasures=False):
845844
continue
846845
else:
847846
previousRest = False
848-
if previousTie is True:
847+
if previousTie:
849848
if n.tie is None or n.tie.type == 'stop':
850849
previousTie = False
851850
continue
@@ -870,7 +869,7 @@ def translateIntervalsAndSpeed(inputStream, returnMeasures=False):
870869
b.append(newName)
871870

872871
joined = ''.join(b)
873-
if returnMeasures is False:
872+
if not returnMeasures:
874873
return joined
875874
else:
876875
return (joined, measures)
@@ -928,10 +927,10 @@ def translateStreamToStringOnlyRhythm(inputStream, returnMeasures=False):
928927
return b
929928

930929

931-
def translateNoteToByte(n: note.GeneralNote):
930+
def translateNoteToByte(n: note.GeneralNote) -> str:
932931
# noinspection PyShadowingNames
933932
'''
934-
takes a note.Note object and translates it to a single byte representation.
933+
takes a note.Note object and translates it to a single byte representation in a string.
935934
936935
currently returns the chr() for the note's midi number. or chr(127) for rests
937936
and unpitched.
@@ -957,10 +956,10 @@ def translateNoteToByte(n: note.GeneralNote):
957956
else:
958957
return chr(127)
959958

960-
def translateNoteWithDurationToBytes(n: note.GeneralNote, includeTieByte=True):
959+
def translateNoteWithDurationToBytes(n: note.GeneralNote, includeTieByte: bool = True) -> str:
961960
# noinspection PyShadowingNames
962961
'''
963-
takes a note.Note object and translates it to a three-byte representation.
962+
takes a note.Note object and translates it to a three-byte representation as a string.
964963
965964
currently returns the chr() for the note's midi number. or chr(127) for rests
966965
followed by the log of the quarter length (fitted to 1-127, see
@@ -987,7 +986,7 @@ def translateNoteWithDurationToBytes(n: note.GeneralNote, includeTieByte=True):
987986
firstByte = translateNoteToByte(n)
988987
secondByte = translateDurationToBytes(n)
989988
thirdByte = translateNoteTieToByte(n)
990-
if includeTieByte is True:
989+
if includeTieByte:
991990
return firstByte + secondByte + thirdByte
992991
else:
993992
return firstByte + secondByte
@@ -1098,7 +1097,6 @@ def mostCommonMeasureRhythms(streamIn, transposeDiatonic=False):
10981097
* Changed in v7: bars are ordered first by number, then by part.
10991098
'''
11001099
returnDicts = []
1101-
distanceToTranspose = 0
11021100

11031101
for thisMeasure in streamIn[Measure]:
11041102
rhythmString = translateStreamToStringOnlyRhythm(thisMeasure.notesAndRests)
@@ -1109,7 +1107,7 @@ def mostCommonMeasureRhythms(streamIn, transposeDiatonic=False):
11091107
entry['number'] += 1
11101108
entry['measures'].append(thisMeasure)
11111109
break
1112-
if rhythmFound is False:
1110+
if not rhythmFound:
11131111
newDict = {
11141112
'number': 1,
11151113
'rhythmString': rhythmString,

0 commit comments

Comments
 (0)