Skip to content
Open
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
41 changes: 41 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ pub enum Action {
TabRight,
SelectTab(usize),
SetAbsoluteVolume(f32),
SetAbsoluteBalance(f32),
SetRelativeBalance(f32),
#[serde(skip_deserializing)]
SelectObject(ObjectId),
#[serde(skip_deserializing)]
Expand Down Expand Up @@ -82,6 +84,8 @@ impl std::fmt::Display for Action {
Action::SetRelativeVolume(vol) => {
Self::format_relative_volume(f, *vol)
}
Action::SetAbsoluteBalance(bal) => Self::format_balance(f, *bal),
Action::SetRelativeBalance(bal) => Self::format_balance(f, *bal),
Action::SetDefault => write!(f, "Set default"),
Action::Help => write!(f, "Show/hide help"),
Action::Exit => write!(f, "Exit wiremix"),
Expand Down Expand Up @@ -110,6 +114,31 @@ impl Action {
}
}
}

fn format_balance(
f: &mut std::fmt::Formatter<'_>,
bal: f32,
) -> std::fmt::Result {
match bal {
0.0 => write!(f, "Center balance"),
0.01 => write!(f, "Shift balance right"),
-0.01 => write!(f, "Shift balance left"),
v if v > 0.0 => {
write!(
f,
"Shift balance right by {}%",
Self::format_percentage(v)
)
}
v => {
write!(
f,
"Shift balance left by {}%",
Self::format_percentage(-v)
)
}
}
}
}

struct Tab {
Expand Down Expand Up @@ -538,6 +567,18 @@ impl Handle for Action {
return Ok(current_list!(app)
.set_relative_volume(&app.view, volume, max));
}
Action::SetAbsoluteBalance(balance) => {
let max = (app.config.enforce_max_volume)
.then_some(app.config.max_volume_percent);
return Ok(current_list!(app)
.set_absolute_balance(&app.view, balance, max));
}
Action::SetRelativeBalance(balance) => {
let max = (app.config.enforce_max_volume)
.then_some(app.config.max_volume_percent);
return Ok(current_list!(app)
.set_relative_balance(&app.view, balance, max));
}
Action::SetDefault => {
current_list!(app).set_default(&app.view);
}
Expand Down
12 changes: 12 additions & 0 deletions src/config/keybinding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ impl Keybinding {
(event(KeyCode::Char('9')), Action::SetAbsoluteVolume(0.90)),
(event(KeyCode::Char('0')), Action::SetAbsoluteVolume(1.00)),
(event(KeyCode::Char('?')), Action::Help),
(
KeyEvent::new(KeyCode::Left, KeyModifiers::SHIFT),
Action::SetRelativeBalance(-0.01),
),
(
KeyEvent::new(KeyCode::Right, KeyModifiers::SHIFT),
Action::SetRelativeBalance(0.01),
),
(
KeyEvent::new(KeyCode::Down, KeyModifiers::SHIFT),
Action::SetAbsoluteBalance(0.0),
),
])
}

Expand Down
38 changes: 38 additions & 0 deletions src/object_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,44 @@ impl ObjectList {
false
}

pub fn set_absolute_balance(
&mut self,
view: &view::View,
balance: f32,
max: Option<f32>,
) -> bool {
if matches!(self.list_kind, ListKind::Device) {
return false;
}
if let Some(node_id) = self.selected {
return view.volume(
node_id,
VolumeAdjustment::AbsoluteBalance(balance),
max,
);
}
false
}

pub fn set_relative_balance(
&mut self,
view: &view::View,
balance: f32,
max: Option<f32>,
) -> bool {
if matches!(self.list_kind, ListKind::Device) {
return false;
}
if let Some(node_id) = self.selected {
return view.volume(
node_id,
VolumeAdjustment::RelativeBalance(balance),
max,
);
}
false
}

pub fn set_default(&mut self, view: &view::View) {
if matches!(self.list_kind, ListKind::Device) {
return;
Expand Down
78 changes: 75 additions & 3 deletions src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ pub struct Device {
pub enum VolumeAdjustment {
Relative(f32),
Absolute(f32),
RelativeBalance(f32),
AbsoluteBalance(f32),
}

#[derive(Default, Debug, Clone, Copy)]
Expand Down Expand Up @@ -687,6 +689,33 @@ impl<'a> View<'a> {
}
}

/// Get current balance (stereo only)
fn balance(&self, volumes: &[f32]) -> Option<f32> {
if volumes.len() == 2 {
if volumes[0] == volumes[1] {
Some(0.0) // handles div by 0 case
} else if volumes[0] > volumes[1] {
Some((volumes[1] / volumes[0]) - 1.0)
} else {
Some(1.0 - volumes[0] / volumes[1])
}
} else {
None
}
}

/// Update channel balance balance (stereo only)
fn rebalance(&self, volumes: &mut [f32], balance: f32) {
if let Some(bal) = self.balance(volumes) {
let bal_new = balance.clamp(-1.0, 1.0);
if bal < 0.0 || (bal == 0.0 && bal_new < 0.0) {
volumes[1] = volumes[0] * (1.0 - bal_new * bal.signum());
} else if bal > 0.0 || (bal == 0.0 && bal_new > 0.0) {
volumes[0] = volumes[1] * (1.0 - bal_new * bal.signum());
}
}
}

/// Changes the volume of the provided node. If max volume is provided,
/// won't change volume if result would be greater than max. Returns true
/// if volume was changed, otherwise false.
Expand All @@ -706,11 +735,54 @@ impl<'a> View<'a> {
}
match adjustment {
VolumeAdjustment::Relative(delta) => {
let avg = volumes.iter().sum::<f32>() / volumes.len() as f32;
volumes.fill((avg.cbrt() + delta).max(0.0).powi(3));
let bal = self.balance(&volumes);
match bal {
// TODO: consolidate first two cases
Some(bal) if bal < 0.0 => {
volumes[0] =
(volumes[0].cbrt() + delta).max(0.0).powi(3);
volumes[1] =
(volumes[0] * (1.0 - bal * bal.signum())).max(0.0);
}
Some(bal) if bal > 0.0 => {
volumes[1] =
(volumes[1].cbrt() + delta).max(0.0).powi(3);
volumes[0] =
(volumes[1] * (1.0 - bal * bal.signum())).max(0.0);
}
_ => {
let avg =
volumes.iter().sum::<f32>() / volumes.len() as f32;
volumes.fill((avg.cbrt() + delta).max(0.0).powi(3));
}
}
}
VolumeAdjustment::Absolute(volume) => {
volumes.fill(volume.max(0.0).powi(3));
let bal = self.balance(&volumes);
match bal {
// TODO: consolidate first two cases
Some(bal) if bal <= 0.0 => {
volumes[0] = volume.max(0.0).powi(3);
volumes[1] =
(volumes[0] * (1.0 - bal * bal.signum())).max(0.0);
}
Some(bal) => {
volumes[1] = volume.max(0.0).powi(3);
volumes[0] =
(volumes[1] * (1.0 - bal * bal.signum())).max(0.0);
}
None => {
volumes.fill(volume.max(0.0).powi(3));
}
}
}
VolumeAdjustment::AbsoluteBalance(balance) => {
self.rebalance(&mut volumes, balance);
}
VolumeAdjustment::RelativeBalance(delta) => {
if let Some(balance) = self.balance(&volumes) {
self.rebalance(&mut volumes, balance + delta);
}
}
}
let volumes = volumes;
Expand Down
Loading