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);
  }
Пример #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);
  }
Пример #3
0
  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();
   }
 }
Пример #5
0
  public SoundControl(String s) throws IOException {
    MinimObject minimObj = new MinimObject();
    minim = new Minim(minimObj);
    maxBass = Constants.MAX_PLAYER_SIZE;
    frame = Constants.FRAME_TO_GENERATE_ENEMIES;
    fftThreshold = Constants.FFT_THRESHOLD;
    fftLeftThreshold = Constants.FFT_LEFT_THRESHOLD;
    consecFramesGenned = 0;

    song = minim.loadFile(s);
    songLength = song.length();
    songOver = false;

    fadeEnter = false;

    fft = new FFT(song.bufferSize(), song.sampleRate());
    fft.linAverages(bands);
    setFftLog(new FFT(song.bufferSize(), song.sampleRate()));
    getFftLog().logAverages(22, 12);

    fftPrev = new float[bands];
    fftDiff = new float[bands];
    groupings = new int[120];
    beats = new boolean[120];

    beat = new BeatDetect(song.bufferSize(), song.sampleRate());
    beat.setSensitivity(10);

    bl = new BeatListener(beat, song);
  }
  private void drawSavedValues() {
    background(0);
    // now draw current spectrum
    for (int i = 0; i < saved_freqs.length; i++) {
      int[] c = calculated_color(i);
      stroke(c[0], c[1], c[2]);
      noFill();
      int x1 =
          (int)
              map(
                  legend_width + i,
                  legend_width,
                  legend_width + spectrum_width,
                  legend_width,
                  width);
      line(x1, spectrum_height, x1, saved_freqs[i]);
      int x2 = x1 + 1;
      line(x2, spectrum_height, x2, saved_freqs[i]);
    }

    int[] color_waveform = calculated_color(loudest_freq);
    stroke(color_waveform[0], color_waveform[1], color_waveform[2]);
    for (int i = 0; i < saved_buffer.length - 1; i++) {
      line(i + 50, height / 2 + saved_buffer[i] * 50, i + 51, height / 2 + saved_buffer[i] * 50);
    }

    drawParticles();

    if (drawScale) {
      // add legend
      // frequency axis
      fill(255);
      stroke(255);
      int y = spectrum_height;
      line(legend_width, y, legend_width + spectrum_width, y); // horizontal line
      // x,y address of text is immediately to the left of the middle of the letters
      textAlign(CENTER, TOP);
      for (float freq = 0.0f; freq < in.sampleRate() / 2; freq += 2000.0) {
        int x = legend_width + (fft.freqToIndex(freq) * 2); // which bin holds this frequency
        line(x, y, x, y + 4); // tick mark
        text(Math.round(freq / 1000) + "kHz", x, y + 5); // add text label
      }

      // level axis

      int x = legend_width;
      line(x, 0, x, spectrum_height); // vertictal line
      textAlign(RIGHT, CENTER);
      for (float level = -100.0f; level < 100.0; level += 20.0) {
        y = spectrum_height - (int) (dB_scale * (level + gain));
        line(x, y, x - 3, y);
        text((int) level + " dB", x - 5, y);
      }
    }

    canvas.endDraw();
    image(canvas, 0, 0, width, height); // draw canvas streched to sketch dimensions		
  }
Пример #7
0
  public void fftLogUpdate() {
    fftThreshold = Constants.FFT_THRESHOLD;
    for (int i = 0; i < getFftLog().avgSize(); i++) {
      fftPrev[i] = getFftLog().getAvg(i);
    }
    getFftLog().forward(song.mix);

    largeAmp = getFftLog().getAvg(0);
    largeBand = 0;
    for (int i = 0; i < getFftLog().avgSize(); i++) {
      fftDiff[i] = getFftLog().getAvg(i) - fftPrev[i];
      if (fftDiff[i] >= fftThreshold) {
        beats[i] = true;
      } else {
        beats[i] = false;
      }
      if (fft.getAvg(i) > largeAmp) {
        largeAmp = getFftLog().getAvg(i);
        largeBand = i;
      }
    }
    if (enemiesGenned > 0) {
      consecFramesGenned++;
    } else if (enemiesGenned == 0) {
      consecFramesGenned--;
      if (consecFramesGenned < 0) {
        consecFramesGenned = 0;
      }
    }

    // System.out.println("EnemiesGenned: " + enemiesGenned + " consecFrames: " +
    // consecFramesGenned);

    if (consecFramesGenned == 0 && Constants.FFT_THRESHOLD > Constants.FFT_THRESHOLD_MIN) {
      Constants.FFT_THRESHOLD--;
      // System.out.println("FFT Minus: "+Constants.FFT_THRESHOLD);
    }
    if (consecFramesGenned >= 2 && Constants.FFT_THRESHOLD < Constants.FFT_THRESHOLD_MAX) {
      Constants.FFT_THRESHOLD++;
      // System.out.println("FFT Plus: "+Constants.FFT_THRESHOLD);

    }
  }
