-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule_gens.go
More file actions
94 lines (82 loc) · 1.58 KB
/
module_gens.go
File metadata and controls
94 lines (82 loc) · 1.58 KB
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
package synth
import (
"math"
"math/rand"
)
type SinGen struct {
Freq float64
phase float64
}
func (g *SinGen) Process(out [][]float32) {
step := g.Freq / SAMPLERATE
for i := range out[0] {
out[0][i] = float32(math.Sin(2 * math.Pi * g.phase))
out[1][i] = out[0][i]
g.phase += step
for g.phase > 1 {
g.phase -= 1
}
}
}
type TriGen struct {
Freq float64
phase float64
}
func (g *TriGen) Process(out [][]float32) {
step := g.Freq / SAMPLERATE
for i := range out[0] {
if g.phase < 0.25 {
out[0][i] = float32(g.phase) * 4
} else if g.phase < 0.75 {
out[0][i] = (0.5 - float32(g.phase)) * 4
} else {
out[0][i] = (float32(g.phase) - 1) * 4
}
out[1][i] = out[0][i]
g.phase += step
for g.phase > 1 {
g.phase -= 1
}
}
}
type SquGen struct {
Freq float64
phase float64
}
func (g *SquGen) Process(out [][]float32) {
step := g.Freq / SAMPLERATE
for i := range out[0] {
if g.phase < 0.5 {
out[0][i] = 0.9
} else {
out[0][i] = -0.9
}
out[1][i] = out[0][i]
g.phase += step
for g.phase > 1 {
g.phase -= 1
}
}
}
type SawGen struct {
Freq float64
phase float64
}
func (g *SawGen) Process(out [][]float32) {
step := g.Freq / SAMPLERATE
for i := range out[0] {
out[0][i] = float32(g.phase)*2 - 1
out[1][i] = out[0][i]
g.phase += step
for g.phase > 1 {
g.phase -= 1
}
}
}
type NoiseGen struct{}
func (g *NoiseGen) Process(out [][]float32) {
for i := range out[0] {
out[0][i] = rand.Float32()*2 - 1
out[1][i] = out[0][i]
}
}