sclass SimpleBaseDrumFinder1 { int windowSize = 20; long count; double factor = 10; // how much higher max must be than avg int maxAscent = 4; transient RollingAverage ra; transient RollingMaximum rm; transient double lastValue, avgAtAscendStart; transient long ascendingSince = 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; // mark start of ascent if (ascending && ascendingSince == 0) { ascendingSince = count; avgAtAscendStart = avg; } if (ascendingSince != 0 && (!ascending || ascendingSince+maxAscent == count)) trigger(ascendingSince, count-1, b/max(0.001, avgAtAscendStart)*factor); if (!ascending) ascendingSince = 0; } void trigger(long begin, long end, double ratio) { if (ratio < 1) ret; print("Base drum found from " + begin + " to " + end + ", ratio=" + ratio); lastBaseDrum = pair(longRange(begin, end), ratio); } }