Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

Check whether an audio input is done initializing?

$
0
0

Often when I execute the following code, I'll get a crash. "Java Platform SE binary has stopped working" (Windows 7)

But at the point of the crash, a couple of max values were already printed in the console. All of them being 0.0.

import processing.sound.*;

FFT fft;
AudioIn in;
int bands = 512;
float[] spectrum = new float[bands];

void setup() {
  size(512, 360);
  background(255);

  // Create an Input stream which is routed into the Amplitude analyzer
  fft = new FFT(this, bands);
  in = new AudioIn(this, 0);

  // start the Audio Input
  in.start();

  // patch the AudioIn
  fft.input(in);
}

void draw() {
  background(255, 0, 0);

  // have to check somehow whether this is ready
  fft.analyze(spectrum);

  float max = 0;
  for(int i = 0; i < bands; i++){
    // The result of the FFT is normalized
    // draw the line for frequency band i scaling it up by 5 to get more amplitude.
    line( i, height, i, height - spectrum[i]*height*50 );
    if(spectrum[i]*50 > max)
      max = spectrum[i];
  }

  print(max + "\n");
}

Viewing all articles
Browse latest Browse all 2896

Trending Articles