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
242
use mode::Mode;
use note_freq::{NoteFreq, NoteFreqGenerator};
use pitch;
use std;
use time;
use voice::Voice;
use unit::{NoteHz, NoteVelocity};
#[derive(Clone, Debug, PartialEq)]
pub struct Instrument<M, NFG>
where NFG: NoteFreqGenerator,
{
pub mode: M,
pub voices: Vec<Voice<NFG::NoteFreq>>,
pub detune: f32,
pub note_freq_gen: NFG,
pub attack_ms: time::Ms,
pub release_ms: time::Ms,
}
pub struct Frames<'a, NF: 'a> {
attack: u64,
release: u64,
voices: &'a mut [Voice<NF>],
}
pub struct FramePerVoice<'a, NF: 'a> {
attack: u64,
release: u64,
voices: std::slice::IterMut<'a, Voice<NF>>,
}
impl<M, NFG> Instrument<M, NFG>
where NFG: NoteFreqGenerator,
{
pub fn new(mode: M, note_freq_gen: NFG) -> Self {
Instrument {
mode: mode,
voices: vec![Voice::new()],
detune: 0.0,
note_freq_gen: note_freq_gen,
attack_ms: time::Ms(0.0),
release_ms: time::Ms(0.0),
}
}
pub fn num_voices(mut self, num_voices: usize) -> Self {
self.set_num_voices(num_voices);
self
}
pub fn fade<A, R>(mut self, attack: A, release: R) -> Self
where A: Into<time::Ms>,
R: Into<time::Ms>,
{
self.attack_ms = attack.into();
self.release_ms = release.into();
self
}
pub fn attack<A>(mut self, attack: A) -> Self
where A: Into<time::Ms>,
{
self.attack_ms = attack.into();
self
}
pub fn release<R>(mut self, release: R) -> Self
where R: Into<time::Ms>,
{
self.release_ms = release.into();
self
}
pub fn detune(mut self, detune: f32) -> Self {
self.detune = detune;
self
}
pub fn note_freq_generator(mut self, generator: NFG) -> Self {
self.note_freq_gen = generator;
self
}
pub fn set_num_voices(&mut self, num_voices: usize) {
if num_voices == 0 {
println!("A Synth must have at least one voice, but the requested number is 0.");
} else {
let len = self.voices.len();
if len < num_voices {
let last_voice = self.voices[len-1].clone();
self.voices.extend(std::iter::repeat(last_voice).take(num_voices - len));
} else if len > num_voices {
self.voices.truncate(num_voices);
}
}
}
pub fn is_active(&self) -> bool {
self.voices.iter().any(|voice| voice.note.is_some())
}
#[inline]
pub fn note_on<T>(&mut self, note_hz: T, note_vel: NoteVelocity)
where M: Mode,
T: Into<pitch::Hz>
{
let Instrument { detune, ref note_freq_gen, ref mut mode, ref mut voices, .. } = *self;
mode.note_on(note_hz.into().hz(), note_vel, detune, note_freq_gen, voices);
}
#[inline]
pub fn note_off<T>(&mut self, note_hz: T)
where M: Mode,
T: Into<pitch::Hz>
{
let Instrument { detune, ref note_freq_gen, ref mut mode, ref mut voices, .. } = *self;
mode.note_off(note_hz.into().hz(), detune, note_freq_gen, voices);
}
#[inline]
pub fn stop(&mut self)
where M: Mode,
{
self.mode.stop();
for voice in self.voices.iter_mut() {
voice.stop();
}
}
#[inline]
pub fn frames(&mut self, sample_hz: time::SampleHz) -> Frames<NFG::NoteFreq> {
Frames {
attack: self.attack_ms.samples(sample_hz) as u64,
release: self.release_ms.samples(sample_hz) as u64,
voices: &mut self.voices,
}
}
#[inline]
pub fn frame_per_voice(&mut self, sample_hz: time::SampleHz) -> FramePerVoice<NFG::NoteFreq> {
FramePerVoice {
attack: self.attack_ms.samples(sample_hz) as u64,
release: self.release_ms.samples(sample_hz) as u64,
voices: self.voices.iter_mut(),
}
}
}
impl<'a, NF> Frames<'a, NF>
where NF: NoteFreq,
{
#[inline]
pub fn num_active_voices(&self) -> usize {
self.voices.iter().filter(|v| v.note.is_some()).count()
}
#[inline]
pub fn next_frame_per_voice(&mut self) -> FramePerVoice<NF> {
FramePerVoice {
attack: self.attack,
release: self.release,
voices: self.voices.iter_mut(),
}
}
}
impl<'a, NF> FramePerVoice<'a, NF>
where NF: NoteFreq,
{
#[inline]
pub fn next_voice_vel_hz(&mut self) -> Option<Option<(NoteVelocity, NoteHz)>> {
let FramePerVoice { ref mut voices, attack, release } = *self;
voices.next().map(|voice| voice.next_vel_hz(attack, release).map(|(vel, hz)| {
(vel, hz)
}))
}
}
impl<'a, NF> Iterator for FramePerVoice<'a, NF>
where NF: NoteFreq,
{
type Item = Option<(NoteVelocity, NoteHz)>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.next_voice_vel_hz()
}
}