-
Notifications
You must be signed in to change notification settings - Fork 29
/
ringmodulator.go
74 lines (66 loc) · 1.54 KB
/
ringmodulator.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
package effects
import (
"math"
)
/*
* Data structure representing a ring modulator effect.
*/
type ringModulator struct {
unitStruct
phase float64
}
/*
* Ring modulator audio processing.
*/
func (this *ringModulator) Process(in []float64, out []float64, sampleRate uint32) {
this.mutex.RLock()
frequency, _ := this.getNumericValue("frequency")
this.mutex.RUnlock()
phase := this.phase
sampleRateFloat := float64(sampleRate)
frequencyFloat := float64(frequency)
angularFrequency := MATH_TWO_PI * frequencyFloat
phaseFraction := angularFrequency / sampleRateFloat
/*
* Process each sample.
*/
for i, sample := range in {
iFloat := float64(i)
phaseOffset := iFloat * phaseFraction
phaseUpdate := phase + phaseOffset
currentPhase := math.Mod(phaseUpdate, MATH_TWO_PI)
carrierWave := math.Sin(currentPhase)
out[i] = carrierWave * sample
}
n := len(in)
nFloat := float64(n)
phaseOffset := nFloat * phaseFraction
phaseUpdate := phase + phaseOffset
this.phase = math.Mod(phaseUpdate, MATH_TWO_PI)
}
/*
* Create a ring modulator effects unit.
*/
func createRingModulator() Unit {
/*
* Create effects unit.
*/
u := ringModulator{
unitStruct: unitStruct{
unitType: UNIT_RINGMODULATOR,
params: []Parameter{
Parameter{
Name: "frequency",
PhysicalUnit: "Hz",
Type: PARAMETER_TYPE_NUMERIC,
Minimum: 1,
Maximum: 100,
NumericValue: 100,
DiscreteValueIndex: -1,
DiscreteValues: nil,
},
},
},
}
return &u
}