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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//! Basic types used in voicemeeter

/// A Zero Indexed Index
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
#[repr(transparent)]
pub struct ZIndex(pub(crate) i32);

impl std::fmt::Display for ZIndex {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.0.to_string())
    }
}
impl From<usize> for ZIndex {
    fn from(i: usize) -> Self {
        ZIndex(i as i32)
    }
}

impl From<i32> for ZIndex {
    fn from(i: i32) -> Self {
        ZIndex(i)
    }
}

impl From<u32> for ZIndex {
    fn from(i: u32) -> Self {
        ZIndex(i as i32)
    }
}

/// A macro button. Zero indexed
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
#[repr(transparent)]
pub struct LogicalButton(pub ZIndex);

impl std::fmt::Display for LogicalButton {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "MB:{}", self.0.0)
    }
}

impl From<usize> for LogicalButton {
    fn from(i: usize) -> Self {
        LogicalButton(ZIndex(i as i32))
    }
}

impl From<i32> for LogicalButton {
    fn from(i: i32) -> Self {
        LogicalButton(ZIndex(i))
    }
}

impl From<u32> for LogicalButton {
    fn from(i: u32) -> Self {
        LogicalButton(ZIndex(i as i32))
    }
}

/// Voicemeeter Parameter
#[aliri_braid::braid()]
pub struct ParameterName;

/// Voicemeeter application type.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[repr(C)]
pub enum VoicemeeterApplication {
    /// Standard "base" voicemeeter.
    Voicemeeter = 1,
    /// Voicemeeter Banana.
    VoicemeeterBanana = 2,
    /// Voicemeeter Potato.
    VoicemeeterPotato = 3,
    /// Voicemeeter Potato x64.
    PotatoX64Bits = 6,
    /// Unknown voicemeeter type
    Other,
    /// No voicmeeter running
    None = 255,
}

impl VoicemeeterApplication {
    /// Return all possible devices for this application
    pub const fn devices(&self) -> &'static [Device] {
        use self::Device::*;
        match self {
            VoicemeeterApplication::Voicemeeter => {
                &[Strip1, Strip2, VirtualInput, OutputA1, VirtualOutputB1]
            }
            VoicemeeterApplication::VoicemeeterBanana => &[
                Strip1,
                Strip2,
                Strip3,
                VirtualInput,
                VirtualInputAux,
                OutputA1,
                OutputA2,
                OutputA3,
                VirtualOutputB1,
                VirtualOutputB2,
            ],
            VoicemeeterApplication::VoicemeeterPotato | VoicemeeterApplication::PotatoX64Bits => {
                Device::all()
            }
            VoicemeeterApplication::Other | VoicemeeterApplication::None => &[],
        }
    }
}

impl std::fmt::Display for VoicemeeterApplication {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            VoicemeeterApplication::Voicemeeter => f.write_str("Voicemeeter"),
            VoicemeeterApplication::VoicemeeterBanana => f.write_str("VoicemeeterBanana"),
            VoicemeeterApplication::VoicemeeterPotato => f.write_str("VoicemeeterPotato"),
            VoicemeeterApplication::PotatoX64Bits => f.write_str("VoicemeeterPotatoX64Bits"),
            VoicemeeterApplication::Other => f.write_str("VoicemeeterUnknown"),
            VoicemeeterApplication::None => f.write_str("None"),
        }
    }
}

impl From<i32> for VoicemeeterApplication {
    fn from(ty: i32) -> Self {
        match ty {
            1 => VoicemeeterApplication::Voicemeeter,
            2 => VoicemeeterApplication::VoicemeeterBanana,
            3 => VoicemeeterApplication::VoicemeeterPotato,
            6 => VoicemeeterApplication::PotatoX64Bits,
            _ => VoicemeeterApplication::Other,
        }
    }
}

/// Level type, used for [`VoicemeeterRemote::get_level`](super::VoicemeeterRemote::get_level)
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[repr(C)]
pub enum LevelType {
    /// Pre fader input levels.
    PreFaderInputLevels = 0,
    /// Pre fader output levels.
    PostFaderInputLevels = 1,
    /// Post mute input levels.
    PostMuteInputLevels = 2,
    /// Output levels
    OutputLevels = 3,
    #[doc(hidden)]
    Other,
}

