maudio 0.1.5

Rust bindings to the miniaudio library
Documentation
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
use std::{marker::PhantomData, mem::MaybeUninit, sync::Arc};

use maudio_sys::ffi as sys;

use crate::{
    engine::{
        node_graph::{
            nodes::{private_node::SplitterNodeProvider, AsNodePtr, NodeRef},
            AsNodeGraphPtr, NodeGraph,
        },
        AllocationCallbacks,
    },
    AsRawRef, Binding, MaResult,
};

/// A node that **duplicates an input signal to multiple outputs** inside a node graph.
///
/// A splitter takes a single input bus and routes the same audio signal to
/// **multiple output buses**, allowing the signal to be processed by multiple
/// downstream nodes in parallel.
///
/// This is commonly used to:
/// - feed the same signal into multiple effects (e.g. dry + reverb + delay)
/// - build parallel processing chains
/// - create complex routing graphs without re-rendering audio
///
/// `SplitterNode` is a node-graph wrapper around miniaudio’s splitter node
/// implementation. It performs **no DSP processing** itself. It only handles
/// routing, and is therefore extremely lightweight.
///
/// ## Behavior
/// - The input signal is copied verbatim to each output bus
/// - All output buses are sample-accurate and phase-aligned
/// - The node has **one input bus** and **N output buses**
///
/// ## Configuration
/// - **channels**: Number of audio channels per bus (e.g. 1 = mono, 2 = stereo)
/// - **output_bus_count**: Number of output buses the signal is split into
///
/// The output bus count is fixed at initialization time and cannot be changed
/// after the node is created.
///
/// ## Routing & Control
/// Per-output behavior (such as volume or connection targets) is controlled
/// via the underlying node APIs exposed through [`NodeRef`], for example:
/// - setting per-output bus volume
/// - attaching output buses to downstream nodes
/// - starting or stopping the node
///
/// ## Notes
/// - `SplitterNode` does not allocate internal buffers beyond what is required
///   by the node graph
/// - It introduces no latency and performs no filtering or mixing
/// - Volume control and routing are handled at the node-graph level
///
/// Use [`SplitterNodeBuilder`] to initialize.
pub struct SplitterNode<'a> {
    inner: *mut sys::ma_splitter_node,
    alloc_cb: Option<Arc<AllocationCallbacks>>,
    _marker: PhantomData<&'a NodeGraph>,
}

impl Binding for SplitterNode<'_> {
    type Raw = *mut sys::ma_splitter_node;

    // !!! unimplemented !!!
    fn from_ptr(_raw: Self::Raw) -> Self {
        unimplemented!()
    }

    fn to_raw(&self) -> Self::Raw {
        self.inner
    }
}

#[doc(hidden)]
impl AsNodePtr for SplitterNode<'_> {
    type __PtrProvider = SplitterNodeProvider;
}

impl<'a> SplitterNode<'a> {
    fn new_with_cfg_alloc_internal<N: AsNodeGraphPtr + ?Sized>(
        node_graph: &N,
        config: &SplitterNodeBuilder<'_, N>,
        alloc: Option<Arc<AllocationCallbacks>>,
    ) -> MaResult<Self> {
        let alloc_cb: *const sys::ma_allocation_callbacks =
            alloc.clone().map_or(core::ptr::null(), |c| c.as_raw_ptr());

        let mut mem: Box<std::mem::MaybeUninit<sys::ma_splitter_node>> =
            Box::new(MaybeUninit::uninit());

        n_splitter_ffi::ma_splitter_node_init(
            node_graph,
            config.as_raw_ptr(),
            alloc_cb,
            mem.as_mut_ptr(),
        )?;

        let inner: *mut sys::ma_splitter_node = Box::into_raw(mem) as *mut sys::ma_splitter_node;

        Ok(Self {
            inner,
            alloc_cb: alloc,
            _marker: PhantomData,
        })
    }

    /// Returns a **borrowed view** as a node in the engine's node graph.
    ///
    /// ### What this is for
    ///
    /// Use `as_node()` when you want to:
    /// - connect this to other nodes (effects, mixers, splitters, etc.)
    /// - insert into a custom routing graph
    /// - query node-level state exposed by the graph
    pub fn as_node(&self) -> NodeRef<'a> {
        assert!(!self.to_raw().is_null());
        let ptr = self.to_raw().cast::<sys::ma_node>();
        NodeRef::from_ptr(ptr)
    }

    #[inline]
    fn alloc_cb_ptr(&self) -> *const sys::ma_allocation_callbacks {
        match &self.alloc_cb {
            Some(cb) => cb.as_raw_ptr(),
            None => core::ptr::null(),
        }
    }
}

pub(crate) mod n_splitter_ffi {
    use crate::{
        engine::node_graph::{
            nodes::routing::splitter::SplitterNode, private_node_graph, AsNodeGraphPtr,
        },
        Binding, MaResult, MaudioError,
    };
    use maudio_sys::ffi as sys;

