Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

52
LINES

< > BotCompany Repo | #1025083 // SimpleBaseDrumFinder1 (not that good yet)

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (2765L/17K).

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<LongRange, Double> 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<LongRange, Double> getAndClearLastEvent() {
    try {
      ret lastBaseDrum;
    } finally {
      lastBaseDrum = null;
    }
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 6 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1025083
Snippet name: SimpleBaseDrumFinder1 (not that good yet)
Eternal ID of this version: #1025083/19
Text MD5: d17206a126d21008d204474b9363e8fc
Transpilation MD5: 66427a83a4fdab0e0395248e68fea53d
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-09-05 14:48:29
Source code size: 1742 bytes / 52 lines
Pitched / IR pitched: No / No
Views / Downloads: 277 / 711
Version history: 18 change(s)
Referenced in: [show references]