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).

1  
sclass SimpleBaseDrumFinder1 {
2  
  int windowSize = 30;
3  
  long count;
4  
  double factor = 1.35; // how much higher max must be than avg
5  
  int maxAscentTime = 4; // wait if it keeps ascending before triggering
6  
  int minDownTime = 10; // don't trigger again for this long
7  
  double minAbsoluteLoudness = 0.03;
8  
9  
  transient RollingAverage ra;
10  
  transient RollingMaximum rm;
11  
  transient double lastValue, avgAtAscendStart;
12  
  transient long ascendingSince = 0, downSince = 0;
13  
  transient Pair<LongRange, Double> lastBaseDrum;
14  
15  
  void addFrequencyImageBrightness(double b) {
16  
    if (ra == null) ra = new RollingAverage(windowSize);
17  
    if (rm == null) rm = new RollingMaximum(windowSize);
18  
    ra.add(b);
19  
    double max = rm.addAndGet(b), avg = ra!;
20  
    ++count;
21  
    bool ascending = b >= lastValue;
22  
    lastValue = b;
23  
    
24  
    if (downSince != 0 && count < downSince+minDownTime) ret;
25  
    
26  
    // mark start of ascent
27  
    if (ascending && ascendingSince == 0) {
28  
      ascendingSince = count;
29  
      avgAtAscendStart = avg;
30  
    }
31  
    
32  
    if (ascendingSince != 0 && (!ascending || ascendingSince+maxAscentTime == count))
33  
      if (lastValue >= minAbsoluteLoudness)
34  
        trigger(ascendingSince, count-1, b/max(0.001, avgAtAscendStart));
35  
    if (!ascending) ascendingSince = 0;
36  
  }
37  
  
38  
  void trigger(long begin, long end, double ratio) {
39  
    if (ratio < factor) ret;
40  
    print("Base drum found from " + begin + " to " + end + ", ratio=" + ratio + ", last loudness: " + lastValue);
41  
    lastBaseDrum = pair(longRange(begin, end), ratio);
42  
    downSince = end;
43  
  }
44  
  
45  
  Pair<LongRange, Double> getAndClearLastEvent() {
46  
    try {
47  
      ret lastBaseDrum;
48  
    } finally {
49  
      lastBaseDrum = null;
50  
    }
51  
  }
52  
}

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: 222 / 638
Version history: 18 change(s)
Referenced in: [show references]