OrPattern.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 » OrPattern.java
// -----------------------------------------------------------------------------
// ------------------------------------------------------------------------------
package com.ordrumbox.core.description;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

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

import com.ordrumbox.core.lgNat.InstrumentType;

// ------------------------------------------------------------------------
public class OrPattern extends Common {
  private static final long  serialVersionUID  = 1L;
  public static boolean    DEBUG_TIME       = false;
  private static Random    rnd               = new Random();
  private List<OrTrack>    orTracks           = new ArrayList<OrTrack>();
  private int                nbStepsPerMeasure   = 8;
  private int                nbMeasures       = 4;

  public OrPattern(String displayName) {
    setDisplayName(displayName);
  }

  // ------------------------------------------------------------------------
  public OrPattern(OrPattern orPattern) {
    setDisplayName(new String(orPattern.getDisplayName()));
    setNbMeasures(orPattern.getNbMeasures());
    setNbStepsPerMeasure(orPattern.getNbStepsPerMeasure());
    for (OrTrack orTrack : orPattern.getTracks()) {
      addTrack(orTrack);
    }
  }

  public OrPattern(Song song, Element patternElement) {
    fromXML(song, patternElement);
  }

  protected void fromXML(Song song, Element patternElement) {
    String name = patternElement.getAttribute("name");
    setDisplayName(name.trim());
    String nb_measures = patternElement.getAttribute("nb_measures");
    setNbMeasures(new Integer(nb_measures.trim()).intValue());
    String nb_steps_per_measures = patternElement.getAttribute("nb_steps_per_measure");
    setNbStepsPerMeasure(new Integer(nb_steps_per_measures.trim()).intValue());
    NodeList trackList = patternElement.getElementsByTagName("track");
    int i = 0;
    while (i < trackList.getLength()) {
      Element trackElement = (Element) trackList.item(i);
      OrTrack orTrack = new OrTrack(song, this, trackElement);
      getTracks().add(orTrack);
      i++;
    }
  }

  public List<OrTrack> getTracks() {
    return orTracks;
  }

  public int getNbSteps() {
    return nbStepsPerMeasure * nbMeasures;
  }

  public void selectAll(boolean newstate) {
    for (int i = 0; i < getTracks().size(); i++) {
      OrTrack orTrack = (OrTrack) getTracks().get(i);
      orTrack.setSelected(newstate);
    }
  }

  // ------------------------------------------------------------------------
  public OrTrack addTrack(OrTrack orTrack) {
    orTracks.add(orTrack);
    return orTrack;
  }

  // ------------------------------------------------------------------------
  public void deleteTrack(OrTrack orTrack) {
    orTracks.remove(orTrack);
    orTrack = null;
  }

  public boolean isFantom() {
    for (int i = 0; i < this.getTracks().size(); i++) {
      OrTrack orTrack = (OrTrack) this.getTracks().get(i);
      if (orTrack.isFantom() == true)
        return true;
    }
    return false;
  }

  public void clear() {
    for (int i = 0; i < orTracks.size(); i++) {
      OrTrack orTrack = (OrTrack) orTracks.get(i);
      orTrack.clear();
    }
  }

  public OrTrack getTrackFromNum(int i) {
    if (getTracks().size() <= 0)
      return null;
    i %= getTracks().size();
    return (OrTrack) getTracks().get(i);
  }

  public int getNumFromTrack(OrTrack track) {
    for (int i = 0; i < orTracks.size(); i++) {
      OrTrack orTrack2 = (OrTrack) orTracks.get(i);
      if (orTrack2.getDisplayName().equals(track.getDisplayName())) {
        return i;
      }
    }
    return 0;
  }

  public OrTrack getTrackFromInstrumentType(InstrumentType instrumentType) {
    for (int i = 0; i < this.orTracks.size(); i++) {
      OrTrack orTrack = getTrackFromNum(i);
      if (orTrack.getInstrumentType() == instrumentType) {
        return orTrack;
      }
    }
    System.out.println("*** no track for instrumentType:" + instrumentType);
    if (orTracks.size() <= 0) {
      return null;
    } else {
      return this.getTrackFromNum(rnd.nextInt(getTracks().size()));
    }
  }

  public void selectTrack(OrTrack orTrack) {
    selectAll(false);
    orTrack.setSelected(true);
  }

  public void doStepzero() {
    for (OrTrack orTrack : orTracks) {
      orTrack.doStepZero();
    }
  }

  public void setNbStepsPerMeasure(int nb) {
    nbStepsPerMeasure = nb;
  }

  public int getNbMeasures() {
    return nbMeasures;
  }

  public void setNbMeasures(int nbMeasures) {
    this.nbMeasures = nbMeasures;
  }

  public int getNbStepsPerMeasure() {
    if (nbStepsPerMeasure <= 0) {
      nbStepsPerMeasure = 8;
    }
    return nbStepsPerMeasure;
  }

  public int getNumberOfTracks() {
    return orTracks.size();
  }

  /**
   * 
   */
  public Element toXml(Document xmldoc) {
    Element patternElement = xmldoc.createElement("pattern");
    patternElement.setAttribute("nb_steps_per_measure", new Integer(this.getNbStepsPerMeasure()).toString());
    patternElement.setAttribute("nb_measures", new Integer(this.getNbMeasures()).toString());
    patternElement.setAttribute("name", this.getDisplayName());
    for (int i = 0; i < this.getNumberOfTracks(); i++) {
      OrTrack orTrack = this.getTrackFromNum(i);
      // if (orTrack.getNbNotes() > 0)
      {
        patternElement.appendChild(orTrack.toXml(xmldoc));
      }
    }
    xmldoc.getDocumentElement().appendChild(patternElement);
    return patternElement;
  }

  public String getInfos() {
    return "tracks:" + getTracks().size() + " measures:" + getNbMeasures() + " sign:" + getNbStepsPerMeasure();
  }

}
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.