july 01 2022
back

some java for Processing IDE I'll try tanslatate to js

double[] finalDurations;
double[] steps;
double skew = 1;
int totalSteps = 10;
double endTempo = 2.2;
float startTime;

void
setup()
{
  size(300, 300);
  noStroke();
  frameRate(60);
  calc();
}

void
draw()
{
  // calc();
  background(0);
  for (int i = 0; i <= totalSteps; i++) {
    rect(10,
         i * height / (totalSteps + 1) + 12,
         (float)finalDurations[i] * 100,
         10);
  }
  // skew = pow(100, (float)mouseY/height) - 1;
  // println(skew + " " + finalDurations[5] + " " + steps[5]);
}

void
mousePressed()
{
  startTime = millis();
  float min = (float)totalSteps;
  float max = (float)endTempo * totalSteps;
  float idealSum = (((float)mouseX / width) * (max - min) + min);
  checker(idealSum);
}
void
checker(float idealSum_)
{
  double[] steps_ = new double[totalSteps];
  double[] durs_ = new double[totalSteps];
  double skew_ = 1.0;
  double naturalSum =
    (1 - endTempo) / (1 - Math.pow(endTempo, (double)1 / totalSteps));
  // println("nat = " + naturalSum +  " ideal: " + idealSum_ );
  if (naturalSum <
      idealSum_) { // reduce the skew to increase the sum toward the idealSum;
    println("making skew: nat = " + naturalSum + " ideal: " + idealSum_);
    skew_ = 0.9999;
    double sumReduction = naturalSum;
    while (sumReduction < idealSum_) {
      double cumsum = 0;
      for (int i = 0; i < totalSteps; i++) {
        steps_[i] = (Math.pow(skew_, (double)i / totalSteps) - 1) *
                    (totalSteps / (skew_ - 1));
        durs_[i] = Math.pow(endTempo, (double)steps_[i] / totalSteps);
        cumsum += durs_[i];
      }
      sumReduction = cumsum;
      if (sumReduction >= idealSum_ || skew_ < 0) {
        break;
      } else {
        skew_ -= 0.0001;
      }
    }
    float elapsedTime = System.currentTimeMillis() - startTime;
    println(skew_ + " current sum: " + sumReduction + " ideal: " + idealSum_ +
            " took: " + elapsedTime + " millis");
  } else if (naturalSum >
             idealSum_) { // increase the skew to lower toward the idealSum;
    println("making skew: nat = " + naturalSum + " ideal: " + idealSum_);
    skew_ = 1.001;
    double sumGrower = naturalSum;
    int iters = 0;
    while (sumGrower > idealSum_) {
      double cumsum = 0;
      for (int i = 0; i < totalSteps; i++) {
        steps_[i] = (Math.pow(skew_, (double)i / totalSteps) - 1) *
                    (totalSteps / (skew_ - 1));
        durs_[i] = Math.pow(endTempo, (double)steps_[i] / totalSteps);
        cumsum += durs_[i];
      }
      iters++;
      sumGrower = cumsum;
      if (sumGrower <= idealSum_ || skew_ > 999999) {
        break;
      } else {
        skew_ += 0.001;
      }
    }
    float elapsedTime = millis() - startTime;
    println(startTime);
    println(skew_ + " current sum: " + sumGrower + " ideal: " + idealSum_ +
            " took: " + elapsedTime + " millis"
            + " iters: " + iters);
  }
  // println(idealSum_);
}

void
calc()
{
  steps = new double[totalSteps];
  finalDurations = new double[totalSteps + 1];
  for (int i = 0; i < totalSteps; i++) {
    steps[i] = skew != 1 ? (Math.pow(skew, (double)i / totalSteps) - 1) *
                             (totalSteps / (skew - 1))
                         : i;
    // println(steps[i]);
    finalDurations[i] = Math.pow(endTempo, steps[i] / totalSteps);
    // println(finalDurations[i]);
  }
  finalDurations[totalSteps] = endTempo;
}