Example #1
0
  /**
   * m = nq + r (m-r) sections would get q bars r sections would get q+1 bars
   *
   * <p>m = the original number of bars n = split q = m/n r = m%n
   */
  public boolean nWaySplit(int index, int split) {
    int m;
    if (index + 1 < size()) m = getSectionMeasure(index + 1) - getSectionMeasure(index);
    else m = measures() - getSectionMeasure(index) + 1;
    // System.out.println("m = " + m + ", split = " + split);
    if (m < split) {
      return false;
    }
    int q = m / split;
    int r = m % split;

    SectionRecord delete = getSectionRecordByIndex(index);
    String styleName = delete.getStyleName();
    int newIndex = delete.getIndex(); // slots
    boolean isPhrase = delete.getIsPhrase();

    records.remove(index);

    for (int j = 0; j < split; j++) {
      if (j != 0) styleName = Style.USE_PREVIOUS_STYLE;
      records.add(index + j, new SectionRecord(styleName, newIndex, isPhrase));
      newIndex = measureToSlotIndex(slotIndexToMeasure(newIndex) + q);
    }

    return true;
  }
Example #2
0
  // row      , startIndex
  public void adjustSection(
      int index, int newMeasure, boolean isPhrase, boolean usePreviousStyleChecked) {
    // System.out.println("1 records = " + records);

    // Do not move first record
    // Its phrase value can be set in place

    if (newMeasure <= 0) return;

    if (index > 0 && newMeasure <= getSectionMeasure(index - 1)) return;

    int endTemp = (index + 1 < size()) ? getSectionMeasure(index + 1) - 1 : measures();

    if (newMeasure > endTemp) return;

    SectionRecord record = records.get(index);
    // gets the start measure # of the section
    // if user wants to change start index to what it is already -_-
    if (getSectionMeasure(index) == newMeasure) {
      record.setIsPhrase(isPhrase);
      if (usePreviousStyleChecked) record.setUsePreviousStyle();
      return;
    }

    String styleName = usePreviousStyleChecked ? Style.USE_PREVIOUS_STYLE : record.getStyleName();
    deleteSection(index);
    addSection(styleName, measureToSlotIndex(newMeasure), isPhrase);
  }
Example #3
0
 public ArrayList<String> getSectionRecordStyleNames() {
   ArrayList<String> secRecStyles = new ArrayList<String>();
   for (SectionRecord sr : records) {
     secRecStyles.add(sr.getStyleName());
   }
   return secRecStyles;
 }
Example #4
0
 public void reloadStyles() {
   ListIterator<SectionRecord> k = records.listIterator();
   while (k.hasNext()) {
     SectionRecord record = k.next();
     k.remove();
     k.add(new SectionRecord(record.getStyleName(), record.getIndex(), record.getIsPhrase()));
   }
 }
Example #5
0
  public void setSize(int size) {

    Iterator<SectionRecord> k = records.iterator();

    while (k.hasNext()) {
      SectionRecord record = k.next();
      int n = record.getIndex();
      if (n >= size) {
        k.remove();
      }
    }
  }
Example #6
0
  public String getInfo(int index) {
    SectionRecord record = records.get(index);

    String styleName = record.getStyleName();
    int startIndex = getSectionMeasure(index);
    int endIndex = measures();
    if (index + 1 < size()) endIndex = getSectionMeasure(index + 1) - 1;

    String info = "mm. " + startIndex + "-" + endIndex + ": " + styleName;
    if (startIndex == endIndex) info = "m. " + startIndex + ": " + styleName;

    return info;
  }
Example #7
0
  /**
   * Get section by slot index
   *
   * @param slot
   * @return
   */
  public SectionRecord getSectionRecordBySlot(int slot) {
    ListIterator<SectionRecord> k = records.listIterator();
    SectionRecord s = k.next();
    SectionRecord previous = s;
    while (s.getIndex() <= slot && k.hasNext()) {
      previous = s;
      s = k.next();
    }

    s = s.getIndex() <= slot ? s : previous;

    // System.out.println("slot = " + slot + " using s = " + s);
    return s;
  }