    #[inline]
    pub fn ma_splitter_node_init<N: AsNodeGraphPtr + ?Sized>(
        node_graph: &N,
        config: *const sys::ma_splitter_node_config,
        alloc_cb: *const sys::ma_allocation_callbacks,
        node: *mut sys::ma_splitter_node,
    ) -> MaResult<()> {
        let res = unsafe {
            sys::ma_splitter_node_init(
                private_node_graph::node_graph_ptr(node_graph),
                config,
                alloc_cb,
                node,
            )
        };
        MaudioError::check(res)
    }

    #[inline]
    pub fn ma_splitter_node_uninit(node: &mut SplitterNode) {
        unsafe {
            sys::ma_splitter_node_uninit(node.to_raw(), node.alloc_cb_ptr());
        }
    }
}

impl<'a> Drop for SplitterNode<'a> {
    fn drop(&mut self) {
        n_splitter_ffi::ma_splitter_node_uninit(self);
        drop(unsafe { Box::from_raw(self.to_raw()) });
    }
}

/// Builder for creating a [`SplitterNode`]
pub struct SplitterNodeBuilder<'a, N: AsNodeGraphPtr + ?Sized> {
    inner: sys::ma_splitter_node_config,
    node_graph: &'a N,
}

impl<N: AsNodeGraphPtr + ?Sized> AsRawRef for SplitterNodeBuilder<'_, N> {
    type Raw = sys::ma_splitter_node_config;

    fn as_raw(&self) -> &Self::Raw {
        &self.inner
    }
}

impl<'a, N: AsNodeGraphPtr + ?Sized> SplitterNodeBuilder<'a, N> {
    pub fn new(node_graph: &'a N, channels: u32) -> Self {
        let ptr = unsafe { sys::ma_splitter_node_config_init(channels) };
        Self {
            inner: ptr,
            node_graph,
        }
    }

    pub fn output_bus_count(&mut self, count: u32) -> &mut Self {
        self.inner.outputBusCount = count;
        self
    }

    pub fn build(&self) -> MaResult<SplitterNode<'a>> {
        SplitterNode::new_with_cfg_alloc_internal(self.node_graph, self, None)
    }
}

#[cfg(test)]
mod test {
    use crate::engine::{
        node_graph::{
            node_builder::NodeState,
            nodes::{routing::splitter::SplitterNodeBuilder, NodeOps},
        },
        Engine, EngineOps,
    };

    #[test]
    fn test_splitter_basic_init() {
        let engine = Engine::new_for_tests().unwrap();
        let node_graph = engine.as_node_graph().unwrap();

        let _node = SplitterNodeBuilder::new(&node_graph, 2)
            .output_bus_count(2)
            .build()
            .unwrap();
    }

    #[test]
    fn test_splitter_default_output_bus_count_builds() {
        let engine = Engine::new_for_tests().unwrap();
        let node_graph = engine.as_node_graph().unwrap();

        let _node = SplitterNodeBuilder::new(&node_graph, 2).build().unwrap();
    }

    #[test]
    fn test_splitter_various_output_bus_counts() {
        let engine = Engine::new_for_tests().unwrap();
        let node_graph = engine.as_node_graph().unwrap();

        for &bus_count in &[1u32, 2, 4, 8] {
            let _node = SplitterNodeBuilder::new(&node_graph, 2)
                .output_bus_count(bus_count)
                .build()
                .unwrap();
        }
    }

    #[test]
    fn test_splitter_repeated_create_drop() {
        let engine = Engine::new_for_tests().unwrap();
        let node_graph = engine.as_node_graph().unwrap();

        for _ in 0..100 {
            let _node = SplitterNodeBuilder::new(&node_graph, 2)
                .output_bus_count(2)
                .build()
                .unwrap();
            // drop happens here each loop iteration
        }
    }

    #[test]
    fn test_splitter_zero_channels_is_err() {
        let engine = Engine::new_for_tests().unwrap();
        let node_graph = engine.as_node_graph().unwrap();

        let res = SplitterNodeBuilder::new(&node_graph, 0)
            .output_bus_count(2)
            .build();

        assert!(res.is_err());
    }

    #[test]
    fn test_splitter_zero_output_buses_is_err() {
        let engine = Engine::new_for_tests().unwrap();
        let node_graph = engine.as_node_graph().unwrap();

        let res = SplitterNodeBuilder::new(&node_graph, 2)
            .output_bus_count(0)
            .build();

        // TODO: zero output busses is ok?
        assert!(res.is_ok());
    }

    #[test]
    fn test_splitter_as_node_is_valid() {
        let engine = Engine::new_for_tests().unwrap();
        let node_graph = engine.as_node_graph().unwrap();

        let node = SplitterNodeBuilder::new(&node_graph, 2)
            .output_bus_count(2)
            .build()
            .unwrap();

        let _as_node = node.as_node();
    }

