music - waves.go
1 package music
2
3 import "math"
4
5 type Wave func(phase float64) float64
6
7 func (n Note) Saw(phase float64) float64 {
8 _, frac := math.Modf(float64(n) * phase)
9 return frac*2 - 1
10 }
11
12 func (n Note) Sine(phase float64) float64 {
13 _, frac := math.Modf(float64(n) * phase)
14 return math.Sin(2 * math.Pi * frac)
15 }
16 func (n Note) Square(phase float64) float64 {
17 _, frac := math.Modf(float64(n) * phase)
18 if frac > 0.5 {
19 return 1
20 }
21 return -1
22 }
23
24 func (n Note) Triangle(phase float64) float64 {
25 _, frac := math.Modf(float64(n) * phase)
26 if frac < 0.5 {
27 return 4*frac - 1
28 }
29 return 3 - 4*frac
30 }