Example #8
0
 public int sectionAtSlot(int n) {
   Iterator<SectionRecord> k = records.listIterator();
   while (k.hasNext()) {
     SectionRecord record = k.next();
     if (record.getIndex() == n) {
       if (record.getIsPhrase()) {
         return Block.PHRASE_END;
       } else {
         return Block.SECTION_END;
       }
     }
   }
   return Block.NO_END;
 }
Example #9
0
 /**
  * getStyleNameAtSlot returns the name of the Style operative at a given slot in the ChordPart
  *
  * @param slot
  * @return
  */
 public String getStyleNameAtSlot(int slot) {
   String previousStyleName = "no-style";
   String styleName = previousStyleName;
   for (SectionRecord k : records) {
     styleName = k.getStyleName();
     if (styleName.equals("*")) {
       styleName = previousStyleName;
     }
     if (k.getIndex() >= slot) {
       return styleName;
     }
     previousStyleName = styleName;
   }
   return styleName;
 }
Example #10
0
 @Override
 public String toString() {
   StringBuilder buffer = new StringBuilder();
   for (SectionRecord record : records) {
     buffer.append("(");
     buffer.append(record.getStyleName());
     buffer.append(" ");
     buffer.append(record.getIndex());
     buffer.append(" ");
     buffer.append(record.getIsPhrase());
     buffer.append(") ");
     buffer.append("\n");
   }
   return buffer.toString();
 }
Example #11
0
  private void saveSectionInfo(BufferedWriter out, SectionRecord record) throws IOException {
    String styleName = record.getUsePreviousStyle() ? "" : " " + record.getStyleName();

    if (record.getIsPhrase()) {
      out.newLine();
      out.write("(phrase (style" + styleName + ")) ");
      out.newLine();
    } else {
      out.newLine();
      out.newLine();
      out.write("(section (style" + styleName + ")) ");
      out.newLine();
      out.newLine();
    }
  }
Example #12
0
  public void addSection(String styleName, int n, boolean isPhrase) {
    ListIterator<SectionRecord> k = records.listIterator();

    while (k.hasNext()) {
      SectionRecord record = k.next();
      int index = record.getIndex();
      if (index == n) {
        k.remove();
        break;
      } else if (index > n) {
        k.previous();
        break;
      }
    }
    k.add(new SectionRecord(styleName, n, isPhrase));
  }
Example #13
0
  public Integer getPrevSectionIndex(int n) {
    ListIterator<SectionRecord> k = records.listIterator();
    while (k.hasNext()) {
      SectionRecord record = k.next();
      int index = record.getIndex();
      if (index > n) {
        k.previous();
        index = k.previous().getIndex();
        if (index == n && k.hasPrevious()) {
          return k.previousIndex();
        } else if (index == n) {
          return -1;
        }
        return index;
      }
    }

    return -1;
  }
Example #14
0
  public ArrayList<Block> toBlockList() {
    ArrayList<Block> blocks = new ArrayList<Block>();
    int chordsSize = chords.size();

    int endIndex;

    // m is a second iterator intended to stay one step ahead of k
    // so as to get the start of the next section

    ListIterator<SectionRecord> k = records.listIterator();
    ListIterator<SectionRecord> m = records.listIterator();
    if (m.hasNext()) {
      m.next();
    }

    while (k.hasNext()) // && (endLimitIndex == ENDSCORE || endIndex <= endLimitIndex) )
    {
      SectionRecord record = k.next();
      String styleName = record.getStyleName();
      int startIndex = record.getIndex();

      endIndex = m.hasNext() ? m.next().getIndex() : chordsSize;

      ChordBlock block = null;

      for (int slot = startIndex; slot < endIndex; slot++) {
        Chord chord = chords.getChord(slot);

        if (chord != null) {
          block = new ChordBlock(chord.getName(), chord.getRhythmValue());
          block.setStyleName(styleName);
          blocks.add(block);
        }
      }

      // For last block in section
      if (block != null) {
        block.setSectionEnd(record.getIsPhrase() ? Block.PHRASE_END : Block.SECTION_END);
      }
    }

    return blocks;
  }
