1#[derive(Clone, Debug, Default)]
15#[non_exhaustive]
16pub struct BusLayout {
17 pub inputs: Vec<BusConfig>,
18 pub outputs: Vec<BusConfig>,
19}
20
21#[derive(Clone, Debug)]
25#[non_exhaustive]
26pub struct BusConfig {
27 pub name: &'static str,
28 pub channels: ChannelConfig,
29 pub kind: BusKind,
30}
31
32#[derive(Clone, Copy, Debug, Eq, PartialEq)]
37pub enum BusKind {
38 Main,
39 Sidechain,
40}
41
42#[derive(Clone, Copy, Debug, PartialEq, Eq)]
43pub enum ChannelConfig {
44 Mono,
45 Stereo,
46 Custom(u32),
47}
48
49impl ChannelConfig {
50 #[must_use]
51 pub fn channel_count(&self) -> u32 {
52 match self {
53 Self::Mono => 1,
54 Self::Stereo => 2,
55 Self::Custom(n) => *n,
56 }
57 }
58}
59
60impl BusLayout {
61 #[must_use]
62 pub fn new() -> Self {
63 Self::default()
64 }
65
66 #[must_use]
67 pub fn stereo() -> Self {
68 Self::new()
69 .with_input("Main", ChannelConfig::Stereo)
70 .with_output("Main", ChannelConfig::Stereo)
71 }
72
73 #[must_use]
77 pub fn with_input(mut self, name: &'static str, channels: ChannelConfig) -> Self {
78 let kind = if self.inputs.is_empty() {
79 BusKind::Main
80 } else {
81 BusKind::Sidechain
82 };
83 self.inputs.push(BusConfig {
84 name,
85 channels,
86 kind,
87 });
88 self
89 }
90
91 #[must_use]
95 pub fn with_sidechain_input(mut self, name: &'static str, channels: ChannelConfig) -> Self {
96 self.inputs.push(BusConfig {
97 name,
98 channels,
99 kind: BusKind::Sidechain,
100 });
101 self
102 }
103
104 #[must_use]
105 pub fn with_output(mut self, name: &'static str, channels: ChannelConfig) -> Self {
106 self.outputs.push(BusConfig {
107 name,
108 channels,
109 kind: BusKind::Main,
110 });
111 self
112 }
113
114 pub fn sidechain_input_indices(&self) -> impl Iterator<Item = usize> + '_ {
116 self.inputs
117 .iter()
118 .enumerate()
119 .filter(|(_, b)| b.kind == BusKind::Sidechain)
120 .map(|(i, _)| i)
121 }
122
123 #[must_use]
124 pub fn total_input_channels(&self) -> u32 {
125 self.inputs.iter().map(|b| b.channels.channel_count()).sum()
126 }
127
128 #[must_use]
129 pub fn total_output_channels(&self) -> u32 {
130 self.outputs
131 .iter()
132 .map(|b| b.channels.channel_count())
133 .sum()
134 }
135}