Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 9 additions & 17 deletions music21/analysis/reduceChords.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,11 @@ def collapseArpeggios(self, scoreTree):
elif one.measureNumber != two.measureNumber:
continue

# is this used?
bothPitches = set()
bothPitches.update([x.nameWithOctave for x in onePitches])
bothPitches.update([x.nameWithOctave for x in twoPitches])
bothPitches = sorted([pitch.Pitch(x) for x in bothPitches])
# # is this used?
# bothPitches = set()
# bothPitches.update([x.nameWithOctave for x in onePitches])
# bothPitches.update([x.nameWithOctave for x in twoPitches])
# bothPitches = sorted([pitch.Pitch(x) for x in bothPitches])

# if not timespanStream.Verticality.pitchesAreConsonant(bothPitches):
# intervalClasses = self._getIntervalClassSet(bothPitches)
Expand Down Expand Up @@ -359,10 +359,7 @@ def computeMeasureChordWeights(
self.numberOfElementsInMeasure = len(measureObject)
for i, c in enumerate(measureObject):
self.positionInMeasure = i
if c.isNote:
p = tuple(c.pitch.pitchClass)
else:
p = tuple({x.pitchClass for x in c.pitches})
p = tuple({x.pitchClass for x in c.pitches})
if p not in presentPCs:
presentPCs[p] = 0.0
presentPCs[p] += weightAlgorithm(c)
Expand Down Expand Up @@ -582,13 +579,8 @@ def reduceMeasureToNChords(
currentGreedyChord = None
currentGreedyChordPCs = None
currentGreedyChordNewLength = 0.0
for c in measureObject:
if isinstance(c, note.Note):
p = tuple(c.pitch.pitchClass)
elif isinstance(c, chord.Chord):
p = tuple({x.pitchClass for x in c.pitches})
else:
continue
for c in measureObject.getElementsByClass(note.NotRest):
p = tuple({x.pitchClass for x in c.pitches})
if p in trimmedMaxChords and p != currentGreedyChordPCs:
# keep this chord
if currentGreedyChord is None and c.offset != 0.0:
Expand All @@ -609,7 +601,7 @@ def reduceMeasureToNChords(
measureObject.remove(c)
if currentGreedyChord is not None:
currentGreedyChord.quarterLength = currentGreedyChordNewLength
# not read later - no need to cleanup.
# not read later - no need to clean up.
# currentGreedyChordNewLength = 0.0
# even chord lengths
for i in range(1, len(measureObject)):
Expand Down
13 changes: 4 additions & 9 deletions music21/analysis/reduceChordsOld.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def reduceMeasureToNChords(self,
weightAlgorithm=None,
trimBelow=0.25):
'''
Takes a measure and reduces it to only one chord or `numChords` chords.

>>> s = analysis.reduceChordsOld.testMeasureStream1()
>>> cr = analysis.reduceChordsOld.ChordReducer()
Expand Down Expand Up @@ -110,10 +111,7 @@ def reduceMeasureToNChords(self,
currentGreedyChordPCs = None
currentGreedyChordNewLength = 0.0
for c in mObj:
if c.isNote:
p = tuple(c.pitch.pitchClass)
else:
p = tuple({x.pitchClass for x in c.pitches})
p = tuple({x.pitchClass for x in c.pitches})
if p in trimmedMaxChords and p != currentGreedyChordPCs:
# keep this chord
if currentGreedyChord is None and c.offset != 0.0:
Expand Down Expand Up @@ -200,10 +198,7 @@ def computeMeasureChordWeights(self, measureObj, weightAlgorithm=None):

for i, c in enumerate(measureObj):
self.positionInMeasure = i
if c.isNote:
p = tuple(c.pitch.pitchClass)
else:
p = tuple({x.pitchClass for x in c.pitches})
p = tuple({x.pitchClass for x in c.pitches})
if p not in presentPCs:
presentPCs[p] = 0.0
presentPCs[p] += weightAlgorithm(c)
Expand Down Expand Up @@ -317,7 +312,7 @@ def reduceThisMeasure(self, mI, measureIndex, maxChords, closedPosition, forceOc
for pitchI in range(len(self._lastPitchedObject)):
if self._lastPitchedObject.pitches[pitchI] != firstPitched.pitches[pitchI]:
allSame = False
if allSame is True:
if allSame:
self._lastPitchedObject.tie = tie.Tie('start')
self._lastPitchedObject = m[-1]

Expand Down
18 changes: 6 additions & 12 deletions music21/clef.py
Original file line number Diff line number Diff line change
Expand Up @@ -967,27 +967,21 @@ def findHeight(pInner):
return height
# environLocal.printDebug(['calling bestClef()'])

totalNotes = 0
totalPitches = 0
totalHeight = 0

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

notes = sIter.notesAndRests

for n in notes:
if n.isRest:
pass
elif n.isNote:
totalNotes += 1
totalHeight += findHeight(n.pitch)
elif n.isChord:
for p in n.pitches:
totalNotes += 1
totalHeight += findHeight(p)
if totalNotes == 0:
for p in n.pitches:
totalPitches += 1
totalHeight += findHeight(p)
if totalPitches == 0:
averageHeight = 29.0
else:
averageHeight = totalHeight / totalNotes
averageHeight = totalHeight / totalPitches

# environLocal.printDebug(['average height', averageHeight])
if averageHeight > 49: # value found with experimentation; revise
Expand Down
9 changes: 4 additions & 5 deletions music21/omr/correctors.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,11 +761,10 @@ def getHashString(self):
hashString += self.hashGrace(n)
elif n.isNote:
hashString += self.hashNote(n)
elif not n.isNote:
if n.isRest:
hashString += self.hashRest(n)
elif n.isChord:
hashString += self.hashNote(n)
elif n.isRest:
hashString += self.hashRest(n)
elif n.isChord:
hashString += self.hashNote(n)
self.hashString = hashString
return hashString

Expand Down
38 changes: 18 additions & 20 deletions music21/search/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ def translateStreamToString(inputStreamOrIterator, returnMeasures=False):
b += translateNoteWithDurationToBytes(n)
if returnMeasures:
measures.append(n.measureNumber)
if returnMeasures is False:
if not returnMeasures:
return b
else:
return (b, measures)
Expand Down Expand Up @@ -750,7 +750,7 @@ def translateDiatonicStreamToString(inputStreamOrIterator, returnMeasures=False)
mNum = n.measureNumber

if n.isRest:
if previousRest is True:
if previousRest:
continue
else:
previousRest = True
Expand All @@ -759,7 +759,7 @@ def translateDiatonicStreamToString(inputStreamOrIterator, returnMeasures=False)
continue
else:
previousRest = False
if previousTie is True:
if previousTie:
if n.tie is None or n.tie.type == 'stop':
previousTie = False
continue
Expand All @@ -778,7 +778,7 @@ def translateDiatonicStreamToString(inputStreamOrIterator, returnMeasures=False)
b.append(newName)

joined = ''.join(b)
if returnMeasures is False:
if not returnMeasures:
return joined
else:
return (joined, measures)
Expand Down Expand Up @@ -822,21 +822,20 @@ def translateIntervalsAndSpeed(inputStream, returnMeasures=False):
b = []
measures = []

previousRest = False
previousTie = False
previousRest = False # was the previous a Rest
previousTie = False # was the previous a Tie?
previousQL = None
previousMidi = 60
for n in inputStream:
if n.isNote:
previousMidi = n.pitches[0].midi
break
for n in inputStream.getElementsByClass(note.Note):
previousMidi = n.pitches[0].midi
break

for n in inputStream:
mNum = None
if returnMeasures:
mNum = n.measureNumber
if n.isRest:
if previousRest is True:
if previousRest:
continue
else:
previousRest = True
Expand All @@ -845,7 +844,7 @@ def translateIntervalsAndSpeed(inputStream, returnMeasures=False):
continue
else:
previousRest = False
if previousTie is True:
if previousTie:
if n.tie is None or n.tie.type == 'stop':
previousTie = False
continue
Expand All @@ -870,7 +869,7 @@ def translateIntervalsAndSpeed(inputStream, returnMeasures=False):
b.append(newName)

joined = ''.join(b)
if returnMeasures is False:
if not returnMeasures:
return joined
else:
return (joined, measures)
Expand Down Expand Up @@ -928,10 +927,10 @@ def translateStreamToStringOnlyRhythm(inputStream, returnMeasures=False):
return b


def translateNoteToByte(n: note.GeneralNote):
def translateNoteToByte(n: note.GeneralNote) -> str:
# noinspection PyShadowingNames
'''
takes a note.Note object and translates it to a single byte representation.
takes a note.Note object and translates it to a single byte representation in a string.

currently returns the chr() for the note's midi number. or chr(127) for rests
and unpitched.
Expand All @@ -957,10 +956,10 @@ def translateNoteToByte(n: note.GeneralNote):
else:
return chr(127)

def translateNoteWithDurationToBytes(n: note.GeneralNote, includeTieByte=True):
def translateNoteWithDurationToBytes(n: note.GeneralNote, includeTieByte: bool = True) -> str:
# noinspection PyShadowingNames
'''
takes a note.Note object and translates it to a three-byte representation.
takes a note.Note object and translates it to a three-byte representation as a string.

currently returns the chr() for the note's midi number. or chr(127) for rests
followed by the log of the quarter length (fitted to 1-127, see
Expand All @@ -987,7 +986,7 @@ def translateNoteWithDurationToBytes(n: note.GeneralNote, includeTieByte=True):
firstByte = translateNoteToByte(n)
secondByte = translateDurationToBytes(n)
thirdByte = translateNoteTieToByte(n)
if includeTieByte is True:
if includeTieByte:
return firstByte + secondByte + thirdByte
else:
return firstByte + secondByte
Expand Down Expand Up @@ -1098,7 +1097,6 @@ def mostCommonMeasureRhythms(streamIn, transposeDiatonic=False):
* Changed in v7: bars are ordered first by number, then by part.
'''
returnDicts = []
distanceToTranspose = 0

for thisMeasure in streamIn[Measure]:
rhythmString = translateStreamToStringOnlyRhythm(thisMeasure.notesAndRests)
Expand All @@ -1109,7 +1107,7 @@ def mostCommonMeasureRhythms(streamIn, transposeDiatonic=False):
entry['number'] += 1
entry['measures'].append(thisMeasure)
break
if rhythmFound is False:
if not rhythmFound:
newDict = {
'number': 1,
'rhythmString': rhythmString,
Expand Down