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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
//! Bus parameters
use super::*;

/// Parameters for a bus.
///
/// A bus is a output.
///
/// Returned by [`VoicemeeterRemote::parameters().bus(i)`](VoicemeeterRemote::parameters)
///
///
/// # Example
///
/// ```rust,no_run
/// use voicemeeter::VoicemeeterRemote;
///
/// // Get the client.
/// let remote = VoicemeeterRemote::new()?;
///
/// // Get the label of bus A1 (index 0)
/// println!("{}", remote.parameters().bus(0)?.label().get()?);
///
/// // Mute bus A4 (index 5)
/// remote.parameters().bus(2)?.mute().set(true)?;
///
/// // Ensure the change is registered.
/// remote.is_parameters_dirty()?;
///
/// // We need to sleep here because otherwise changes won't be registered,
/// // in a long running program this is not needed.
/// std::thread::sleep(std::time::Duration::from_millis(50));
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub struct Bus<'a> {
    remote: &'a VoicemeeterRemote,
    bus_index: ZIndex,
}

impl<'a> Bus<'a> {
    #[doc(hidden)]
    pub fn new(remote: &'a VoicemeeterRemote, bus_index: ZIndex) -> Self {
        Bus { remote, bus_index }
    }

    /// Get the identifier for a parameter on this bus: `Bus[i].{dot}`
    pub fn param(&self, dot: impl ToString) -> Cow<'static, ParameterNameRef> {
        // TODO: Should this maybe allow custom names?
        Cow::Owned(format!("{BUS}[{}].{}", self.bus_index, dot.to_string()).into())
    }

    /// Label
    pub fn label(&self) -> StringParameter {
        StringParameter::new(self.param("Label"), self.remote)
    }

    /// Mono Button
    pub fn mono(&self) -> IntParameter {
        IntParameter::new(self.param("Mono"), self.remote, 0..=2)
    }

    /// Mute Button
    pub fn mute(&self) -> BoolParameter {
        BoolParameter::new(self.param("Mute"), self.remote)
    }

    /// EQ Button
    pub fn eq_on(&self) -> BoolParameter {
        BoolParameter::new(self.param("EQ.on"), self.remote)
    }

    /// EQ Memory Slot
    pub fn eq_ab(&self) -> BoolParameter {
        BoolParameter::new(self.param("EQ.AB"), self.remote)
    }

    /// Gain slider
    pub fn gain(&self) -> FloatParameter {
        FloatParameter::new(self.param("gain"), self.remote, -60.0..=12.)
    }

    /// Bus mode Normal
    pub fn mode(&self) -> BusModeParameter {
        BusModeParameter::new(self.remote, self.bus_index)
    }

    /// EQ on channel
    pub fn eq(&self, channel: usize) -> EqChannelParameter {
        EqChannelParameter::new_bus(self.remote, self.bus_index, channel)
    }
    /// Fade to
    pub fn fade_to(&self) -> TupleParameter<'_, f32, usize> {
        TupleParameter::new(self.param("FadeTo"), self.remote)
    }
    /// Fade by
    pub fn fade_by(&self) -> TupleParameter<'_, f32, usize> {
        TupleParameter::new(self.param("FadeBy"), self.remote)
    }
    /// BUS SEL Button
    pub fn sel(&self) -> BoolParameter {
        BoolParameter::new(self.param("Sel"), self.remote)
    }
    /// Reverb return
    pub fn return_reverb(&self) -> IntParameter {
        IntParameter::new(self.param("ReturnReverb"), self.remote, 0..=10)
    }
    /// Delay return
    pub fn return_delay(&self) -> IntParameter {
        IntParameter::new(self.param("ReturnDelay"), self.remote, 0..=10)
    }
    /// Fx1 Return
    pub fn return_fx1(&self) -> IntParameter {
        IntParameter::new(self.param("ReturnFx1"), self.remote, 0..=10)
    }
    /// Fx2 Return
    pub fn return_fx2(&self) -> IntParameter {
        IntParameter::new(self.param("ReturnFx2"), self.remote, 0..=10)
    }
    /// Monitor
    pub fn monitor(&self) -> BoolParameter {
        BoolParameter::new(self.param("Monitor"), self.remote)
    }
    /// Audio Device information
    pub fn device(&self) -> BusDevice {
        BusDevice::new(self.remote, self.bus_index)
    }
}

/// Parameters for bus mode
pub struct BusModeParameter<'a> {
    remote: &'a VoicemeeterRemote,
    bus_index: ZIndex,
}

impl<'a> BusModeParameter<'a> {
    fn new(remote: &'a VoicemeeterRemote, bus_index: ZIndex) -> Self {
        Self { remote, bus_index }
    }

