-
Notifications
You must be signed in to change notification settings - Fork 29
/
overdrive.go
234 lines (212 loc) · 5.15 KB
/
overdrive.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package effects
import (
"github.com/andrepxx/go-dsp-guitar/oversampling"
"math"
)
const (
VALVE_TYPE_INVALID = iota - 1
VALVE_TYPE_ECC82
VALVE_TYPE_ECC83
)
/*
* Data structure representing an overdrive effect.
*/
type overdrive struct {
unitStruct
bufferIn []float64
bufferOut []float64
oversamplerTwo oversampling.OversamplerDecimator
oversamplerFour oversampling.OversamplerDecimator
}
/*
* Internal (oversampled) overdrive audio processing.
*/
func (this *overdrive) processOversampled(in []float64, out []float64, sampleRate uint32) {
this.mutex.RLock()
boost, _ := this.getNumericValue("boost")
gain, _ := this.getNumericValue("gain")
drive, _ := this.getNumericValue("drive")
level, _ := this.getNumericValue("level")
valve, _ := this.getDiscreteValue("valve")
this.mutex.RUnlock()
totalGain := boost + gain
gainFactor := decibelsToFactor(totalGain)
driveFloat := float64(drive)
driveFactor := 0.01 * driveFloat
cleanFactor := 1.0 - driveFactor
levelFactor := decibelsToFactor(level)
valveType := int(VALVE_TYPE_INVALID)
/*
* Select type of valve.
*/
switch valve {
case "ECC82 (12AU7)":
valveType = VALVE_TYPE_ECC82
case "ECC83 (12AX7)":
valveType = VALVE_TYPE_ECC83
}
/*
* Process each sample.
*/
for i, sample := range in {
arg := gainFactor * sample
dist := 0.0
/*
* Apply valve-specific non-linear function.
*/
switch valveType {
case VALVE_TYPE_ECC82:
aarg := MATH_QUARTER_PI * arg
x := math.Atan(aarg)
dist = MATH_TWO_OVER_PI * x
case VALVE_TYPE_ECC83:
x := math.Exp(-arg)
dist = (2.0 / (1.0 + x)) - 1.0
}
mix := (driveFactor * dist) + (cleanFactor * sample)
out[i] = levelFactor * mix
}
}
/*
* Overdrive audio processing.
*/
func (this *overdrive) Process(in []float64, out []float64, sampleRate uint32) {
this.mutex.RLock()
oversampling, _ := this.getDiscreteValue("oversampling")
this.mutex.RUnlock()
factor := 1
/*
* Enable two- or four-times oversampling.
*/
switch oversampling {
case "2":
factor = 2
case "4":
factor = 4
}
/*
* Check if we require oversampling.
*/
if factor > 1 {
numSamples := factor * len(in)
bufferIn := this.bufferIn
/*
* Ensure that the oversampled input buffer has sufficient
* size.
*/
if len(bufferIn) != numSamples {
bufferIn = make([]float64, numSamples)
this.bufferIn = bufferIn
}
bufferOut := this.bufferOut
/*
* Ensure that the oversampled output buffer has sufficient
* size.
*/
if len(bufferOut) != numSamples {
bufferOut = make([]float64, numSamples)
this.bufferOut = bufferOut
}
oversampler := this.oversamplerTwo
/*
* Check oversampling factor.
*/
if factor == 4 {
oversampler = this.oversamplerFour
}
oversampler.Oversample(in, bufferIn)
factor32 := uint32(factor)
oversampledRate := factor32 * sampleRate
this.processOversampled(bufferIn, bufferOut, oversampledRate)
oversampler.Decimate(bufferOut, out)
} else {
this.processOversampled(in, out, sampleRate)
}
}
/*
* Create an overdrive effects unit.
*/
func createOverdrive() Unit {
oversamplerTwo := oversampling.CreateOversamplerDecimator(2)
oversamplerFour := oversampling.CreateOversamplerDecimator(4)
/*
* Create effects unit.
*/
u := overdrive{
unitStruct: unitStruct{
unitType: UNIT_OVERDRIVE,
params: []Parameter{
Parameter{
Name: "boost",
Type: PARAMETER_TYPE_NUMERIC,
PhysicalUnit: "dB",
Minimum: 0,
Maximum: 30,
NumericValue: 0,
DiscreteValueIndex: -1,
DiscreteValues: nil,
},
Parameter{
Name: "gain",
Type: PARAMETER_TYPE_NUMERIC,
PhysicalUnit: "dB",
Minimum: -30,
Maximum: 30,
NumericValue: 0,
DiscreteValueIndex: -1,
DiscreteValues: nil,
},
Parameter{
Name: "drive",
Type: PARAMETER_TYPE_NUMERIC,
PhysicalUnit: "%",
Minimum: 0,
Maximum: 100,
NumericValue: 100,
DiscreteValueIndex: -1,
DiscreteValues: nil,
},
Parameter{
Name: "level",
Type: PARAMETER_TYPE_NUMERIC,
PhysicalUnit: "dB",
Minimum: -30,
Maximum: 0,
NumericValue: 0,
DiscreteValueIndex: -1,
DiscreteValues: nil,
},
Parameter{
Name: "valve",
Type: PARAMETER_TYPE_DISCRETE,
PhysicalUnit: "",
Minimum: -1,
Maximum: -1,
NumericValue: -1,
DiscreteValueIndex: 1,
DiscreteValues: []string{
"ECC82 (12AU7)",
"ECC83 (12AX7)",
},
},
Parameter{
Name: "oversampling",
Type: PARAMETER_TYPE_DISCRETE,
PhysicalUnit: "",
Minimum: -1,
Maximum: -1,
NumericValue: -1,
DiscreteValueIndex: 0,
DiscreteValues: []string{
"- NONE -",
"2",
"4",
},
},
},
},
oversamplerTwo: oversamplerTwo,
oversamplerFour: oversamplerFour,
}
return &u
}