-
Notifications
You must be signed in to change notification settings - Fork 29
/
signalgenerator.go
238 lines (215 loc) · 5.64 KB
/
signalgenerator.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
235
236
237
238
package effects
import (
"github.com/andrepxx/go-dsp-guitar/random"
"math"
)
/*
* Data structure representing a signal generator.
*/
type signalGenerator struct {
unitStruct
phase float64
prng random.PseudoRandomNumberGenerator
}
/*
* Signal generator audio processing.
*/
func (this *signalGenerator) Process(in []float64, out []float64, sampleRate uint32) {
this.mutex.RLock()
inputAmplitude, _ := this.getNumericValue("input_amplitude")
inputGain, _ := this.getNumericValue("input_gain")
signalType, _ := this.getDiscreteValue("signal_type")
signalFrequency, _ := this.getNumericValue("signal_frequency")
signalAmplitude, _ := this.getNumericValue("signal_amplitude")
signalGain, _ := this.getNumericValue("signal_gain")
this.mutex.RUnlock()
inputAmplitudeFloat := float64(inputAmplitude)
facInputGain := decibelsToFactor(inputGain)
facInput := (0.01 * inputAmplitudeFloat) * facInputGain
facSignalGain := decibelsToFactor(signalGain)
signalAmplitudeFloat := float64(signalAmplitude)
facSignal := (0.01 * signalAmplitudeFloat) * facSignalGain
phase := this.phase
signalFrequencyFloat := float64(signalFrequency)
sampleRateFloat := float64(sampleRate)
phaseIncrement := MATH_TWO_PI * (signalFrequencyFloat / sampleRateFloat)
twoOverPi := 2.0 / math.Pi
n := len(in)
nFloat := float64(n)
/*
* Generate the appropriate signal.
*/
switch signalType {
case "sine":
/*
* Process each sample.
*/
for i, sample := range in {
iFloat := float64(i)
updatedPhase := phase + (iFloat * phaseIncrement)
currentPhase := math.Mod(updatedPhase, MATH_TWO_PI)
signal := math.Sin(currentPhase)
out[i] = (facInput * sample) + (facSignal * signal)
}
phase += nFloat * phaseIncrement
phase = math.Mod(phase, MATH_TWO_PI)
break
case "triangle":
/*
* Process each sample.
*/
for i, sample := range in {
iFloat := float64(i)
updatedPhase := phase + (iFloat * phaseIncrement)
currentPhase := math.Mod(updatedPhase, MATH_TWO_PI)
signal := 0.0
/*
* Check whether the waveform is rising or falling.
*/
if currentPhase < math.Pi {
signal = (twoOverPi * currentPhase) - 1.0
} else {
signal = 3.0 - (twoOverPi * currentPhase)
}
out[i] = (facInput * sample) + (facSignal * signal)
}
phase += nFloat * phaseIncrement
phase = math.Mod(phase, MATH_TWO_PI)
break
case "square":
/*
* Process each sample.
*/
for i, sample := range in {
iFloat := float64(i)
updatedPhase := phase + (iFloat * phaseIncrement)
currentPhase := math.Mod(updatedPhase, MATH_TWO_PI)
signal := signFloat(math.Pi - currentPhase)
out[i] = (facInput * sample) + (facSignal * signal)
}
phase += nFloat * phaseIncrement
phase = math.Mod(phase, MATH_TWO_PI)
break
case "sawtooth":
/*
* Process each sample.
*/
for i, sample := range in {
iFloat := float64(i)
updatedPhase := phase + (iFloat * phaseIncrement)
currentPhase := math.Mod(updatedPhase, MATH_TWO_PI)
signal := currentPhase / math.Pi
/*
* Check whether we're after the phase jump.
*/
if currentPhase > math.Pi {
signal -= 2.0
}
out[i] = (facInput * sample) + (facSignal * signal)
}
phase += nFloat * phaseIncrement
phase = math.Mod(phase, MATH_TWO_PI)
break
case "noise":
prng := this.prng
/*
* Check if pseudo-random number generator is initialized.
*/
if prng == nil {
prng = random.CreatePRNG(1337)
this.prng = prng
}
/*
* Process each sample.
*/
for i, sample := range in {
r := prng.NextFloat()
uniform := (1.0 - (2.0 * r))
out[i] = (facInput * sample) + (facSignal * uniform)
}
break
}
this.phase = phase
}
/*
* Create a signal generator effects unit.
*/
func createSignalGenerator() Unit {
/*
* Create effects unit.
*/
u := signalGenerator{
unitStruct: unitStruct{
unitType: UNIT_SIGNALGENERATOR,
params: []Parameter{
Parameter{
Name: "input_amplitude",
Type: PARAMETER_TYPE_NUMERIC,
PhysicalUnit: "%",
Minimum: 0,
Maximum: 100,
NumericValue: 100,
DiscreteValueIndex: -1,
DiscreteValues: nil,
},
Parameter{
Name: "input_gain",
Type: PARAMETER_TYPE_NUMERIC,
PhysicalUnit: "dB",
Minimum: -60,
Maximum: 0,
NumericValue: 0,
DiscreteValueIndex: -1,
DiscreteValues: nil,
},
Parameter{
Name: "signal_type",
Type: PARAMETER_TYPE_DISCRETE,
PhysicalUnit: "",
Minimum: -1,
Maximum: -1,
NumericValue: -1,
DiscreteValueIndex: 0,
DiscreteValues: []string{
"sine",
"triangle",
"square",
"sawtooth",
"noise",
},
},
Parameter{
Name: "signal_frequency",
Type: PARAMETER_TYPE_NUMERIC,
PhysicalUnit: "Hz",
Minimum: 1,
Maximum: 20000,
NumericValue: 440,
DiscreteValueIndex: -1,
DiscreteValues: nil,
},
Parameter{
Name: "signal_amplitude",
Type: PARAMETER_TYPE_NUMERIC,
PhysicalUnit: "%",
Minimum: 0,
Maximum: 100,
NumericValue: 100,
DiscreteValueIndex: -1,
DiscreteValues: nil,
},
Parameter{
Name: "signal_gain",
Type: PARAMETER_TYPE_NUMERIC,
PhysicalUnit: "dB",
Minimum: -60,
Maximum: 0,
NumericValue: 0,
DiscreteValueIndex: -1,
DiscreteValues: nil,
},
},
},
}
return &u
}