xxxxxxxxxx
123
//move mouse around the canvas
//to change the filter q value(mouseX)
//and filter envelope decay (mouseY)
//visuals
var n =40;
var d0=100;
var button = false;
var synth;
var decay = 0.1; //filter envelop decay
var q = 5; //filter Q
var B0notes = ["C2", "F3", "C2", "G#2"]; //16th notes for beat 0
var B1notes = ["C2", "C2", "F2", "C2"]; //...for beat 1
var B2notes = ["C2", "C2", "G#2", "A2"]; //... for beat 2
var B3notes = ["A#2", "B2", "C3", "C#3"]; //... for beat 3
var Bnotes = [B0notes, B1notes, B2notes, B3notes];
var kick = new Tone.Player("sounds/kick.wav").toMaster(); //beat tracker
var vol = new Tone.Volume(5);
kick.chain(vol, Tone.Master);
Tone.Transport.bpm.value = 120;
Tone.Transport.scheduleRepeat(playkick, "4n");
Tone.Transport.scheduleRepeat(playSynth, "16n");
function setup() {
createCanvas(800, 800);
button = createButton('play/stop(may need to click twice)');
button.position(19, 19);
button.mousePressed(playStop);
synth = new Tone.MonoSynth({
"oscillator": {
"type": "square"
},
"filter": {
"Q": q, //resonance at the cutoff frequency
//"frequency": 1000, //not affecting? vs Tone.filter?
"type": "lowpass",
"rolloff": -24
},
"envelope": {
"attack": 0.0001,
"decay": 0.2, //?exponential decay
"sustain": 0.5,
"release": 0.5
},
"filterEnvelope": {
"attack": 0.001,
"decay": decay, //?exponential decay 0.2 - 2s
"sustain": 0,
"min": 200,
"max": 2000
}
}).toMaster();
}
function playSynth() {
let beat = Tone.Transport.position.split(":")[1];
let sixteenth = Tone.Transport.position.split(":")[2] | 0;
//nested for loop to play beats and sixteenth;
for (let i = 0; i < 4; i++) {
if (beat == i) {
for (let j = 0; j < 4; j++) {
if (sixteenth == j) {
synth.triggerAttackRelease(Bnotes[i][j], "16n");
}
}
}
}
}
function playkick() {
if (kick.loaded) {
kick.start();
}
}
function draw() {
//mouse interaction:
//mouseX to control decay value;
synth.filterEnvelope.decay = map(mouseX, 0, height, 0.2, 5);
//synth.filter.frequency = map(mouseX, 0, height, 350, 1000);
// mouseY to control Q value;
synth.filter.Q = map(mouseY, 0, width, 0, 10);
//visual:
background(0);
noFill();
translate(width / 2, height / 2);
//center reference ellipse:
noStroke();
if(button == true){
d=d0+d0/10* sin(frameCount /4.6);
d0 = map(mouseY, 0, height, 100, 500); //size relative to mouseY
n = floor(map(mouseX, 0, width, 40, 200)); //density relative to mouseX
}else{
d=d0;
}
ellipse(0, 0, d);
//repeating ellipse:
for (a = 0; a < 2 * PI; a = a + 2 * PI / n) {
stroke(150);
ellipse(d / 2 * cos(a), d / 2 * sin(a), d);
}
}
//play/stop button
function playStop(){
if (button == false){
Tone.Transport.start();
}else{
Tone.Transport.stop();
}
button = ! button;
}