Esempio n. 1
0
  protected Element convert(Document doc, Note note) {
    Element noteEl = doc.createElement(NOTE_TAG);
    Element durationEl = doc.createElement(DURATION_TAG);

    String stepValue = null;
    byte strictHeight = note.getStrictHeight();
    int octave = note.getOctaveTransposition();
    if (note.isRest()) {
      Element rest = doc.createElement(REST_TAG);
      noteEl.appendChild(rest);
    } else {
      switch (strictHeight) {
        case Note.C:
          stepValue = "C";
          break;
        case Note.D:
          stepValue = "D";
          break;
        case Note.E:
          stepValue = "E";
          break;
        case Note.F:
          stepValue = "F";
          break;
        case Note.G:
          stepValue = "G";
          break;
        case Note.A:
          stepValue = "A";
          break;
        case Note.B:
          stepValue = "B";
          break;
      }

      octave = octave + 4;
      String octaveValue = new Integer(octave).toString();
      // Element typeEl = doc.createElement(TYPE_TAG);
      // typeEl.appendChild(doc.createTextNode("quarter"));
      Element pitchEl = doc.createElement(PITCH_TAG);
      Element stepEl = doc.createElement(STEP_TAG);
      stepEl.appendChild(doc.createTextNode(stepValue));
      pitchEl.appendChild(stepEl);

      if (keySignature != null) {
        Accidental accidental = note.getAccidental();
        if (accidental.isInTheKey()) {
          accidental = keySignature.getAccidentalFor(note.getStrictHeight());
        }
        if (accidental.isDefined()) {
          int alterValue = accidental.getNearestOccidentalValue();

          Element alterEl = doc.createElement(ALTER_TAG);
          alterEl.appendChild(doc.createTextNode(Integer.toString(alterValue)));
          pitchEl.appendChild(alterEl);
        }
      }

      Element octaveEl = doc.createElement(OCTAVE_TAG);
      octaveEl.appendChild(doc.createTextNode(octaveValue));
      pitchEl.appendChild(octaveEl);

      noteEl.appendChild(pitchEl);

      if (note.hasAccidental()) {
        Node acc = doc.createElement(ACCIDENTAL_TAG);
        Node accValue = null;
        if (note.getAccidental().isFlat()) accValue = doc.createTextNode("flat");
        else if (note.getAccidental().isNatural()) accValue = doc.createTextNode("natural");
        else if (note.getAccidental().isSharp()) accValue = doc.createTextNode("sharp");
        // TODO double flat/sharp
        acc.appendChild(accValue);
        noteEl.appendChild(acc);
      }
    }
    int relDuration = note.getDuration() * DIVISIONS_PER_QUARTER_NOTE / Note.QUARTER;
    durationEl.appendChild(doc.createTextNode(new Integer(relDuration).toString()));
    noteEl.appendChild(durationEl);
    if (note.isTied()) {
      String type = null;
      if (note.isBeginningTie()) {
        type = "start";
      } else if (note.isEndingTie()) {
        type = "stop";
      }
      if (type != null) {
        Element tieEl = doc.createElement(TIE_TAG);
        tieEl.setAttribute(TYPE_ATTRIBUTE, type);
        noteEl.appendChild(tieEl);
        Element notationsEl = (Element) noteEl.getElementsByTagName(NOTATIONS_TAG).item(0);
        if (notationsEl == null) {
          notationsEl = doc.createElement(NOTATIONS_TAG);
          noteEl.appendChild(notationsEl);
        }
        Element tiedEl = doc.createElement(TIED_TAG);
        tiedEl.setAttribute(TYPE_ATTRIBUTE, type);
        notationsEl.appendChild(tiedEl);
      }
    }

    if (note.isPartOfTuplet()) {
      Tuplet tuplet = note.getTuplet();
      Vector notes = tuplet.getNotesAsVector();
      int d = 0;
      for (Iterator iterator = notes.iterator(); iterator.hasNext(); ) {
        Note n = (Note) iterator.next();
        d += n.getStrictDuration();
      }
      int gcd = MathUtils.pgcd(d, tuplet.getTotalDuration());
      Element timeModificationEl = doc.createElement(TIMEMODIFICATION_TAG);

      Element actualNotesEl = doc.createElement(ACTUALNOTES_TAG);
      actualNotesEl.appendChild(doc.createTextNode(Integer.toString(d / gcd)));
      timeModificationEl.appendChild(actualNotesEl);

      Element normalNotesEl = doc.createElement(NORMALNOTES_TAG);
      normalNotesEl.appendChild(
          doc.createTextNode(Integer.toString(tuplet.getTotalDuration() / gcd)));
      timeModificationEl.appendChild(normalNotesEl);

      noteEl.appendChild(timeModificationEl);

      Element notationsEl = doc.createElement(NOTATIONS_TAG);
      Element tupletEl = doc.createElement(TUPLET_TAG);
      if (note.equals(notes.get(0))) {
        tupletEl.setAttribute(TYPE_ATTRIBUTE, "start");
        notationsEl.appendChild(tupletEl);
        noteEl.appendChild(notationsEl);
      } else if (note.equals(notes.get(notes.size() - 1))) {
        tupletEl.setAttribute(TYPE_ATTRIBUTE, "stop");
        notationsEl.appendChild(tupletEl);
        noteEl.appendChild(notationsEl);
      }
    }

    Node type = doc.createElement(TYPE_TAG);
    Node typeValue = null;
    switch (note.getStrictDuration()) {
      case Note.SIXTY_FOURTH:
        typeValue = doc.createTextNode("64th");
        break;
      case Note.THIRTY_SECOND:
        typeValue = doc.createTextNode("32nd");
        break;
      case Note.SIXTEENTH:
        typeValue = doc.createTextNode("16th");
        break;
      case Note.EIGHTH:
        typeValue = doc.createTextNode("eighth");
        break;
      case Note.QUARTER:
        typeValue = doc.createTextNode("quarter");
        break;
      case Note.HALF:
        typeValue = doc.createTextNode("half");
        break;
      case Note.WHOLE:
        typeValue = doc.createTextNode("whole");
        break;
    }
    if (typeValue != null) {
      type.appendChild(typeValue);
      noteEl.appendChild(type);
    }

    if (note.countDots() >= 1) {
      for (int i = 0; i < note.countDots(); i++) {
        Node dot = doc.createElement(DOT_TAG);
        noteEl.appendChild(dot);
      }
    }
    return noteEl;
  }