Example #15
0
  public SectionInfo extract(int first, int last, ChordPart chords) {
    SectionInfo si = new SectionInfo(chords);

    si.records = new ArrayList<SectionRecord>();

    Iterator<SectionRecord> k = records.iterator();

    while (k.hasNext()) {
      SectionRecord record = k.next();
      String styleName = record.getStyleName();
      int index = record.getIndex() - first;
      if (index < 0) {
        si.records.add(new SectionRecord(styleName, 0, record.getIsPhrase()));
      } else if (index <= last - first) {
        si.records.add(new SectionRecord(styleName, index, record.getIsPhrase()));
      }
    }

    return si;
  }
Example #16
0
  /**
   * Returns ArrayList of indices of section starts that are not phrases.
   *
   * @return
   */
  public ArrayList<Integer> getSectionStartIndices() {
    ArrayList<Integer> result = new ArrayList<Integer>();
    boolean isStart = true; // The first Section Record must be a Section Start

    for (SectionRecord record : records) {
      if (isStart == true) {
        result.add(record.getIndex());
      }
      if (!record.getIsPhrase()) {
        // Any Section Record that follows a Section Record that is
        // not a phrase must be a Section Start
        isStart = true;
      } else {
        // Otherwise, if a Section Record is a Phrase, i.e.,
        // ends with a breath mark as opposed to a double bar line,
        // the next Section Record cannot be a Section Start
        isStart = false;
      }
    }
    return result;
  }
Example #17
0
  public void newSection(int index) {
    int measureLength = chords.getMeasureLength();

    SectionRecord record = records.get(index);
    int startIndex = record.getIndex();

    int endIndex = chords.size();

    if (index + 1 < size()) {
      SectionRecord nextRecord = records.get(index + 1);
      endIndex = nextRecord.getIndex();
    }

    int measure = (endIndex - startIndex) / measureLength;

    if (measure % 2 == 0) measure /= 2;
    else measure = measure / 2 + 1;

    addSection(
        Style.USE_PREVIOUS_STYLE, startIndex + measure * measureLength, record.getIsPhrase());
  }
Example #18
0
  public void deleteSection(int index) {
    if (size() <= 1) return;
    SectionRecord fillSpot = null;
    if (index == 0) fillSpot = records.get(1);

    if (index < size() - 1) {
      SectionRecord nextRecord = records.get(index + 1);
      if (nextRecord.usePreviousStyle()) nextRecord.setStyleName(records.get(index).getStyleName());
    }

    ListIterator<SectionRecord> k = records.listIterator(index);
    k.next();

    int newStartIndex = getSectionMeasure(index);

    k.remove();

    if (fillSpot != null) {
      fillSpot.setIndex(measureToSlotIndex(newStartIndex));
    }
  }
Example #19
0
  // markermarkermarker
  public void adjustEndOfSection(
      int index, int endIndex, boolean isPhrase, boolean usePreviousStyleChecked) {
    // *
    if (endIndex <= 0) return;

    SectionRecord record = records.get(index);

    int endTemp = (index + 1 < size()) ? getSectionMeasure(index + 1) - 1 : measures();

    int nextSectionEnd = (index + 2 < size()) ? getSectionMeasure(index + 2) - 1 : measures();

    if (index < size() - 1 && endIndex >= nextSectionEnd) return;

    if (endIndex < getSectionMeasure(index)) return;

    // if user wants to change end index to what it is already -_-
    if (endIndex == endTemp) {
      record.setIsPhrase(isPhrase);
      if (usePreviousStyleChecked) record.setUsePreviousStyle();
      return;
    }

    String styleName = usePreviousStyleChecked ? Style.USE_PREVIOUS_STYLE : record.getStyleName();

    if (size() <= 1) return;
    if (index < size() - 1) {
      SectionRecord nextRecord = records.get(index + 1);
      nextRecord.setIndex(measureToSlotIndex(endIndex + 1));
    }
    // */

  }
