xxxxxxxxxx
77
// TODO: beat rect전용(위치, boolean 속성) 클래스 만들기
// reference: https://p5js.org/reference/#/libraries/p5.sound
let sound, delay;
let beats = [1, 0, 0, 0, 1, 0, 0, 0];
let beatCnt = 8;
function preload() {
sound = loadSound("sound1.wav");
sound.setVolume(10);
}
function setup() {
canvas = createCanvas(400, 400);
canvas.mousePressed(playSound);
let offset = 1 / beatCnt;
print("offset: " + offset);
}
function draw() {
background(220);
drawBeatRects();
}
function playSound() {
sound.play();
}
function startBeat() {
sound.setLoop(false);
for (let i = 0; i < beatCnt; i++) {
if (beats[i]) {
sound.loop(0 + (i * offset), undefined, undefined, undefined, 1.0);
}
}
}
function drawBeatRects() {
fill(255, 0, 0);
let w = width / beatCnt;
let h = height / beatCnt;
for(let i = 0; i < beatCnt; i++) {
rect(0 + (i * w), 0, w, h);
}
}