lv2rs_core/
ports.rs

1//! Wrappers for raw LV2 audio IO.
2//!
3//! The wrappers provided in this module increase the safety when dealing with the raw IO pointers
4//! provided by a plugin's [`connect_port`](../trait.Plugin.html#tymythod.connect_port) function by
5//! granting only safe access to the data.
6//!
7//! You should use these wrappers in your plugin struct, since they clearly communicate what type of
8//! data they contain. If you only store raw pointers to the ports, you can not tell an
9//! audio port from a parameter port only looking at the type, for example.
10
11/// Wrapper for raw audio input lists.
12pub struct AudioInputPort {
13    raw: *const f32,
14}
15
16impl AudioInputPort {
17    /// Create a new instance that points to null.
18    pub fn new() -> Self {
19        Self {
20            raw: std::ptr::null(),
21        }
22    }
23
24    /// Set the internal data pointer.
25    ///
26    /// This function should only be called by a plugin's `connect_port` function.
27    pub fn connect(&mut self, raw: *const f32) {
28        self.raw = raw
29    }
30
31    /// Try to create an immutable slice of the audio data with the given length.
32    ///
33    /// This function is unsafe since invalid slices can be created by passing an invalid sample
34    /// count. Therefore, only a plugin's `run` function should use this function and must pass
35    /// the sample count it received from the host.
36    pub unsafe fn as_slice(&self, n_samples: u32) -> Option<&[f32]> {
37        if self.raw.is_null() {
38            None
39        } else {
40            Some(std::slice::from_raw_parts(self.raw, n_samples as usize))
41        }
42    }
43}
44
45/// Wrapper for raw audio output lists.
46pub struct AudioOutputPort {
47    raw: *mut f32,
48}
49
50impl AudioOutputPort {
51    /// Create a new instance that points to null.
52    pub fn new() -> Self {
53        Self {
54            raw: std::ptr::null_mut(),
55        }
56    }
57
58    /// Set the internal data pointer.
59    ///
60    /// This function should only be called by a plugin's `connect_port` function.
61    pub fn connect(&mut self, raw: *mut f32) {
62        self.raw = raw;
63    }
64
65    /// Try to create a mutable slice of the audio data with the given length.
66    ///
67    /// This function is unsafe since invalid slices can be created by passing an invalid sample
68    /// count. Therefore, only a plugin's `run` function should use this function and must pass
69    /// the sample count it receives from the host.
70    pub unsafe fn as_slice(&mut self, n_samples: u32) -> Option<&mut [f32]> {
71        if self.raw.is_null() {
72            None
73        } else {
74            Some(std::slice::from_raw_parts_mut(self.raw, n_samples as usize))
75        }
76    }
77}
78
79/// Wrapper for raw parameter inputs.
80pub struct ParameterInputPort {
81    raw: *const f32,
82}
83
84impl ParameterInputPort {
85    /// Create a new instance that points to null.
86    pub fn new() -> Self {
87        Self {
88            raw: std::ptr::null(),
89        }
90    }
91
92    /// Set the internal data pointer.
93    ///
94    /// This function should only be called by a plugin's `connect_port` function.
95    pub fn connect(&mut self, raw: *const f32) {
96        self.raw = raw;
97    }
98
99    /// Try to access the parameter.
100    ///
101    /// This is just a wrapper for `self.raw.as_ref()`
102    pub unsafe fn get(&self) -> Option<&f32> {
103        self.raw.as_ref()
104    }
105}
106
107/// Safer wrapper for raw parameter outputs.
108pub struct ParameterOutputPort {
109    raw: *mut f32,
110}
111
112impl ParameterOutputPort {
113    /// Create a new instance that points to null.
114    pub fn new() -> Self {
115        Self {
116            raw: std::ptr::null_mut(),
117        }
118    }
119
120    /// Set the internal data pointer.
121    ///
122    /// This function should only be called by a plugin's `connect_port` function.
123    pub fn connect(&mut self, raw: *mut f32) {
124        self.raw = raw;
125    }
126
127    /// Try to access the parameter.
128    ///
129    /// This is just a wrapper for `self.raw.as_mut()`
130    pub unsafe fn get_mut(&mut self) -> Option<&mut f32> {
131        self.raw.as_mut()
132    }
133}