Package jpianotrain.staff

Source Code of jpianotrain.staff.Tonnetz

package jpianotrain.staff;

import java.awt.Color;
import java.util.ArrayList;
import java.util.List;

import jpianotrain.gui.NoteButton;

/**
* TODO: Build factory to transform the grid of notes into
* a grid of gui-components.
*
* @author onkobu
* @since 0.0.3
*/
public class Tonnetz {
 
  public enum Sort {
    NORMAL,
    WHOLESTEP
  };
 
  public Tonnetz() {
    this(Sort.NORMAL);
  }
 
  public Tonnetz(Sort s) {
    this(s, 0);
  }
 
  public Tonnetz(Sort s, int ofs) {
    offset=ofs;
    sort=s;
    switch (s) {
    case NORMAL: {
      createNormal(ofs);
    }break;
    case WHOLESTEP:{
      createWholeStep(ofs);
    }
    }
  }
 
  private void createNormal(int offset) {
    // k�nnte man auch r�ckw�rts durchlaufen
    notes=new Note[24][12];
    for(int row=0;row<24;row++) {
      int lastCol=0;
      for (int col=0;col<12;col++) {
        int ofs=(row*9)%12;
        lastCol=(col+ofs-(row/2))%12;
        if (lastCol<0) {
          lastCol+=12;
        }
        notes[row][col]=new Note(Scale.MAJOR_SCALES_CLASSIC[(lastCol+(offset*7))%12]);
      }
    }
  }
 
  private void createWholeStep(int offset) {
    // k�nnte man auch r�ckw�rts durchlaufen
    notes=new Note[24][12];
    for(int row=0;row<24;row++) {
      for (int col=0;col<12;col++) {
        int ofs=col*2-row;
        if (row%2==0) {
          ofs-=(row/2)*2;
        } else {
          ofs-=((row-1)/2)*2;
        }
        int idx=(ofs+offset)%12;
        if (idx<0) {
          idx+=12;
        }
        notes[row][col]=new Note(Scale.MAJOR_SCALES_CLASSIC[idx]);
      }
    }
  }
 
  public int getRows() {
    if (layer==null) {
      return notes.length;
    } else {
      return notes.length*2;
    }
  }
 
  public int getCols() {
    if (layer==null) {
      return notes[0].length;
    } else {
      return notes[0].length*2;
    }
  }

  public boolean hasLayer() {
    return layer!=null;
  }
 
  public Tonnetz merge(Tonnetz t2) {
    if (t2.getRows()!=getRows() ||
        t2.getCols()!=getCols()) {
      throw new IllegalArgumentException("Layer must be of same dimension");
    }
    setLayer(t2);
    return this;
  }
 
  public List<NoteButton> getComponents() {
    List<NoteButton> results=new ArrayList<NoteButton>();
    if (layer==null) {
      for(Note[] rows:notes) {
        for (Note col:rows) {
          results.add(new NoteButton(col));
        }
      }
    } else {
      int rowIdx=0;
      int colCount=notes[0].length;
      for(Note[] rows:notes) {
        int colIdx=0;
        for (Note col:rows) {
          NoteButton black=new NoteButton(col);
          if (rowIdx%2==0) {
            results.add(black);
            NoteButton red=new NoteButton(layer.notes[rowIdx][colIdx]);
            red.setForeground(Color.RED);
            results.add(red);
          } else {
            NoteButton red=new NoteButton(layer.notes[rowIdx][(colIdx+colCount-1)%colCount]);
            red.setForeground(Color.RED);
            results.add(red);
            results.add(black);
          }
          colIdx++;
        }
        rowIdx++;
      }
    }
    return results;
  }
 
  public Sort getSort()  {
    return sort;
  }
 
  protected void setLayer(Tonnetz t) {
    layer=t;
  }
 
  protected int getOffset() {
    return offset;
  }

  protected void setOffset(int offset) {
    this.offset = offset;
  }
 
  private int offset;
 
  private Tonnetz layer;
 
  private Sort sort;
  private Note[][] notes;
}
TOP

Related Classes of jpianotrain.staff.Tonnetz

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.