xxxxxxxxxx
61
let sound;
let analyzer;
//サウンドファイルをプリロード
function preload() {
sound = loadSound('./beat.wav');
}
function setup() {
createCanvas(windowWidth, windowHeight);
//音量アナライザーを初期化
analyzer = new p5.Amplitude();
//サウンドを音量アナライザーに入力
analyzer.setInput(sound);
}
function draw() {
background(0);
//音量のRMS(二乗平均平方根)を計算
var rms = analyzer.getLevel();
for(let i=0; i < 10; i++){
fill(random(255), random(255), random(255));
noStroke();
//RMSの値を直径にして円を描く
ellipse(width / 2 + 100*sin(TWO_PI/10*i + frameCount * 0.03), height / 2 + 100*cos(TWO_PI/10*i + frameCount * 0.03), rms * width);
}
let vertexNum = int(rms * width / 10);
let r = rms * width /3;
beginShape();
push();
// スケッチの中心を原点に
translate(width / 2, height / 2);
for (let i = 0; i < vertexNum; i++) {
// 360度を頂点の数(六角形なら6つ)で割って、その何番目かで角度を決定
let theta = i * TWO_PI / vertexNum;
let x = r * cos(theta);
let y = r * sin(theta);
vertex(x, y);
}
endShape(CLOSE);
}
function mousePressed(){
if(sound.isPlaying() == false) {
sound.loop();
}
}
function mouseReleased() {
sound.stop();
}
//多角形の参考文献
//http://blog.livedoor.jp/reona396/archives/55768147.html