I’m struggling with a simple code where I want to store an array of arrays containing FFT audio data. It seems like there might be a JavaScript issue because when I try to push the array into another array called spectrums, all the values inside spectrums turn out to be 0s instead of their actual values. I am using array.splice(0) for what’s supposed to be a deep copy. Can anyone spot what I’m doing wrong here?
var fft,mic;
function setup(){
var myCanvas = createCanvas(800,800);
fft = new p5.FFT();
ellipse(400,400,50,50)
colorMode(HSB,100)
spectrums=[]
mic = new p5.AudioIn();
mic.start();
fft = new p5.FFT();
fft.setInput(mic);
}
function draw() {
background(255)
s = fft.analyze(16)
for(si=0;si<s.length;si++){
fill(s[si]%100,100,100)
rect(si*10,0,si*10,s[si])
}
spectrums.push(s.splice(0))
if(spectrums.length > 5){
spectrums.splice(-1,1)
}
console.log(spectrums[0][0]) //this prints 0 always
for(si=0;si<spectrums[0].length;si++){
fill(spectrums[0][si]%100,100,100)
rect(si*10,400,si*10,spectrums[0][si])
}
}