impl From<i32> for LevelType {
    fn from(ty: i32) -> Self {
        match ty {
            0 => LevelType::PreFaderInputLevels,
            1 => LevelType::PostFaderInputLevels,
            2 => LevelType::PostMuteInputLevels,
            3 => LevelType::OutputLevels,
            _ => LevelType::Other,
        }
    }
}

/// A device.
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum Device {
    /// Input Strip 1. Available on all Voicemeeter versions.
    Strip1,
    /// Input Strip 2. Available on all Voicemeeter versions.
    Strip2,
    /// Input Strip 3. Available on Voicemeeter Banana and Potato.
    Strip3,
    /// Input Strip 4. Available on Voicemeeter Potato.
    Strip4,
    /// Input Strip 5. Available on Voicemeeter Potato.
    Strip5,
    /// Output A1. Available on all Voicemeeter versions.
    OutputA1,
    /// Output A2. Available on all Voicemeeter versions.
    OutputA2,
    /// Output A3. Available on Voicemeeter Banana and Potato.
    OutputA3,
    /// Output A4. Available on Voicemeeter Potato.
    OutputA4,
    /// Output A5. Available on Voicemeeter Potato.
    OutputA5,
    /// Virtual Output B1. Available on all Voicemeeter versions.
    VirtualOutputB1,
    /// Virtual Output B2. Available on Voicemeeter Banana and Potato.
    VirtualOutputB2,
    /// Virtual Output B3. Available on Voicemeeter Potato.
    VirtualOutputB3,
    /// Virtual Input. Available on all Voicemeeter versions.
    VirtualInput,
    /// Virtual Input Aux. Available on Voicemeeter Banana and Potato.
    VirtualInputAux,
    /// Virtual Input8. Available on Voicemeeter Potato.
    VirtualInput8,
}

/// Index in the buffers for a [devices'](Device) channel.
#[derive(Debug, Clone, Copy)]
pub struct ChannelIndex {
    /// Start index.
    pub start: usize,
    /// End index.
    pub size: usize,
}

impl ChannelIndex {
    /// Create a new index.
    pub(crate) const fn new(start: usize, size: usize) -> Self {
        Self { start, size }
    }

    /// Get the channel index for
    pub fn get(&self, channel: usize) -> Option<usize> {
        (self.start..(self.start + self.size)).nth(channel)
    }
}
const fn ci(start: usize, size: usize) -> Option<ChannelIndex> {
    Some(ChannelIndex::new(start, size))
}

