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);
  }
  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		
  }
Esempio n. 3
0
  public void analyze(int start) {
    diffs.clear();

    loopStart = start;
    int SUB = 64;
    int N = 2048 * SUB;
    float maxDiff = 0;
    int upper = (int) min(samples.length - N, start + 30 * jingle.getFormat().getSampleRate());
    for (int i = start + N; i < upper - N; i++) {
      if (i % 10000 == 0) System.out.println(i + "/" + (upper - start));
      float diff = 0;
      for (int j = 0; j < N; j += SUB) {
        diff += Math.abs(samples[i + j] - samples[start + j]);
      }
      maxDiff = max(maxDiff, diff);
      diffs.put(i, new PVector(i, diff));
    }

    preview.beginDraw();
    preview.clear();
    preview.noFill();
    preview.stroke(255);
    preview.beginShape();
    for (int i = 0; i <= width; i++) {
      preview.vertex(
          i, preview.height / 2 + height / 2 * samples[(int) (i * (samples.length - 1l) / width)]);
    }
    preview.endShape();
    preview.endDraw();

    preview.beginDraw();
    preview.clear();
    preview.stroke(255, 0, 0);
    preview.beginShape();
    int n = 0;
    int prevX = 0;
    float sum = maxDiff;
    for (PVector diff : diffs.values()) {
      int x = (int) (diff.x * (long) width / samples.length);
      sum = min(diff.y, sum);
      n = 1;
      if (x != prevX) {
        preview.vertex(x, preview.height * sum / maxDiff / n);
        n = 0;
        sum = maxDiff;
        prevX = x;
      }
    }
    preview.endShape();
    preview.endDraw();
  }
 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();
   }
 }
  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		
  }
  @SuppressWarnings("unchecked")
  public static List<Map<String, Object>> receiveOSC(
      String deviceName, String oscAddressPrefix, String arguments, NodeContext context) {
    Map<String, List<Object>> oscMessages =
        (Map<String, List<Object>>) context.getData().get(deviceName + ".messages");
    if (oscMessages == null) return ImmutableList.of();
    if (oscAddressPrefix.isEmpty()) return ImmutableList.of();
    Pattern userPattern = Pattern.compile("(<[a-z0-9-_]+?(?::[ifs]|:string|:int|:float)?>)+");
    Matcher upMatcher = userPattern.matcher(oscAddressPrefix);

    Map<String, String> itemTypeMap = new HashMap<String, String>();
    ImmutableList.Builder<String> builder = ImmutableList.builder();
    while (upMatcher.find()) {
      String s = upMatcher.group(0);
      if (s.startsWith("<") && s.endsWith(">")) s = s.substring(1, s.length() - 1);
      String[] tokens = s.split(":");
      if (tokens.length == 2) {
        s = tokens[0];
        itemTypeMap.put(s, tokens[1].substring(0, 1));
      } else itemTypeMap.put(s, "s");
      builder.add(s);
    }
    ImmutableList<String> messageData = builder.build();

    ArrayList<String> argumentNames = new ArrayList<String>();
    if (!arguments.isEmpty()) {
      for (String arg : arguments.split(",")) argumentNames.add(arg.trim());
    }

    String convertedAddressPrefix = upMatcher.replaceAll("(XXXPLHXXX)");
    if (!convertedAddressPrefix.endsWith("*"))
      convertedAddressPrefix = convertedAddressPrefix + "*";
    convertedAddressPrefix = convertedAddressPrefix.replaceAll("\\*", ".*?");
    convertedAddressPrefix =
        "^" + convertedAddressPrefix.replaceAll("(XXXPLHXXX)", "[^\\/]*") + "$";

    Pattern lookupPattern = Pattern.compile(convertedAddressPrefix);
    ImmutableList.Builder<Map<String, Object>> b = ImmutableList.builder();

    int maxArgs = 0;

    for (Map.Entry<String, List<Object>> e : oscMessages.entrySet()) {
      Matcher lpMatcher = lookupPattern.matcher(e.getKey());
      if (lpMatcher.find()) maxArgs = Math.max(maxArgs, e.getValue().size());
    }

    int argNamesSize = argumentNames.size();
    for (int i = 0; i < maxArgs - argNamesSize; i++) argumentNames.add("Column");

    Map<String, Integer> argumentDuplicates = new HashMap<String, Integer>();
    for (String arg : argumentNames) {
      if (argumentDuplicates.containsKey(arg)) argumentDuplicates.put(arg, 1);
      else argumentDuplicates.put(arg, 0);
    }

    ArrayList<String> newArgumentNames = new ArrayList<String>();
    for (String arg : argumentNames) {
      if (argumentDuplicates.get(arg) > 0) {
        newArgumentNames.add(arg + argumentDuplicates.get(arg));
        argumentDuplicates.put(arg, argumentDuplicates.get(arg) + 1);
      } else newArgumentNames.add(arg);
    }

    for (Map.Entry<String, List<Object>> e : oscMessages.entrySet()) {
      Matcher lpMatcher = lookupPattern.matcher(e.getKey());

      if (lpMatcher.find()) {
        ImmutableMap.Builder<String, Object> mb = ImmutableMap.builder();
        mb.put("address", e.getKey());
        for (int i = 0; i < lpMatcher.groupCount(); i++) {
          String msg = messageData.get(i);
          String msgData = lpMatcher.group(i + 1);
          if (itemTypeMap.get(msg).equals("s")) {
            mb.put(msg, msgData);
          } else if (itemTypeMap.get(msg).equals("i")) {
            try {
              mb.put(msg, Integer.parseInt(msgData));
            } catch (NumberFormatException nfe) {
              mb.put(msg, 0);
            }
          } else if (itemTypeMap.get(msg).equals("f")) {
            try {
              mb.put(msg, Double.parseDouble(msgData));
            } catch (NumberFormatException nfe) {
              mb.put(msg, 0.0d);
            }
          }
        }
        int i = 0;
        for (Object o : e.getValue()) {
          String arg = newArgumentNames.get(i);
          mb.put(arg, o);
          i++;
        }
        for (; i < newArgumentNames.size(); i++) {
          mb.put(newArgumentNames.get(i), 0);
        }
        b.add(mb.build());
      }
    }
    return b.build();
  }