    #[test]
    fn test_splitter_node_ref_bus_counts_and_channels() {
        let engine = Engine::new_for_tests().unwrap();
        let node_graph = engine.as_node_graph().unwrap();

        let splitter = SplitterNodeBuilder::new(&node_graph, 2)
            .output_bus_count(4)
            .build()
            .unwrap();

        let node_ref = splitter.as_node();

        // Splitter should have 1 input bus and N output buses.
        assert_eq!(node_ref.in_bus_count(), 1);
        assert_eq!(node_ref.out_bus_count(), 4);

        // Each bus should be 2 channels (stereo).
        assert_eq!(node_ref.input_channels(0), 2);

        for out_bus in 0..4 {
            assert_eq!(node_ref.output_channels(out_bus), 2);
        }
    }

    #[test]
    fn test_splitter_attach_and_detach_output_bus() {
        let engine = Engine::new_for_tests().unwrap();
        let node_graph = engine.as_node_graph().unwrap();

        let splitter_a = SplitterNodeBuilder::new(&node_graph, 2)
            .output_bus_count(2)
            .build()
            .unwrap();

        let splitter_b = SplitterNodeBuilder::new(&node_graph, 2)
            .output_bus_count(2)
            .build()
            .unwrap();

        // NodeRef is a borrowed view, so make them mutable locals.
        let mut a = splitter_a.as_node();
        let mut b = splitter_b.as_node();

        // Attach A.out[0] -> B.in[0]
        a.attach_output_bus(0, &mut b, 0).unwrap();

        // Detach just that bus.
        a.detach_output_bus(0).unwrap();

        // And detach-all should be safe even if nothing is attached.
        a.detach_all_outputs().unwrap();
    }

    #[test]
    fn test_splitter_detach_all_outputs_after_multiple_attaches() {
        let engine = Engine::new_for_tests().unwrap();
        let node_graph = engine.as_node_graph().unwrap();

        let splitter_a = SplitterNodeBuilder::new(&node_graph, 2)
            .output_bus_count(4)
            .build()
            .unwrap();

        let splitter_b = SplitterNodeBuilder::new(&node_graph, 2)
            .output_bus_count(2)
            .build()
            .unwrap();

        let mut a = splitter_a.as_node();
        let mut b = splitter_b.as_node();

        // Attach multiple outputs of A to the same input of B (legal for routing graphs;
        // the graph may mix them or the node may receive multiple connections depending
        // on miniaudio internals, but attach should succeed).
        for out_bus in 0..4 {
            a.attach_output_bus(out_bus, &mut b, 0).unwrap();
        }

        a.detach_all_outputs().unwrap();
    }

    #[test]
    fn test_splitter_output_bus_volume_roundtrip() {
        let engine = Engine::new_for_tests().unwrap();
        let node_graph = engine.as_node_graph().unwrap();

        let splitter = SplitterNodeBuilder::new(&node_graph, 2)
            .output_bus_count(3)
            .build()
            .unwrap();

        let mut node_ref = splitter.as_node();

        let vols = [0.0_f32, 0.5_f32, 1.0_f32];

        for (bus, &v) in vols.iter().enumerate() {
            node_ref.set_output_bus_volume(bus as u32, v).unwrap();
            let got = node_ref.output_bus_volume(bus as u32);
            assert!((got - v).abs() < 1.0e-6);
        }
    }

    #[test]
    fn test_splitter_state_set_get() {
        let engine = Engine::new_for_tests().unwrap();
        let node_graph = engine.as_node_graph().unwrap();

        let splitter = SplitterNodeBuilder::new(&node_graph, 2)
            .output_bus_count(2)
            .build()
            .unwrap();

        let mut node_ref = splitter.as_node();

        node_ref.set_state(NodeState::Stopped).unwrap();
        assert_eq!(node_ref.state().unwrap(), NodeState::Stopped);

        node_ref.set_state(NodeState::Started).unwrap();
        assert_eq!(node_ref.state().unwrap(), NodeState::Started);
    }

    #[test]
    fn test_splitter_node_graph_ref_is_some() {
        let engine = Engine::new_for_tests().unwrap();
        let node_graph = engine.as_node_graph().unwrap();

        let splitter = SplitterNodeBuilder::new(&node_graph, 2)
            .output_bus_count(2)
            .build()
            .unwrap();

        let node_ref = splitter.as_node();
        assert!(node_ref.node_graph().is_some());
    }

    #[test]
    fn test_splitter_invalid_attach_indices_is_err() {
        let engine = Engine::new_for_tests().unwrap();
        let node_graph = engine.as_node_graph().unwrap();

        let splitter_a = SplitterNodeBuilder::new(&node_graph, 2)
            .output_bus_count(2)
            .build()
            .unwrap();

        let splitter_b = SplitterNodeBuilder::new(&node_graph, 2)
            .output_bus_count(2)
            .build()
            .unwrap();

        let mut a = splitter_a.as_node();
        let mut b = splitter_b.as_node();

        // output_bus index 999 should be out of range => Err
        assert!(a.attach_output_bus(999, &mut b, 0).is_err());

        // input bus index 999 should be out of range => Err (splitter has 1 input bus)
        assert!(a.attach_output_bus(0, &mut b, 999).is_err());
    }
}