impl Device {
    /// Get the [`ChannelIndex`] for this channel in the buffers when in [main mode](crate::interface::callback::CallbackCommand::BufferMain), if available in the current program.
    pub const fn main(
        &self,
        program: &VoicemeeterApplication,
    ) -> (Option<ChannelIndex>, Option<ChannelIndex>) {
        match program {
            VoicemeeterApplication::Voicemeeter => match self {
                Device::Strip1 => (ci(0, 2), None),
                Device::Strip2 => (ci(2, 2), None),
                Device::OutputA1 => (ci(12, 8), ci(0, 8)),
                //Device::OutputA2 => (ci(12, 8), ci(0, 8)),
                Device::VirtualOutputB1 => (ci(20, 8), ci(8, 8)),
                Device::VirtualInput => (ci(4, 8), None),
                _ => (None, None),
            },
            VoicemeeterApplication::VoicemeeterBanana => match self {
                Device::Strip1 => (ci(0, 2), None),
                Device::Strip2 => (ci(2, 2), None),
                Device::Strip3 => (ci(4, 2), None),
                Device::OutputA1 => (ci(22, 8), ci(0, 8)),
                Device::OutputA2 => (ci(30, 8), ci(8, 8)),
                Device::OutputA3 => (ci(38, 8), ci(16, 8)),
                Device::VirtualOutputB1 => (ci(46, 8), ci(24, 8)),
                Device::VirtualOutputB2 => (ci(54, 8), ci(32, 8)),
                Device::VirtualInput => (ci(6, 8), None),
                Device::VirtualInputAux => (ci(14, 8), None),
                _ => (None, None),
            },
            VoicemeeterApplication::VoicemeeterPotato | VoicemeeterApplication::PotatoX64Bits => {
                match self {
                    Device::Strip1 => (ci(0, 2), None),
                    Device::Strip2 => (ci(2, 2), None),
                    Device::Strip3 => (ci(4, 2), None),
                    Device::Strip4 => (ci(6, 2), None),
                    Device::Strip5 => (ci(8, 2), None),
                    Device::OutputA1 => (ci(34, 8), ci(0, 8)),
                    Device::OutputA2 => (ci(42, 8), ci(8, 8)),
                    Device::OutputA3 => (ci(50, 8), ci(16, 8)),
                    Device::OutputA4 => (ci(58, 8), ci(24, 8)),
                    Device::OutputA5 => (ci(66, 8), ci(32, 8)),
                    Device::VirtualOutputB1 => (ci(74, 8), ci(40, 8)),
                    Device::VirtualOutputB2 => (ci(82, 8), ci(48, 8)),
                    Device::VirtualOutputB3 => (ci(82, 8), ci(56, 8)),
                    Device::VirtualInput => (ci(10, 8), None),
                    Device::VirtualInputAux => (ci(18, 8), None),
                    Device::VirtualInput8 => (ci(26, 8), None),
                }
            }
            _ => (None, None),
        }
    }
    /// Get the [`ChannelIndex`] for this channel in the buffers when in [input mode](crate::interface::callback::CallbackCommand::BufferIn), if available in the current program.
    pub const fn input(&self, program: &VoicemeeterApplication) -> Option<ChannelIndex> {
        match self {
            Device::OutputA1
            | Device::OutputA2
            | Device::OutputA3
            | Device::OutputA4
            | Device::OutputA5
            | Device::VirtualOutputB1
            | Device::VirtualOutputB2
            | Device::VirtualOutputB3 => None,
            _ => self.main(program).0,
        }
    }
    /// Get the [`ChannelIndex`] for this channel in the buffers when in [output mode](crate::interface::callback::CallbackCommand::BufferOut), if available in the current program.
    pub const fn output(&self, program: &VoicemeeterApplication) -> Option<ChannelIndex> {
        self.main(program).1
    }
    /// Get all channels available.
    pub const fn all() -> &'static [Self] {
        &[
            Device::Strip1,
            Device::Strip2,
            Device::Strip3,
            Device::Strip4,
            Device::Strip5,
            Device::OutputA1,
            Device::OutputA2,
            Device::OutputA3,
            Device::OutputA4,
            Device::OutputA5,
            Device::VirtualOutputB1,
            Device::VirtualOutputB2,
            Device::VirtualOutputB3,
            Device::VirtualInput,
            Device::VirtualInputAux,
            Device::VirtualInput8,
        ]
    }

    /// Gives the devices number for the current application for getting level.
    pub(crate) fn as_level_device_num(
        &self,
        program: &VoicemeeterApplication,
        level_type: LevelType,
        channel: usize,
    ) -> Option<usize> {
        match level_type {
            LevelType::PreFaderInputLevels
            | LevelType::PostFaderInputLevels
            | LevelType::PostMuteInputLevels => self.input(program)?.get(channel),
            LevelType::OutputLevels => self.output(program)?.get(channel),
            LevelType::Other => None,
        }
    }

    /// Get the strip index for this device in the current program.
    pub const fn as_strip_index(&self, program: &VoicemeeterApplication) -> Option<ZIndex> {
        let i = match program {
            VoicemeeterApplication::Voicemeeter => Some(match self {
                Device::Strip1 => 0,
                Device::Strip2 => 1,
                Device::VirtualInput => 2,
                _ => return None,
            }),
            VoicemeeterApplication::VoicemeeterBanana => Some(match self {
                Device::Strip1 => 0,
                Device::Strip2 => 1,
                Device::Strip3 => 2,
                Device::VirtualInput => 3,
                Device::VirtualInputAux => 4,
                _ => return None,
            }),
            VoicemeeterApplication::VoicemeeterPotato | VoicemeeterApplication::PotatoX64Bits => {
                Some(match self {
                    Device::Strip1 => 0,
                    Device::Strip2 => 1,
                    Device::Strip3 => 2,
                    Device::Strip4 => 3,
                    Device::Strip5 => 4,
                    Device::VirtualInput => 5,
                    Device::VirtualInputAux => 6,
                    Device::VirtualInput8 => 7,
                    _ => return None,
                })
            }
            _ => return None,
        };
        match i {
            Some(i) => Some(ZIndex(i)),
            None => None,
        }
    }

    /// Get the bus index for this device in the current program.
    pub const fn as_bus_index(
        &self,
        program: &VoicemeeterApplication,
    ) -> Option<(ZIndex, &'static str)> {
        let i = match program {
            VoicemeeterApplication::Voicemeeter => Some(match self {
                Device::OutputA1 => (0, "A1"),
                Device::OutputA2 => (1, "A2"),
                Device::VirtualOutputB1 => (2, "B1"),
                _ => return None,
            }),
            VoicemeeterApplication::VoicemeeterBanana => Some(match self {
                Device::OutputA1 => (0, "A1"),
                Device::OutputA2 => (1, "A2"),
                Device::OutputA3 => (2, "A3"),
                Device::VirtualOutputB1 => (3, "B1"),
                Device::VirtualOutputB2 => (4, "B2"),
                _ => return None,
            }),
            VoicemeeterApplication::VoicemeeterPotato | VoicemeeterApplication::PotatoX64Bits => {
                Some(match self {
                    Device::OutputA1 => (0, "A1"),
                    Device::OutputA2 => (1, "A2"),
                    Device::OutputA3 => (2, "A3"),
                    Device::OutputA4 => (3, "A4"),
                    Device::OutputA5 => (4, "A5"),
                    Device::VirtualOutputB1 => (5, "B1"),
                    Device::VirtualOutputB2 => (6, "B2"),
                    Device::VirtualOutputB3 => (7, "B3"),
                    _ => return None,
                })
            }
            _ => return None,
        };
        match i {
            Some((i, s)) => Some((ZIndex(i), s)),
            None => None,
        }
    }

    /// Check if this device is a strip
    pub fn is_strip(&self) -> bool {
        matches!(
            self,
            Device::Strip1
                | Device::Strip2
                | Device::Strip3
                | Device::Strip4
                | Device::Strip5
                | Device::VirtualInput
                | Device::VirtualInputAux
                | Device::VirtualInput8
        )
    }

    /// Check if this device is a bus
    pub fn is_bus(&self) -> bool {
        matches!(
            self,
            Device::OutputA1
                | Device::OutputA2
                | Device::OutputA3
                | Device::OutputA4
                | Device::OutputA5
                | Device::VirtualOutputB1
                | Device::VirtualOutputB2
                | Device::VirtualOutputB3
        )
    }
}

