113 lines
4.0 KiB
Rust
113 lines
4.0 KiB
Rust
use fader_core::{
|
|
BackendEpoch, ChannelGains, Gain, RelativeGroupState, StreamHandle, plan_volume, volume_view,
|
|
};
|
|
use std::collections::BTreeMap;
|
|
|
|
fn gain(value: f64) -> Gain {
|
|
Gain::new(value).unwrap()
|
|
}
|
|
fn channels(values: &[f64]) -> ChannelGains {
|
|
ChannelGains::new(values.iter().copied().map(gain).collect()).unwrap()
|
|
}
|
|
fn handle(id: u64) -> StreamHandle {
|
|
StreamHandle::new(BackendEpoch::new(1), id, 1)
|
|
}
|
|
|
|
#[test]
|
|
fn absolute_plan_preserves_channel_balance_without_mutation() {
|
|
let live = BTreeMap::from([
|
|
(handle(1), channels(&[0.8, 0.4])),
|
|
(handle(2), channels(&[0.2])),
|
|
]);
|
|
let before = live.clone();
|
|
let absolute = plan_volume(&live, gain(0.4), Gain::UNITY).unwrap();
|
|
assert_eq!(absolute[&handle(1)], channels(&[0.4, 0.2]));
|
|
assert_eq!(absolute[&handle(2)], channels(&[0.4]));
|
|
let clipped = plan_volume(&live, gain(2.0), Gain::UNITY).unwrap();
|
|
assert_eq!(clipped[&handle(1)], channels(&[1.0, 0.5]));
|
|
assert_eq!(clipped[&handle(2)], channels(&[1.0]));
|
|
assert_eq!(live, before);
|
|
assert!(volume_view(&live).unwrap().mixed);
|
|
assert_eq!(volume_view(&live).unwrap().maximum, gain(0.8));
|
|
}
|
|
|
|
#[test]
|
|
fn empty_absolute_groups_have_explicit_behavior() {
|
|
let empty = BTreeMap::new();
|
|
assert_eq!(volume_view(&empty), None);
|
|
assert!(
|
|
plan_volume(&empty, Gain::UNITY, Gain::UNITY)
|
|
.unwrap()
|
|
.is_empty()
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn relative_group_scales_zero_to_each_members_remembered_maximum() {
|
|
let mut group = RelativeGroupState::new();
|
|
group.observe_independent(handle(1), channels(&[0.8, 0.4]));
|
|
group.observe_independent(handle(2), channels(&[0.2]));
|
|
|
|
let half = group.plan(gain(0.5), Gain::UNITY).unwrap();
|
|
assert_eq!(half[&handle(1)], channels(&[0.4, 0.2]));
|
|
assert_eq!(half[&handle(2)], channels(&[0.1]));
|
|
|
|
let silent = group.plan(Gain::SILENCE, Gain::UNITY).unwrap();
|
|
assert_eq!(silent[&handle(1)], channels(&[0.0, 0.0]));
|
|
assert_eq!(silent[&handle(2)], channels(&[0.0]));
|
|
|
|
let restored = group.plan(Gain::UNITY, Gain::UNITY).unwrap();
|
|
assert_eq!(restored[&handle(1)], channels(&[0.8, 0.4]));
|
|
assert_eq!(restored[&handle(2)], channels(&[0.2]));
|
|
}
|
|
|
|
#[test]
|
|
fn relative_group_position_is_normalized() {
|
|
let group = RelativeGroupState::new();
|
|
let error = group.plan(gain(1.01), Gain::UNITY).unwrap_err();
|
|
assert!(matches!(
|
|
error,
|
|
fader_core::ChannelGainError::AboveWriteCeiling {
|
|
target: 1.01,
|
|
ceiling: 1.0
|
|
}
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn independent_or_external_changes_rebase_only_that_relative_member() {
|
|
let mut group = RelativeGroupState::new();
|
|
group.observe_independent(handle(1), channels(&[0.8]));
|
|
group.observe_independent(handle(2), channels(&[0.2]));
|
|
let _ = group.plan(gain(0.5), Gain::UNITY).unwrap();
|
|
|
|
group.observe_independent(handle(2), channels(&[0.6]));
|
|
let next = group.plan(gain(0.5), Gain::UNITY).unwrap();
|
|
assert_eq!(next[&handle(1)], channels(&[0.4]));
|
|
assert_eq!(next[&handle(2)], channels(&[0.3]));
|
|
|
|
group.remove(handle(1));
|
|
assert!(
|
|
!group
|
|
.plan(Gain::UNITY, Gain::UNITY)
|
|
.unwrap()
|
|
.contains_key(&handle(1))
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn partial_confirmation_and_new_members_do_not_change_absolute_semantics() {
|
|
let mut live = BTreeMap::from([(handle(1), channels(&[0.8])), (handle(2), channels(&[0.2]))]);
|
|
let plan = plan_volume(&live, gain(0.4), Gain::UNITY).unwrap();
|
|
// Only the first stream confirms; the failed second stream stays truthful.
|
|
live.insert(handle(1), plan[&handle(1)].clone());
|
|
assert!(volume_view(&live).unwrap().mixed);
|
|
assert_eq!(live[&handle(2)], channels(&[0.2]));
|
|
// New streams are adopted without replaying the previous absolute intent.
|
|
live.insert(handle(3), channels(&[1.0]));
|
|
let next = plan_volume(&live, gain(0.5), Gain::UNITY).unwrap();
|
|
assert_eq!(next[&handle(1)], channels(&[0.5]));
|
|
assert_eq!(next[&handle(2)], channels(&[0.5]));
|
|
assert_eq!(next[&handle(3)], channels(&[0.5]));
|
|
}
|