unable to copy fft arrays in p5js, should be simple, what am i missing?

I have this simple code, Im trying to keep an array of arrays , which is just the FFT audio data. I think this is a javascript issue, but Im not sure what is wrong. fft.analyze() just returns an array of numbers. after I push the array into the array spectrums, all the values inside of spectrums are 0s. Using array.splice(0) is supposed to be a deep copy. Can anyone tell me what Im 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]) 
 }
}

Answers

Sign In or Register to comment.