xxxxxxxxxx
let sound;
let fft;
//サウンドファイルをプリロード
function preload() {
sound = loadSound('./sound.mp3');
}
function setup() {
createCanvas(windowWidth, windowHeight);
colorMode(HSB,360,256,256,256);
//FFTの初期化
//FFTの長さに関しては2の累乗で指定する
fft = new p5.FFT(0.0,2048);
fft.setInput(sound);
}
function draw() {
blendMode(BLEND);
background(0);
blendMode(ADD);
noStroke();
let spectrum = fft.analyze(); //FFT解析 spectrumに配列で収まる
for(let i=0;i<spectrum.length;i++){
let x1 = map(log(i),0,log(spectrum.length),width/2,width);
let x2 = map(log(i),0,log(spectrum.length),width/2,0);
let h = map(log(i + 1),0,log(spectrum.length),0,360);
let diameter = map(spectrum[i],0,255,0,height);
fill(h,255,30,31);
circle(x1,height/2, diameter);
circle(x2,height/2,diameter);
}
}
function mousePressed(){
if(sound.isPlaying() === false){
sound.loop();
}
}
function mouseReleased(){
sound.stop();
}