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

77
LINES

< > BotCompany Repo | #1033037 // PianoSampler2 [much simpler, seems to produce a nice image]

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

Libraryless. Click here for Pure Java version (11595L/70K).

1  
sclass PianoSampler2 > SlidingWindow {
2  
  macro dprint { if (debug) printVars } bool debug;
3  
  bool printFactor;
4  
    
5  
  gettable L<Frequency> frequencies;
6  
  Double fixedFactor;
7  
 
8  
  *(int *channels, double *sampleRate, IQuerySound *sound,
9  
    int *start, int *length) {
10  
    init();
11  
  }
12  
    
13  
  *() { init(); }
14  
    
15  
  void init { 
16  
    frequencies = pianoFrequencies88();
17  
  }
18  
 
19  
  long haarFeaturesChecked;
20  
  
21  
  // one channel
22  
  L<Complex> measureWindow() {
23  
    ret measureWindow(bounds());
24  
  }
25  
  
26  
  L<Complex> measureWindow(DoubleRange timeRange, Cl<Freq> frequencies default frequencies()) {
27  
    int channel = 0;
28  
    moveToCenter(iround(timeRange.center()));
29  
    ret map(frequencies(), f -> {
30  
      var interval = f.interval()*sampleRate;
31  
      int period1 = iceil(doubleRatio(timeRange.start, interval));
32  
      int period2 = ifloor(doubleRatio(timeRange.end, interval))-1;
33  
      if (period2 <= period1) ret complex(0);
34  
      int periods = period2-period1;
35  
      SumOfVibrations sum = new(this, channel, period1*interval, f!, periods);
36  
      haarFeaturesChecked += periods*2;
37  
      ret sum.complexSum();
38  
    });
39  
  }
40  
    
41  
  Frequency lowestFrequency() { ret first(frequencies); }
42  
  Frequency highestFrequency() { ret last(frequencies); }
43  
  
44  
  int minWindowSizeForBase() {
45  
    ret iceil(sampleRate*lowestFrequency().interval());
46  
  }
47  
  
48  
  void makeWindow(DoubleRange r) {
49  
    start = ifloor(r.start);
50  
    length = iceil(r.end())-start;
51  
    grab();
52  
  }
53  
  
54  
  void makeWindowAround(double t, double size) {
55  
    double start = max(0, t-size)/2;
56  
    makeWindow(DoubleRange(start, start+size));
57  
  }
58  
  
59  
  S stats() { ret renderVars(+haarFeaturesChecked); }
60  
  
61  
  IIntegralImage imageColumn(L<Complex> intensities) {
62  
    int n = l(intensities);
63  
    double[] col = new[n];
64  
    for y to n: 
65  
      col[n-y-1] = abs(intensities.get(y));
66  
    double factor = fixedFactor != null ? fixedFactor : normalizationFactor(col, 255);
67  
    if (printFactor) printVars(+factor, log := log10(factor));
68  
    double[] col2 = doubleMul(col, factor);
69  
      
70  
    ret bwIntegralImageFromFunction(1, n, (x, y) -> ifloor(col2[y]));
71  
  }
72  
  
73  
  void loadSound(IQuerySound sound, int channels) {
74  
    this.sound = sound;
75  
    this.channels = channels;
76  
  }
77  
}

Author comment

Began life as a copy of #1032970

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, ekrmjmnbrukm, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1033037
Snippet name: PianoSampler2 [much simpler, seems to produce a nice image]
Eternal ID of this version: #1033037/21
Text MD5: 4792046791d8d3021c0ef31921f8db2f
Transpilation MD5: 1eb0ff1bb7d5dcbf3ae4b40c699a3a7e
Author: stefan
Category: javax / audio analysis
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-10-12 15:22:43
Source code size: 2281 bytes / 77 lines
Pitched / IR pitched: No / No
Views / Downloads: 141 / 276
Version history: 20 change(s)
Referenced in: [show references]