def insertMeasureByPosition(self, width, position=None, counter=None): if position is None: if self.numStaffs() == 0: self._addStaff() position = NotePosition(self.numStaffs() - 1) staff = self.getStaff(self.numStaffs() - 1) position.measureIndex = staff.numMeasures() self._checkStaffIndex(position.staffIndex) newMeasure = Measure(width) newMeasure.counter = counter staff = self.getStaff(position.staffIndex) staff.insertMeasure(position, newMeasure) return newMeasure
def insertMeasureByPosition(self, width, position = None, counter = None): if position is None: if self.numStaffs() == 0: self._addStaff() position = NotePosition(self.numStaffs() - 1) staff = self.getStaff(self.numStaffs() - 1) position.measureIndex = staff.numMeasures() self._checkStaffIndex(position.staffIndex) newMeasure = Measure(width) newMeasure.counter = counter staff = self.getStaff(position.staffIndex) staff.insertMeasure(position, newMeasure) return newMeasure
def _setrepeatCount(self, value): if value != self._info.repeatCount: if self.isRepeatEnd(): self._info.repeatCount = max(value, 2) else: self._info.repeatCount = 1 self._runCallBack(NotePosition())
def trailingEmptyMeasures(self): emptyMeasures = [] np = NotePosition(staffIndex = self.numStaffs() - 1) staff = self.getItemAtPosition(np) np.measureIndex = staff.numMeasures() - 1 measure = self.getItemAtPosition(np) while ((np.staffIndex > 0 or np.measureIndex > 0) and measure.isEmpty()): # pylint:disable-msg=E1103 emptyMeasures.append(np.makeMeasurePosition()) if np.measureIndex == 0: np.staffIndex -= 1 staff = self.getStaff(np.staffIndex) np.measureIndex = staff.numMeasures() np.measureIndex -= 1 measure = self.getItemAtPosition(np) return emptyMeasures
def trailingEmptyMeasures(self): emptyMeasures = [] np = NotePosition(staffIndex=self.numStaffs() - 1) staff = self.getItemAtPosition(np) np.measureIndex = staff.numMeasures() - 1 measure = self.getItemAtPosition(np) while ((np.staffIndex > 0 or np.measureIndex > 0) and measure.isEmpty()): # pylint:disable-msg=E1103 emptyMeasures.append(np.makeMeasurePosition()) if np.measureIndex == 0: np.staffIndex -= 1 staff = self.getStaff(np.staffIndex) np.measureIndex = staff.numMeasures() np.measureIndex -= 1 measure = self.getItemAtPosition(np) return emptyMeasures
def _deleteStaffByIndex(self, index): self._checkStaffIndex(index) staff = self._staffs[index] staff.clearCallBack() if staff.isSectionEnd(): if index == 0 or self.getStaff(index - 1).isSectionEnd(): position = NotePosition(staffIndex=index) sectionIndex = self.getSectionIndex(position) self._deleteSectionTitle(sectionIndex) else: prevStaff = self.getStaff(index - 1) position = NotePosition(staffIndex=index - 1, measureIndex=prevStaff.numMeasures() - 1) prevStaff.setSectionEnd(position, True) self._staffs.pop(index) for offset, nextStaff in enumerate(self._staffs[index:]): self._setStaffCallBack(nextStaff, index + offset)
def _setWidth(self, newWidth): assert (newWidth > 0) if newWidth == len(self): return self._width = newWidth badTimes = [ noteTime for noteTime in self._notes.iterTimes() if noteTime >= self._width ] for badTime in badTimes: self._notes.deleteAllNotesAtTime(badTime) self._runCallBack(NotePosition())
def getMeasurePosition(self, index): staffIndex = 0 staff = self.getStaff(0) while index >= staff.numMeasures(): index -= staff.numMeasures() staffIndex += 1 if staffIndex == self.numStaffs(): break staff = self.getStaff(staffIndex) if staffIndex == self.numStaffs(): raise BadTimeError(index) return NotePosition(staffIndex=staffIndex, measureIndex=index)
def iterMeasuresBetween(self, start, end): if self.getMeasureIndex(end) < self.getMeasureIndex(start): start, end = end, start staffIndex = start.staffIndex measureIndex = start.measureIndex absIndex = self.getMeasureIndex(start) while staffIndex < end.staffIndex: staff = self.getStaff(staffIndex) while measureIndex < staff.numMeasures(): yield (staff[measureIndex], absIndex, NotePosition(staffIndex, measureIndex)) measureIndex += 1 absIndex += 1 measureIndex = 0 staffIndex += 1 staff = self.getStaff(staffIndex) while measureIndex <= end.measureIndex: yield (staff[measureIndex], absIndex, NotePosition(staffIndex, measureIndex)) absIndex += 1 measureIndex += 1
def changeKit(self, newKit, changes): transposed = defaultdict(lambda: defaultdict(dict)) for note, head in self._notes.iterNotesAndHeads(): transposed[note.drumIndex][note.noteTime] = head self._notes.clear() for newDrumIndex, newDrum in enumerate(newKit): oldDrumIndex = changes[newDrumIndex] if oldDrumIndex == -1: continue for noteTime, head in transposed[oldDrumIndex].iteritems(): if not newDrum.isAllowedHead(head): head = newDrum.head self.addNote(NotePosition(None, None, noteTime, newDrumIndex), head)
def insertMeasureByIndex(self, width, index=None, counter=None): if index is None: index = self.numMeasures() if self.numStaffs() == 0: self._addStaff() staff = self.getStaff(0) elif index == self.numMeasures(): staff = self.getStaff(-1) index = staff.numMeasures() else: staff, index = self._staffContainingMeasure(index) newMeasure = Measure(width) newMeasure.counter = counter staff.insertMeasure(NotePosition(measureIndex=index), newMeasure) return newMeasure
def setBeatCount(self, counter): if counter == self.counter: return if self.counter is None: self.counter = counter self._setWidth(len(counter)) return oldNotes = copy.deepcopy(self._notes) oldTimes = list(self.counter.iterTime()) self._setWidth(len(counter)) self.clear() newTimes = list(counter.iterTime()) oldIndex = 0 newIndex = 0 while oldIndex < len(oldTimes) and newIndex < len(newTimes): oldBeat, oldTick, oldCount = oldTimes[oldIndex] newBeat, newTick, newCount = newTimes[newIndex] if oldBeat < newBeat: oldIndex += 1 continue elif newBeat < oldBeat: newIndex += 1 continue prod1 = oldTick * newCount prod2 = newTick * oldCount if prod1 < prod2: oldIndex += 1 continue elif prod2 < prod1: newIndex += 1 continue if oldIndex in oldNotes: for position, head in oldNotes.iterNotesAtTime(oldIndex): self._notes.setNote(newIndex, position.drumIndex, head) oldIndex += 1 newIndex += 1 self.counter = counter self._runCallBack(NotePosition())
def iterNotesAndHeads(self): for noteTime in self.iterTimes(): drumDict = self._notes[noteTime] for drumIndex, drumHead in drumDict.iteritems(): yield (NotePosition(noteTime=noteTime, drumIndex=drumIndex), drumHead)
def iterNotesAtTime(self, noteTime): for index, head in self._notes[noteTime].iteritems(): yield (NotePosition(noteTime=noteTime, drumIndex=index), head)
def _readNote(self, lineData): noteTime, drumIndex, head = lineData.split(",") pos = NotePosition(noteTime=int(noteTime), drumIndex=int(drumIndex)) self.addNote(pos, head)
def clear(self): self._notes.clear() self._runCallBack(NotePosition())