xxxxxxxxxx
95
// A simple bass synth with a rhythm generated by
// adding two generated euclidean rhythms
//
// uses: Algo.euclid(), Util.add()
let Algo = TotalSerialism.Algorithmic;
let Util = TotalSerialism.Utility;
let osc, osc2, envelope, fft, reverb, filter, delay;
let notes = [];
let note = 0;
let rhythm = [];
let beat = 0;
let period = 1;
let count = 0;
function setup() {
createCanvas(windowWidth, windowHeight);
// create oscillators and filter
osc = new p5.SawOsc();
osc2 = new p5.SqrOsc();
filter = new p5.LowPass();
filter.freq(400);
// Instantiate the envelope
envelope = new p5.Envelope();
// set attackTime, decayTime, sustainRatio, releaseTime
envelope.setADSR(0.001, 0.05, 0.5, 0);
// set attackLevel, releaseLevel
envelope.setRange(1, 0);
osc.start();
osc2.start();
// disconnect sound from output first
osc.disconnect();
osc2.disconnect();
// connect filter to osc
osc.connect(filter);
osc2.connect(filter);
// for visuals
fft = new p5.FFT();
rectMode(CENTER);
noStroke();
// A simple array of notes
notes = [ 36, 39, 45, 43 ];
console.log('notes:', notes);
// ALGORITHMIC COMPOSITION
// rhythm for the bassline, 2 euclidean rhythms added together
rhythm = Util.add(Algo.euclid(16, 5), Algo.euclid(11, 3, 2));
console.log('rhythm:', rhythm);
}
function draw() {
background(0);
// play a note every 8 frames (about every 133 ms)
if (frameCount % 8 === 0 || frameCount === 1) {
if (count % 16 === 0){
// increment the next note every 16 notes
note = (note + 1) % notes.length;
}
// only play the note if rhythm > 0
if ( rhythm[beat] > 0){
let midiValue = notes[note];
let freqValue = midiToFreq(midiValue);
osc.freq(freqValue);
osc2.freq(freqValue * 1.01213);
envelope.play(osc, 0, 0.1);
envelope.play(osc2, 0, 0.1);
}
// count the next beat
beat = (beat + 1) % rhythm.length;
count++;
}
// FFT analys for visuals
translate(0, height/2);
let spectrum = fft.analyze();
for (let i = 0; i < spectrum.length / 20; i++) {
fill(spectrum[i]);
let x = map(i, 0, spectrum.length / 20, 0, width);
let h = pow(spectrum[i]/255, 10) * height * 0.9;
rect(x, 0, spectrum.length / 100, h);
}
}