public Integer getMeasureOf(Rational r) {
    TimeSignature ts = getObjectAt(r);
    Rational lastTSChange = _data.lowerKey(r);

    // Calculate the measures since the last time signature change
    Integer measures =
        Double.valueOf(Math.ceil((r.minus(lastTSChange)).times(new Rational(1, ts.TOP)).toDouble()))
            .intValue();

    // Go backwards through underlying data to count all measures defined in the piece (ideally
    // there are none before 0?)
    for (Map.Entry<Rational, TimeSignature> e :
        _data.headMap(lastTSChange, false).descendingMap().entrySet()) {
      measures =
          measures
              + Double.valueOf(
                      Math.ceil(
                          (lastTSChange.minus(e.getKey()))
                              .times(new Rational(1, e.getValue().TOP))
                              .toDouble()))
                  .intValue();
      lastTSChange = e.getKey();
    }

    return measures;
  }
Exemple #2
0
  // bin search based square root computation,
  // works for rationals in the range [0,2147483647]
  public Rational root() throws IllegalArgumentToSquareRootException {

    Rational low = new Rational(0, 1);
    // Max Int value is 2147483647, square root is 46341
    // Rational high = new Rational(46341,1);
    Rational high = new Rational(46340, 1);

    if (this.isLessThan(low) || !this.isLessThan(high)) {
      throw new IllegalArgumentToSquareRootException(this);
    }
    System.out.println("46341* 46341 = " + (46341 * 46341));
    System.out.println("46340* 46340 = " + (46340 * 46340));

    Rational half = new Rational(1, 2);
    Rational midpoint = (low.plus(high)).times(half);
    Rational tmp = null;
    while (!high.minus(low).abs().isLessThan(TOLERANCE)) {
      System.out.println("low high: " + low + " " + high);
      System.out.println("square of midpoint is: " + midpoint.times(midpoint));
      if (midpoint.times(midpoint).isLessThan(this)) {
        tmp = new Rational(midpoint);
        midpoint = high.plus(midpoint).times(half);
        low = tmp;
      } else {
        tmp = new Rational(midpoint);
        midpoint = low.plus(midpoint).times(half);
        high = tmp;
      }
    }
    return midpoint;
  }
  public Rational getBeatOf(Rational r) {
    TimeSignature ts = getObjectAt(r);
    if (ts == null) {
      return null;
    }
    Rational meterEstablishedAt = _data.floorKey(r);
    if (meterEstablishedAt == null) return null;

    Rational newResult = r.minus(meterEstablishedAt).mod(new Rational(ts.TOP)).plus(Rational.ONE);
    return newResult;

    // Rational n = r.minus(meterEstablishedAt);
    // Integer i = Double.valueOf( n.toDouble() ).intValue() % ts.TOP;
    // Rational result = n.minus( new Rational(ts.TOP, 1).times(new Rational(i, 1)) );
    // return result;
  }
 public void subtract() {
   operand_0 = operand_1.minus(operand_0);
   operand_1 = new Rational();
 }
 /** Aftrekking van operand 1 en operand 2 */
 public void subtract() {
   operand_0 = operand_1.minus(operand_0);
   operand_1 = new Rational();
   processEvent(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, null));
 }