Skip to main content

kira/
effect.rs

1/*!
2Modifies audio signals.
3
4Any type that implements [`EffectBuilder`] can be added to a mixer track by
5using [`TrackBuilder::add_effect`](crate::track::TrackBuilder::add_effect). Kira
6comes with a number of commonly used effects.
7
8If needed, you can create custom effects by implementing the [`EffectBuilder`]
9and [`Effect`] traits.
10*/
11
12pub mod compressor;
13pub mod delay;
14pub mod distortion;
15pub mod eq_filter;
16pub mod filter;
17pub mod panning_control;
18pub mod reverb;
19pub mod volume_control;
20
21use crate::{frame::Frame, info::Info};
22
23/// Configures an effect.
24pub trait EffectBuilder {
25	/// Allows the user to control the effect from gameplay code.
26	type Handle;
27
28	/// Creates the effect and a handle to the effect.
29	#[must_use]
30	fn build(self) -> (Box<dyn Effect>, Self::Handle);
31}
32
33/// Receives input audio from a mixer track and outputs modified audio.
34///
35/// For performance reasons, avoid allocating and deallocating memory in any methods
36/// of this trait besides [`on_change_sample_rate`](Effect::on_change_sample_rate).
37#[allow(unused_variables)]
38pub trait Effect: Send {
39	/// Called when the effect is first sent to the renderer.
40	fn init(&mut self, sample_rate: u32, internal_buffer_size: usize) {}
41
42	/// Called when the sample rate of the renderer is changed.
43	fn on_change_sample_rate(&mut self, sample_rate: u32) {}
44
45	/// Called whenever a new batch of audio samples is requested by the backend.
46	///
47	/// This is a good place to put code that needs to run fairly frequently,
48	/// but not for every single audio sample.
49	fn on_start_processing(&mut self) {}
50
51	/// Transforms a slice of input [`Frame`]s.
52	///
53	/// `dt` is the time between each frame (in seconds).
54	fn process(&mut self, input: &mut [Frame], dt: f64, info: &Info);
55}