-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquant.go
More file actions
71 lines (69 loc) · 1.55 KB
/
quant.go
File metadata and controls
71 lines (69 loc) · 1.55 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package gmasklib
import "math"
// Quantizer factory. The first argument is the generator which will send
// values to the quantizer. The second arg fixes the quantization grid interval.
// The optional third arg fixes the quantization strength (0..1) (defaults to 1),
// and an optional fourth arg fixes the offset (defaults to 0). Those three args
// can be each a single value or a generator.
func QuantGen(gen Generator, params ...interface{}) Generator {
var q Generator
g1 := params[0]
switch g1.(type) {
case int:
q = ConstGen(float64(g1.(int)))
case float64:
q = ConstGen(g1.(float64))
case Generator:
q = g1.(Generator)
}
s := ConstGen(1.0)
if len(params) > 1 {
g2 := params[1]
switch g2.(type) {
case int:
s = ConstGen(float64(g2.(int)))
case float64:
s = ConstGen(g2.(float64))
case Generator:
s = g2.(Generator)
}
}
o := ConstGen(0.0)
if len(params) == 3 {
g3 := params[2]
switch g3.(type) {
case int:
o = ConstGen(float64(g3.(int)))
case float64:
o = ConstGen(g3.(float64))
case Generator:
o = g3.(Generator)
}
}
return func(t ...float64) float64 {
val := gen(t...)
delta := q(t...)
strength := s(t...)
offset := o(t...)
var factor float64
if strength >= 1.0 {
factor = 0.0
} else if strength <= 0.0 {
factor = 1.0
} else {
factor = 1.0 - strength
}
val -= offset
diff := math.Mod(val, delta)
qval := val - diff
if diff > delta/2.0 {
qval += delta
}
if diff < -delta/2.0 {
qval -= delta
}
diff = val - qval
qval += diff * factor
return qval + offset
}
}