メニュー 閉じる

097. p5.jsで027のサウンドイコライザーを3D変換してみる & BOXにしてみる

各周波数の大きさをBOXの大きさで。

http://www.velvet-number.com/p5_test/test066/

var sound = ['x', '*', 'o', '-'];

var targetx;
var targety;

var rotx = 0.0;
var roty = 0.0;

var drum;
var drum_string;
var fft_size = 256;

var count = 1;

function setup () {

	pixelDensity (displayDensity ());
	createCanvas (windowWidth, windowHeight, WEBGL);

	drum_string = 'x';

	drum = EDrums (drum_string);

	bass = Mono ('bass').note.seq ([0, 7], 1 / count);
	bass.amp (0.2);

	Gibber.scale.root.seq (['c4','eb4', 'g4', 'c4'], 1);

	fft = FFT (fft_size);

}

function draw() {

	background (0);

	background (0, 0, 0);



  	camera (0.0, 0.0, -1000.0,
			0.0, 0.0, 0.0,
			0.0, 1.0, 0.0);

  	targetx = mouseX;
  	targety = mouseY;

  	rotx += (targetx - rotx) * 0.06;
  	roty += (targety - roty) * 0.06;

  	rotateY (rotx * 0.01);
  	rotateZ (roty * 0.01);
  
	var num_bars = fft_size / 1;
	var bar_width = width / num_bars;
	var value = null;
    
	for (var i = 0; i < num_bars; i++ ) {
		stroke (0);
		strokeWeight (5);
		fill (255);
		rectMode (CENTER);
		value = (fft[i] / 255) * width;
		push ()
		translate ((-width / 0.5) + bar_width * 10.0 * i, 0.0, 0.0);
		box (value / 2);
		pop ();
	}

}

function mousePressed () {

	count += 1;

	if (count > 20) {
		count = 0;
		drum_string = "";
	}

	bass.kill ();
	drum.kill ();
	var num = int(random (5));
	drum_string += sound[num];
	drum = EDrums (drum_string);

	bass.kill ();
	bass = Mono ('bass').note.seq ([0, 8], 1 / count);
	bass.amp (0.2);
	
}

Posted in p5.js , processing