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

sclass PianoSampler2 > SlidingWindow {
  macro dprint { if (debug) printVars } bool debug;
  bool printFactor;
    
  gettable L<Frequency> frequencies;
  Double fixedFactor;
 
  *(int *channels, double *sampleRate, IQuerySound *sound,
    int *start, int *length) {
    init();
  }
    
  *() { init(); }
    
  void init { 
    frequencies = pianoFrequencies88();
  }
 
  long haarFeaturesChecked;
  
  // one channel
  L<Complex> measureWindow() {
    ret measureWindow(bounds());
  }
  
  L<Complex> measureWindow(DoubleRange timeRange, Cl<Freq> frequencies default frequencies()) {
    int channel = 0;
    moveToCenter(iround(timeRange.center()));
    ret map(frequencies(), f -> {
      var interval = f.interval()*sampleRate;
      int period1 = iceil(doubleRatio(timeRange.start, interval));
      int period2 = ifloor(doubleRatio(timeRange.end, interval))-1;
      if (period2 <= period1) ret complex(0);
      int periods = period2-period1;
      SumOfVibrations sum = new(this, channel, period1*interval, f!, periods);
      haarFeaturesChecked += periods*2;
      ret sum.complexSum();
    });
  }
    
  Frequency lowestFrequency() { ret first(frequencies); }
  Frequency highestFrequency() { ret last(frequencies); }
  
  int minWindowSizeForBase() {
    ret iceil(sampleRate*lowestFrequency().interval());
  }
  
  void makeWindow(DoubleRange r) {
    start = ifloor(r.start);
    length = iceil(r.end())-start;
    grab();
  }
  
  void makeWindowAround(double t, double size) {
    double start = max(0, t-size)/2;
    makeWindow(DoubleRange(start, start+size));
  }
  
  S stats() { ret renderVars(+haarFeaturesChecked); }
  
  IIntegralImage imageColumn(L<Complex> intensities) {
    int n = l(intensities);
    double[] col = new[n];
    for y to n: 
      col[n-y-1] = abs(intensities.get(y));
    double factor = fixedFactor != null ? fixedFactor : normalizationFactor(col, 255);
    if (printFactor) printVars(+factor, log := log10(factor));
    double[] col2 = doubleMul(col, factor);
      
    ret bwIntegralImageFromFunction(1, n, (x, y) -> ifloor(col2[y]));
  }
  
  void loadSound(IQuerySound sound, int channels) {
    this.sound = sound;
    this.channels = channels;
  }
}

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: 134 / 265
Version history: 20 change(s)
Referenced in: [show references]