-
Notifications
You must be signed in to change notification settings - Fork 0
/
voice.go
96 lines (80 loc) · 1.69 KB
/
voice.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package voice
import (
"time"
"github.com/heucuva/go-qwertysynth/internal/standards/note"
"github.com/heucuva/go-qwertysynth/internal/standards/scale"
"github.com/heucuva/go-qwertysynth/internal/synth/envelope"
"github.com/heucuva/go-qwertysynth/internal/synth/pwm"
)
type Voice interface {
Get() (float32, bool)
IsPlaying() bool
Advance(amt time.Duration)
KeyOn()
KeyOff()
Cut()
SetNote(n note.Note)
SetOutputSampleRate(sampleRate float64)
SetFM(mod pwm.Modulator, env envelope.Envelope)
}
type voice struct {
am voiceOp
fm voiceOp
baseNote note.Note
curNote note.Note
}
func NewVoice(mod pwm.Modulator, env envelope.Envelope, base note.Note) Voice {
return &voice{
am: voiceOp{
mod: mod,
env: env,
},
baseNote: base,
curNote: base,
}
}
func (v *voice) SetFM(mod pwm.Modulator, env envelope.Envelope) {
v.fm.mod = mod
v.fm.env = env
}
func (v *voice) SetNote(n note.Note) {
v.curNote = n
v.setNoteRatio(n)
}
func (v *voice) setNoteRatio(n note.Note) {
var ratio float64
if f0 := v.baseNote.ToFrequency(); f0 != 0 {
ratio = n.ToFrequency() / f0
}
v.am.SetNoteRatio(ratio)
}
func (v *voice) SetOutputSampleRate(sampleRate float64) {
v.am.SetOutputSampleRate(sampleRate)
}
func (v voice) Get() (float32, bool) {
return v.am.Get()
}
func (v voice) IsPlaying() bool {
return v.am.IsPlaying()
}
func (v *voice) Advance(amt time.Duration) {
mod, used := v.fm.Get()
if used {
n := v.curNote.AddMicrotones(scale.Microtone(mod))
v.setNoteRatio(n)
}
v.am.Advance(amt)
v.fm.Advance(amt)
}
func (v *voice) KeyOn() {
v.am.KeyOn()
v.fm.KeyOn()
}
func (v *voice) KeyOff() {
v.am.KeyOff()
v.fm.KeyOff()
}
func (v *voice) Cut() {
v.am.Cut()
v.fm.Cut()
}