-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMaterialMixer.java
More file actions
49 lines (41 loc) · 1.25 KB
/
MaterialMixer.java
File metadata and controls
49 lines (41 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import java.util.HashMap;
// Weighted mixer for multiple BiquadComponents.
// Weights are normalized internally, so feel free to put crazy weights on.
class MaterialMixer {
private HashMap<String, BiquadComponent> components;
private HashMap<String, Float> weights;
private float totalWeight;
public float calculate(float in){
float sample = 0;
for(String name: components.keySet()){
// Pass the input through the biquad, then multiply it by its weight, then divide it by the total weights.
sample += components.get(name).calculate(in, this) * weights.get(name) / totalWeight;
}
return sample;
}
public void put(String name, BiquadComponent filter, float weight){
components.put(name, filter);
weights.put(name, weight);
updateTotal();
}
public void remove(String name){
components.remove(name);
weights.remove(name);
updateTotal();
}
public void setWeight(String name, float weight){
if(weights.containsKey(name)){
weights.put(name, weight);
}
updateTotal();
}
// Private methods
// Update the total weight of the mixer.
private void updateTotal(){
float sum = 0;
for(float weight : weights.values()){
sum += weight;
}
totalWeight = sum;
}
}