xxxxxxxxxx
39
let cnv, soundFile, pitchShifter;
let shifts = [-1, 1, 2, -2, 8, 4];
function preload() {
soundFile = loadSound('dd2ed901-8e3b-49d4-b57e-78908870861e.mp3');
}
function setup() {
describe('a sketch that loops a sample of a guitar note, each time the note is played the pitch is shifted');
cnv = createCanvas(100, 100);
cnv.mousePressed(startSound);
background(220);
textAlign(CENTER);
textSize(9);
text('click to play sound', width/2, height/2);
pitchShifter = new p5.PitchShifter();
soundFile.disconnect();
soundFile.connect(pitchShifter);
//change the pitch and retrigger sample when done playing
soundFile.onended(changePitch);
}
function startSound () {
//only let the sound file play once
if(!soundFile.isPlaying()) {
soundFile.play();
background(220);
text('sound is playing', width/2, height/2);
}
}
function changePitch () {
let pitchValue = random(shifts);
pitchShifter.shift(pitchValue);
soundFile.play();
}