insta_fun/input.rs
1/// Input provided to the audio unit
2pub enum InputSource {
3 /// No input
4 None,
5 /// Input provided by a channel vec
6 ///
7 /// - First vec contains all **channels**
8 /// - Second vec contains **samples** per channel
9 VecByChannel(Vec<Vec<f32>>),
10 /// Input provided by a tick vec
11 ///
12 /// - First vec contains all **ticks**
13 /// - Second vec contains **samples** for all **channels** per tick
14 VecByTick(Vec<Vec<f32>>),
15 /// Input **repeated** on every tick
16 ///
17 /// - Vector contains **samples** for all **channels** for **one** tick
18 Flat(Vec<f32>),
19 /// Input provided by a generator function
20 ///
21 /// - First argument is the sample index
22 /// - Second argument is the channel index
23 Generator(Box<dyn Fn(usize, usize) -> f32>),
24}
25
26impl InputSource {
27 pub fn impulse() -> Self {
28 Self::Generator(Box::new(|i, _| if i == 0 { 1.0 } else { 0.0 }))
29 }
30 pub fn sine(freq: f32, sample_rate: f32) -> Self {
31 Self::Generator(Box::new(move |i, _| {
32 let phase = 2.0 * std::f32::consts::PI * freq * i as f32 / sample_rate;
33 phase.sin()
34 }))
35 }
36}