-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.go
More file actions
44 lines (37 loc) · 970 Bytes
/
basic.go
File metadata and controls
44 lines (37 loc) · 970 Bytes
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
package main
import "math"
type SinWave struct{}
func (s SinWave) Value(tick uint64, note Tone, effect Effect) float32 {
x := float32(math.Sin(2 * math.Pi * float64(tick) * note.Frequency / baseRate))
if effect == nil {
return x
}
return effect(x)
}
type SawTooth struct{}
func (s SawTooth) Value(tick uint64, note Tone, effect Effect) float32 {
// Sawtooth wave ranges from -1 to 1 over one period
period := baseRate / note.Frequency
pos := tick % uint64(period)
x := float32((2.0 * (float64(pos) / period)) - 1.0)
if effect == nil {
return x
}
return effect(x)
}
type SquareWave struct {
DutyCycle float64 // 0.0 to 1.0
}
func (s SquareWave) Value(tick uint64, note Tone, effect Effect) float32 {
// Sawtooth wave ranges from -1 to 1 over one period
period := baseRate / note.Frequency
pos := tick % uint64(period)
x := float32(-1.0)
if float64(pos) < s.DutyCycle*period {
x = 1.0
}
if effect == nil {
return x
}
return effect(x)
}