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(); }
@Override public void keyPressed() { if (key == 'r') { loopEnd = (int) (mouseX * (long) samples.length / width); } if (key == 'a') { PVector best = new PVector(0, Float.MAX_VALUE); int sampleNr = (int) (mouseX * (long) samples.length / width); for (int i = sampleNr - 500000; i < sampleNr + 500000; i++) { PVector diff = diffs.get(i); if (diff != null) { if (diff.y < best.y) best = diff; } } if (best.x != 0) { loopEnd = (int) best.x; } } if (keyCode == RIGHT) { jingle.cue((int) (loopEnd * 1000l / jingle.getFormat().getSampleRate() - 1000)); } }