    /// Get the identifier for a mode parameter on this bus: `Bus[i].mode.{dot}`
    pub fn param(&self, dot: impl ToString) -> Cow<'static, ParameterNameRef> {
        // TODO: Should this maybe allow custom names?
        Cow::Owned(format!("{BUS}[{}].mode.{}", self.bus_index, dot.to_string()).into())
    }

    /// Get the current bus mode
    pub fn get(&self) -> Result<Option<BusMode>, GetParameterError> {
        Ok(Some(if self.is_normal()? {
            BusMode::Normal
        } else if self.is_amix()? {
            BusMode::Amix
        } else if self.is_bmix()? {
            BusMode::Bmix
        } else if self.is_repeat()? {
            BusMode::Repeat
        } else if self.is_composite()? {
            BusMode::Composite
        } else if self.is_tv_mix()? {
            BusMode::TvMix
        } else if self.is_up_mix21()? {
            BusMode::UpMix21
        } else if self.is_up_mix41()? {
            BusMode::UpMix41
        } else if self.is_up_mix61()? {
            BusMode::UpMix61
        } else if self.is_center_only()? {
            BusMode::CenterOnly
        } else if self.is_lfe_only()? {
            BusMode::LfeOnly
        } else if self.is_rear_only()? {
            BusMode::RearOnly
        } else {
            return Ok(None);
        }))
    }
    /// Set the bus mode
    pub fn set(&self, mode: BusMode) -> Result<(), SetParameterError> {
        match mode {
            BusMode::Normal => self.set_normal(true),
            BusMode::Amix => self.set_amix(true),
            BusMode::Bmix => self.set_bmix(true),
            BusMode::Repeat => self.set_repeat(true),
            BusMode::Composite => self.set_composite(true),
            BusMode::TvMix => self.set_tv_mix(true),
            BusMode::UpMix21 => self.set_up_mix21(true),
            BusMode::UpMix41 => self.set_up_mix41(true),
            BusMode::UpMix61 => self.set_up_mix61(true),
            BusMode::CenterOnly => self.set_center_only(true),
            BusMode::LfeOnly => self.set_lfe_only(true),
            BusMode::RearOnly => self.set_rear_only(true),
        }
    }

    /// Returns `true` if the bus mode is [`Normal`](BusMode::Normal)
    pub fn is_normal(&self) -> Result<bool, GetParameterError> {
        BoolParameter::<'_, false, true>::new(self.param("normal"), self.remote).get()
    }
    /// Returns `true` if the bus mode is [`Amix`](BusMode::Amix)
    pub fn is_amix(&self) -> Result<bool, GetParameterError> {
        BoolParameter::<'_, false, true>::new(self.param("Amix"), self.remote).get()
    }
    /// Returns `true` if the bus mode is [`Bmix`](BusMode::Bmix)
    pub fn is_bmix(&self) -> Result<bool, GetParameterError> {
        BoolParameter::<'_, false, true>::new(self.param("Bmix"), self.remote).get()
    }
    /// Returns `true` if the bus mode is [`Repeat`](BusMode::Repeat)
    pub fn is_repeat(&self) -> Result<bool, GetParameterError> {
        BoolParameter::<'_, false, true>::new(self.param("Repeat"), self.remote).get()
    }
    /// Returns `true` if the bus mode is [`Composite`](BusMode::Composite)
    pub fn is_composite(&self) -> Result<bool, GetParameterError> {
        BoolParameter::<'_, false, true>::new(self.param("Composite"), self.remote).get()
    }
    /// Returns `true` if the bus mode is [`TvMix`](BusMode::TvMix)
    pub fn is_tv_mix(&self) -> Result<bool, GetParameterError> {
        BoolParameter::<'_, false, true>::new(self.param("TVMix"), self.remote).get()
    }
    /// Returns `true` if the bus mode is [`UpMix21`](BusMode::UpMix21)
    pub fn is_up_mix21(&self) -> Result<bool, GetParameterError> {
        BoolParameter::<'_, false, true>::new(self.param("UpMix21"), self.remote).get()
    }
    /// Returns `true` if the bus mode is [`UpMix41`](BusMode::UpMix41)
    pub fn is_up_mix41(&self) -> Result<bool, GetParameterError> {
        BoolParameter::<'_, false, true>::new(self.param("UpMix41"), self.remote).get()
    }
    /// Returns `true` if the bus mode is [`UpMix61`](BusMode::UpMix61)
    pub fn is_up_mix61(&self) -> Result<bool, GetParameterError> {
        BoolParameter::<'_, false, true>::new(self.param("UpMix61"), self.remote).get()
    }
    /// Returns `true` if the bus mode is [`CenterOnly`](BusMode::CenterOnly)
    pub fn is_center_only(&self) -> Result<bool, GetParameterError> {
        BoolParameter::<'_, false, true>::new(self.param("CenterOnly"), self.remote).get()
    }
    /// Returns `true` if the bus mode is [`LfeOnly`](BusMode::LfeOnly)
    pub fn is_lfe_only(&self) -> Result<bool, GetParameterError> {
        BoolParameter::<'_, false, true>::new(self.param("LFEOnly"), self.remote).get()
    }
    /// Returns `true` if the bus mode is [`RearOnly`](BusMode::RearOnly)
    pub fn is_rear_only(&self) -> Result<bool, GetParameterError> {
        BoolParameter::<'_, false, true>::new(self.param("RearOnly"), self.remote).get()
    }

    /// Set the bus mode for [`Normal`](BusMode::Normal)
    pub fn set_normal(&self, val: bool) -> Result<(), SetParameterError> {
        BoolParameter::<'_, true, false>::new(self.param("normal"), self.remote).set(val)
    }
    /// Set the bus mode for [`Amix`](BusMode::Amix)
    pub fn set_amix(&self, val: bool) -> Result<(), SetParameterError> {
        BoolParameter::<'_, true, false>::new(self.param("Amix"), self.remote).set(val)
    }
    /// Set the bus mode for [`Bmix`](BusMode::Bmix)
    pub fn set_bmix(&self, val: bool) -> Result<(), SetParameterError> {
        BoolParameter::<'_, true, false>::new(self.param("Bmix"), self.remote).set(val)
    }
    /// Set the bus mode for [`Repeat`](BusMode::Repeat)
    pub fn set_repeat(&self, val: bool) -> Result<(), SetParameterError> {
        BoolParameter::<'_, true, false>::new(self.param("Repeat"), self.remote).set(val)
    }
    /// Set the bus mode for [`Composite`](BusMode::Composite)
    pub fn set_composite(&self, val: bool) -> Result<(), SetParameterError> {
        BoolParameter::<'_, true, false>::new(self.param("Composite"), self.remote).set(val)
    }
    /// Set the bus mode for [`TvMix`](BusMode::TvMix)
    pub fn set_tv_mix(&self, val: bool) -> Result<(), SetParameterError> {
        BoolParameter::<'_, true, false>::new(self.param("TVMix"), self.remote).set(val)
    }
    /// Set the bus mode for [`UpMix21`](BusMode::UpMix21)
    pub fn set_up_mix21(&self, val: bool) -> Result<(), SetParameterError> {
        BoolParameter::<'_, true, false>::new(self.param("UpMix21"), self.remote).set(val)
    }
    /// Set the bus mode for [`UpMix41`](BusMode::UpMix41)
    pub fn set_up_mix41(&self, val: bool) -> Result<(), SetParameterError> {
        BoolParameter::<'_, true, false>::new(self.param("UpMix41"), self.remote).set(val)
    }
    /// Set the bus mode for [`UpMix61`](BusMode::UpMix61)
    pub fn set_up_mix61(&self, val: bool) -> Result<(), SetParameterError> {
        BoolParameter::<'_, true, false>::new(self.param("UpMix61"), self.remote).set(val)
    }
    /// Set the bus mode for [`CenterOnly`](BusMode::CenterOnly)
    pub fn set_center_only(&self, val: bool) -> Result<(), SetParameterError> {
        BoolParameter::<'_, true, false>::new(self.param("CenterOnly"), self.remote).set(val)
    }
    /// Set the bus mode for [`LfeOnly`](BusMode::LfeOnly)
    pub fn set_lfe_only(&self, val: bool) -> Result<(), SetParameterError> {
        BoolParameter::<'_, true, false>::new(self.param("LFEOnly"), self.remote).set(val)
    }
    /// Set the bus mode for [`RearOnly`](BusMode::RearOnly)
    pub fn set_rear_only(&self, val: bool) -> Result<(), SetParameterError> {
        BoolParameter::<'_, true, false>::new(self.param("RearOnly"), self.remote).set(val)
    }
}

