def getMeasureByPosition(self, position): if position.staffIndex is None or position.measureIndex is None: raise BadTimeError() if not (0 <= position.staffIndex < len(self._staffs)): raise BadTimeError() staff = self.getStaffByIndex(position.staffIndex) if not (0 <= position.measureIndex < staff.numMeasures()): raise BadTimeError() return staff[position.measureIndex]
def _staffContainingMeasure(self, index): measuresSoFar = 0 for staff in self.iterStaffs(): if measuresSoFar <= index < measuresSoFar + staff.numMeasures(): return staff, index - measuresSoFar measuresSoFar += staff.numMeasures() raise BadTimeError()
def nthVisibleLineIndex(self, staffIndex, lineIndex): count = -1 staff = self.getStaffByIndex(staffIndex) for lineNum, drum in enumerate(self.drumKit): if (drum.locked or self.scoreData.emptyLinesVisible or staff.lineIsVisible(lineNum)): count += 1 if count == lineIndex: return lineNum if count == -1 and lineIndex == 0: return 0 raise BadTimeError(staffIndex)
def measureIndexToPosition(self, index): staffIndex = 0 staff = self.getStaffByIndex(0) while index >= staff.numMeasures(): index -= staff.numMeasures() staffIndex += 1 if staffIndex == self.numStaffs(): break staff = self.getStaffByIndex(staffIndex) if staffIndex == self.numStaffs(): raise BadTimeError(index) return NotePosition(staffIndex=staffIndex, measureIndex=index)
def iterMeasuresInSection(self, sectionIndex): if not (0 <= sectionIndex < self.numSections()): raise BadTimeError() thisSection = 0 inSection = (thisSection == sectionIndex) for measure in self.iterMeasures(): if inSection: yield measure if measure.isSectionEnd(): break if measure.isSectionEnd(): thisSection += 1 inSection = (thisSection == sectionIndex)
def nextMeasure(self, position): self._checkStaffIndex(position.staffIndex) staff = self.getStaffByIndex(position.staffIndex) if not (0 <= position.measureIndex < staff.numMeasures()): raise BadTimeError() position = position.makeMeasurePosition() position.measureIndex += 1 if position.measureIndex == staff.numMeasures(): position.staffIndex += 1 if position.staffIndex == self.numStaffs(): position.staffIndex = None position.measureIndex = None else: position.measureIndex = 0 return position
def _isValidPosition(self, position, afterOk=False): if not (0 <= position.measureIndex < self.numMeasures()): if not (afterOk and position.measureIndex == self.numMeasures()): raise BadTimeError(position)
def noteAt(self, noteTime, drumIndex): self._checkValidNoteTime(noteTime) if drumIndex < 0: raise BadTimeError() return self._notes.getNote(noteTime, drumIndex)
def _checkValidNoteTime(self, noteTime): if not (0 <= noteTime < len(self)): raise BadTimeError(noteTime)
def _checkMeasureIndex(self, index, endOk=False): if not (0 <= index < self.numMeasures()): if not (endOk and index == self.numMeasures()): raise BadTimeError()
def _checkDrumIndex(self, index): if not (0 <= index < len(self.drumKit)): raise BadTimeError()
def _checkStaffIndex(self, index): if not (0 <= index < self.numStaffs()): raise BadTimeError()