unnieversal/crates/utils/src/crossfade.rs

31 lines
748 B
Rust

/// Linear crossfade
/// x is the crossfading param, [0, 1]
/// Returns (fade_in, fade_out)
pub fn lin_crossfade(x: f32) -> (f32, f32) {
(x, 1.0 - x)
}
// next two are from https://signalsmith-audio.co.uk/writing/2021/cheap-energy-crossfade/
/// Amplitude preserving crossfade
/// x is the crossfading param, [0, 1]
/// Returns (fade_in, fade_out)
pub fn ap_crossfade(x: f32) -> (f32, f32) {
let fin = x * x * (3.0 - 2.0 * x);
(fin, 1.0 - fin)
}
/// Energy preserving crossfade
/// x is the crossfading param, [0, 1]
/// Returns (fade_in, fade_out)
pub fn ep_crossfade(x: f32) -> (f32, f32) {
let x2 = 1.0 - x;
let a = x * x2;
let b = a * (1.0 + 1.4186 * a);
let c = b + x;
let d = b + x2;
(c * c, d * d)
}