/// Bus device parameters
pub struct BusDevice<'a> {
    remote: &'a VoicemeeterRemote,
    bus_index: ZIndex,
}

impl<'a> BusDevice<'a> {
    #[doc(hidden)]
    pub fn new(remote: &'a VoicemeeterRemote, bus_index: ZIndex) -> Self {
        Self { remote, bus_index }
    }

    /// Get the identifier for a device parameter on this bus: `Bus[i].device.{dot}`
    pub fn param(&self, dot: impl ToString) -> Cow<'static, ParameterNameRef> {
        Cow::Owned(format!("{BUS}[{}].device.{}", self.bus_index, dot.to_string()).into())
    }
    /// Name of the device.
    pub fn name(&self) -> StringParameter<'a, false, true> {
        StringParameter::new(self.param("name"), self.remote)
    }
    /// Samplerate of the device.
    pub fn sr(&self) -> IntParameter<'a, false, true> {
        IntParameter::new_unranged(self.param("sr"), self.remote)
    }
    /// WDM device
    pub fn wdm(&self) -> StringParameter<'a, true, false> {
        StringParameter::new(self.param("wdm"), self.remote)
    }
    /// KS device
    pub fn ks(&self) -> StringParameter<'a, true, false> {
        StringParameter::new(self.param("ks"), self.remote)
    }
    /// MME device
    pub fn mme(&self) -> StringParameter<'a, true, false> {
        StringParameter::new(self.param("mme"), self.remote)
    }
    /// ASIO device
    pub fn asio(&self) -> StringParameter<'a, true, false> {
        StringParameter::new(self.param("asio"), self.remote)
    }
}