xxxxxxxxxx
53
/* Granular Synthesis by Cassie Tarakajian 🙏
https://github.com/ITPNYU/ICM-2019-Media/wiki/Homework-Cassie#week-10
https://p5js.org/reference/#/p5.SoundFile/duration
https://p5js.org/reference/#/p5.Delay
https://p5js.org/reference/#/p5.SoundFile/jump
*/
let sample;
let gong, synth, chorus;
let startTime = 0;
let duration;
let delay;
let playing = false;
function preload() {
gong = loadSound("gong.mp3");
synth = loadSound("synth.wav");
chorus = loadSound("chorus.wav");
}
function mousePressed() {
playing = true;
}
function setup() {
createCanvas(400, 400);
masterVolume(0.2);
// set sample
sample = chorus;
// get length of song
duration = sample.duration();
// create a delay object for an echo effect
delay = new p5.Delay();
}
function draw() {
background(220);
// set new playback position based on mouseX
startTime = constrain(map(mouseX, 0, width, 0, duration), 0, duration - 0.5);
if (playing) {
// play song from new position for 0.1 seconds
sample.jump(startTime, 0.1);
// output a delayed version of sound
delay.process(sample, 0.12, 0.7, 2300);
}
}