xxxxxxxxxx
43
//understanding frequency
let osc;
//diatonic ratio
ratios = [1, 1.125, 1.25, 1.33, 1.5, 1.66, 1.875, 2];
//pentatonic = skip 1.34, 1.875
function setup() {
createCanvas(400, 400);
osc = new p5.Oscillator();
osc.setType("sine");
osc.freq(0);
osc.start();
}
function draw() {
background(220);
}
let BASE = 200;
let t = 0;
function keyPressed() {
/*
let f = random(50,500); //sci-fi
let f = random(200,400); //bizarre
let f = random(50,200); //low freq, cave, tunnel
let r = random(ratios); //diatonic scale of index value
*/
//let r = noise(t) * 8 //average of random value
//t += 1;
//if random is greater than 0.3, use 0.5, if not use 1 - in line conditional
t += random(1) > 0.3 ? 0.5 : 1;
let r = (sin(t) + 1) * 4; //using sine wave to create a melody
let f = ratios[floor(r)] * BASE;
osc.freq(f);
}