/// Bus mode
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BusMode {
    /// Bus mode normal
    Normal,
    /// Bus mode Amix
    Amix,
    /// Bus mode Bmix
    Bmix,
    /// Bus mode Repeat
    Repeat,
    /// Bus mode Composite
    Composite,
    /// Bus mode TVMix
    TvMix,
    /// Bus mode UpMix21
    UpMix21,
    /// Bus mode UpMix41
    UpMix41,
    /// Bus mode UpMix61
    UpMix61,
    /// Bus mode CenterOnly
    CenterOnly,
    /// Bus mode LFEOnly
    LfeOnly,
    /// Bus mode RearOnly
    RearOnly,
}

impl std::fmt::Display for BusMode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            BusMode::Normal => f.write_str("normal"),
            BusMode::Amix => f.write_str("Amix"),
            BusMode::Bmix => f.write_str("Bmix"),
            BusMode::Repeat => f.write_str("Repeat"),
            BusMode::Composite => f.write_str("Composite"),
            BusMode::TvMix => f.write_str("TVMix"),
            BusMode::UpMix21 => f.write_str("UpMix21"),
            BusMode::UpMix41 => f.write_str("UpMix41"),
            BusMode::UpMix61 => f.write_str("UpMix61"),
            BusMode::CenterOnly => f.write_str("CenterOnly"),
            BusMode::LfeOnly => f.write_str("LFEOnly"),
            BusMode::RearOnly => f.write_str("RearOnly"),
        }
    }
}