Example #20
0
  public void makeSwing(SectionInfo sectionInfo) {
    // The index here iterates through the start of every beat

    Style previousStyle = Style.getStyle("swing"); // TEMP: FIX!
    for (int i = 0; i + beatValue - 1 < size; i += beatValue) {
      SectionRecord record = sectionInfo.getSectionRecordBySlot(i);

      Style s;
      if (record.getUsePreviousStyle()) {
        s = previousStyle;
      } else {
        s = record.getStyle();
        previousStyle = s;
      }

      if (s == null) {
        ErrorLog.log(ErrorLog.FATAL, "It will not be possible to continue");
      }

      double swingValue = s.getSwing();

      // System.out.println("i = " + i + ", style = " + s + " swing = " + swingValue);

      // FIX: Notice the problem here when i < size, the original condition, is used.

      // we get the Units where a second sixteenth note would fall,
      // an eighth note, and a fourth sixteenth note

      Unit unit1 = slots.get(i + 1 * beatValue / 4);
      Unit unit2 = slots.get(i + 1 * beatValue / 2);
      Unit unit3 = slots.get(i + 3 * beatValue / 4);

      // we only use swingValue if there is no second sixteenth note
      // (we don't want to swingValue a beat of four sixteenths)

      if (unit1 == null && unit2 != null) {

        /* formerly:
        // swingValue if there is a second eighth note
        if(unit2.getRhythmValue() == beatValue/2) {
            slots.set(i+beatValue/2, null);
            slots.set(i+(int)(beatValue*swingValue), unit2);
        }
        */

        int trailingRhythm = unit2.getRhythmValue();

        // swingValue if there is a second eighth note or longer
        if (trailingRhythm >= beatValue / 2) {

          int offset = (int) (beatValue * swingValue);
          Unit unit2mod = unit2.copy();
          unit2mod.setRhythmValue(unit2.getRhythmValue() - offset);
          slots.set(i + beatValue / 2, null);
          slots.set(i + offset, unit2mod);
        }
      }
    }

    try {
      // After the Units are shifted, go through and reset each rhythm value
      Part.PartIterator i = iterator();
      while (i.hasNext()) {
        int index = i.nextIndex();
        slots.get(index).setRhythmValue(getUnitRhythmValue(index));
        i.next();
      }
    } catch (Exception e) {

    }
  }
Example #21
0
 public void modifySection(Style style, int n, boolean isPhrase) {
   SectionRecord record = getSectionRecordByIndex(n);
   record.setStyle(style);
 }
Example #22
0
  public long render(
      MidiSequence seq,
      long time,
      Track track,
      int transposition,
      boolean useDrums,
      int endLimitIndex,
      boolean constantBass)
      throws InvalidMidiDataException {
    // to trace sequencing info:
    // System.out.println("Sequencing SectionInfo time = "
    // + time + " endLimitIndex = " + endLimitIndex + " useDrums = " + useDrums);

    // Iterate over list of sections, each a Style

    int chordsSize = chords.size();

    int endIndex;

    // m is a second iterator intended to stay one step ahead of k
    // so as to get the start of the next section

    Style mostRecentStyle = new Style();

    ListIterator<SectionRecord> k = records.listIterator();
    ListIterator<SectionRecord> m = records.listIterator();
    if (m.hasNext()) {
      m.next();
    }

    while (k.hasNext()) // && (endLimitIndex == ENDSCORE || endIndex <= endLimitIndex) )
    {
      SectionRecord record = k.next();
      Style style;
      if (record.getUsePreviousStyle()) {
        style = mostRecentStyle;
        // System.out.println("using previous style " + style);
      } else {
        style = record.getStyle();
        mostRecentStyle = style;
      }

      int startIndex = record.getIndex();

      endIndex = m.hasNext() ? m.next().getIndex() : chordsSize;

      if (style != null) {
        time =
            style.render(
                seq,
                time,
                chords,
                startIndex,
                endIndex,
                transposition,
                useDrums,
                endLimitIndex,
                constantBass);
      }
    }
    return time;
  }
