Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 2 additions & 0 deletions src/actuator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
23 changes: 23 additions & 0 deletions src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ pub enum Cmd {
bearer: Option<String>,
},

/// 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<String>),

// ── Experimental LED control ──
/// Replace the full per-LED configuration set.
SetLeds(Box<[Option<crate::model::LedConfig>; crate::model::NUM_LEDS]>),
Expand Down Expand Up @@ -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 {
Expand Down
101 changes: 92 additions & 9 deletions src/glue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Expand All @@ -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(),
};
Expand All @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -604,6 +618,14 @@ pub fn wire(
.or_else(|| name.parse::<u32>().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
Expand All @@ -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,
Expand Down Expand Up @@ -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> = 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 {
Expand All @@ -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; }
}
}
Expand Down Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -1635,15 +1690,15 @@ 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,
}
}

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,
}
}
Expand All @@ -1663,8 +1718,36 @@ fn push_wizard_picker(ui: &AppWindow, shared: Arc<Mutex<Shared>>) {
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<SharedString> = s.preset.mixer.channels.iter().map(|c| c.name.as_str().into()).collect();
let chosen: Vec<String> = ui.get_wizard_property().lines().map(str::to_string).collect();
let rows: Vec<crate::PickerEntry> = 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;
Expand Down
5 changes: 5 additions & 0 deletions src/mixer_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
78 changes: 77 additions & 1 deletion src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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",
}
}
}
Expand All @@ -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<ApiCall>,
/// 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<MixTarget>,
}
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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<bool> {
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<String> {
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<f32> {
match t {
Expand Down
Loading
Loading