clack_common/process.rs
1//! Types exposing data and metadata to be used by plugins during audio processing.
2
3use clap_sys::process::*;
4use std::fmt::Debug;
5
6mod constant_mask;
7pub use constant_mask::*;
8
9/// Status returned by a plugin after processing.
10///
11/// This is mainly used as a way for the plugin to tell the host when it can be safely put to sleep.
12///
13/// Note that Clack uses a [`Result`] enum for relaying a failed processing to the host,
14/// unlike the C CLAP API which uses an extra state in enum (`CLAP_PROCESS_ERROR`) to indicate failure.
15#[repr(i32)]
16#[derive(Debug, Copy, Clone, Eq, PartialEq)]
17pub enum ProcessStatus {
18 /// Processing should continue: the plugin has no desire to be put to sleep.
19 Continue = CLAP_PROCESS_CONTINUE,
20 /// Processing should continue, unless all outputs are quiet.
21 ContinueIfNotQuiet = CLAP_PROCESS_CONTINUE_IF_NOT_QUIET,
22 /// The plugin is currently processing its tail (e.g. release, reverb, etc.).
23 ///
24 /// Use the `tail` extension to query the plugin for its current tail length.
25 Tail = CLAP_PROCESS_TAIL,
26 /// No more processing is required until the next event or variation in audio input.
27 Sleep = CLAP_PROCESS_SLEEP,
28}
29
30impl ProcessStatus {
31 /// Gets a [`ProcessStatus`] from the raw, C-FFI compatible value.
32 ///
33 /// In order to match Clack's APIs, this returns `Some(Err(()))` if the value is
34 /// `CLAP_PROCESS_ERROR`.
35 ///
36 /// If the given integer does not match any known CLAP Processing status codes, [`None`] is
37 /// returned.
38 #[inline]
39 pub fn from_raw(raw: clap_process_status) -> Option<Result<Self, ()>> {
40 use ProcessStatus::*;
41
42 match raw {
43 CLAP_PROCESS_CONTINUE => Some(Ok(Continue)),
44 CLAP_PROCESS_CONTINUE_IF_NOT_QUIET => Some(Ok(ContinueIfNotQuiet)),
45 CLAP_PROCESS_SLEEP => Some(Ok(Sleep)),
46 CLAP_PROCESS_TAIL => Some(Ok(Tail)),
47 CLAP_PROCESS_ERROR => Some(Err(())),
48 _ => None,
49 }
50 }
51
52 /// Combines two [`ProcessStatus`]es, returning one that meets the minimum processing requirements
53 /// of both statuses.
54 #[inline]
55 pub fn combined_with(self, other: ProcessStatus) -> ProcessStatus {
56 use ProcessStatus::*;
57
58 match (self, other) {
59 (Continue, _) | (_, Continue) => Continue,
60 (ContinueIfNotQuiet, _) | (_, ContinueIfNotQuiet) => ContinueIfNotQuiet,
61 (Tail, _) | (_, Tail) => Tail,
62 (Sleep, Sleep) => Sleep,
63 }
64 }
65}
66
67/// The audio configuration passed to a plugin's audio processor upon activation.
68///
69/// Those settings are constant throughout the audio processor's lifetime,
70/// i.e. from a plugin's activation until its deactivation.
71#[derive(Copy, Clone, Debug, PartialEq)]
72pub struct PluginAudioConfiguration {
73 /// The audio's sample rate.
74 pub sample_rate: f64,
75 /// The minimum amount of frames that will be processed at once.
76 pub min_frames_count: u32,
77 /// The maximum amount of frames that will be processed at once.
78 pub max_frames_count: u32,
79}
80
81use clap_sys::audio_buffer::clap_audio_buffer;
82
83/// Processing-related information about an audio port.
84pub struct AudioPortProcessingInfo {
85 channel_count: u32,
86 latency: u32,
87 constant_mask: ConstantMask,
88}
89
90impl AudioPortProcessingInfo {
91 /// Extracts the processing-related information from a raw, C-FFI compatible audio buffer
92 /// descriptor.
93 #[inline]
94 pub fn from_raw(raw: &clap_audio_buffer) -> Self {
95 Self {
96 channel_count: raw.channel_count,
97 latency: raw.latency,
98 constant_mask: ConstantMask::from_bits(raw.constant_mask),
99 }
100 }
101
102 /// Returns the number of audio channels this port provides.
103 #[inline]
104 pub fn channel_count(&self) -> u32 {
105 self.channel_count
106 }
107
108 /// Returns the latency to or from the audio interface, in samples.
109 ///
110 /// Whether this latency is to or from the audio interface depends on which kind of port
111 /// this describes, an output port or an input port respectively
112 #[inline]
113 pub fn latency(&self) -> u32 {
114 self.latency
115 }
116
117 /// Returns the [`ConstantMask`] of this port, hinting which audio channels are constant.
118 #[inline]
119 pub fn constant_mask(&self) -> ConstantMask {
120 self.constant_mask
121 }
122}