-
Notifications
You must be signed in to change notification settings - Fork 29
/
noisegate.go
145 lines (127 loc) · 2.94 KB
/
noisegate.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package effects
import (
"math"
)
/*
* Data structure representing a noise gate effect.
*/
type noiseGate struct {
unitStruct
gateOpen bool
onHoldSince uint32
}
/*
* Noise gate audio processing.
*/
func (this *noiseGate) Process(in []float64, out []float64, sampleRate uint32) {
this.mutex.RLock()
levelOpen, _ := this.getNumericValue("threshold_open")
levelClose, _ := this.getNumericValue("threshold_close")
holdTime, _ := this.getNumericValue("hold_time")
this.mutex.RUnlock()
facOpen := decibelsToFactor(levelOpen)
facClose := decibelsToFactor(levelClose)
/*
* If opening threshold lies BELOW closing threshold, bypass the gate altogether,
* but still keep it open.
*/
if levelOpen < levelClose {
copy(out, in)
this.gateOpen = true
this.onHoldSince = 0
} else {
holdTimeFloat := float64(holdTime)
holdTimeSeconds := 0.001 * holdTimeFloat
sampleRateFloat := float64(sampleRate)
holdSamplesFloat := math.Floor((holdTimeSeconds * sampleRateFloat) + 0.5)
holdSamples := uint32(holdSamplesFloat)
gateOpen := this.gateOpen
onHoldSince := this.onHoldSince
/*
* Process each sample.
*/
for i, sample := range in {
amplitude := math.Abs(sample)
/*
* Check if amplitude is above opening threshold.
*/
if amplitude > facOpen {
gateOpen = true
}
/*
* Check if amplitude is above closing threshold.
*/
if amplitude > facClose {
onHoldSince = 0
}
/*
* If we're on hold for too long, close the gate.
*/
if onHoldSince >= holdSamples {
gateOpen = false
}
fac := float64(0.0)
/*
* Check if gate is open.
*/
if gateOpen {
fac = 1.0
}
out[i] = fac * sample
/*
* Increment time on hold, unless it overflows.
*/
if onHoldSince < math.MaxUint32 {
onHoldSince++
}
}
this.gateOpen = gateOpen
this.onHoldSince = onHoldSince
}
}
/*
* Create a noise gate effects unit.
*/
func createNoiseGate() Unit {
/*
* Create effects unit.
*/
u := noiseGate{
unitStruct: unitStruct{
unitType: UNIT_NOISEGATE,
params: []Parameter{
Parameter{
Name: "threshold_open",
Type: PARAMETER_TYPE_NUMERIC,
PhysicalUnit: "dB",
Minimum: -60,
Maximum: 0,
NumericValue: -20,
DiscreteValueIndex: -1,
DiscreteValues: nil,
},
Parameter{
Name: "threshold_close",
Type: PARAMETER_TYPE_NUMERIC,
PhysicalUnit: "dB",
Minimum: -60,
Maximum: 0,
NumericValue: -40,
DiscreteValueIndex: -1,
DiscreteValues: nil,
},
Parameter{
Name: "hold_time",
Type: PARAMETER_TYPE_NUMERIC,
PhysicalUnit: "ms",
Minimum: 0,
Maximum: 1000,
NumericValue: 50,
DiscreteValueIndex: -1,
DiscreteValues: nil,
},
},
},
}
return &u
}