def conditionalAdd(ts, n: note.Note) -> None: ''' Add an element only if it is not already in the chord. If it has more tie information than the previously added note, then remove the previously added note and add it ''' from music21 import stream nonlocal pitchBust # love Py3!!! p = n.pitch pitchKey = p.nameWithOctave pitchGroup = None if addPartIdAsGroup: partContext = n.getContextByClass(stream.Part) if partContext is not None: pidStr = str(partContext.id) pitchGroup = pidStr.replace( ' ', '_') # spaces are not allowed as group names n.pitch.groups.append(pitchGroup) n.groups.append(pitchGroup) if pitchKey not in seenPitches: seenPitches.add(pitchKey) notesToAdd[pitchKey] = newNote(ts, n) return elif not removeRedundantPitches: notesToAdd[pitchKey + str(pitchBust)] = newNote(ts, n) pitchBust += 1 return elif addPartIdAsGroup and pitchGroup is not None: notesToAdd[pitchKey].groups.append(pitchGroup) notesToAdd[pitchKey].pitch.groups.append(pitchGroup) if not addTies: return # else add derivation once multiple derivations are allowed. oldNoteTie = notesToAdd[pitchKey].tie if oldNoteTie is not None and oldNoteTie.type == 'continue': return # previous note was as good or better possibleNewNote = newNote(ts, n) possibleNewNote.groups = notesToAdd[pitchKey].groups if possibleNewNote.tie is None: return # do nothing elif oldNoteTie is None: notesToAdd[pitchKey] = possibleNewNote # a better note to add elif {oldNoteTie.type, possibleNewNote.tie.type} == startStopSet: notesToAdd[pitchKey].tie.type = 'continue' elif possibleNewNote.tie.type == 'continue': notesToAdd[pitchKey] = possibleNewNote # a better note to add elif possibleNewNote.tie.type == oldNoteTie.type: return else: raise VerticalityException('Did I miss one? ', possibleNewNote.tie, oldNoteTie)