Skip to main content

truce_core/
bus.rs

1/// Describes the audio bus configuration of a plugin.
2#[derive(Clone, Debug)]
3pub struct BusLayout {
4    pub inputs: Vec<BusConfig>,
5    pub outputs: Vec<BusConfig>,
6}
7
8#[derive(Clone, Debug)]
9pub struct BusConfig {
10    pub name: &'static str,
11    pub channels: ChannelConfig,
12}
13
14#[derive(Clone, Copy, Debug, PartialEq, Eq)]
15pub enum ChannelConfig {
16    Mono,
17    Stereo,
18    Custom(u32),
19}
20
21impl ChannelConfig {
22    pub fn channel_count(&self) -> u32 {
23        match self {
24            Self::Mono => 1,
25            Self::Stereo => 2,
26            Self::Custom(n) => *n,
27        }
28    }
29}
30
31impl BusLayout {
32    pub fn new() -> Self {
33        Self {
34            inputs: Vec::new(),
35            outputs: Vec::new(),
36        }
37    }
38
39    pub fn stereo() -> Self {
40        Self::new()
41            .with_input("Main", ChannelConfig::Stereo)
42            .with_output("Main", ChannelConfig::Stereo)
43    }
44
45    pub fn with_input(mut self, name: &'static str, channels: ChannelConfig) -> Self {
46        self.inputs.push(BusConfig { name, channels });
47        self
48    }
49
50    pub fn with_output(mut self, name: &'static str, channels: ChannelConfig) -> Self {
51        self.outputs.push(BusConfig { name, channels });
52        self
53    }
54
55    pub fn total_input_channels(&self) -> u32 {
56        self.inputs.iter().map(|b| b.channels.channel_count()).sum()
57    }
58
59    pub fn total_output_channels(&self) -> u32 {
60        self.outputs
61            .iter()
62            .map(|b| b.channels.channel_count())
63            .sum()
64    }
65}
66
67impl Default for BusLayout {
68    fn default() -> Self {
69        Self::new()
70    }
71}