xxxxxxxxxx
let mic
let fft
// let myVoxell
let voxells = [] // empty array
function setup() {
createCanvas(windowWidth, windowHeight, WEBGL);
background(100);
mic = new p5.AudioIn() // creates new mic object
mic.start() // object method
fft = new p5.FFT()
fft.setInput(mic) // object method, passing mic as input
let step = PI/8
for (var angle = 0; angle < TWO_PI; angle += step){
myVoxell = getVoxel(0,0,0,10, angle)
voxells.push(myVoxell)
}
}
function draw() {
background(0)
let vol = mic.getLevel() // object method
let h = map(vol,0,1,25,100) // (input,curMin,curMax, targMin, tarMax)
let spectrum = fft.analyze()
push()
rotateX(mouseX/1000+frameCount/1000)
rotateY(mouseY/1000+frameCount/1000)
for (var i = 0; i < voxells.length; i++){
let amplitude = spectrum[i]
let t = map(amplitude, 0,255,1,2)
let offsets = map(amplitude, 0,255,1,10)
let myVoxell = voxells[i]
myVoxell.radius = h*t
myVoxell.display(offsets)
}
pop()
// fill(255,0,0)
// textAlign(CENTER, CENTER)
// textSize(72)
// text(h, width/2, height/2+h)
// ellipse(mouseX, mouseY, 20, 20);
}
function getVoxel(x,y,z,radius, angle){
let obj = {} // creates an empty object
obj.x = x
obj.y = y
obj.z = z
obj.angle = angle
obj.radius = radius
// make obj method called display
obj.display = function(numOffsets){
// noStroke()
noFill()
push()
rotateZ(this.angle)
translate(0,200,0)
for (var i = 0; i < numOffsets; i++){
stroke(255,i*50,i*50,50)
sphere(this.radius+i*5) // makes a sphere
}
pop()
}
return obj
}