xxxxxxxxxx
101
let sampler = new Tone.Sampler({
"A4": "samples/violin/A4.mp3"
// "C4": "samples/violin/C4.mp3"
});
sampler.envelope = {
attack: 0.2,
decay: 0.5,
sustain: 0.5,
release: 0.1
}
sampler.toMaster();
var vibrato = new Tone.Vibrato({
maxDelay : 0.005 ,
frequency : 5 ,
depth : 0.1
}).toMaster();
sampler.connect(vibrato);
// var pingPong = new Tone.PingPongDelay("1n", 0.05).toMaster();
// sampler.connect(pingPong);
// var distortion = new Tone.Distortion(0.5).toMaster();
// sampler.connect(distortion);
function keyPressed() {
let pitch;
if(key == '1'){
pitch = "A4";
}
else if(key == '2'){
pitch = "B4";
}
else if(key == '3'){
pitch = "C5";
}
else if(key == '4'){
pitch = "D5";
}
else if(key == '5'){
pitch = "E5";
}
else if(key == '6'){
pitch = "F5";
}
else if(key == '7'){
pitch = "G5";
}
else if(key == '8'){
pitch = "A5";
}
else if(key == '9'){
pitch = "B5";
}
else if(key == '0'){
pitch = "C6";
}
if(pitch && sampler.loaded){
// Sampler will repitch the closest sample
sampler.triggerAttack(pitch);
}
}
function keyReleased() {
sampler.triggerRelease();
}
function setup() {
createCanvas(700, 600);
// mic = new Tone.UserMedia();
analyzer = new Tone.Waveform(256);
sampler.connect(analyzer);
// mic.open();
}
function draw(){
background(0);
let waveform = analyzer.getValue();
strokeWeight(2);
noFill();
stroke(255);
beginShape();
for (let i = 0; i < waveform.length; i++) {
let x = map(i, 0, waveform.length, 0, width);
let y = map(waveform[i], -1, 1, 600, 0);
vertex(x, y);
}
endShape();
}
let mic;
let analyzer;