Example #23
0
  /**
   * Writes the Part to the passed BufferedWriter in Leadsheet notation.
   *
   * @param out the BufferedWriter to write the Part onto
   */
  public void saveLeadsheet(BufferedWriter out, String type) throws IOException {
    out.write("(part");
    out.newLine();
    out.write("    (type " + type + ")");
    out.newLine();
    out.write("    (title " + title + ")");
    out.newLine();
    out.write("    (composer " + composer + ")");
    out.newLine();
    out.write("    (instrument " + instrument + ")");
    out.newLine();
    out.write("    (volume " + volume + ")");
    out.newLine();
    out.write("    (key " + keySig + ")");
    out.newLine();

    if (this instanceof MelodyPart) {
      out.write("    (stave " + staveType.toString().toLowerCase() + ")");
      out.newLine();
    }
    // For now, saving roadmaps is disabled
    //    else
    //    {
    //        out.write(Formatting.prettyFormat(4, ((ChordPart)this).getRoadMap() == null ? "" :
    // ((ChordPart)this).getRoadmapPoly()));
    //        out.newLine();
    //    }

    out.write(")");
    out.newLine();

    Note.initializeSaveLeadsheet();

    Part.PartIterator i = iterator();

    // Should be refactored into separate methods for each derived class

    if (this instanceof MelodyPart) {
      while (i.hasNext()) {
        i.next().saveLeadsheet(out, metre);
      }
    } else {
      SectionInfo sectionInfo = ((ChordPart) this).getSectionInfo();

      Iterator<SectionRecord> sec = sectionInfo.iterator();

      SectionRecord record = sec.next();

      boolean lastSection = !sec.hasNext();

      Chord residualChord = null;

      int slot = 0;

      int slotLimit = size();

      int nextSectionStart;

      // iSystem.out.println("slotLimit = " + slotLimit);

      Chord chord = null;

      int sectionsToGo = sectionInfo.size();

      do // do-while
      {
        // System.out.println("\nrecord = " + record);

        // Save the section record
        saveSectionInfo(out, record);

        // Get the next section record, if any.
        if (sec.hasNext()) {
          record = sec.next();
          nextSectionStart = record.getIndex();
        } else {
          nextSectionStart = slotLimit;
        }

        // System.out.println("next section start = " + nextSectionStart);

        // Pack Chords into section

        while ((chord != null || i.hasNext()) && slot < nextSectionStart) {
          if (chord == null) {
            Chord nextChord = (Chord) i.next();
            if (nextChord != null) {
              chord = nextChord.copy();
            }
          }
          // Otherwise use the residue of previous chord

          assert chord != null;
          // Where the next slot would normally be
          int nextSlot = slot + chord.getRhythmValue();

          if (nextSlot <= nextSectionStart) {
            // This chord fits in the current section.

            chord.saveLeadsheet(out, metre);
            chord = null;
            slot = nextSlot;
          } else {
            // This chord does not fit in the current section.
            // Calculate how much of this section can be used.
            int available = nextSectionStart - slot;
            chord.setRhythmValue(available);
            chord.saveLeadsheet(out, metre);

            // Determine what is left over.
            int residual = nextSlot - nextSectionStart;
            chord.setRhythmValue(residual);

            // System.out.println("overflow at slot " + slot + ", next section start = " +
            // nextSectionStart + " " + chord + ", residual = " + residual);

            // This should force the end of this while, among other things

            slot = nextSectionStart;
          }
        }
        sectionsToGo--;
      } while (sectionsToGo > 0); // end of do-while
    }
  }