fractalkitty looking for math? fractalkitty.com

Genuary 2024

radial chimes

#random#trig#sound#interactive#animation#polar#music#genuary

sorry for the memory usage - //PLEASE NOTE - I AM STILL WORKING ON THIS CODE!

playBoolnotesanglesradiicmultpNotesmelodymelodyHmelodyLtLimitborderstopAllSounds


let playBool = false;
let notes = [];
let t = 0;
let angles;
let radii;
let cmult;
let pNotes;
let melody, melodyH, melodyL;
let tLimit = 4;
let t1;
function preload() {
  userStartAudio();
  i = 45;
  cnt = 0;
  while (i < 66) {
    notes[cnt] = loadSound(
      "/assets/" + str(i) + ".mp3"
    );
    cnt++;
    i += 2;
    notes[cnt] = loadSound(
      "/assets/" + str(i) + ".mp3"
    );
    cnt++;
    i += 2;
    notes[cnt] = loadSound(
      "/assets/" + str(i) + ".mp3"
    );
    cnt++;
    i += 1;
    notes[cnt] = loadSound(
      "/assets/" + str(i) + ".mp3"
    );
    cnt++;
    i += 2;
    notes[cnt] = loadSound(
      "/assets/" + str(i) + ".mp3"
    );
    cnt++;
    i += 2;
    notes[cnt] = loadSound(
      "/assets/" + str(i) + ".mp3"
    );
    cnt++;
    i += 1;
  }
}
function setup() {
  createCanvas(windowWidth, windowHeight);
  c = min(width, height);
  nNotes = int(random(10, 32));
  interval = 360 / nNotes;
  r1 = random(0, 255);
  g1 = random(0, 255);
  b1 = random(0, 255);
  pNotes = [];
  angles = [];
  radii = [];
  cmult = [];
  for (let i = 0; i < nNotes; i++) {
    pNotes[i] = random(notes);
    angles[i] = random(0, 360);
    radii[i] = random(0, c * 0.4);
    cmult[i] = random(0.1, 1.2);
  }
  angleMode(DEGREES);

  for (let i = 0; i < notes.length; i++) {
    if (i < notes.length / 3 || i > notes.length / 1.7) {
      notes[i].amp(0.2);
    } else {
      notes[i].amp(0.4);
    }
  }
  nRot = random(1, 5);
  delta = random(0.1, 0.4);
}

function draw() {
  translate(width / 2, height / 2);
  background(r1 - 100, g1 - 100, b1 - 100, 2);
  t1 = frameCount;

  rotate(t1 / 20);
  t += delta;
  stroke(255, 255, 255, 10);
  for (let i = 0; i < nRot; i++) {
    line(0, 0, c * cos(t + (360 / nRot) * i), c * sin(t + (360 / nRot) * i));
  }

  for (let i = 0; i < pNotes.length; i++) {
    cond1 = false;
    for (let h = 0; h < nRot; h++) {
      if (abs(((t + (360 / nRot) * h) % 360) - angles[i]) <= delta) {
        cond1 = true;
      }
    }

    if (cond1) {
      fill(r1 * cmult[i], g1 * cmult[i], b1 * cmult[i], 20);
      if (pNotes[i].currentTime() < 1) {
      } else {
        pNotes[i].stop();
      }
      if (playBool) {
        pNotes[i].play();
      }
      tmpR = 100;
    } else {
      fill(r1 * 1.2, g1 * 1.2, b1 * 1.2, 100);
      tmpR = 10;
    }
    if (pNotes[i].currentTime() > tLimit) {
      pNotes[i].stop();
    }
    circle(radii[i] * cos(angles[i]), radii[i] * sin(angles[i]), tmpR);
  }
}

function mousePressed() {
  playBool = true;
  setup();
  draw();
}

function keyPressed() {
  stopAllSounds();
  playBool = !playBool;
}

function border() {
  noFill();
  stroke(20, 50, 60);
  strokeWeight(4);
  rect(0, 0, width - 20, height - 20);
}

function stopAllSounds() {
  for (let i = 0; i < notes.length; i++) {
    notes[i].stop();
  }
}