xxxxxxxxxx
let sound;
let fft;
//サウンドファイルをプリロード
function preload() {
sound = loadSound('./beat.wav');
}
function setup() {
createCanvas(windowWidth, windowHeight);
//FFTを初期化
fft = new p5.FFT();
//サウンドファイルをFFTの入力に
fft.setInput(sound);
}
function draw() {
background(0);
//マウスクリックでサウンドループ再生
if(mouseIsPressed && sound.isPlaying() == false){
sound.loop();
}
stroke(255);
noFill();
//FFT解析
let spectrum = fft.analyze();
//結果をグラフで描画
beginShape();
for (i = 0; i < spectrum.length; i++) {
let x = map(i, 0, spectrum.length - 1, 0, width);
let y = map(spectrum[i], 0, 255, height, 0);
vertex(x, y);
}
endShape();
}