fractalkitty looking for math? fractalkitty.com

sound

unwound

#trig#classes#sound#interactive#animation#study#music

kittyplayBoolsArrayspectrumwavesfftsoundProcessdots

let kitty;
let playBool = false;
let sArray = [];
let spectrum;
let waves;
let fft;
let t = 0;
let n = 200;
function preload() {
  userStartAudio();
  kitty = loadSound("/assets/pendulum.mp3");
}
function setup() {
  sArray = [];
  createCanvas(windowWidth, windowHeight);
  background(100);
  kitty.amp(0.4);
  fft = new p5.FFT();
  spectrum = fft.analyze();
  for (let i = 0; i < spectrum.length; i++) {
    sArray.push(new dots(i));
  }
}

function draw() {
  translate(width / 2, height / 2);
  if (playBool) {
    t += 0.001;

    if (!kitty.isPlaying()) {
      kitty.play();
    }
  } else {
    kitty.pause();
  }
  rotate(t);
  background(200 + 255 * cos(t), 255 + 255 * cos(t), 255 + 255 * cos(t), 10);
  soundProcess();
}

function soundProcess() {
  spectrum = fft.analyze();
  waves = fft.waveform();
  for (let i = 0; i < spectrum.length; i += 10) {
    sArray[i].display(spectrum[i] / 10, waves[i]);
  }
}

class dots {
  constructor(i) {
    this.L = i / 10 + width / 10;
    this.theta = PI;
    this.x = this.L * sin(this.theta);
    this.y = this.L * cos(this.theta);
    this.t = 0;
  }
  display(dt, w) {
    this.t += 0.0001 + abs(w / 20 + dt / 5000);
    this.theta = TWO_PI * 2 * sin(this.t);
    this.x =
      (this.L * sin(this.theta * sin(this.t)) * sin(this.theta + this.y / 5)) /
        2 +
      (this.L / 2) * sin(this.theta + this.x / 100);
    this.y = this.L * cos(this.theta) + (this.L / 3) * cos(this.theta);
    fill(
      255 * sin(t / 2),
      355 * sin(t / 2) - this.y,
      355 * sin(t / 2) - this.x
    );
    circle(this.x, this.y, 10 + dt);
    strokeWeight(0.1);
    stroke(
      255 * sin(t) + 50,
      355 * sin(t) - this.y + 100,
      355 * sin(t) - this.x + 100,
      10
    );
    let f = width / 2;

    for (let k = 0; k < n; k++) {
      line(
        this.x,
        this.y,
        -f * sin(t + (k * PI * 2) / n),
        -f * cos(t + (k * PI * 2) / n)
      );
    }
    noStroke();
  }
}
function mousePressed() {
  playBool = !playBool;
}
function keyPressed() {
  kitty.stop();
  setup();
  draw();
}