sclass SimpleBaseDrumFinder1 { int windowSize = 30; long count; double factor = 1.35; // how much higher max must be than avg int maxAscentTime = 4; // wait if it keeps ascending before triggering int minDownTime = 10; // don't trigger again for this long double minAbsoluteLoudness = 0.03; transient RollingAverage ra; transient RollingMaximum rm; transient double lastValue, avgAtAscendStart; transient long ascendingSince = 0, downSince = 0; transient Pair lastBaseDrum; 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; } } }