/
limiter.go
88 lines (78 loc) · 2.49 KB
/
limiter.go
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package player
import (
"math"
)
// Insertion は、インサーションエフェクトを抽象化したインタフェースです。
type Insertion interface {
// Next は、次のサンプルを生成し、その左右それぞれの振幅を返します。
Next(l, r float64) (float64, float64)
}
// Limiter は、インサーションエフェクト「リミッター」です。
type Limiter struct {
sampleRate float64
attack float64
attackInv float64
release float64
threshold float64
thresholdDB float64
attenuation float64
buffer [][2]float64
bufferPos int
}
// NewLimiter は、新しい Limiter を作成します。
func NewLimiter(sampleRate float64) *Limiter {
lim := &Limiter{
sampleRate: sampleRate,
}
return lim.SetThreshold(-3.0).SetLookAhead(.005).SetAttack(.005).SetRelease(.02)
}
// SetThreshold は、スレッショルドレベル [dB] を設定します。
func (lim *Limiter) SetThreshold(v float64) *Limiter {
lim.threshold = math.Pow(10, v/20.0)
lim.thresholdDB = v
return lim
}
// SetLookAhead は、先読み時間 [秒] を設定します。
func (lim *Limiter) SetLookAhead(v float64) *Limiter {
n := int(math.Ceil(lim.sampleRate * v))
lim.buffer = make([][2]float64, n)
lim.bufferPos = 0
return lim
}
// SetAttack は、アタックタイムを設定します。
func (lim *Limiter) SetAttack(sec float64) *Limiter {
lim.attack = lim.timeToMultiplier(sec)
lim.attackInv = 1.0 - lim.attack
return lim
}
// SetRelease は、リリースタイムを設定します。
func (lim *Limiter) SetRelease(sec float64) *Limiter {
lim.release = lim.timeToMultiplier(sec)
return lim
}
func (lim *Limiter) timeToMultiplier(sec float64) float64 {
n := sec * lim.sampleRate
return math.Pow(0.1/0.9, 1/n) // result ^ n = 0.1/0.9
// return math.Exp(-0.9542 / n)
}
// Next は、次のサンプルを生成し、その左右それぞれの振幅を返します。
func (lim *Limiter) Next(l, r float64) (float64, float64) {
lim.buffer[lim.bufferPos][0] = l
lim.buffer[lim.bufferPos][1] = r
lim.bufferPos = (lim.bufferPos + 1) % len(lim.buffer)
v := math.Max(math.Abs(l), math.Abs(r))
l = lim.buffer[lim.bufferPos][0]
r = lim.buffer[lim.bufferPos][1]
if .0 <= lim.thresholdDB {
return l, r
}
if lim.threshold <= v {
db := 20.0 * math.Log10(v)
target := lim.thresholdDB - db
lim.attenuation = lim.attack*lim.attenuation + lim.attackInv*target
} else {
lim.attenuation *= lim.release
}
a := math.Pow(10.0, lim.attenuation/20.0)
return l * a, r * a
}