xxxxxxxxxx
67
let interval1;
let interval2;
let synth = new Tone.PolySynth();
synth.set({
"volume": 0.2,
"envelope": {
"attack": 0.02,
"decay": 0.1,
"release": 0.5
}
})
synth.toMaster();
//Melodies
let ml = new Tone.Loop(melodyLoop, "2n");
let major = [0, 2, 4, 5, 7, 9, 11];
let minor = [0, 2, 3, 5, 7, 9, 10];
let root = 21; // lowest A in MIDI
let octave = 1;
let myScale = major; // try minor and other modes
function melodyLoop(time) {
let degree = floor(random(0, myScale.length));
playDiatonicChord(degree, time);
}
function playDiatonicChord(degree, time) {
playNote(degree, time);
playNote(degree + 2, time);
playNote(degree + 4, time);
playNote(degree + 6, time);
}
function playNote(pos, time) {
let degree = pos % myScale.length;
let octave2 = octave + floor(pos / myScale.length);
let note = root + myScale[degree] + octave2 * 12;
let noteObject = Tone.Frequency(note, "midi");
synth.triggerAttackRelease(noteObject, "2n", time);
}
// ...and one might want to separate the calculation of the MIDI note from the actual playback
// Start
function mouseClicked() {
if (Tone.context.state !== 'running') {
Tone.context.resume();
}
Tone.Transport.start();
ml.start();
}
function setup() {
createCanvas(400, 400);
//createSlider(min, max, value, step)
}
function draw() {
background(220);
}