-
Notifications
You must be signed in to change notification settings - Fork 4
/
oscil.go
75 lines (59 loc) · 1.23 KB
/
oscil.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
75
package snd
import (
"dasa.cc/signal"
)
// TODO consider functional options
type Oscil struct {
*mono
in signal.Discrete
amp float64
freq float64
phase float64
ampmod Sound
freqmod Sound
phasemod Sound
}
func NewOscil(in signal.Discrete, freq float64, freqmod Sound) *Oscil {
return &Oscil{
mono: newmono(nil),
in: in,
amp: 1,
freq: freq,
freqmod: freqmod,
}
}
func (osc *Oscil) SetFreq(hz float64, mod Sound) {
osc.freq = hz
osc.freqmod = mod
}
func (osc *Oscil) SetAmp(fac float64, mod Sound) {
osc.amp = fac
osc.ampmod = mod
}
func (osc *Oscil) SetPhase(mod Sound) {
osc.phasemod = mod
}
func (osc *Oscil) Inputs() []Sound {
return []Sound{osc.freqmod, osc.ampmod, osc.phasemod}
}
func (osc *Oscil) Prepare(tc uint64) {
frame := int(tc-1) * len(osc.out)
nfreq := osc.freq / osc.sr
// phase := float64(frame) * nfreq
for i := range osc.out {
interval := nfreq
if osc.freqmod != nil {
interval *= osc.freqmod.Index(frame + i)
}
offset := 0.0
if osc.phasemod != nil {
offset = osc.phasemod.Index(frame + i)
}
amp := osc.amp
if osc.ampmod != nil {
amp *= osc.ampmod.Index(frame + i)
}
osc.out[i] = amp * osc.in.At(osc.phase+offset)
osc.phase += interval
}
}