public void setup() {
    int window_height = 220;
    size((int) (window_height * 5.12f), 220);
    canvas = createGraphics((int) (window_height * 5.12f) / 2, 220, JAVA2D);

    textMode(SCREEN);
    textFont(createFont("SanSerif", 12));

    minim = new Minim(this);

    in = minim.getLineIn(Minim.MONO, buffer_size, sample_rate);

    // create an FFT object that has a time-domain buffer
    // the same size as line-in's sample buffer
    fft = new FFT(in.bufferSize(), in.sampleRate());
    // Tapered window important for log-domain display
    fft.window(FFT.HAMMING);

    // initialize peak-hold structures
    peaksize = 1 + Math.round(fft.specSize() / binsperband);
    peaks = new float[peaksize];
    peak_age = new int[peaksize];

    particles = new Particle[fft.specSize()];
    for (int i = 0; i < fft.specSize(); i++) particles[i] = new Particle(i);
  }
Example #2
0
  public void draw() {
    background(0);
    stroke(255);

    // perform a forward FFT on the samples in jingle's mix buffer,
    // which contains the mix of both the left and right channels of the file
    fft.forward(jingle.mix);

    for (int i = 0; i < fft.specSize(); i++) {
      // draw the line for frequency band i, scaling it up a bit so we can see it
      line(i, height / 2, i, height / 2 - fft.getBand(i) * 4);
    }

    stroke(255, 0, 0);
    for (int beat : beats) {
      float x = beat * width / totalSamples;
      line(x, 0, x, height);
    }

    stroke(255, 255, 0);
    float x = jingle.position() * width / jingle.length();
    line(x, 0, x, height);

    image(preview, 0, 0);

    stroke(0, 255, 0);
    float s = (float) width / samples.length;
    line(loopStart * s, 0, loopStart * s, height);
    line(loopEnd * s, 0, loopEnd * s, height);
  }
  public static List<Double> audioAnalysis(
      String deviceName, String channel, long averages, NodeContext context) {
    AudioSource source = (AudioSource) context.getData().get(deviceName + ".source");
    if (source == null) return ImmutableList.of();
    FFT fft = new FFT(source.bufferSize(), source.sampleRate());
    fft.window(FFT.HANN);

    if (averages > 0) fft.linAverages((int) averages);

    if (channel.equals("left")) {
      fft.forward(source.left);
    } else if (channel.equals("right")) {
      fft.forward(source.right);
    } else {
      fft.forward(source.mix);
    }

    ImmutableList.Builder<Double> b = new ImmutableList.Builder<Double>();
    if (averages == 0) {
      for (int i = 0; i < fft.specSize(); i++) b.add((double) fft.getBand(i));
    } else {
      for (int i = 0; i < fft.avgSize(); i++) b.add((double) fft.getAvg(i));
    }
    return b.build();
  }
 private void drawParticles() {
   pushStyle();
   colorMode(RGB, 255);
   // background(0);
   popStyle();
   for (int i = 0; i < fft.specSize() - 1; i++) {
     float val = dB_scale * (20 * ((float) Math.log10(fft.getBand(i))));
     if (fft.getBand(i) == 0) {
       val = -200;
     } // avoid log(0)
     particles[i].update(val);
     particles[i].render();
   }
 }