fractalkitty looking for math? fractalkitty.com

sound

kolakoski

#trig#sound#interactive#animation#accumulates#math#number-theory#spiral

kolatimeskolaSoundspectrumwaveslevelfftsoundProcesspulsespecsbuildK

let kola;
let times = 0;
let kolaSound;
let spectrum;
let waves;
let level = 0;
let fft;

function preload() {
  kolaSound = loadSound(
    "/assets/%20%20Kolakoski.mp3"
  );
}
function setup() {
  createCanvas(windowWidth, windowHeight);
  stroke(255);
  strokeWeight(1);
  buildK(400);
  fft = new p5.FFT();
  fft.setInput(kolaSound);
  spectrum = fft.analyze();
  describe("spiral derived from kolokoski sequence with the twos getting larger that plays a sequence based sound on a click")
}

function mousePressed() {
  userStartAudio();
  if (kolaSound.isPlaying()) {
    kolaSound.pause();
  } else {
    kolaSound.loop();
  }
}

function soundProcess() {
  spectrum = fft.analyze();
  waves = fft.waveform();
  level = fft.getEnergy(20, 200) / 255;
}

function draw() {
  soundProcess();
  let pulse = level * 0.8;
  times += 0.0005;
  translate(width / 2, height / 2);
  scaleF = 2 * abs(sin(times) * cos(times) * sin(times));
  scale(0.1 + scaleF, 0.1 + scaleF);
  rotate(times * 2 + pulse / 10);
  stroke(255 - abs(255 * sin(times * 5)));
  for (let i = 20; i >= 0; i--) {
    d = 0;
    pTwo = pow(2, i);
    towerH = (height / 15) * i;
    towerB = 2;

    let specs = spectrum
      ? spectrum[floor(map(i, 0, 20, 0, spectrum.length - 1))] / 25
      : 0;
    for (let j = 0; j < kola.length; j++) {
      if (kola[j] === 1) {
        fill(50 + i * 10, 50 + i * 10, 140 + i * 10, 20);
        push();
        rotate(d / 50);
        circle(
          d - specs * 10,
          0,
          towerB / 2 + (abs(specs) / 10) * (50 * sin((j + 1) * (i * 1)))
        );
        pop();
        d += towerB;
      } else {
        fill(50 + i, 120 + i + specs / 10, 150 + specs, 10);
        push();
        strokeWeight(0.5);
        rotate(d / 50);
        circle(d, 0, towerB * pTwo + specs * 10);
        pop();
        d += pTwo * towerB;
      }
    }
  }
}

function buildK(n) {
  kola = [1];
  cnt = 0;
  currT = 2;
  currN = 2;
  currP = 1;
  while (kola.length < n) {
    if (currT === 1) {
      kola.push(currN);
      currN = 3 - currN;
      currP++;
      currT = kola[currP];
    } else if (currT === 2) {
      kola.push(currN);
      kola.push(currN);
      currP++;
      currT = kola[currP];
      currN = 3 - currN;
    }
  }
}