xxxxxxxxxx
53
// Array of oscillators
let oscs = [];
let ratios = [];
// Western Diatonic Major Ratios
ratios = [1, 1.125, 1.25, 1.34, 1.5, 1.67, 1.875, 2];
// Western Diatonic Minor Ratios
//ratios = [1, 1.125, 1.2, 1.34, 1.5, 1.6, 1.875, 2];
// Pentatonic Scale
//ratios = [1, 1.125, 1.25, 1.5, 1.67, 2];
// Arabic / Indian Scale
//ratios = [1, 1.067, 1.25, 1.34, 1.5, 1.6, 1.875, 2];
// Base frequency
let BASE = 300;
function setup() {
createCanvas(400, 400);
}
function draw() {
background(220);
// Loop through all the oscillators
for (let o = oscs.length - 1; o >= 0; o--) {
let osc = oscs[o];
// Get the oscillator's volume level
let volume = osc.amp().value;
// If it's reached 1, fade it out over 5 seconds
if (volume >= 1) osc.amp(0, 5);
// If it's fully faded out...
else if (volume <= 0) {
// Stop the oscillator
osc.stop();
// Remove it from the array
oscs.splice(o, 1);
}
}
}
// Create a new oscillator on each keypress
function keyPressed() {
let osc = new p5.Oscillator();
osc.setType('sine');
osc.freq(BASE * random(ratios));
osc.start();
osc.amp(0);
// Fade it in over 5 seconds
osc.amp(1, 5);
// Add it to the array of oscillators
oscs.push(osc);
}