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
use super::Voice;
use super::VoiceAssignerHelper;
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum SimpleVoiceState<Identifier>
where
Identifier: Copy + Eq,
{
Idle,
Releasing(Identifier),
Active(Identifier),
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct SimpleEventDispatcher;
impl<V, Identifier> VoiceAssignerHelper<V, Identifier> for SimpleEventDispatcher
where
V: Voice<SimpleVoiceState<Identifier>>,
Identifier: Copy + Eq,
{
fn find_active_voice(&mut self, identifier: Identifier, voices: &mut [V]) -> Option<usize> {
voices
.iter()
.position(|voice| voice.state() == SimpleVoiceState::Active(identifier))
}
fn find_new_voice(&mut self, identifier: Identifier, voices: &mut [V]) -> usize {
let mut second_best = None;
let mut third_best = 0;
for (index, voice) in voices.iter().enumerate() {
match voice.state() {
SimpleVoiceState::Idle => {
return index;
}
SimpleVoiceState::Releasing(i) => {
if i == identifier {
second_best = Some(index);
} else {
third_best = index;
}
}
SimpleVoiceState::Active(_) => {}
}
}
second_best.unwrap_or(third_best)
}
}