mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-30 10:15:00 +01:00
715 lines
26 KiB
Rust
715 lines
26 KiB
Rust
//! Per-track mixer state: inserts, sends, routing, fader, pan, mute/solo.
|
|
|
|
use crate::{AutomationMode, BusId, SourceId};
|
|
use std::collections::BTreeMap;
|
|
|
|
pub const INSERT_SLOTS: usize = 10;
|
|
pub const SEND_SLOTS: usize = 10;
|
|
|
|
/// Channel formats (track widths).
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, serde::Serialize, serde::Deserialize)]
|
|
pub enum ChannelFormat {
|
|
#[default]
|
|
Mono,
|
|
Stereo,
|
|
Lcr,
|
|
Quad,
|
|
Lcrs,
|
|
Surround50,
|
|
Surround51,
|
|
Surround60,
|
|
Surround61,
|
|
Sdds70,
|
|
Sdds71,
|
|
Surround70,
|
|
Surround71,
|
|
Atmos702,
|
|
Atmos712,
|
|
Atmos502,
|
|
Atmos512,
|
|
Atmos504,
|
|
Atmos514,
|
|
Atmos704,
|
|
Atmos714,
|
|
Atmos706,
|
|
Atmos716,
|
|
Atmos904,
|
|
Atmos914,
|
|
Atmos906,
|
|
Atmos916,
|
|
/// Ambisonics of order 1..=7 ((n+1)² channels).
|
|
Ambisonics(u8),
|
|
}
|
|
|
|
impl ChannelFormat {
|
|
pub const ALL: [ChannelFormat; 34] = [
|
|
ChannelFormat::Mono,
|
|
ChannelFormat::Stereo,
|
|
ChannelFormat::Lcr,
|
|
ChannelFormat::Quad,
|
|
ChannelFormat::Lcrs,
|
|
ChannelFormat::Surround50,
|
|
ChannelFormat::Surround51,
|
|
ChannelFormat::Surround60,
|
|
ChannelFormat::Surround61,
|
|
ChannelFormat::Sdds70,
|
|
ChannelFormat::Sdds71,
|
|
ChannelFormat::Surround70,
|
|
ChannelFormat::Surround71,
|
|
ChannelFormat::Atmos702,
|
|
ChannelFormat::Atmos712,
|
|
ChannelFormat::Ambisonics(1),
|
|
ChannelFormat::Ambisonics(2),
|
|
ChannelFormat::Ambisonics(3),
|
|
ChannelFormat::Ambisonics(4),
|
|
ChannelFormat::Ambisonics(5),
|
|
ChannelFormat::Ambisonics(6),
|
|
ChannelFormat::Ambisonics(7),
|
|
ChannelFormat::Atmos502,
|
|
ChannelFormat::Atmos512,
|
|
ChannelFormat::Atmos504,
|
|
ChannelFormat::Atmos514,
|
|
ChannelFormat::Atmos704,
|
|
ChannelFormat::Atmos714,
|
|
ChannelFormat::Atmos706,
|
|
ChannelFormat::Atmos716,
|
|
ChannelFormat::Atmos904,
|
|
ChannelFormat::Atmos914,
|
|
ChannelFormat::Atmos906,
|
|
ChannelFormat::Atmos916,
|
|
];
|
|
pub fn channels(self) -> usize {
|
|
match self {
|
|
ChannelFormat::Mono => 1,
|
|
ChannelFormat::Stereo => 2,
|
|
ChannelFormat::Lcr => 3,
|
|
ChannelFormat::Quad | ChannelFormat::Lcrs => 4,
|
|
ChannelFormat::Surround50 => 5,
|
|
ChannelFormat::Surround51 | ChannelFormat::Surround60 => 6,
|
|
ChannelFormat::Surround61 | ChannelFormat::Sdds70 | ChannelFormat::Surround70 | ChannelFormat::Atmos502 => 7,
|
|
ChannelFormat::Sdds71 | ChannelFormat::Surround71 | ChannelFormat::Atmos512 => 8,
|
|
ChannelFormat::Atmos702 | ChannelFormat::Atmos504 => 9,
|
|
ChannelFormat::Atmos712 | ChannelFormat::Atmos514 => 10,
|
|
ChannelFormat::Atmos704 => 11,
|
|
ChannelFormat::Atmos714 => 12,
|
|
ChannelFormat::Atmos706 | ChannelFormat::Atmos904 => 13,
|
|
ChannelFormat::Atmos716 | ChannelFormat::Atmos914 => 14,
|
|
ChannelFormat::Atmos906 => 15,
|
|
ChannelFormat::Atmos916 => 16,
|
|
ChannelFormat::Ambisonics(n) => {
|
|
let n = usize::from(n.clamp(1, 7)) + 1;
|
|
n * n
|
|
}
|
|
}
|
|
}
|
|
pub fn label(self) -> &'static str {
|
|
match self {
|
|
ChannelFormat::Mono => "Mono",
|
|
ChannelFormat::Stereo => "Stereo",
|
|
ChannelFormat::Lcr => "LCR",
|
|
ChannelFormat::Quad => "Quad",
|
|
ChannelFormat::Lcrs => "LCRS",
|
|
ChannelFormat::Surround50 => "5.0",
|
|
ChannelFormat::Surround51 => "5.1",
|
|
ChannelFormat::Surround60 => "6.0",
|
|
ChannelFormat::Surround61 => "6.1",
|
|
ChannelFormat::Sdds70 => "7.0 SDDS",
|
|
ChannelFormat::Sdds71 => "7.1 SDDS",
|
|
ChannelFormat::Surround70 => "7.0",
|
|
ChannelFormat::Surround71 => "7.1",
|
|
ChannelFormat::Atmos702 => "7.0.2",
|
|
ChannelFormat::Atmos712 => "7.1.2",
|
|
ChannelFormat::Atmos502 => "5.0.2",
|
|
ChannelFormat::Atmos512 => "5.1.2",
|
|
ChannelFormat::Atmos504 => "5.0.4",
|
|
ChannelFormat::Atmos514 => "5.1.4",
|
|
ChannelFormat::Atmos704 => "7.0.4",
|
|
ChannelFormat::Atmos714 => "7.1.4",
|
|
ChannelFormat::Atmos706 => "7.0.6",
|
|
ChannelFormat::Atmos716 => "7.1.6",
|
|
ChannelFormat::Atmos904 => "9.0.4",
|
|
ChannelFormat::Atmos914 => "9.1.4",
|
|
ChannelFormat::Atmos906 => "9.0.6",
|
|
ChannelFormat::Atmos916 => "9.1.6",
|
|
ChannelFormat::Ambisonics(1) => "1st Order Ambisonics",
|
|
ChannelFormat::Ambisonics(2) => "2nd Order Ambisonics",
|
|
ChannelFormat::Ambisonics(3) => "3rd Order Ambisonics",
|
|
ChannelFormat::Ambisonics(4) => "4th Order Ambisonics",
|
|
ChannelFormat::Ambisonics(5) => "5th Order Ambisonics",
|
|
ChannelFormat::Ambisonics(6) => "6th Order Ambisonics",
|
|
ChannelFormat::Ambisonics(_) => "7th Order Ambisonics",
|
|
}
|
|
}
|
|
pub fn from_id(s: &str) -> Option<ChannelFormat> {
|
|
ChannelFormat::ALL.into_iter().find(|f| f.label().eq_ignore_ascii_case(s.trim()))
|
|
}
|
|
/// Speakers in channel order, or empty for Ambisonics (ACN order, SN3D).
|
|
///
|
|
/// Channel order is SMPTE / WAV order: speakers with a WAVE_FORMAT_EXTENSIBLE mask bit come in
|
|
/// ascending bit order (L R C LFE Ls Rs Lc Rc Cs Lss Rss Ltf Rtf Ltr Rtr; 7.x puts the rear
|
|
/// surrounds Lrs/Rrs on the "back" bits before the sides Lss/Rss), followed by speakers WAV
|
|
/// cannot name (the top-middle pair of .6 layouts). E.g. 5.1 = L R C LFE Ls Rs,
|
|
/// 7.1 = L R C LFE Lrs Rrs Lss Rss, 7.1.4 = L R C LFE Lrs Rrs Lss Rss Ltf Rtf Ltr Rtr.
|
|
pub fn speakers(self) -> &'static [Speaker] {
|
|
use Speaker::*;
|
|
match self {
|
|
ChannelFormat::Mono => &[C],
|
|
ChannelFormat::Stereo => &[L, R],
|
|
ChannelFormat::Lcr => &[L, R, C],
|
|
ChannelFormat::Quad => &[L, R, Ls, Rs],
|
|
ChannelFormat::Lcrs => &[L, R, C, Cs],
|
|
ChannelFormat::Surround50 => &[L, R, C, Ls, Rs],
|
|
ChannelFormat::Surround51 => &[L, R, C, Lfe, Ls, Rs],
|
|
ChannelFormat::Surround60 => &[L, R, C, Ls, Rs, Cs],
|
|
ChannelFormat::Surround61 => &[L, R, C, Lfe, Ls, Rs, Cs],
|
|
ChannelFormat::Sdds70 => &[L, R, C, Ls, Rs, Lc, Rc],
|
|
ChannelFormat::Sdds71 => &[L, R, C, Lfe, Ls, Rs, Lc, Rc],
|
|
ChannelFormat::Surround70 => &[L, R, C, Lrs, Rrs, Lss, Rss],
|
|
ChannelFormat::Surround71 => &[L, R, C, Lfe, Lrs, Rrs, Lss, Rss],
|
|
ChannelFormat::Atmos702 => &[L, R, C, Lrs, Rrs, Lss, Rss, Ltm, Rtm],
|
|
ChannelFormat::Atmos712 => &[L, R, C, Lfe, Lrs, Rrs, Lss, Rss, Ltm, Rtm],
|
|
ChannelFormat::Atmos502 => &[L, R, C, Ls, Rs, Ltm, Rtm],
|
|
ChannelFormat::Atmos512 => &[L, R, C, Lfe, Ls, Rs, Ltm, Rtm],
|
|
ChannelFormat::Atmos504 => &[L, R, C, Ls, Rs, Ltf, Rtf, Ltr, Rtr],
|
|
ChannelFormat::Atmos514 => &[L, R, C, Lfe, Ls, Rs, Ltf, Rtf, Ltr, Rtr],
|
|
ChannelFormat::Atmos704 => &[L, R, C, Lrs, Rrs, Lss, Rss, Ltf, Rtf, Ltr, Rtr],
|
|
ChannelFormat::Atmos714 => &[L, R, C, Lfe, Lrs, Rrs, Lss, Rss, Ltf, Rtf, Ltr, Rtr],
|
|
ChannelFormat::Atmos706 => &[L, R, C, Lrs, Rrs, Lss, Rss, Ltf, Rtf, Ltr, Rtr, Ltm, Rtm],
|
|
ChannelFormat::Atmos716 => &[L, R, C, Lfe, Lrs, Rrs, Lss, Rss, Ltf, Rtf, Ltr, Rtr, Ltm, Rtm],
|
|
ChannelFormat::Atmos904 => &[L, R, C, Lrs, Rrs, Lw, Rw, Lss, Rss, Ltf, Rtf, Ltr, Rtr],
|
|
ChannelFormat::Atmos914 => &[L, R, C, Lfe, Lrs, Rrs, Lw, Rw, Lss, Rss, Ltf, Rtf, Ltr, Rtr],
|
|
ChannelFormat::Atmos906 => &[L, R, C, Lrs, Rrs, Lw, Rw, Lss, Rss, Ltf, Rtf, Ltr, Rtr, Ltm, Rtm],
|
|
ChannelFormat::Atmos916 => &[L, R, C, Lfe, Lrs, Rrs, Lw, Rw, Lss, Rss, Ltf, Rtf, Ltr, Rtr, Ltm, Rtm],
|
|
ChannelFormat::Ambisonics(_) => &[],
|
|
}
|
|
}
|
|
pub fn is_ambisonic(self) -> bool {
|
|
matches!(self, ChannelFormat::Ambisonics(_))
|
|
}
|
|
pub fn has_lfe(self) -> bool {
|
|
self.speakers().contains(&Speaker::Lfe)
|
|
}
|
|
pub fn has_height(self) -> bool {
|
|
self.speakers().iter().any(|s| s.is_height())
|
|
}
|
|
/// Short per-channel labels for meters (`W Y Z X 5…` for Ambisonics).
|
|
pub fn channel_label(self, ch: usize) -> String {
|
|
match self.speakers().get(ch) {
|
|
Some(s) => s.label().to_string(),
|
|
None if self.is_ambisonic() => ["W", "Y", "Z", "X"].get(ch).map_or_else(|| format!("{}", ch + 1), ToString::to_string),
|
|
None => format!("{}", ch + 1),
|
|
}
|
|
}
|
|
/// WAVE_FORMAT_EXTENSIBLE channel mask (0 = no positions, e.g. Ambisonics). Speakers that WAV
|
|
/// cannot name (top middle) borrow the top-front bits when the layout has no top fronts.
|
|
pub fn channel_mask(self) -> u32 {
|
|
let sp = self.speakers();
|
|
let has_tf = sp.contains(&Speaker::Ltf);
|
|
sp.iter().fold(0u32, |m, s| match s {
|
|
Speaker::Ltm if !has_tf => m | 0x1000,
|
|
Speaker::Rtm if !has_tf => m | 0x4000,
|
|
s => m | s.wave_bit(),
|
|
})
|
|
}
|
|
/// Best format for a channel count.
|
|
pub fn for_channels(n: usize) -> ChannelFormat {
|
|
[
|
|
ChannelFormat::Mono,
|
|
ChannelFormat::Stereo,
|
|
ChannelFormat::Lcr,
|
|
ChannelFormat::Quad,
|
|
ChannelFormat::Surround50,
|
|
ChannelFormat::Surround51,
|
|
ChannelFormat::Surround70,
|
|
ChannelFormat::Surround71,
|
|
]
|
|
.into_iter()
|
|
.find(|f| f.channels() == n)
|
|
.unwrap_or(if n <= 1 { ChannelFormat::Mono } else { ChannelFormat::Stereo })
|
|
}
|
|
}
|
|
|
|
/// A loudspeaker position in a channel format.
|
|
///
|
|
/// Angles follow ITU-R BS.775 / Dolby conventions: azimuth in degrees, 0 = front centre, positive
|
|
/// = to the right; elevation in degrees above the listener plane.
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
|
pub enum Speaker {
|
|
L,
|
|
R,
|
|
C,
|
|
Lfe,
|
|
/// 5.x surrounds (±110°).
|
|
Ls,
|
|
Rs,
|
|
/// SDDS inner fronts (±15°).
|
|
Lc,
|
|
Rc,
|
|
/// Centre surround (180°).
|
|
Cs,
|
|
/// 7.x side surrounds (±90°).
|
|
Lss,
|
|
Rss,
|
|
/// 7.x rear surrounds (±150°).
|
|
Lrs,
|
|
Rrs,
|
|
/// 9.x front wides (±60°).
|
|
Lw,
|
|
Rw,
|
|
/// Top front (±45°, elevation 45°).
|
|
Ltf,
|
|
Rtf,
|
|
/// Top middle (±90°, elevation 45°).
|
|
Ltm,
|
|
Rtm,
|
|
/// Top rear (±135°, elevation 45°).
|
|
Ltr,
|
|
Rtr,
|
|
}
|
|
|
|
impl Speaker {
|
|
pub fn azimuth(self) -> f32 {
|
|
match self {
|
|
Speaker::L => -30.0,
|
|
Speaker::R => 30.0,
|
|
Speaker::C | Speaker::Lfe => 0.0,
|
|
Speaker::Ls => -110.0,
|
|
Speaker::Rs => 110.0,
|
|
Speaker::Lc => -15.0,
|
|
Speaker::Rc => 15.0,
|
|
Speaker::Cs => 180.0,
|
|
Speaker::Lss => -90.0,
|
|
Speaker::Rss => 90.0,
|
|
Speaker::Lrs => -150.0,
|
|
Speaker::Rrs => 150.0,
|
|
Speaker::Lw => -60.0,
|
|
Speaker::Rw => 60.0,
|
|
Speaker::Ltf => -45.0,
|
|
Speaker::Rtf => 45.0,
|
|
Speaker::Ltm => -90.0,
|
|
Speaker::Rtm => 90.0,
|
|
Speaker::Ltr => -135.0,
|
|
Speaker::Rtr => 135.0,
|
|
}
|
|
}
|
|
pub fn elevation(self) -> f32 {
|
|
if self.is_height() { 45.0 } else { 0.0 }
|
|
}
|
|
pub fn is_height(self) -> bool {
|
|
matches!(self, Speaker::Ltf | Speaker::Rtf | Speaker::Ltm | Speaker::Rtm | Speaker::Ltr | Speaker::Rtr)
|
|
}
|
|
pub fn is_lfe(self) -> bool {
|
|
self == Speaker::Lfe
|
|
}
|
|
pub fn label(self) -> &'static str {
|
|
match self {
|
|
Speaker::L => "L",
|
|
Speaker::R => "R",
|
|
Speaker::C => "C",
|
|
Speaker::Lfe => "LFE",
|
|
Speaker::Ls => "Ls",
|
|
Speaker::Rs => "Rs",
|
|
Speaker::Lc => "Lc",
|
|
Speaker::Rc => "Rc",
|
|
Speaker::Cs => "Cs",
|
|
Speaker::Lss => "Lss",
|
|
Speaker::Rss => "Rss",
|
|
Speaker::Lrs => "Lrs",
|
|
Speaker::Rrs => "Rrs",
|
|
Speaker::Lw => "Lw",
|
|
Speaker::Rw => "Rw",
|
|
Speaker::Ltf => "Ltf",
|
|
Speaker::Rtf => "Rtf",
|
|
Speaker::Ltm => "Ltm",
|
|
Speaker::Rtm => "Rtm",
|
|
Speaker::Ltr => "Ltr",
|
|
Speaker::Rtr => "Rtr",
|
|
}
|
|
}
|
|
/// WAVE_FORMAT_EXTENSIBLE `dwChannelMask` bit (0 for speakers WAV cannot name).
|
|
pub fn wave_bit(self) -> u32 {
|
|
match self {
|
|
Speaker::L => 0x1,
|
|
Speaker::R => 0x2,
|
|
Speaker::C => 0x4,
|
|
Speaker::Lfe => 0x8,
|
|
Speaker::Ls | Speaker::Lrs => 0x10,
|
|
Speaker::Rs | Speaker::Rrs => 0x20,
|
|
Speaker::Lc | Speaker::Lw => 0x40,
|
|
Speaker::Rc | Speaker::Rw => 0x80,
|
|
Speaker::Cs => 0x100,
|
|
Speaker::Lss => 0x200,
|
|
Speaker::Rss => 0x400,
|
|
Speaker::Ltf => 0x1000,
|
|
Speaker::Rtf => 0x4000,
|
|
Speaker::Ltr => 0x8000,
|
|
Speaker::Rtr => 0x2_0000,
|
|
Speaker::Ltm | Speaker::Rtm => 0,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Surround panner position (Pro Tools-style X/Y puck). `None` on a mixer means "use the stereo
|
|
/// pan", which keeps sessions made before surround mixing sounding exactly as they did.
|
|
#[derive(Debug, Clone, Copy, PartialEq, serde::Serialize, serde::Deserialize)]
|
|
#[serde(default)]
|
|
pub struct SurroundPan {
|
|
/// -1 (left) ..= 1 (right).
|
|
pub x: f32,
|
|
/// -1 (back) ..= 1 (front).
|
|
pub y: f32,
|
|
/// Elevation 0 (ear level) ..= 1 (overhead), used by height formats.
|
|
pub z: f32,
|
|
/// 0 = point source ..= 1 = spread over every speaker.
|
|
pub divergence: f32,
|
|
/// Centre speaker share of front-centre images, 0..=100 % (0 = phantom centre on L/R).
|
|
pub center: f32,
|
|
/// LFE send level, dB (-144 = off).
|
|
pub lfe_db: f32,
|
|
}
|
|
|
|
impl Default for SurroundPan {
|
|
fn default() -> Self {
|
|
SurroundPan { x: 0.0, y: 1.0, z: 0.0, divergence: 0.0, center: 100.0, lfe_db: -144.0 }
|
|
}
|
|
}
|
|
|
|
impl SurroundPan {
|
|
/// Puck at the stereo pan position (front edge).
|
|
pub fn from_stereo(pan: f32) -> Self {
|
|
SurroundPan { x: finite_or(pan, 0.0).clamp(-1.0, 1.0), ..SurroundPan::default() }
|
|
}
|
|
/// Clamp into range (NaN → defaults).
|
|
pub fn sanitize(&mut self) {
|
|
let d = SurroundPan::default();
|
|
self.x = finite_or(self.x, d.x).clamp(-1.0, 1.0);
|
|
self.y = finite_or(self.y, d.y).clamp(-1.0, 1.0);
|
|
self.z = finite_or(self.z, d.z).clamp(0.0, 1.0);
|
|
self.divergence = finite_or(self.divergence, d.divergence).clamp(0.0, 1.0);
|
|
self.center = finite_or(self.center, d.center).clamp(0.0, 100.0);
|
|
self.lfe_db = finite_or(self.lfe_db, d.lfe_db).clamp(-144.0, 12.0);
|
|
}
|
|
}
|
|
|
|
/// An internal mix bus (Pro Tools "bus" paths).
|
|
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
|
|
pub struct Bus {
|
|
pub id: BusId,
|
|
pub name: String,
|
|
pub format: ChannelFormat,
|
|
}
|
|
|
|
/// Physical output paths (from the I/O setup).
|
|
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
|
|
pub struct OutputPath {
|
|
pub name: String,
|
|
/// First hardware channel (0-based).
|
|
pub first_channel: u16,
|
|
pub format: ChannelFormat,
|
|
}
|
|
|
|
/// Where a signal goes or comes from.
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default, serde::Serialize, serde::Deserialize)]
|
|
pub enum Route {
|
|
#[default]
|
|
None,
|
|
/// The main output (hardware outputs 1-2, or the bounce source).
|
|
Main,
|
|
Bus(BusId),
|
|
/// Hardware input/output by path name.
|
|
Hardware(String),
|
|
}
|
|
|
|
/// A plugin instance on an insert slot.
|
|
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
|
|
pub struct Insert {
|
|
/// Registry id from `soundcraft-dsp` (e.g. `eq7`, `comp`).
|
|
pub plugin: String,
|
|
#[serde(default, deserialize_with = "finite_params")]
|
|
pub params: BTreeMap<String, f32>,
|
|
#[serde(default)]
|
|
pub bypass: bool,
|
|
#[serde(default = "yes")]
|
|
pub active: bool,
|
|
/// Preset name shown in the plugin window header.
|
|
#[serde(default)]
|
|
pub preset: String,
|
|
/// Opaque plugin state (third-party plugins: CLAP state / VST3 component + controller
|
|
/// state), standard base64. Restored into a new instance before it first processes.
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub state: Option<String>,
|
|
/// The session audio source a sample-playing instrument (the built-in Sampler) plays.
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub sample: Option<SourceId>,
|
|
}
|
|
|
|
fn yes() -> bool {
|
|
true
|
|
}
|
|
|
|
impl Insert {
|
|
pub fn new(plugin: impl Into<String>) -> Self {
|
|
Insert {
|
|
plugin: plugin.into(),
|
|
params: BTreeMap::new(),
|
|
bypass: false,
|
|
active: true,
|
|
preset: String::from("<factory default>"),
|
|
state: None,
|
|
sample: None,
|
|
}
|
|
}
|
|
|
|
/// The decoded plugin state (`None` when absent or malformed).
|
|
pub fn state_bytes(&self) -> Option<Vec<u8>> {
|
|
self.state.as_deref().and_then(crate::b64::decode)
|
|
}
|
|
|
|
/// Stores a plugin state blob (base64-encoded); an empty blob clears it.
|
|
pub fn set_state_bytes(&mut self, data: &[u8]) {
|
|
self.state = (!data.is_empty()).then(|| crate::b64::encode(data));
|
|
}
|
|
}
|
|
|
|
/// A send to a bus.
|
|
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
|
|
pub struct SendSlot {
|
|
pub target: Route,
|
|
pub level_db: f32,
|
|
pub pan: f32,
|
|
pub mute: bool,
|
|
pub pre_fader: bool,
|
|
#[serde(default = "yes")]
|
|
pub follow_main_pan: bool,
|
|
/// Surround position into a multichannel bus (None = follow the track / stereo pan).
|
|
#[serde(default)]
|
|
pub surround: Option<SurroundPan>,
|
|
}
|
|
|
|
impl SendSlot {
|
|
pub fn new(target: Route) -> Self {
|
|
SendSlot { target, level_db: -144.0, pan: 0.0, mute: false, pre_fader: false, follow_main_pan: true, surround: None }
|
|
}
|
|
}
|
|
|
|
/// The channel strip.
|
|
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
|
|
pub struct Mixer {
|
|
pub volume_db: f32,
|
|
/// One panner per source channel for mono/stereo (stereo tracks: two panners, default hard L/R).
|
|
pub pan: Vec<f32>,
|
|
pub mute: bool,
|
|
pub solo: bool,
|
|
#[serde(default)]
|
|
pub solo_safe: bool,
|
|
/// Live record-enable state. Runtime-only: opening a session must never arm an input.
|
|
#[serde(skip)]
|
|
pub record_arm: bool,
|
|
/// Live input-monitor state. Runtime-only: opening a session must never enable monitoring.
|
|
#[serde(skip)]
|
|
pub input_monitor: bool,
|
|
#[serde(default)]
|
|
pub phase_invert: bool,
|
|
pub input: Route,
|
|
pub output: Route,
|
|
pub inserts: Vec<Option<Insert>>,
|
|
pub sends: Vec<Option<SendSlot>>,
|
|
pub automation_mode: AutomationMode,
|
|
/// Input gain / trim, dB.
|
|
#[serde(default)]
|
|
pub trim_db: f32,
|
|
/// VCA master that controls this track, if any (track id of the VCA).
|
|
#[serde(default)]
|
|
pub vca: Option<u64>,
|
|
/// Surround panner, used when the output is multichannel (None = the stereo pan to L/R).
|
|
#[serde(default)]
|
|
pub surround: Option<SurroundPan>,
|
|
}
|
|
|
|
impl Mixer {
|
|
pub fn new(format: ChannelFormat) -> Self {
|
|
let pan = match format {
|
|
ChannelFormat::Mono => vec![0.0],
|
|
ChannelFormat::Stereo => vec![-1.0, 1.0],
|
|
_ => Vec::new(),
|
|
};
|
|
Mixer {
|
|
volume_db: 0.0,
|
|
pan,
|
|
mute: false,
|
|
solo: false,
|
|
solo_safe: false,
|
|
record_arm: false,
|
|
input_monitor: false,
|
|
phase_invert: false,
|
|
input: Route::None,
|
|
output: Route::Main,
|
|
inserts: vec![None; INSERT_SLOTS],
|
|
sends: vec![None; SEND_SLOTS],
|
|
automation_mode: AutomationMode::Read,
|
|
trim_db: 0.0,
|
|
vca: None,
|
|
surround: None,
|
|
}
|
|
}
|
|
|
|
/// Clamp everything into range (after deserialising untrusted files).
|
|
pub fn sanitize(&mut self) {
|
|
self.volume_db = finite_or(self.volume_db, 0.0).clamp(-144.0, 12.0);
|
|
for p in &mut self.pan {
|
|
*p = finite_or(*p, 0.0).clamp(-1.0, 1.0);
|
|
}
|
|
self.inserts.resize(INSERT_SLOTS, None);
|
|
self.sends.resize(SEND_SLOTS, None);
|
|
for s in self.sends.iter_mut().flatten() {
|
|
s.level_db = finite_or(s.level_db, -144.0).clamp(-144.0, 12.0);
|
|
s.pan = finite_or(s.pan, 0.0).clamp(-1.0, 1.0);
|
|
if let Some(sp) = &mut s.surround {
|
|
sp.sanitize();
|
|
}
|
|
}
|
|
if let Some(sp) = &mut self.surround {
|
|
sp.sanitize();
|
|
}
|
|
self.trim_db = finite_or(self.trim_db, 0.0).clamp(-144.0, 24.0);
|
|
}
|
|
}
|
|
|
|
impl crate::Session {
|
|
/// The main mix format: the format of the first (main) output path, at least stereo and at
|
|
/// most 16 channels (wider formats fall back to stereo).
|
|
pub fn main_format(&self) -> ChannelFormat {
|
|
match self.outputs.first().map(|o| o.format) {
|
|
Some(f) if (2..=16).contains(&f.channels()) => f,
|
|
_ => ChannelFormat::Stereo,
|
|
}
|
|
}
|
|
}
|
|
|
|
pub(crate) fn finite_or(v: f32, d: f32) -> f32 {
|
|
if v.is_finite() { v } else { d }
|
|
}
|
|
|
|
/// Parameters saved as `null` (how JSON writes NaN and infinity) or beyond `f32` are dropped, so
|
|
/// they keep their defaults instead of failing the whole session.
|
|
fn finite_params<'de, D: serde::Deserializer<'de>>(d: D) -> Result<BTreeMap<String, f32>, D::Error> {
|
|
let params: BTreeMap<String, Option<f64>> = serde::Deserialize::deserialize(d)?;
|
|
Ok(params.into_iter().filter_map(|(k, v)| Some((k, v.map(|v| v as f32).filter(|v| v.is_finite())?))).collect())
|
|
}
|
|
|
|
/// Fader taper used by Pro Tools-style faders: position 0..1 ↔ dB (-inf..+12).
|
|
pub fn fader_pos_to_db(pos: f32) -> f32 {
|
|
let p = finite_or(pos, 0.0).clamp(0.0, 1.0);
|
|
if p <= 0.0 {
|
|
return -144.0;
|
|
}
|
|
// Piecewise: top 75 % covers -24..+12 linearly-ish, the bottom compresses to -inf.
|
|
if p >= 0.25 { -24.0 + (p - 0.25) / 0.75 * 36.0 } else { -24.0 - (1.0 - p / 0.25).powf(1.5) * 120.0 }
|
|
}
|
|
|
|
pub fn fader_db_to_pos(db: f32) -> f32 {
|
|
let db = finite_or(db, -144.0);
|
|
if db <= -144.0 {
|
|
return 0.0;
|
|
}
|
|
if db >= -24.0 {
|
|
(0.25 + (db.min(12.0) + 24.0) / 36.0 * 0.75).min(1.0)
|
|
} else {
|
|
let x = ((-24.0 - db) / 120.0).clamp(0.0, 1.0).powf(1.0 / 1.5);
|
|
((1.0 - x) * 0.25).max(0.0)
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn insert_state_round_trips_through_json() {
|
|
let mut i = Insert::new("vst3:00");
|
|
assert_eq!(i.state_bytes(), None);
|
|
let blob: Vec<u8> = (0..=255u8).cycle().take(1000).collect();
|
|
i.set_state_bytes(&blob);
|
|
let json = serde_json::to_string(&i).unwrap();
|
|
let back: Insert = serde_json::from_str(&json).unwrap();
|
|
assert_eq!(back.state_bytes().unwrap(), blob);
|
|
i.set_state_bytes(&[]);
|
|
assert_eq!(i.state, None);
|
|
// Old sessions have no `state`; stateless inserts don't write one.
|
|
let old: Insert = serde_json::from_str(r#"{"plugin":"eq_7band"}"#).unwrap();
|
|
assert_eq!(old.state, None);
|
|
assert!(!serde_json::to_string(&old).unwrap().contains("state"));
|
|
// A hostile state string decodes to nothing rather than failing the load.
|
|
let bad: Insert = serde_json::from_str(r#"{"plugin":"x","state":"!!!"}"#).unwrap();
|
|
assert_eq!(bad.state_bytes(), None);
|
|
}
|
|
|
|
#[test]
|
|
fn params_saved_as_null_or_out_of_range_are_dropped() {
|
|
let mut i = Insert::new("eq_7band");
|
|
i.params.extend([("gain".to_string(), f32::NAN), ("q".to_string(), 0.7)]);
|
|
let back: Insert = serde_json::from_str(&serde_json::to_string(&i).unwrap()).unwrap();
|
|
assert_eq!(back.params, BTreeMap::from([("q".to_string(), 0.7)]));
|
|
let big: Insert = serde_json::from_str(r#"{"plugin":"x","params":{"gain":1e39,"q":0.5}}"#).unwrap();
|
|
assert_eq!(big.params, BTreeMap::from([("q".to_string(), 0.5)]));
|
|
}
|
|
|
|
#[test]
|
|
fn fader_taper_round_trips() {
|
|
for db in [-100.0f32, -60.0, -24.0, -10.0, 0.0, 6.0, 12.0] {
|
|
let p = fader_db_to_pos(db);
|
|
assert!((fader_pos_to_db(p) - db).abs() < 0.05, "{db} → {p}");
|
|
}
|
|
assert_eq!(fader_pos_to_db(0.0), -144.0);
|
|
assert_eq!(fader_db_to_pos(f32::NAN), 0.0);
|
|
}
|
|
|
|
#[test]
|
|
fn mixer_defaults_and_sanitize() {
|
|
let mut m = Mixer::new(ChannelFormat::Stereo);
|
|
assert_eq!(m.pan, vec![-1.0, 1.0]);
|
|
m.volume_db = f32::INFINITY;
|
|
m.inserts.truncate(2);
|
|
m.sanitize();
|
|
assert_eq!(m.volume_db, 0.0);
|
|
assert_eq!(m.inserts.len(), INSERT_SLOTS);
|
|
}
|
|
|
|
#[test]
|
|
fn speaker_layouts_match_channel_counts_and_masks() {
|
|
for f in ChannelFormat::ALL {
|
|
if !f.is_ambisonic() {
|
|
assert_eq!(f.speakers().len(), f.channels(), "{f:?}");
|
|
// Mask bits are unique and in ascending channel order.
|
|
let bits: Vec<u32> = f.speakers().iter().map(|s| s.wave_bit()).filter(|b| *b != 0).collect();
|
|
assert!(bits.windows(2).all(|w| w[0] < w[1]), "{f:?} not in WAV order");
|
|
}
|
|
}
|
|
assert_eq!(ChannelFormat::Surround51.channel_mask(), 0x3F);
|
|
assert_eq!(ChannelFormat::Surround71.channel_mask(), 0x63F);
|
|
assert_eq!(ChannelFormat::Atmos714.channel_mask(), 0x2_D63F);
|
|
assert_eq!(ChannelFormat::Ambisonics(1).channel_mask(), 0);
|
|
}
|
|
|
|
#[test]
|
|
fn surround_pan_defaults_and_old_sessions() {
|
|
let m = Mixer::new(ChannelFormat::Mono);
|
|
assert_eq!(m.surround, None);
|
|
// A mixer saved before surround existed still loads.
|
|
let mut v = serde_json::to_value(&m).unwrap();
|
|
v.as_object_mut().unwrap().remove("surround");
|
|
let back: Mixer = serde_json::from_value(v).unwrap();
|
|
assert_eq!(back.surround, None);
|
|
let mut sp: SurroundPan = serde_json::from_str(r#"{"x": 5.0}"#).unwrap();
|
|
sp.sanitize();
|
|
assert_eq!(sp.x, 1.0);
|
|
assert_eq!(sp.center, 100.0);
|
|
}
|
|
|
|
#[test]
|
|
fn formats() {
|
|
assert_eq!(ChannelFormat::for_channels(6), ChannelFormat::Surround51);
|
|
assert_eq!(ChannelFormat::from_id("stereo"), Some(ChannelFormat::Stereo));
|
|
}
|
|
}
|