xxxxxxxxxx
// FOR THE WWWCHALLENGE/ UNSATISFYING. USE YOUR VOICE OR THE MOUSE TO INTERACT AND MAKE SOME NOISE.
let poly = []
var mic;
let n = 1000 // feel free to play with this number :)
let w = 500
let h = 500
// oscillators
let chord = []
let root = 30
let major = [ 7, 8, 9 ]
let minor = [ 20, 22, 25 ]
function setup() {
createCanvas(500, 500);
n++ // add extra point for closing the polygon
// Create an Audio input
mic = new p5.AudioIn();
mic.start();
for (let i = 0; i < n; i++) {
// populate regular polygon vertices given number of points n
let a = {
x: (w/2) + 100*sin(map(i, 0, n-1, 0, TAU)),
y: (h/2) + 100*cos(map(i, 0, n-1, 0, TAU))
}
poly.push(a)
}
// initialize oscillators
if (n < 25) {
for (let i = 0; i < 3; i++)
chord[i] = new p5.TriOsc()
} else {
for (let i = 0; i < 3; i++)
chord[i] = new p5.SinOsc()
}
// initialize with major chord intervals
for (let i = 0; i < chord.length; i++) {
chord[i].freq(major[i] * root)
chord[i].amp(0.0)
chord[i].stop()
}
}
function draw() {
let a = color(255, 0, 0);
let b = color(0, 255, 0);
let c = color(0, 0, 255);
// use default blend mode for background
blendMode(BLEND)
background(50, 10);
noFill();
stroke(1);
// Get the overall volume (between 0 and 1.0)
// Map to height
// use additive blend mode to separate color channels
blendMode(ADD);
let r = map(mic.getLevel(), 0, 0.7, 0, height);
stroke(a);
ellipse(width/2, height/2, r, r);
blendMode(ADD);
let s = map(mic.getLevel(), 0, 0.7, 0, height);
stroke(b);
ellipse(width/2, height/2, r+10, r+10);
blendMode(ADD);
let t = map(mic.getLevel(), 0, 0.7, 0, height);
stroke(c);
ellipse(width/2, height/2, r+20, r+20);
// distort oscillatiors
warpOsc()
}
function warpOsc() {
// uses max dist to determine the frequency distortion
let bias = 0
for (let i = 0; i < n; i++)
bias = max(bias, dist(mouseX, mouseY, poly[i].x, poly[i].y))
for (let i = 0; i < chord.length; i++)
chord[i].freq(map(bias, w, 0, major[i], minor[i]) * root)
}
function mousePressed() {
// toggles synths on
for (let i = 0; i < chord.length; i++) {
chord[i].start()
chord[i].amp(0.3, 0.5)
}
}
function mouseReleased() {
// toggles synths off
for (let i = 0; i < chord.length; i++) {
chord[i].amp(0.0, 0.05)
chord[i].stop()
}
}