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
|
use super::{ Partial, Sample, Hz, SamplesPerSecond };
pub struct Instrument {
pub partials: Vec<Partial>,
}
impl Instrument {
pub fn new(base_freq: Hz, partial_multipliers: Vec<f32>, sample_rate: SamplesPerSecond) -> Self {
Self {
partials: partial_multipliers.iter()
.map(|&p| Partial::new(1.0, 1.0, p, 0.0, sample_rate, 2000, base_freq))
.collect()
}
}
pub fn sample(&mut self) -> Sample {
match self.next() {
Some(s) => { s },
None => 0.0
}
}
}
impl Iterator for Instrument {
type Item = Sample;
fn next(&mut self) -> Option<Self::Item> {
let partial_samps : Vec<Option<Sample>> = self.partials.iter_mut()
.map(|o| o.next()).collect();
let filtered : Vec<Sample> = partial_samps.iter().filter(|opt| opt.is_some())
.map(|i| i.unwrap()).collect();
if filtered.is_empty() {
None
} else {
Some(filtered.iter().sum())
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_name() {
let sr = 44800.0;
let mut inst = Instrument::new(220.0, vec![1.0, 2.0, 4.0], sr);
assert_eq!(inst.next(), Some(0.0));
assert_eq!(inst.last(), Some(0.0));
}
}
|