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

73
LINES

< > BotCompany Repo | #1033281 // AudioAdder [dev.]

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

Libraryless. Click here for Pure Java version (6795L/40K).

sclass AudioAdder extends AbstractAudioSample is IAddPlateau {
  new Cl<IAudioSample> sources;
  
  srecord SumAndSlope(double sum, double slope) {
    double sumFor(double myTime, double time) {
      ret sum+(time-myTime)*slope;
    }
  }

  // for now same value in all channels
  new TreeMap<Double, SumAndSlope> additions;
  
  void addSource(IAudioSample source) {
    addUnlessNull(sources, source);
  }
  
  void grabSpecsFromSource(IAudioSample source) {
    if (source == null) ret;
    setSampleRate(source.sampleRate());
    setChannels(source.channels());
    setBounds(source.bounds());
  }
  
  public void addPlateau(double start, double end, double intensity) {
    //printVars("addPlateau", +start, +end, +intensity);
    if (start >= end) ret;
    
    _incSlope(start, intensity);
    _incSlope(end, -intensity);
  }
  
  // We will probably add plateaus rather randomly throughout an area,
  // so could optimize this doing something akin to LogNArray.
  void _incSlope(double time, double intensity) {    
    if (intensity == 0) ret;

    // create, overwrite or delete
    var last = floorPair(additions, time);
    if (last == null) { // we are first node
      mapPut(additions, time, new SumAndSlope(0, intensity);
    } else if (last.a == time) { // we are our own node already
      last.b.slope += intensity;
    } else { // we are an insertion!
      mapPut(additions, time, new SumAndSlope(
        last.b.sumFor(last.a, time), last.b.slope+intensity);
    }
      
    recalcFrom(time);

    //printVars _incSlope(+time, +intensity, v := additions.get(time));
  }
  
  void recalcFrom(double time) {
    SumAndSlope me = additions.get(time);
    if (me == null) ret;
    Pair<Double, SumAndSlope> next;
    while ((next = higherPair(additions, time)) != null) {
      next.b.sum = me.sumFor(time, next.a);
      time = next.a;
      me = next.b;
    }
  }

  @Override
  public double readSumTable(int channel, int t) {
    double t2 = t+1;
    var p = floorPair(additions, t2);
    double sum = p == null ? 0.0 : p.b.sumFor(p.a, t2);
    //printVars readSumTable(+t, +p, +sum);
    for (source : sources) sum += source.readSumTable(channel, t);
    ret sum;
  }
}

Author comment

Began life as a copy of #1033269

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1033281
Snippet name: AudioAdder [dev.]
Eternal ID of this version: #1033281/33
Text MD5: a4dfa45a539b926eba07de3f6d490d15
Transpilation MD5: 377f09a22462875d68cb70f189b2945b
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-10-18 09:29:27
Source code size: 2269 bytes / 73 lines
Pitched / IR pitched: No / No
Views / Downloads: 220 / 389
Version history: 32 change(s)
Referenced in: [show references]