clap_clap/
ext.rs

1//! CLAP Extensions.
2//!
3//! The [CLAP API] defines the interface between plugins and hosts as similarly
4//! structured C interfaces. This library adopts the plugin's perspective,
5//! meaning that host extensions can be implemented as concrete types that the
6//! plugin can use by querying: [`Host::get_extension()']. Plugin extensions, on
7//! the other hand, are to be specified by the user as trait implementations.
8//! The traits describing plugin extensions are declared in this module.
9//!
10//! You can also find here some concrete implementations provided as a
11//! convenience -- e.g., [`StereoPorts`]  defines a static stereo port layout.
12//!
13//! [CLAP API]: https://github.com/free-audio/clap/tree/main/include/clap
14//! [`Host::get_extension()']: crate::host::Host::get_extension
15//! [`StereoPorts`]: audio_ports::StereoPorts
16
17use std::fmt::{Display, Formatter};
18
19use crate::{
20    ext::{
21        audio_ports::AudioPorts, latency::Latency, note_ports::NotePorts, params::Params,
22        state::State, tail::Tail,
23    },
24    plugin::Plugin,
25};
26
27pub mod audio_ports;
28pub mod latency;
29pub mod log;
30pub mod note_ports;
31pub mod params;
32pub mod state;
33pub mod tail;
34
35/// Plugin extensions.
36pub trait Extensions<P: Plugin> {
37    fn audio_ports() -> Option<impl AudioPorts<P>> {
38        None::<()>
39    }
40
41    fn latency() -> Option<impl Latency<P>> {
42        None::<()>
43    }
44
45    fn note_ports() -> Option<impl NotePorts<P>> {
46        None::<()>
47    }
48
49    fn params() -> Option<impl Params<P>> {
50        None::<()>
51    }
52
53    fn state() -> Option<impl State<P>> {
54        None::<()>
55    }
56
57    fn tail() -> Option<impl Tail<P>> {
58        None::<()>
59    }
60}
61
62#[derive(Debug)]
63pub enum Error {
64    Log(log::Error),
65    AudioPorts(audio_ports::Error),
66    NotePorts(note_ports::Error),
67    Params(params::Error),
68    State(state::Error),
69}
70
71impl Display for Error {
72    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
73        match self {
74            Error::Log(e) => write!(f, "log: {e}"),
75            Error::AudioPorts(e) => write!(f, "audio_ports: {e}"),
76            Error::NotePorts(e) => write!(f, "note_ports: {e}"),
77            Error::Params(e) => write!(f, "params: {e}"),
78            Error::State(e) => write!(f, "state: {e}"),
79        }
80    }
81}
82
83impl From<Error> for crate::Error {
84    fn from(value: Error) -> Self {
85        Self::Extension(value)
86    }
87}