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
use lv2_raw::*;

type LV2_Handle = LV2Handle;
type LV2_Descriptor = LV2Descriptor;

/// Connect a port to a data location.
/// This may be called regardless of whether the plugin is activated,
/// activation and deactivation does not destroy port connections.
///
/// # Safety
/// Makes use of unsafe ffi functions.
#[inline(always)]
pub unsafe fn lilv_instance_get_uri(
    instance: *const crate::LilvInstance,
) -> *const std::os::raw::c_char {
    instance
        .as_ref()
        .iter()
        .flat_map(|i| i.lv2_descriptor.as_ref())
        .map(|d| d.uri)
        .next()
        .unwrap_or(std::ptr::null())
}

/// Connect a port to a data location.
/// This may be called regardless of whether the plugin is activated,
/// activation and deactivation does not destroy port connections.
///
/// # Safety
/// Makes use of unsafe ffi functions.
#[inline(always)]
pub unsafe fn lilv_instance_connect_port(
    instance: *mut crate::LilvInstance,
    port_index: u32,
    data_location: *mut ::std::os::raw::c_void,
) {
    if let Some(instance) = instance.as_mut() {
        if let Some(descriptor) = instance.lv2_descriptor.as_ref() {
            (descriptor.connect_port)(instance.lv2_handle, port_index, data_location);
        }
    }
}

/// Activate a plugin instance.
/// This resets all state information in the plugin, except for port data
/// locations (as set by lilv_instance_connect_port()).  This MUST be called
/// before calling lilv_instance_run().
///
/// # Safety
/// Makes use of unsafe ffi functions.
pub unsafe fn lilv_instance_activate(instance: *const crate::LilvInstance) {
    if let Some(instance) = instance.as_ref() {
        if let Some(descriptor) = instance.lv2_descriptor.as_ref() {
            if let Some(activate_fn) = descriptor.activate {
                (activate_fn)(instance.lv2_handle);
            }
        }
    }
}

/// Run `instance` for `sample_count` frames.
/// If the hint lv2:hardRTCapable is set for this plugin, this function is
/// guaranteed not to block.
///
/// # Safety
/// Makes use of unsafe ffi functions.
#[inline(always)]
pub unsafe fn lilv_instance_run(instance: *const crate::LilvInstance, sample_count: u32) {
    if let Some(instance) = instance.as_ref() {
        if let Some(descriptor) = instance.lv2_descriptor.as_ref() {
            (descriptor.run)(instance.lv2_handle, sample_count);
        }
    }
}

/// Deactivate a plugin instance.
/// Note that to run the plugin after this you must activate it, which will
/// reset all state information (except port connections).
///
/// # Safety
/// Makes use of unsafe ffi functions.
#[inline(always)]
pub unsafe fn lilv_instance_deactivate(instance: *mut crate::LilvInstance) {
    if let Some(instance) = instance.as_ref() {
        if let Some(descriptor) = instance.lv2_descriptor.as_ref() {
            if let Some(deactivate_fn) = descriptor.deactivate {
                (deactivate_fn)(instance.lv2_handle);
            }
        }
    }
}

/// Get extension data from the plugin instance.
/// The type and semantics of the data returned is specific to the particular
/// extension, though in all cases it is shared and must not be deleted.
///
/// # Safety
/// Makes use of unsafe ffi functions.
#[inline(always)]
pub unsafe fn lilv_instance_get_extension_data(
    instance: *mut crate::LilvInstance,
    uri: *const u8,
) -> *const ::std::os::raw::c_void {
    if let Some(instance) = instance.as_ref() {
        if let Some(descriptor) = instance.lv2_descriptor.as_ref() {
            return (descriptor.extension_data)(uri);
        }
    };
    std::ptr::null()
}

/// Get the LV2_Descriptor of the plugin instance.
/// Normally hosts should not need to access the LV2_Descriptor directly,
/// use the lilv_instance_* functions.
///
/// # Safety
/// Makes use of unsafe ffi functions.
#[inline(always)]
pub unsafe fn lilv_instance_get_descriptor(
    instance: *const crate::LilvInstance,
) -> *const LV2_Descriptor {
    instance
        .as_ref()
        .map(|i| i.lv2_descriptor)
        .unwrap_or(::std::ptr::null())
}

/// Get the LV2_Handle of the plugin instance.
/// Normally hosts should not need to access the LV2_Handle directly,
/// use the lilv_instance_* functions.
///
/// The returned handle is shared and must not be deleted.
///
/// # Safety
/// Makes use of unsafe ffi functions.
#[inline(always)]
pub unsafe fn lilv_instance_get_handle(instance: *const crate::LilvInstance) -> LV2_Handle {
    instance
        .as_ref()
        .map(|i| i.lv2_handle)
        .unwrap_or(::std::ptr::null_mut())
}