Пример #8
0
  public static List<Double> audioLogAvg(
      String deviceName, String channel, long baseFreq, long bandsPerOctave, 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);

    fft.logAverages((int) baseFreq, (int) bandsPerOctave);

    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>();
    for (int i = 0; i < fft.avgSize(); i++) b.add((double) fft.getAvg(i));
    return b.build();
  }
  public void draw() {
    canvas.beginDraw();
    if (paused) {
      drawSavedValues();
      return;
    }
    // clear window
    // background(0);
    fill(0, fill_amount); // semi-transparent white
    rect(0, 0, width, height);
    fill(random(255));
    // perform a forward FFT on the samples in input buffer
    fft.forward(in.mix);
    loudest_freq = 0;
    loudest_db = -1000;

    // now draw current spectrum
    for (int i = 0; i < spectrum_width; i++) {
      // draw the line for frequency band i using dB scale
      float val = dB_scale * (20 * ((float) Math.log10(fft.getBand(i))) + gain);
      if (fft.getBand(i) == 0) {
        val = -200;
      } // avoid log(0)
      int y = spectrum_height - Math.round(val);
      y = y > spectrum_height ? spectrum_height : y;

      if (i > 20 && val > loudest_db) {
        loudest_db = (int) val;
        loudest_freq = i;
      }

      int[] c = calculated_color(i);
      stroke(c[0], c[1], c[2]);
      noFill();
      int x1 =
          (int)
              map(
                  legend_width + i,
                  legend_width,
                  legend_width + spectrum_width,
                  legend_width,
                  width);
      // int x1 = legend_width + i;
      int x2 = x1 + 1;
      if (i < 520) {
        line(x1, spectrum_height, x1, y);
        line(x2, spectrum_height, x2, y);
      }
      saved_freqs[i] = y;
    }

    int[] color_waveform = calculated_color(loudest_freq);
    println(
        "Peak: "
            + loudest_freq
            + " dB: "
            + loudest_db
            + " Color "
            + color_waveform[0]
            + " "
            + color_waveform[1]
            + " "
            + color_waveform[2]);
    stroke(color_waveform[0], color_waveform[1], color_waveform[2]);
    drawWaveForm();

    drawParticles();

    // level axis
    if (drawScale) {
      // add legend
      // frequency axis
      fill(255);
      stroke(255);
      int y = spectrum_height;
      // line(legend_width,y,legend_width+spectrum_width,y); // horizontal line
      // x,y address of text is immediately to the left of the middle of the letters
      textAlign(CENTER, TOP);
      for (float freq = 0.0f; freq < in.sampleRate() / 2; freq += 2000.0) {
        int x = legend_width + (fft.freqToIndex(freq) * 2); // which bin holds this frequency

        line(x, y, x, y + 4); // tick mark
        text(Math.round(freq / 1000) + "kHz", x, y + 5); // add text label
      }

      int x = legend_width;
      line(x, 0, x, spectrum_height); // vertictal line
      textAlign(RIGHT, CENTER);
      for (float level = -100.0f; level < 100.0; level += 20.0) {
        y = spectrum_height - (int) (dB_scale * (level + gain));
        line(x, y, x - 3, y);
        text((int) level + " dB", x - 5, y);
      }
    }
    drawArcs();
    canvas.endDraw();
    image(canvas, 0, 0, width, height); // draw canvas streched to sketch dimensions		
  }