Note.java :  » Music » orDrumbox-0.9.0 » com » ordrumbox » core » description » Java Open Source

Java Open Source » Music » orDrumbox 0.9.0 
orDrumbox 0.9.0 » com » ordrumbox » core » description » Note.java
// -----------------------------------------------------------------------------
// ------------------------------------------------------------------------------
package com.ordrumbox.core.description;

import java.io.Serializable;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

// ------------------------------------------------------------------------
public class Note extends Scalenote implements Serializable {

  /**
   * 
   */
  private static final long  serialVersionUID    = 1L;
  public static final int    LOOP          = 10;
  public static final int    MUTE          = 20;
  public static final int    ARPEGIED        = 30;
  public static final int    FANTOM          = 40;
  public static final int    NORMAL          = 0;
  public static final int    ZEROVELO        = 0;
  public static final int    MAXVELO          = 99;
  public static final int    LOUDVELO        = 90;
  public static final int    MOYVELO          = 60;
  public static final int    MINVELO          = 5;
  public static final int    VELO_1          = 30;
  public static final int    VELO_2          = 60;
  public static final int    VELO_3          = 90;
  public static final int    STEPVELO        = 20;
  public static final int    ZEROPITCH        = 0;
  public static final int    MAXPITCH        = 24;
  public static final int    MINPITCH        = -24;
  public static final int    ZEROPANO        = 0;
  public static final int    MAXPANO          = 8;
  public static final int    MINPANO          = -8;
  public static final int    NB_TICKS_PER_MEASURE  = 64;

  // ------------------------------------------------------------------------
  private int          velo;
  private int          type;
  private int          pano;
  private OrTrack        orTrack          = null;
  private int step;


/**
 * 
 * @param orTrack
 * @param step
 * @param type
 * @param pitch
 * @param velo
 */
  public Note(OrTrack orTrack, int step, int type, int pitch, int velo) {
    int measure = getMeasureFromStep(orTrack, step);
    int tick = getTickPositionFromStep(orTrack, step);
    this.step = step;
    newNote(orTrack, tick, measure, type, pitch, velo);
  }

  // ------------------------------------------------------------------------
  public Note(OrTrack orTrack, int tickPosition, int measure, int type, int pitch, int velo) {
    newNote(orTrack, tickPosition, measure, type, pitch, velo);
  }

  private void newNote(OrTrack orTrack, int tickPosition, int measure, int type, int pitch, int velo) {
    this.setTrack(orTrack);
    this.setTickPosition(tickPosition);
    this.setMeasure(measure);
    this.setPitch(pitch);
    this.setVelo(velo);
    this.setType(type);
    this.step = computeStepFromTickPos(tickPosition, measure, 4);
    this.setDisplayName(this.computeDisplayName(0)); // pitch du track parent !!!
  }

  // ------------------------------------------------------------------------
  public Note(Note note) {
    this.setTrack(note.getTrack());
    this.setTickPosition(note.getTickPosition());
    this.setPitch(note.getPitch());
    this.setVelo(note.getVelo());
    this.setType(note.getType());
    this.setDisplayName(new String(note.getDisplayName()));
  }

  public Note(OrTrack orTrack, Element noteElement, int type) {
    setType(type);
    setTrack(orTrack);
    String measure = noteElement.getAttribute("measure");
    setMeasure(new Integer(measure.trim()).intValue());
    String pitch = noteElement.getAttribute("pitch");
    if (pitch.length() > 0) {
      setPitch(new Integer(pitch.trim()).intValue());
    }
    String tick_position = noteElement.getAttribute("tick_position");
    setTickPosition(new Integer(tick_position.trim()).intValue());
    String velo = noteElement.getAttribute("velo");
    if (velo.length() > 0) {
      setVelo(new Integer(velo.trim()).intValue());
    }
    // OrLog.print(Level.INFO, "new note: " + measure + " velo=" + velo);
  }

  public void clear() {
    setType(NORMAL);
    setPitch(ZEROPITCH);
    setVelo(ZEROVELO);
  }

  public String toString() {
    return " tick=" + this.getTickPosition() + " measure=" + this.getMeasure() + " pitch=" + this.getPitch()
        + " velocity=" + velo + " type= " + type;
  }

  public OrTrack getTrack() {
    return orTrack;
  }

  public void setTrack(OrTrack orTrack) {
    this.orTrack = orTrack;
  }

  public int getVelo() {
    if (velo > Note.MAXVELO) {
      velo = Note.MAXVELO;
    }
    if (velo < Note.MINVELO) {
      velo = Note.ZEROVELO;
    }
    return velo;
  }

  public int getType() {
    return type;
  }

  public void setVelo(int velo) {
    this.velo = velo;
  }

  public void setType(int type) {
    this.type = type;
    if (type == Note.LOOP) {
      this.velo = Note.MAXVELO; // TODO PROVISIORE
    }
  }

  public void toggleVelo() {
    if (this.getVelo() <= Note.MINVELO) {
      this.setVelo(Note.MAXVELO);
    } else {
      this.clear();
    }
  }

  public void toggleLoop() {
    if (this.getType() == Note.LOOP) {
      this.clear();
    } else {
      this.clear();
      this.setType(Note.LOOP);
    }
  }

  public void incrPitch() {
    this.setPitch(this.getPitch() + 1);

  }

  // ------------------------------------------------------------------------------
  public Element toXml(Document xmldoc) {
    Element noteElement = null;
    if (this.getType() == Note.LOOP) {
      noteElement = xmldoc.createElement("loopnote");
    }
    if (this.getType() == Note.NORMAL) {
      noteElement = xmldoc.createElement("note");
      noteElement.setAttribute("velo", (new Integer(this.getVelo())).toString());
      noteElement.setAttribute("pitch", (new Integer(this.getPitch())).toString());
    }
    // ATTN CONFUSION scale_note != arpegied_note
    if (this.getType() == Note.ARPEGIED) {
      noteElement = xmldoc.createElement("arpegied_note");
      noteElement.setAttribute("velo", (new Integer(this.getVelo())).toString());
    }

    noteElement.setAttribute("tick_position", (new Integer(getTickPosition())).toString());
    noteElement.setAttribute("measure", (new Integer(getMeasure())).toString());
    return noteElement;
  }

  public int computeStepFromTickPositionAndMeasure() {
    step =  Scalenote.computeStepFromTickPos(tickPosition, measure, getTrack().getPattern().getNbStepsPerMeasure());
    return step;
  }

  public int getPano() {
    return pano;
  }

  public void setPano(int pano) {
    this.pano = pano;
  }

  public int getStep() {
      return step;
    }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.