-
Notifications
You must be signed in to change notification settings - Fork 1
/
delay.rs
65 lines (55 loc) · 1.49 KB
/
delay.rs
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
use crate::{InputId, Node, OutputId};
/// Node that delays the input by one processing cycle.
pub struct Delay {
value: (f64, f64),
}
impl Delay {
/// Creates new delay node.
pub fn new() -> Self {
Delay { value: (0.0, 0.0) }
}
}
impl Node for Delay {
fn delayed_processing(&self) -> bool {
true
}
fn get_output(&self, id: OutputId) -> f64 {
match id.0 {
0 => self.value.1,
_ => panic!("Output with id {} does not exist.", id.0),
}
}
fn list_inputs(&self) -> &[InputId] {
// 0 -> value.0 (input).
&[InputId(0)]
}
fn list_outputs(&self) -> &[OutputId] {
// 0 -> value.1 (delayed output).
&[OutputId(0)]
}
fn process(&mut self) {
// Since delay nodes are processed last, output changes will be visible only in the next processing cycle.
self.value.1 = self.value.0;
}
fn set_input(&mut self, id: InputId, value: f64) {
match id.0 {
0 => self.value.0 = value,
_ => panic!("Input with id {} does not exist.", id.0),
}
}
}
/// Unit tests.
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn delays_value() {
let mut del = Delay::new();
del.set_input(InputId(0), 2.0);
assert_eq!(del.get_output(OutputId(0)), 0.0);
del.process();
assert_eq!(del.get_output(OutputId(0)), 2.0);
del.process();
assert_eq!(del.get_output(OutputId(0)), 2.0);
}
}