This repository was archived by the owner on Jul 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoice.go
241 lines (202 loc) · 6.53 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package voice
import (
"time"
"github.com/gotracker/gomixing/panning"
"github.com/gotracker/gomixing/sampling"
"github.com/gotracker/gomixing/volume"
"github.com/gotracker/voice/period"
)
// Voice is a voice interface
type Voice interface {
Controller
sampling.SampleStream
// == optional control interfaces ==
//Positioner
//FreqModulator
//AmpModulator
//PanModulator
//VolumeEnveloper
//PanEnveloper
//PitchEnveloper
//FilterEnveloper
// == required function interfaces ==
Advance(tickDuration time.Duration)
GetSampler(samplerRate float32) sampling.Sampler
Clone() Voice
StartTransaction() Transaction
}
// Controller is the instrument actuation control interface
type Controller interface {
Attack()
Release()
Fadeout()
IsKeyOn() bool
IsFadeout() bool
IsDone() bool
SetActive(active bool)
IsActive() bool
}
// == Positioner ==
// SetPos sets the position within the positioner, if the interface for it exists on the voice
func SetPos(v Voice, pos sampling.Pos) {
if p, ok := v.(Positioner); ok {
p.SetPos(pos)
}
}
// GetPos gets the position from the positioner, if the interface for it exists on the voice
func GetPos(v Voice) sampling.Pos {
if p, ok := v.(Positioner); ok {
return p.GetPos()
}
return sampling.Pos{}
}
// == FreqModulator ==
// SetPeriod sets the period into the frequency modulator, if the interface for it exists on the voice
func SetPeriod(v Voice, period period.Period) {
if fm, ok := v.(FreqModulator); ok {
fm.SetPeriod(period)
}
}
// GetPeriod gets the period from the frequency modulator, if the interface for it exists on the voice
func GetPeriod(v Voice) period.Period {
if fm, ok := v.(FreqModulator); ok {
return fm.GetPeriod()
}
return nil
}
// SetPeriodDelta sets the period delta into the frequency modulator, if the interface for it exists on the voice
func SetPeriodDelta(v Voice, delta period.Delta) {
if fm, ok := v.(FreqModulator); ok {
fm.SetPeriodDelta(delta)
}
}
// GetPeriodDelta returns the period delta from the frequency modulator, if the interface for it exists on the voice
func GetPeriodDelta(v Voice) period.Delta {
if fm, ok := v.(FreqModulator); ok {
return fm.GetPeriodDelta()
}
return period.Delta(0)
}
// GetFinalPeriod returns the final period from the frequency modulator, if the interface for it exists on the voice
func GetFinalPeriod(v Voice) period.Period {
if fm, ok := v.(FreqModulator); ok {
return fm.GetFinalPeriod()
}
return nil
}
// == AmpModulator ==
// SetVolume sets the volume into the amplitude modulator, if the interface for it exists on the voice
func SetVolume(v Voice, vol volume.Volume) {
if am, ok := v.(AmpModulator); ok {
am.SetVolume(vol)
}
}
// GetVolume gets the volume from the amplitude modulator, if the interface for it exists on the voice
func GetVolume(v Voice) volume.Volume {
if am, ok := v.(AmpModulator); ok {
return am.GetVolume()
}
return volume.Volume(1)
}
// GetFinalVolume returns the final volume from the amplitude modulator, if the interface for it exists on the voice
func GetFinalVolume(v Voice) volume.Volume {
if am, ok := v.(AmpModulator); ok {
return am.GetFinalVolume()
}
return volume.Volume(1)
}
// == PanModulator ==
// SetPan sets the period into the pan modulator, if the interface for it exists on the voice
func SetPan(v Voice, pan panning.Position) {
if pm, ok := v.(PanModulator); ok {
pm.SetPan(pan)
}
}
// GetPan gets the period from the pan modulator, if the interface for it exists on the voice
func GetPan(v Voice) panning.Position {
if pm, ok := v.(PanModulator); ok {
return pm.GetPan()
}
return panning.CenterAhead
}
// GetFinalPan returns the final panning position from the pan modulator, if the interface for it exists on the voice
func GetFinalPan(v Voice) panning.Position {
if pm, ok := v.(PanModulator); ok {
return pm.GetFinalPan()
}
return panning.CenterAhead
}
// == VolumeEnveloper ==
// EnableVolumeEnvelope sets the volume envelope enable flag, if the interface for it exists on the voice
func EnableVolumeEnvelope(v Voice, enabled bool) {
if ve, ok := v.(VolumeEnveloper); ok {
ve.EnableVolumeEnvelope(enabled)
}
}
// IsVolumeEnvelopeEnabled returns true if the volume envelope is enabled and the interface for it exists on the voice
func IsVolumeEnvelopeEnabled(v Voice) bool {
if ve, ok := v.(VolumeEnveloper); ok {
return ve.IsVolumeEnvelopeEnabled()
}
return false
}
// SetVolumeEnvelopePosition sets the volume envelope position, if the interface for it exists on the voice
func SetVolumeEnvelopePosition(v Voice, pos int) {
if ve, ok := v.(VolumeEnveloper); ok {
ve.SetVolumeEnvelopePosition(pos)
}
}
// == PanEnveloper ==
// EnablePanEnvelope sets the pan envelope enable flag, if the interface for it exists on the voice
func EnablePanEnvelope(v Voice, enabled bool) {
if pe, ok := v.(PanEnveloper); ok {
pe.EnablePanEnvelope(enabled)
}
}
// SetPanEnvelopePosition sets the pan envelope position, if the interface for it exists on the voice
func SetPanEnvelopePosition(v Voice, pos int) {
if pe, ok := v.(PanEnveloper); ok {
pe.SetPanEnvelopePosition(pos)
}
}
// == PitchEnveloper ==
// EnablePitchEnvelope sets the pitch envelope enable flag, if the interface for it exists on the voice
func EnablePitchEnvelope(v Voice, enabled bool) {
if pe, ok := v.(PitchEnveloper); ok {
pe.EnablePitchEnvelope(enabled)
}
}
// SetPitchEnvelopePosition sets the pitch envelope position, if the interface for it exists on the voice
func SetPitchEnvelopePosition(v Voice, pos int) {
if pe, ok := v.(PitchEnveloper); ok {
pe.SetPitchEnvelopePosition(pos)
}
}
// == FilterEnveloper ==
// EnableFilterEnvelope sets the filter envelope enable flag, if the interface for it exists on the voice
func EnableFilterEnvelope(v Voice, enabled bool) {
if pe, ok := v.(FilterEnveloper); ok {
pe.EnableFilterEnvelope(enabled)
}
}
// SetFilterEnvelopePosition sets the filter envelope position, if the interface for it exists on the voice
func SetFilterEnvelopePosition(v Voice, pos int) {
if pe, ok := v.(FilterEnveloper); ok {
pe.SetFilterEnvelopePosition(pos)
}
}
// GetCurrentFilterEnvelope returns the filter envelope's current value, if the interface for it exists on the voice
func GetCurrentFilterEnvelope(v Voice) int8 {
if pe, ok := v.(FilterEnveloper); ok {
return pe.GetCurrentFilterEnvelope()
}
return 1
}
// == Envelopes ==
// SetEnvelopePosition sets the envelope position(s) on the voice
func SetAllEnvelopePositions(v Voice, pos int) {
SetVolumeEnvelopePosition(v, pos)
SetPanEnvelopePosition(v, pos)
SetPitchEnvelopePosition(v, pos)
SetFilterEnvelopePosition(v, pos)
}