-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwaves.go
73 lines (61 loc) · 1.86 KB
/
waves.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
package gotes
import (
"math"
)
// FixedAmplify creates a fixed amplifier function that will multiply a wave fn
// by a constant.
//
// Note that this is a direct constant; more often than not `Gain` should
// be used; which adjusts for decibels.
func FixedAmplify(amp float64) AmpFn {
return func(t float64) float64 {
return amp
}
}
// MultiplyTime applies a constant multiple to the given time.
func MultiplyTime(mult float64) TimeFn {
return func(t float64) float64 {
return mult * t
}
}
// SinWave produces a sine wave function at the given frequency.
func SinWave(freq float64) WaveFn {
return IntegrateWave(MultiplyTime(freq), lookupSin)
}
// SquareWave produces a square wave function at the given frequency.
func SquareWave(freq float64) WaveFn {
return IntegrateWave(MultiplyTime(freq), lookupSquare)
}
// TriangleWave produces a triangle wave function at the given frequency.
func TriangleWave(freq float64) WaveFn {
return IntegrateWave(MultiplyTime(freq), lookupTriangle)
}
// SawWave produces a sawtooth wave function at the given frequency.
func SawWave(freq float64) WaveFn {
return IntegrateWave(MultiplyTime(freq), lookupSaw)
}
// ZeroWave is a wave that is always zero (produces no sound).
func ZeroWave() WaveFn {
return func(t float64) float64 {
return 0
}
}
// BasicSinFn is a simple WaveFn that produces a 1hz sin wave.
func BasicSinFn(t float64) float64 {
return math.Sin(2 * math.Pi * t)
}
// BasicSquareFn is a simple WaveFn that produces a 1hz square wave.
func BasicSquareFn(t float64) float64 {
if BasicSinFn(t) > 0 {
return 1
}
return -1
}
// BasicTriangleFn is a simple WaveFn that produces a 1hz triangle wave.
func BasicTriangleFn(t float64) float64 {
return math.Abs(4*(t-math.Floor(t)-0.5)) - 1
}
// BasicSawFn is a simple WaveFn that produces a 1hz saw wave.
func BasicSawFn(t float64) float64 {
return 2 * (t - math.Floor(t) - 0.5)
}