/** Empties the destination and copies the section of the source into the destination. */ public void execute() { Trace.log(2, "executing CopyCommand"); Part newPart = source.extract(startSlot, endSlot); Part.PartIterator i = newPart.iterator(); dest.empty(); while (i.hasNext()) dest.addUnit(i.next()); }
/** * Returns an exact copy of this Part * * @return Part copy */ public Part copy() { Trace.log(3, "copying part of size " + size); Part newPart = new Part(size); /* * PartIterator i = iterator(); while(i.hasNext()) * newPart.slots.set(i.nextIndex(), i.next().copy()); */ for (int i = 0; i < size; i++) { Unit unit = slots.get(i); if (unit != null) { unit = unit.copy(); } newPart.slots.set(i, unit); } newPart.unitCount = unitCount; newPart.title = title; newPart.volume = volume; newPart.instrument = instrument; newPart.swing = swing; newPart.setMetre(metre[0], metre[1]); newPart.keySig = keySig; return newPart; }
/** * Overwrites a section of the Part (starting at the specified index) with the slots contained in * the given Part. * * @param part the Part to paste into this * @param index the slot at which to start pasting over */ public void pasteSlots(Part part, int index) { if (part == null) return; Trace.log(2, "pasting part of length " + part.size() + " at index " + index); for (int i = 0; i < part.size(); i++) { if (slots.get(index + i) != null) unitCount--; if (part.slots.get(i) != null) unitCount++; slots.set(index + i, part.slots.get(i)); } int prevIndex = this.getPrevIndex(index); if (prevIndex == -1) prevIndex = 0; int rv = getUnitRhythmValue(prevIndex); Trace.log(3, "in pasteSlots, setting rhythmValue"); getUnit(prevIndex).setRhythmValue(rv); }
/** * Overwrites a section of the Part (starting at the specified index) with the Units contained in * the given Part. WARNING: DOES NOT WORK IF GIVEN PART WILL EXTEND OVER END OF THIS PART. TODO: * Fix this. //rk Maybe I fixed it * * @param part the Part to paste into this * @param index the slot at which to start pasting over */ public void pasteOver(Part part, int index) { // Trace.log(3, "pasteOver " + part + " onto " + index); int limit = size(); int incoming = part.size(); if (index + incoming < limit) { limit = index + incoming; } for (int i = 0, j = index; j < limit; i++, j++) { Unit unit = part.getUnit(i); if (unit != null) { Unit newUnit = unit.copy(); setUnit(j, newUnit); } else { setUnit(j, null); } } }
public Part fitPart(int freeSlots) { if (freeSlots == 0) return null; Part fitPart = this.copy(); while (fitPart.size() > freeSlots) { Part scaledPart = new Part(); Part.PartIterator i = fitPart.iterator(); while (i.hasNext()) { Unit unit = i.next(); if (unit.getRhythmValue() != 1) { Trace.log(3, "in fitPart, setting rhythmValue"); unit.setRhythmValue(unit.getRhythmValue() / 2); if (unit.getRhythmValue() <= MIN_RHYTHM_VALUE) return null; } scaledPart.addUnit(unit); } fitPart = scaledPart; } return fitPart; }
/** * A rewritten version of pastOver * * @param part the Part to paste into this * @param index the slot at which to start pasting over */ public void newPasteOver(Part part, int index) { Trace.log(3, "pasteOver " + part + " onto " + index + " " + this.hashCode()); int limit = size(); int incoming = part.size(); // if( index + incoming < limit ) // { // limit = index + incoming; // } int i = 0; int j = index; for (; i < incoming && j < limit; i++, j++) { Unit unit = part.getUnit(i); if (unit == null) { newSetUnit(j, null); } else { newSetUnit(j, unit.copy()); } } }
/** * Modified from pasteOver for time warping Overwrites a section of the Part (starting at the * specified index) with the Units contained in the given Part. * * @param part the Part to paste into this * @param index the slot at which to start pasting over */ public void altPasteOver(Part part, int index) { int limit = size(); int incoming = part.size(); if (index + incoming < limit) { limit = index + incoming; } // How to prevent long duration last note?? // int lastSlot = index + incoming - 1; // setUnit(lastSlot, new Rest()); for (int i = 0, j = index; j < limit; i++, j++) { Unit unit = part.getUnit(i); if (unit != null) { Unit newUnit = unit.copy(); int rhythmValue = newUnit.getRhythmValue(); setUnit(j, newUnit); } else { setUnit(j, null); } } }