xxxxxxxxxx
var audio;
var peaks;
var duration;
var maxPeak;
var analyzer;
var xAngle = 0;
var yAngle = 0;
var zAngle = 0;
function preload(){
audio = loadSound('casinojaq-lowres.mp3');
}
function setup() {
createCanvas(windowWidth, windowHeight,WEBGL);
channels = audio.channels();
peaks = audio.getPeaks(width/50);
audio.loop();
// create a new Amplitude analyzer
analyzer = new p5.Amplitude();
// Patch the input to an volume analyzer
analyzer.setInput(audio);
fft = new p5.FFT();
fft.setInput(audio);
peaks = audio.getPeaks(50);
duration = audio.duration();
maxPeak = max(peaks);
console.log(peaks);
}
function draw() {
background(0);
noFill();
// noStroke();
ambientLight(255);
ambientMaterial(255);
push()
rotateX(xAngle);
rotateY(yAngle);
rotateZ(zAngle);
stroke(143,42,34,40);
sphere(analyzer.getLevel()*height/2);
pop();
let spectrum = fft.analyze();
var powerbands = [];
var maxSpectrum = max(spectrum);
push()
rotateX(xAngle);
rotateY(yAngle);
rotateZ(zAngle);
stroke(255,40);
sphere(maxSpectrum);
pop();
var numBands= 3;
for(var band=0;band<numBands;band++){
var fn = int(spectrum.length/numBands)
var power = 0;
for (i = 0; i < fn; i++) {
var x = i+ fn*band;
power += spectrum[x];
}
powerbands.push(power/spectrum.length);
if(band<numBands/2){
push();
// translate(width/band-width/2,map(power/spectrum.length,0,maxSpectrum,-height/10,height/10));
rotateX(map(power/spectrum.length,0,maxSpectrum,-HALF_PI,HALF_PI));
rotateY(map(power/spectrum.length,0,maxSpectrum,-HALF_PI,HALF_PI));
rotateZ(map(power/spectrum.length,0,maxSpectrum,-HALF_PI,HALF_PI));
stroke(255,40);
sphere(5*power/spectrum.length);
pop();
}
else{
pointLight(2*maxSpectrum, 2*maxSpectrum, 2*maxSpectrum, power/spectrum.length, power/spectrum.length, power/spectrum.length);
// pointLight(map(power/spectrum.length,0,maxSpectrum,-HALF_PI,HALF_PI),map(power/spectrum.length,0,maxSpectrum,-HALF_PI,HALF_PI))
}
}
xAngle = map(powerbands[0],0,maxSpectrum,-PI,PI)+frameCount/60;
yAngle = map(powerbands[1],0,maxSpectrum,-PI,PI)+frameCount/60;
zAngle = map(powerbands[2],0,maxSpectrum,-PI,PI)+frameCount/60;
}
var socket = io.connect($OP.getEchoServerURL(702275));
Learn more See an example