diff --git a/Cargo.lock b/Cargo.lock index b4c2816..108a8c7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4908,7 +4908,7 @@ checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" [[package]] name = "slidr" -version = "0.5.0" +version = "0.6.0" dependencies = [ "anyhow", "chrono", diff --git a/Cargo.toml b/Cargo.toml index 2687c26..5b5b1ec 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "slidr" -version = "0.5.0" +version = "0.6.0" edition = "2021" description = "Cross-platform Arduino-based audio/macro pad controller" license = "MIT" diff --git a/src/actuator.rs b/src/actuator.rs index f10db74..d258eed 100644 --- a/src/actuator.rs +++ b/src/actuator.rs @@ -168,6 +168,8 @@ fn apply( crate::mixer::set_level(m, value); } Cmd::ToggleMute(Target::Mixer(m)) => crate::mixer::toggle_mute(m), + // Handled on the UI thread (it edits the profile); never sent here. + Cmd::ToggleMixerFx(_) | Cmd::SwitchMixerPreset(..) => {} Cmd::SetVolume { target, value, unmute } => { if unmute { audio.set_mute(mute_target(&target), false); diff --git a/src/events.rs b/src/events.rs index cc1f310..980898b 100644 --- a/src/events.rs +++ b/src/events.rs @@ -39,6 +39,14 @@ pub enum Cmd { bearer: Option, }, + /// Flip a mixer channel's "Effects & EQ" switch. It is part of the + /// profile, so the UI thread handles it (saves, updates the page) and it + /// never reaches the actuator. + ToggleMixerFx(crate::model::MixTarget), + /// Load the next of these effect presets into a mixer channel (empty = + /// all presets). Handled on the UI thread like `ToggleMixerFx`. + SwitchMixerPreset(crate::model::MixTarget, Vec), + // ── Experimental LED control ── /// Replace the full per-LED configuration set. SetLeds(Box<[Option; crate::model::NUM_LEDS]>), @@ -371,6 +379,21 @@ fn push_button_cmds( } } } + ActionKind::ToggleMixerFx => { + if just_pressed { + if let Some(m) = action.mixer { + out.push(Cmd::ToggleMixerFx(m)); + } + } + } + ActionKind::SwitchMixerPreset => { + if just_pressed { + if let Some(m) = action.mixer { + let names = action.property.as_deref().unwrap_or("").lines().filter(|l| !l.is_empty()); + out.push(Cmd::SwitchMixerPreset(m, names.map(str::to_string).collect())); + } + } + } ActionKind::OpenProcess | ActionKind::OpenWebsite => { if just_pressed { if let Some(t) = &action.property { diff --git a/src/glue.rs b/src/glue.rs index 684c3ac..74600c7 100644 --- a/src/glue.rs +++ b/src/glue.rs @@ -430,6 +430,7 @@ pub fn wire( s.pending_wizard = Some((kind, cat_id as u32)); s.editing_idx = None; } + ui.set_wizard_mixer_channel(0); ui.set_wizard_target_kind(kind); ui.set_wizard_category_id(cat_id); ui.set_wizard_step(0); @@ -451,6 +452,8 @@ pub fn wire( let Some(ui) = weak.upgrade() else { return }; let idx = idx as usize; let cat_id = cat_id as u32; + // "Switch mixer preset": position of its channel in the wizard's list. + let mut channel_idx = 0; // Resolve existing entry → wizard kind/property/display (+ optional API). let (wkind, prop, disp, api) = { let s = shared.lock(); @@ -468,6 +471,10 @@ pub fn wire( let Some(a) = c.actions.get(idx) else { return }; let k = action_kind_index(a.kind); let prop = match a.mixer { + Some(crate::model::MixTarget::Channel(id)) if a.kind == ActionKind::SwitchMixerPreset => { + channel_idx = s.preset.mixer.channels.iter().position(|c| c.id == id).unwrap_or(0) as i32; + a.property.clone().unwrap_or_default() + } Some(m) => m.to_value(), None => a.property.clone().unwrap_or_default(), }; @@ -484,6 +491,7 @@ pub fn wire( } }; shared.lock().editing_idx = Some(idx); + ui.set_wizard_mixer_channel(channel_idx); ui.set_wizard_target_kind(target_kind); ui.set_wizard_category_id(cat_id as i32); ui.set_wizard_kind(wkind); @@ -558,7 +566,13 @@ pub fn wire( let weak = ui.as_weak(); let shared = shared.clone(); ui.on_wizard_finish(move |result| { + let ui_channel = weak.upgrade().map_or(0, |ui| ui.get_wizard_mixer_channel()); let mut s = shared.lock(); + // "Switch mixer preset": the channel picked in the wizard (id, name). + let picked_channel = usize::try_from(ui_channel) + .ok() + .and_then(|i| s.preset.mixer.channels.get(i)) + .map(|c| (c.id, c.name.clone())); let editing = s.editing_idx.take(); let cat_id = result.category_id as u32; if result.target_kind == 0 { @@ -604,6 +618,14 @@ pub fn wire( .or_else(|| name.parse::().ok()) .unwrap_or(0); (Some(vk.to_string()), Some(name)) + } else if kind == ActionKind::SwitchMixerPreset { + // Property: the preset names (empty = all). The label + // names the channel and the presets, shown in the list. + let list = result.property.to_string(); + let names: Vec<&str> = list.lines().filter(|l| !l.is_empty()).collect(); + let channel = picked_channel.as_ref().map(|c| c.1.clone()).unwrap_or_default(); + let presets = if names.is_empty() { "all presets".to_string() } else { names.join(" → ") }; + ((!names.is_empty()).then(|| names.join("\n")), Some(format!("{channel}: {presets}"))) } else if kind == ActionKind::CycleOutput { // Property is the newline-separated device list (empty = all). // The human-readable label is derived at render time @@ -617,10 +639,18 @@ pub fn wire( (prop, disp) }; // A mixer action keeps its strip in `mixer`, not in the string property. - let mixer = (kind == ActionKind::MuteMixer) - .then(|| crate::model::MixTarget::from_value(&result.property)) - .flatten(); - let prop = if mixer.is_some() { None } else { prop }; + let mixer = match kind { + ActionKind::MuteMixer | ActionKind::ToggleMixerFx => { + crate::model::MixTarget::from_value(&result.property) + } + ActionKind::SwitchMixerPreset => { + picked_channel.as_ref().map(|c| crate::model::MixTarget::Channel(c.0)) + } + _ => None, + }; + // Mute / toggle keep their strip in `mixer` only; the preset + // switch also keeps its preset list in `property`. + let prop = if mixer.is_some() && kind != ActionKind::SwitchMixerPreset { None } else { prop }; let action = ButtonAction { kind, property: prop, display: disp, api: None, mixer }; match editing { Some(i) if i < c.actions.len() => c.actions[i] = action, @@ -1192,7 +1222,26 @@ pub fn wire( let preset = s.preset.clone(); let (dirty, cmds) = s.engine.ingest(&frame, &preset); let mut led_toggled = false; + let mut mixer_changed = false; for cmd in cmds { + // The effects switch is part of the profile: flip and + // save it here; the mixer then switches with its sound. + if let crate::events::Cmd::ToggleMixerFx(t) = &cmd { + mixer_changed |= s.preset.mixer.toggle_fx(*t).is_some(); + continue; + } + if let crate::events::Cmd::SwitchMixerPreset(t, names) = &cmd { + let presets: Vec = crate::model::FxPreset::built_in() + .into_iter() + .chain(s.settings.fx_presets.iter().cloned()) + .collect(); + if let Some(name) = s.preset.mixer.switch_preset(*t, names, &presets) { + let label = s.preset.mixer.label(*t); + toast(&ui, &format!("{label}: {name}")); + mixer_changed = true; + } + continue; + } // Persist a manual-LED toggle into the preset so it // survives a restart (the actuator owns the live state). if let crate::events::Cmd::LedManualToggle(led) = &cmd { @@ -1203,9 +1252,15 @@ pub fn wire( } let _ = s.cmd_tx.send(cmd); } - if led_toggled { + if led_toggled || mixer_changed { s.save_preset(); } + if mixer_changed { + s.push_mixer(); + if ui.get_current_page() == 4 { + crate::mixer_ui::refresh_channels(&ui, &s); + } + } if dirty { changed = true; } } } @@ -1483,7 +1538,7 @@ fn action_icon_kind(k: ActionKind) -> i32 { match k { MuteProcess => 0, MuteMic => 1, MuteMainAudio => 5, OpenProcess => 0, OpenWebsite => 3, KeyPress => 4, CycleOutput => 2, ApiCall => 3, - MuteMixer => 5, + MuteMixer => 5, ToggleMixerFx => 5, SwitchMixerPreset => 5, } } @@ -1635,7 +1690,7 @@ fn action_kind_index(k: ActionKind) -> i32 { match k { MuteProcess => 0, MuteMainAudio => 1, MuteMic => 2, OpenProcess => 3, OpenWebsite => 4, KeyPress => 5, CycleOutput => 6, ApiCall => 7, - MuteMixer => 8, + MuteMixer => 8, ToggleMixerFx => 9, SwitchMixerPreset => 10, } } @@ -1643,7 +1698,7 @@ fn button_kind_from_index(i: i32) -> ActionKind { use ActionKind::*; match i { 0 => MuteProcess, 1 => MuteMainAudio, 2 => MuteMic, - 3 => OpenProcess, 4 => OpenWebsite, 6 => CycleOutput, 7 => ApiCall, 8 => MuteMixer, + 3 => OpenProcess, 4 => OpenWebsite, 6 => CycleOutput, 7 => ApiCall, 8 => MuteMixer, 9 => ToggleMixerFx, 10 => SwitchMixerPreset, _ => KeyPress, } } @@ -1663,8 +1718,36 @@ fn push_wizard_picker(ui: &AppWindow, shared: Arc>) { let kind = ui.get_wizard_kind(); let filter = ui.get_wizard_filter().to_string().to_lowercase(); + // Switch mixer preset: the channels to pick one from, and every effect + // preset (built-in, then the user's) as a multi-select list. + if (target_kind, kind) == (1, 10) { + let s = shared.lock(); + let channels: Vec = s.preset.mixer.channels.iter().map(|c| c.name.as_str().into()).collect(); + let chosen: Vec = ui.get_wizard_property().lines().map(str::to_string).collect(); + let rows: Vec = crate::model::FxPreset::built_in() + .into_iter() + .chain(s.settings.fx_presets.iter().cloned()) + .filter(|p| filter.is_empty() || p.name.to_lowercase().contains(filter.trim())) + .map(|p| crate::PickerEntry { + selected: chosen.contains(&p.name), + name: p.name.as_str().into(), + value: p.name.as_str().into(), + icon_kind: 5, + is_group: false, + }) + .collect(); + drop(s); + let n = channels.len() as i32; + ui.set_wizard_mixer_channels(ModelRc::new(VecModel::from(channels))); + if ui.get_wizard_mixer_channel() >= n { + ui.set_wizard_mixer_channel(0); + } + ui.set_wizard_picker_source(ModelRc::new(VecModel::from(rows))); + return; + } + // Mixer strips: fixed list from the profile, labelled by channel name. - if (target_kind, kind) == (0, 4) || (target_kind, kind) == (1, 8) { + if (target_kind, kind) == (0, 4) || (target_kind, kind) == (1, 8) || (target_kind, kind) == (1, 9) { use crate::model::MixTarget; let s = shared.lock(); let m = &s.preset.mixer; diff --git a/src/mixer_ui.rs b/src/mixer_ui.rs index 00fa591..23b7915 100644 --- a/src/mixer_ui.rs +++ b/src/mixer_ui.rs @@ -184,6 +184,11 @@ fn push_channels(ui: &AppWindow, m: &MixerConfig) { ui.set_mixer_channels(ModelRc::new(VecModel::from(rows))); } +/// Redraw the channel cards after the board changed one (effects switch). +pub fn refresh_channels(ui: &AppWindow, s: &Shared) { + push_channels(ui, &s.preset.mixer); +} + /// Let the page's faders follow the board. Rows are updated in place, and /// only where a level actually moved, so nothing on the page is rebuilt. pub fn follow_levels(ui: &AppWindow, s: &Shared) { diff --git a/src/model.rs b/src/model.rs index e6a69c1..a11150d 100644 --- a/src/model.rs +++ b/src/model.rs @@ -144,6 +144,12 @@ pub enum ActionKind { ApiCall, /// Toggle mute of a mixer strip (`ButtonAction::mixer`). MuteMixer, + /// Toggle a mixer channel's "Effects & EQ" switch (`ButtonAction::mixer`). + ToggleMixerFx, + /// Load the next of some effect presets into a mixer channel + /// (`ButtonAction::mixer`; `property` = newline-separated preset names, + /// empty = all of them). + SwitchMixerPreset, } impl ActionKind { @@ -158,6 +164,8 @@ impl ActionKind { Self::CycleOutput => "Cycle output device", Self::ApiCall => "API call", Self::MuteMixer => "Mute mixer channel", + Self::ToggleMixerFx => "Toggle mixer effects", + Self::SwitchMixerPreset => "Switch mixer preset", } } } @@ -180,7 +188,8 @@ pub struct ButtonAction { /// HTTP request config, used when `kind == ApiCall`. #[serde(default, skip_serializing_if = "Option::is_none")] pub api: Option, - /// Mixer strip, used when `kind == MuteMixer`. + /// Mixer strip, used by the mixer kinds (`MuteMixer`, `ToggleMixerFx`, + /// `SwitchMixerPreset`). #[serde(default, skip_serializing_if = "Option::is_none")] pub mixer: Option, } @@ -727,6 +736,38 @@ mod tests { assert_eq!(p.mixer.channels.len(), 2); } + #[test] + fn board_flips_a_channels_effects() { + let mut m = MixerConfig::default(); + m.channels.push(MixChannel { + id: 2, + name: "Music".into(), + programs: vec![], + level: 1.0, + muted: false, + fx: Effects::default(), + reroute: true, + mic_send: None, + collapsed: false, + }); + assert_eq!(m.toggle_fx(MixTarget::Channel(2)), Some(false), "effects start on"); + assert_eq!(m.toggle_fx(MixTarget::Send(2)), Some(true), "a send means its channel"); + assert_eq!(m.toggle_fx(MixTarget::Channel(9)), None, "deleted channel"); + + // Presets: the next one each press, wrapping; effects come on. + m.channels[0].fx.on = false; + let all = FxPreset::built_in(); + let names = vec!["Movies & series".to_string(), "JBL-style".to_string(), "No such preset".to_string()]; + let mut press = || m.switch_preset(MixTarget::Channel(2), &names, &all); + assert_eq!(press().as_deref(), Some("Movies & series"), "unknown chain → first"); + assert_eq!(press().as_deref(), Some("JBL-style")); + assert_eq!(press().as_deref(), Some("Movies & series"), "wraps"); + assert!(m.channels[0].fx.on); + // Empty = all presets: the one after "Movies & series" in the full list. + assert_eq!(m.switch_preset(MixTarget::Channel(2), &[], &all).as_deref(), Some("JBL-style"), "empty = all"); + assert_eq!(m.switch_preset(MixTarget::Channel(2), &["gone".into()], &all), None); + } + #[test] fn preset_files_say_what_they_hold() { let eq = PresetFile::Eq(MixerConfig::default_eq_presets().remove(0)); @@ -863,6 +904,41 @@ impl MixerConfig { } } + /// Flip a strip's "Effects & EQ" switch (a send means its channel). + /// The new state, or `None` for a channel that doesn't exist (anymore). + pub fn toggle_fx(&mut self, t: MixTarget) -> Option { + let fx = self.effects_mut(t)?; + fx.on = !fx.on; + Some(fx.on) + } + + /// Load the next of `names` (from `presets`) into a strip's effects: the + /// one after the preset its chain matches now, else the first. An empty + /// `names` goes through all `presets`. Effects are switched on, so the + /// press is heard. The loaded preset's name, or `None` if there is none. + pub fn switch_preset(&mut self, t: MixTarget, names: &[String], presets: &[FxPreset]) -> Option { + let chosen: Vec<&FxPreset> = if names.is_empty() { + presets.iter().collect() + } else { + names.iter().filter_map(|n| presets.iter().find(|p| &p.name == n)).collect() + }; + let fx = self.effects_mut(t)?; + let now = chosen.iter().position(|p| p.chain == fx.chain); + let next = chosen.get(now.map_or(0, |i| (i + 1) % chosen.len()))?; + fx.chain = next.chain.clone(); + fx.on = true; + Some(next.name.clone()) + } + + fn effects_mut(&mut self, t: MixTarget) -> Option<&mut Effects> { + match t { + MixTarget::Voice => Some(&mut self.voice.fx), + MixTarget::Channel(id) | MixTarget::Send(id) => { + self.channels.iter_mut().find(|c| c.id == id).map(|c| &mut c.fx) + } + } + } + /// A strip's saved fader position. pub fn level(&self, t: MixTarget) -> Option { match t { diff --git a/ui/app.slint b/ui/app.slint index 0616852..fcecbce 100644 --- a/ui/app.slint +++ b/ui/app.slint @@ -1834,6 +1834,9 @@ component WizardPopup inherits Rectangle { // True while `property` holds a program-category reference rather than a // concrete program. Drives the category chip in the "Open application" form. in-out property picked-group: false; + // Mixer channels for "Switch mixer preset", and the chosen one. + in property <[string]> mixer-channels; + in-out property mixer-channel; callback cancel(); callback finish(); @@ -1847,7 +1850,8 @@ component WizardPopup inherits Rectangle { property <[string]> button-kinds: ["Mute process", "Mute main audio", "Mute microphone", "Open application", "Open website", "Simulate key", - "Cycle output device", "API call", "Mute mixer channel"]; + "Cycle output device", "API call", "Mute mixer channel", + "Toggle mixer effects", "Switch mixer preset"]; // Mixer entries go last so the indices above (the API call's included) stay put. property <[string]> slider-kinds: ["Process volume", "Microphone", "Output device", "API call", "Mixer channel"]; @@ -1873,7 +1877,10 @@ component WizardPopup inherits Rectangle { property needs-path-form: root.target-kind == 1 && (root.kind == 3 || root.kind == 4) && !root.is-api; - property multi-select: root.target-kind == 1 && root.kind == 6; + // "Switch mixer preset" (button kind 10): a channel, then the presets to + // switch between (multi-select, like cycle output). + property is-preset-switch: root.target-kind == 1 && root.kind == 10; + property multi-select: root.target-kind == 1 && (root.kind == 6 || root.kind == 10); Rectangle { x: parent.width / 2 - self.width / 2; @@ -1951,8 +1958,17 @@ component WizardPopup inherits Rectangle { font-family: "Inter"; color: Palette.foreground.with-alpha(0.7); } + if root.is-preset-switch: HorizontalLayout { spacing: 10px; + Text { text: "Channel"; font-family: "Inter"; vertical-alignment: center; } + Select { field-width: 240px; model: root.mixer-channels; + current-index <=> root.mixer-channel; + selected => { root.state-changed(); } } + } + if root.multi-select: Text { - text: "Pick the devices to cycle through, in order. Leave empty to cycle all."; + text: root.is-preset-switch + ? "Pick the effect presets to switch between, in order. Each press loads the next one. Leave empty to go through all." + : "Pick the devices to cycle through, in order. Leave empty to cycle all."; font-family: "Inter"; font-size: 12px; color: Palette.foreground.with-alpha(0.7); wrap: word-wrap; } @@ -2142,8 +2158,13 @@ component WizardPopup inherits Rectangle { text: "Auth: Bearer token set"; font-family: "Inter"; } if root.is-api && root.target-kind == 0: Text { text: "Interval: " + root.throttle + " ms"; font-family: "Inter"; } - if !root.is-api && root.needs-property: Text { text: "Target: " + root.property; - font-family: "Inter"; } + if root.is-preset-switch && root.mixer-channels.length > 0: Text { + text: "Channel: " + root.mixer-channels[root.mixer-channel]; font-family: "Inter"; } + if !root.is-api && root.needs-property: Text { + text: root.is-preset-switch + ? "Presets: " + (root.property == "" ? "all" : root.property) + : "Target: " + root.property; + font-family: "Inter"; wrap: word-wrap; } if !root.is-api && root.display != "" && root.display != root.property: Text { text: "Label: " + root.display; font-family: "Inter"; } } } @@ -2168,6 +2189,7 @@ component WizardPopup inherits Rectangle { // need a chosen target. enabled: root.step == 0 || (root.is-api ? root.api-valid + : root.is-preset-switch ? root.mixer-channels.length > 0 : (!root.needs-property || root.multi-select || root.property != "")); clicked => { root.step = root.step + 1; } @@ -3267,6 +3289,8 @@ export component AppWindow inherits Window { in-out property wizard-bearer: ""; in-out property wizard-throttle: 500; in property <[PickerEntry]> wizard-picker-source; + in property <[string]> wizard-mixer-channels; + in-out property wizard-mixer-channel; in-out property toast-text: ""; @@ -3764,6 +3788,8 @@ export component AppWindow inherits Window { filter <=> root.wizard-filter; picked-group <=> root.wizard-picked-group; picker-source: root.wizard-picker-source; + mixer-channels: root.wizard-mixer-channels; + mixer-channel <=> root.wizard-mixer-channel; live-processes: root.live-processes; live-mics: root.live-mics; live-outputs: root.live-outputs;