xxxxxxxxxx
243
var osc;
var delay = new Tone.FeedbackDelay(0.5).connect(Tone.Master);
var reverb = new Tone.JCReverb(0.01).connect(Tone.Master);
var tremolo = new Tone.Tremolo(9, 0.75).toMaster();
var mic, fft;
var colorName = "gray";
var vol = new Tone.Volume(-12);
function setup() {
createCanvas(1000, 500);
background('gray');
Tone.Transport.start();
Tone.Master.mute = true;
noFill();
mic = new p5.AudioIn();
mic.start();
fft = new p5.FFT();
fft.setInput(mic);
}
// Audio
osc = new Tone.Oscillator(200, "sine").connect(delay).toMaster().start();
osc.toMaster();
osc.start();
//Synths are capable of a wide range of sounds depending on their settings
var synthA = new Tone.Synth({
oscillator: {
type: 'square8',
modulationType: 'sine',
modulationIndex: 3,
harmonicity: 3.4
},
envelope: {
attack: 0.001,
decay: 0.1,
sustain: 0.1,
release: 0.1
}
}).connect(delay).toMaster();
var synthApattern = new Tone.Pattern(function(time, note) {
synthA.triggerAttackRelease(note, time);
}, ["E2", "D3", "C3", "G3", "A3"], "alternateDown");
synthApattern.humanize = true;
var synthB = new Tone.Synth({
oscillator: {
type: 'triangle8'
},
envelope: {
attack: 2,
decay: 1,
sustain: 0.4,
release: 4
}
}).connect(delay).toMaster();
var synthBpattern = new Tone.Pattern(function(time, note) {
synthB.triggerAttackRelease(note, time);
}, ["E2", "D3", "C3", "G3", "A3"], "alternateUp");
synthBpattern.humanize = true;
var synthC = new Tone.PolySynth(4, Tone.Synth).connect(delay).toMaster();
synthC.set({
"filter": {
"type": "lowpass"
},
"envelope": {
"attack": 0.25
}
});
var chord_1 = ["E3", "G3", "C2"];
var chord_2 = ["C3", "E3", "G3"];
var chord_3 = ["G3", "E3", "D3"];
var chord_4 = ["D3", "G3", "E3"];
var pianoPart = new Tone.Part(function(time, chord) {
synthC.triggerAttackRelease(chord, "8n", time);
}, [
["0:0", chord_1],
["0:05", chord_2],
["0:1", chord_3],
["0:2", chord_4]
])
var synthD = new Tone.DuoSynth().toMaster();
synthC.set({
"filter": {
"type": "highpass"
},
"envelope": {
"sustain": 1
}
});
var synthDpattern = new Tone.Pattern(function(time, note) {
synthD.triggerAttackRelease(note, time);
}, ["E2", "D3", "C3", "G3", "A3"], "alternateDown");
synthDpattern.humanize = true;
var synthE = new Tone.MonoSynth({
"oscillator": {
"type": "square3"
},
"envelope": {
"attack": 0.05,
"decay": 0.3,
"sustain": 0.4,
"release": 0.8,
},
"filterEnvelope": {
"attack": 0.001,
"decay": 0.7,
"sustain": 0.1,
"release": 0.8,
"baseFrequency": 400,
"octaves": 4
}
}).connect(tremolo).toMaster();
var synthEpattern = new Tone.Pattern(function(time, note) {
synthE.triggerAttackRelease(note, time);
}, ["E2", "D3", "C3", "G3", "A3"], "alternateDown");
synthDpattern.humanize = true;
document.querySelector('#synthC').addEventListener('mousedown', function() {
//an array of notes can be passed into PolySynth
synthC.triggerAttack(['C4', 'E2', 'G4', 'B4', 'E3'])
})
document.querySelector('#synthC').addEventListener('mouseup', function() {
//unlike the other instruments, the notes need to be passed into triggerRelease
synthC.triggerRelease(['C4', 'E4', 'G4', 'B4', 'E4'])
})
//mouse events
document.querySelector('#synthA').addEventListener('mousedown', function() {
synthApattern.start();
orangeButton();
})
document.querySelector('#synthA').addEventListener('mouseup', function() {
synthApattern.stop();
clearButton();
})
document.querySelector('#synthB').addEventListener('mousedown', function() {
synthBpattern.start();
greenButton();
})
document.querySelector('#synthB').addEventListener('mouseup', function() {
synthBpattern.stop();
clearButton();
})
document.querySelector('#synthC').addEventListener('mousedown', function() {
pianoPart.start();
yellowButton();
})
document.querySelector('#synthC').addEventListener('mouseup', function() {
pianoPart.stop();
clearButton();
})
document.querySelector('#synthD').addEventListener('mousedown', function() {
synthDpattern.start();
pinkButton();
})
// document.querySelector('#synthD').addEventListener('mouseup', function() {
// synthDpattern.stop();
// clearButton();
// })
document.querySelector('#synthE').addEventListener('mousedown', function() {
synthEpattern.start();
magentaButton();
})
// document.querySelector('#synthE').addEventListener('mouseup', function() {
// synthEpattern.stop();
// clearButton();
// })
document.querySelector('#vol').addEventListener('mousedown', function() {
if (Tone.Master.mute == false) {
Tone.Master.mute = true;
soundVisualizer();
} else if (Tone.Master.mute == true) {
Tone.Master.mute = false;
}
var slider = document.getElementById("myRange");
var output = document.getElementById("demo");
output.innerHTML = slider.value; // Display the default slider value
// Update the current slider value (each time you drag the slider handle)
slider.oninput = function() {
output.innerHTML = this.value;
}
})
function clearButton() {
colorName = "gray";
}
function orangeButton() {
colorName = "orange";
}
function greenButton() {
colorName = "lightgreen";
}
function yellowButton() {
colorName = "yellow";
}
function pinkButton() {
colorName = "lightpink";
}
function magentaButton() {
colorName = "magenta";
}
function soundVisualizer() {
var spectrum = fft.analyze();
fill(colorName);
beginShape();
for (i = 0; i < spectrum.length; i++) {
vertex(i, map(spectrum[i], 0, 255, height, 0) );
}
endShape();
}
function draw() {
var sliderValue = document.getElementById("myRange").value;
osc.frequency.value = map(mouseX, 0, width, 40, sliderValue);
if (Tone.Master.mute == false) {
soundVisualizer();
} else {}
}