-
Notifications
You must be signed in to change notification settings - Fork 29
/
phaser.go
174 lines (158 loc) · 4.54 KB
/
phaser.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
package effects
import (
"math"
)
/*
* Data structure representing a phaser effect.
*/
type phaser struct {
unitStruct
buffer []float64
previousPhase float64
}
/*
* Phaser audio processing.
*/
func (this *phaser) Process(in []float64, out []float64, sampleRate uint32) {
this.mutex.RLock()
depth, _ := this.getNumericValue("depth")
speed, _ := this.getNumericValue("speed")
phase, _ := this.getNumericValue("phase")
this.mutex.RUnlock()
depthFloat := float64(depth)
depthValue := 0.01 * depthFloat
/*
* Limit depth to [0.0; 1.0].
*/
if depthValue < 0.0 {
depthValue = 0.0
} else if depthValue > 1.0 {
depthValue = 1.0
}
speedFloat := float64(speed)
angularSpeed := MATH_TWO_PI_HUNDREDTH * speedFloat
phaseFloat := float64(phase)
phaseFloatRadians := MATH_DEGREE_TO_RADIANS * phaseFloat
phaseFac := 0.5 * math.Sin(phaseFloatRadians)
phaseFacInv := 1.0 - math.Abs(phaseFac)
sampleRateFloat := float64(sampleRate)
sampleRateFloatInv := 1.0 / sampleRateFloat
maxDelaySamplesFloat := math.Floor((0.002 * sampleRateFloat) + 0.5)
maxDelaySamples := int(maxDelaySamplesFloat)
buffer := this.buffer
bufferSize := len(buffer)
previousPhase := this.previousPhase
/*
* Make sure the buffer has the appropriate size.
*/
if bufferSize != maxDelaySamples {
buffer = make([]float64, maxDelaySamples)
this.buffer = buffer
bufferSize = maxDelaySamples
}
/*
* Mix the straight output with the delayed signal.
*/
for i, sample := range in {
iFloat := float64(i)
time := iFloat * sampleRateFloatInv
phaseChange := angularSpeed * time
phaseChanged := previousPhase + phaseChange
phase := math.Mod(phaseChanged, MATH_TWO_PI)
offset := depthValue * math.Sin(phase)
currentDelayTime := 0.001 * (depthValue + offset)
currentDelaySamples := currentDelayTime * sampleRateFloat
currentDelaySamplesEarly := math.Floor(currentDelaySamples)
currentDelaySamplesLate := math.Ceil(currentDelaySamples)
delayedIdxEarly := i - int(currentDelaySamplesEarly)
delayedIdxLate := i - int(currentDelaySamplesLate)
delayedSampleEarly := float64(0.0)
delayedSampleLate := float64(0.0)
/*
* Check whether the delayed sample can be found in the current input
* signal or the delay buffer.
*/
if delayedIdxEarly >= 0 {
delayedSampleEarly = in[delayedIdxEarly]
} else {
bufferPtr := bufferSize + delayedIdxEarly
delayedSampleEarly = buffer[bufferPtr]
}
/*
* Check whether the delayed sample can be found in the current input
* signal or the delay buffer.
*/
if delayedIdxLate >= 0 {
delayedSampleLate = in[delayedIdxLate]
} else {
bufferPtr := bufferSize + delayedIdxLate
delayedSampleLate = this.buffer[bufferPtr]
}
weightEarly := 1.0 - (currentDelaySamples - currentDelaySamplesEarly)
weightLate := 1.0 - (currentDelaySamplesLate - currentDelaySamples)
delayedSample := (weightEarly * delayedSampleEarly) + (weightLate * delayedSampleLate)
out[i] = (phaseFacInv * sample) + (phaseFac * delayedSample)
}
bufferSizeFloat := float64(bufferSize)
duration := bufferSizeFloat * sampleRateFloatInv
phaseIncrement := angularSpeed * duration
updatedPhase := previousPhase + phaseIncrement
this.previousPhase = math.Mod(updatedPhase, MATH_TWO_PI)
numSamples := len(in)
boundary := bufferSize - numSamples
/*
* Check whether our buffer is larger than the number of samples processed.
*/
if boundary >= 0 {
copy(buffer[0:boundary], buffer[numSamples:bufferSize])
copy(buffer[boundary:bufferSize], in)
} else {
copy(buffer, in[-boundary:numSamples])
}
}
/*
* Create a phaser effects unit.
*/
func createPhaser() Unit {
/*
* Create effects unit.
*/
u := phaser{
unitStruct: unitStruct{
unitType: UNIT_PHASER,
params: []Parameter{
Parameter{
Name: "depth",
Type: PARAMETER_TYPE_NUMERIC,
PhysicalUnit: "%",
Minimum: 0,
Maximum: 100,
NumericValue: 100,
DiscreteValueIndex: -1,
DiscreteValues: nil,
},
Parameter{
Name: "speed",
Type: PARAMETER_TYPE_NUMERIC,
PhysicalUnit: "%",
Minimum: 1,
Maximum: 100,
NumericValue: 10,
DiscreteValueIndex: -1,
DiscreteValues: nil,
},
Parameter{
Name: "phase",
Type: PARAMETER_TYPE_NUMERIC,
PhysicalUnit: "°",
Minimum: -90,
Maximum: 90,
NumericValue: 45,
DiscreteValueIndex: -1,
DiscreteValues: nil,
},
},
},
}
return &u
}