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

1  
sclass AudioAdder extends AbstractAudioSample is IAddPlateau {
2  
  new Cl<IAudioSample> sources;
3  
  
4  
  srecord SumAndSlope(double sum, double slope) {
5  
    double sumFor(double myTime, double time) {
6  
      ret sum+(time-myTime)*slope;
7  
    }
8  
  }
9  
10  
  // for now same value in all channels
11  
  new TreeMap<Double, SumAndSlope> additions;
12  
  
13  
  void addSource(IAudioSample source) {
14  
    addUnlessNull(sources, source);
15  
  }
16  
  
17  
  void grabSpecsFromSource(IAudioSample source) {
18  
    if (source == null) ret;
19  
    setSampleRate(source.sampleRate());
20  
    setChannels(source.channels());
21  
    setBounds(source.bounds());
22  
  }
23  
  
24  
  public void addPlateau(double start, double end, double intensity) {
25  
    //printVars("addPlateau", +start, +end, +intensity);
26  
    if (start >= end) ret;
27  
    
28  
    _incSlope(start, intensity);
29  
    _incSlope(end, -intensity);
30  
  }
31  
  
32  
  // We will probably add plateaus rather randomly throughout an area,
33  
  // so could optimize this doing something akin to LogNArray.
34  
  void _incSlope(double time, double intensity) {    
35  
    if (intensity == 0) ret;
36  
37  
    // create, overwrite or delete
38  
    var last = floorPair(additions, time);
39  
    if (last == null) { // we are first node
40  
      mapPut(additions, time, new SumAndSlope(0, intensity);
41  
    } else if (last.a == time) { // we are our own node already
42  
      last.b.slope += intensity;
43  
    } else { // we are an insertion!
44  
      mapPut(additions, time, new SumAndSlope(
45  
        last.b.sumFor(last.a, time), last.b.slope+intensity);
46  
    }
47  
      
48  
    recalcFrom(time);
49  
50  
    //printVars _incSlope(+time, +intensity, v := additions.get(time));
51  
  }
52  
  
53  
  void recalcFrom(double time) {
54  
    SumAndSlope me = additions.get(time);
55  
    if (me == null) ret;
56  
    Pair<Double, SumAndSlope> next;
57  
    while ((next = higherPair(additions, time)) != null) {
58  
      next.b.sum = me.sumFor(time, next.a);
59  
      time = next.a;
60  
      me = next.b;
61  
    }
62  
  }
63  
64  
  @Override
65  
  public double readSumTable(int channel, int t) {
66  
    double t2 = t+1;
67  
    var p = floorPair(additions, t2);
68  
    double sum = p == null ? 0.0 : p.b.sumFor(p.a, t2);
69  
    //printVars readSumTable(+t, +p, +sum);
70  
    for (source : sources) sum += source.readSumTable(channel, t);
71  
    ret sum;
72  
  }
73  
}

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: 221 / 391
Version history: 32 change(s)
Referenced in: [show references]