lv2_core/plugin/info.rs
1use std::os::raw::c_char;
2use std::path::Path;
3use std::str::Utf8Error;
4use urid::Uri;
5
6#[derive(Debug)]
7pub enum PluginInfoError {
8 InvalidBundlePathUtf8(Utf8Error),
9}
10
11/// Holds various data that is passed from the host at plugin instantiation time.
12pub struct PluginInfo<'a> {
13 plugin_uri: &'a Uri,
14 bundle_path: &'a Path,
15 sample_rate: f64,
16}
17
18impl<'a> PluginInfo<'a> {
19 /// Create a new plugin info instance from raw information.
20 ///
21 /// # Safety
22 ///
23 /// This method is unsafe since it dereferences raw pointers. It panics when one of the pointers is null,
24 /// but does not check the pointers for other validity.
25 pub unsafe fn from_raw(
26 plugin_descriptor: *const crate::sys::LV2_Descriptor,
27 bundle_path: *const c_char,
28 sample_rate: f64,
29 ) -> Result<Self, PluginInfoError> {
30 let bundle_path = Path::new(
31 Uri::from_ptr(bundle_path)
32 .to_str()
33 .map_err(PluginInfoError::InvalidBundlePathUtf8)?,
34 );
35 Ok(Self::new(
36 Uri::from_ptr((*plugin_descriptor).URI),
37 bundle_path,
38 sample_rate,
39 ))
40 }
41
42 /// Create a new plugin info instance.
43 pub fn new(plugin_uri: &'a Uri, bundle_path: &'a Path, sample_rate: f64) -> Self {
44 Self {
45 sample_rate,
46 plugin_uri,
47 bundle_path,
48 }
49 }
50
51 /// The URI of the plugin that is being instantiated.
52 pub fn plugin_uri(&self) -> &Uri {
53 self.plugin_uri
54 }
55
56 /// The path to the LV2 bundle directory which contains this plugin binary.
57 ///
58 /// This is useful to get if the plugin needs to store extra resources in its bundle directory,
59 /// such as presets, or any other kind of data.
60 pub fn bundle_path(&self) -> &Path {
61 self.bundle_path
62 }
63
64 /// The sample rate, in Hz, that is being used by the host.
65 /// The host will always send audio data to the plugin at this sample rate.
66 pub fn sample_rate(&self) -> f64 {
67 self.sample_rate
68 }
69}