xxxxxxxxxx
221
var osc;
var delay = new Tone.FeedbackDelay(0.5).connect(Tone.Master);
var reverb = new Tone.JCReverb(0.01).connect(Tone.Master);
var crusher = new Tone.BitCrusher(6).toMaster();
var tremolo = new Tone.Tremolo(9, 0.75).toMaster();
var pitch = new Tone.PitchShift(-6).toMaster();
var vol = new Tone.Volume(-100).toMaster();
var mic, fft;
var colorName = "gray";
function setup() {
createCanvas(1000, 700);
background('gray');
Tone.Transport.start();
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: 'triangle7',
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);
}, ["C2", "D2", "E3", "G3", "A4"], "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);
}, ["C2", "D2", "E3", "G3", "A4"], "alternateUp");
synthBpattern.humanize = true;
var synthC = new Tone.PolySynth(4, Tone.Synth).connect(crusher).toMaster();
synthC.set({
"filter": {
"type": "lowpass"
},
"envelope": {
"attack": 0.25
}
});
//Mary Kate's progression
//Em C G D
var e_chord = ["E3", "G3", "C2"];
var C_chord = ["C3", "E3", "G3"];
var G_chord = ["G3", "E3", "D3"];
var D_chord = ["D3", "G3", "E3"];
var pianoPart = new Tone.Part(function(time, chord) {
synthC.triggerAttackRelease(chord, "8n", time);
}, [
["0:0", e_chord],
["0:05", C_chord],
["0:1", G_chord],
["0:2", D_chord]
])
var synthD = new Tone.MembraneSynth().connect(vol).toMaster();
synthC.set({
"filter": {
"type": "highpass"
},
"envelope": {
"sustain": 0.25
}
});
var synthDpattern = new Tone.Pattern(function(time, note) {
synthD.triggerAttackRelease(note, time);
}, ["C2", "D2", "E3", "G3", "A4"], "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);
}, ["C2", "D2", "E3", "G3", "A4"], "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();
})
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 draw() {
osc.frequency.value = map(mouseX, 0, width, 40, 200);
var spectrum = fft.analyze();
fill(colorName);
beginShape();
for (i = 0; i < spectrum.length; i++) {
vertex(i, map(spectrum[i], 0, 200, height, 0));
}
endShape();
}