sclass SimpleBaseDrumFinder2 { double windowSize = 5.0; // seconds int imageHeight = 10; // must be >= segmenter.gridSize transient ISegmenter segmenter; transient new L buf; transient int bufSize; transient BWImage lastImage; transient L ranges; void addFrequencyImageBrightness(double b) { if (ra == null) ra = new RollingAverage(windowSize); if (rm == null) rm = new RollingMaximum(windowSize); ra.add(b); double max = rm.addAndGet(b), avg = ra!; ++count; bool ascending = b >= lastValue; lastValue = b; if (downSince != 0 && count < downSince+minDownTime) ret; // mark start of ascent if (ascending && ascendingSince == 0) { ascendingSince = count; avgAtAscendStart = avg; } if (ascendingSince != 0 && (!ascending || ascendingSince+maxAscentTime == count)) if (lastValue >= minAbsoluteLoudness) trigger(ascendingSince, count-1, b/max(0.001, avgAtAscendStart)); if (!ascending) ascendingSince = 0; } void trigger(long begin, long end, double ratio) { if (ratio < factor) ret; print("Base drum found from " + begin + " to " + end + ", ratio=" + ratio + ", last loudness: " + lastValue); lastBaseDrum = pair(longRange(begin, end), ratio); downSince = end; } Pair getAndClearLastEvent() { try { ret lastBaseDrum; } finally { lastBaseDrum = null; } } }