243 lines
6.5 KiB
Rust
243 lines
6.5 KiB
Rust
use fader_core::{
|
|
Binding, BindingAction, BindingKey, Bindings, FaderId, Gain, MessageKind, MidiChannel,
|
|
MidiData, SurfaceId,
|
|
};
|
|
use midi::{ButtonEdge, DecodeError, MidiIntent, MidiMessage, MidiRouter, decode_message};
|
|
|
|
fn channel(value: u16) -> MidiChannel {
|
|
MidiChannel::new(value).unwrap()
|
|
}
|
|
|
|
fn data(value: u16) -> MidiData {
|
|
MidiData::new(value).unwrap()
|
|
}
|
|
|
|
fn key(surface: &str, kind: MessageKind, number: u16) -> BindingKey {
|
|
BindingKey {
|
|
surface: SurfaceId::new(surface).unwrap(),
|
|
channel: channel(1),
|
|
kind,
|
|
number: data(number),
|
|
}
|
|
}
|
|
|
|
fn binding(fader: &str, action: BindingAction) -> Binding {
|
|
Binding {
|
|
fader: FaderId::new(fader).unwrap(),
|
|
action,
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn decodes_absolute_cc_across_all_channels_and_endpoints() {
|
|
for wire_channel in 0_u8..16 {
|
|
let message = decode_message(&[0xb0 | wire_channel, 7, 127]).unwrap();
|
|
assert_eq!(
|
|
message,
|
|
Some(MidiMessage::ControlChange {
|
|
channel: channel(u16::from(wire_channel) + 1),
|
|
number: data(7),
|
|
value: data(127),
|
|
})
|
|
);
|
|
}
|
|
assert!(matches!(
|
|
decode_message(&[0xb0, 0, 0]).unwrap(),
|
|
Some(MidiMessage::ControlChange { number, value, .. })
|
|
if number == data(0) && value == data(0)
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn note_on_zero_and_note_off_are_release_edges() {
|
|
assert!(matches!(
|
|
decode_message(&[0x90, 32, 100]).unwrap(),
|
|
Some(MidiMessage::Note {
|
|
edge: ButtonEdge::Press,
|
|
..
|
|
})
|
|
));
|
|
for bytes in [[0x90, 32, 0], [0x80, 32, 64]] {
|
|
assert!(matches!(
|
|
decode_message(&bytes).unwrap(),
|
|
Some(MidiMessage::Note {
|
|
edge: ButtonEdge::Release,
|
|
..
|
|
})
|
|
));
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn malformed_supported_messages_are_rejected_precisely() {
|
|
assert_eq!(decode_message(&[]), Err(DecodeError::Empty));
|
|
assert_eq!(
|
|
decode_message(&[7, 1, 2]),
|
|
Err(DecodeError::MissingStatus(7))
|
|
);
|
|
assert_eq!(
|
|
decode_message(&[0xb0, 7]),
|
|
Err(DecodeError::Length {
|
|
status: 0xb0,
|
|
expected: 3,
|
|
actual: 2,
|
|
})
|
|
);
|
|
assert_eq!(
|
|
decode_message(&[0x90, 0x80, 1]),
|
|
Err(DecodeError::InvalidData {
|
|
position: 1,
|
|
value: 0x80,
|
|
})
|
|
);
|
|
assert_eq!(
|
|
decode_message(&[0xb0, 1, 0xff]),
|
|
Err(DecodeError::InvalidData {
|
|
position: 2,
|
|
value: 0xff,
|
|
})
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn unrelated_and_clock_traffic_is_ignored() {
|
|
for bytes in [
|
|
&[0xf8][..],
|
|
&[0xfe][..],
|
|
&[0xf0, 1, 2, 0xf7][..],
|
|
&[0xc0, 3][..],
|
|
&[0xd0, 4][..],
|
|
&[0xe0, 0, 64][..],
|
|
] {
|
|
assert_eq!(decode_message(bytes).unwrap(), None);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn volume_routes_to_the_bound_surface_and_normalizes_seven_bits() {
|
|
let bindings = Bindings::new([
|
|
(
|
|
key("left", MessageKind::ControlChange, 7),
|
|
binding("left-volume", BindingAction::Volume),
|
|
),
|
|
(
|
|
key("right", MessageKind::ControlChange, 7),
|
|
binding("right-volume", BindingAction::Volume),
|
|
),
|
|
])
|
|
.unwrap();
|
|
let mut router = MidiRouter::new(bindings);
|
|
let message = MidiMessage::ControlChange {
|
|
channel: channel(1),
|
|
number: data(7),
|
|
value: data(127),
|
|
};
|
|
|
|
assert_eq!(
|
|
router.route(&SurfaceId::new("left").unwrap(), message),
|
|
Some(MidiIntent::AbsoluteVolume {
|
|
surface: SurfaceId::new("left").unwrap(),
|
|
fader: FaderId::new("left-volume").unwrap(),
|
|
gain: Gain::UNITY,
|
|
})
|
|
);
|
|
assert_eq!(
|
|
router.route(
|
|
&SurfaceId::new("right").unwrap(),
|
|
MidiMessage::ControlChange {
|
|
channel: channel(1),
|
|
number: data(7),
|
|
value: data(0),
|
|
}
|
|
),
|
|
Some(MidiIntent::AbsoluteVolume {
|
|
surface: SurfaceId::new("right").unwrap(),
|
|
fader: FaderId::new("right-volume").unwrap(),
|
|
gain: Gain::SILENCE,
|
|
})
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn note_and_cc_buttons_emit_once_per_press_until_release() {
|
|
let bindings = Bindings::new([
|
|
(
|
|
key("desk", MessageKind::Note, 32),
|
|
binding("note-mute", BindingAction::ToggleMute),
|
|
),
|
|
(
|
|
key("desk", MessageKind::ControlChange, 33),
|
|
binding("cc-mute", BindingAction::ToggleMute),
|
|
),
|
|
])
|
|
.unwrap();
|
|
let mut router = MidiRouter::new(bindings);
|
|
let surface = SurfaceId::new("desk").unwrap();
|
|
let note = |edge| MidiMessage::Note {
|
|
channel: channel(1),
|
|
number: data(32),
|
|
velocity: data(if edge == ButtonEdge::Press { 127 } else { 0 }),
|
|
edge,
|
|
};
|
|
assert!(matches!(
|
|
router.route(&surface, note(ButtonEdge::Press)),
|
|
Some(MidiIntent::ToggleMute { .. })
|
|
));
|
|
assert_eq!(router.route(&surface, note(ButtonEdge::Press)), None);
|
|
assert_eq!(router.route(&surface, note(ButtonEdge::Release)), None);
|
|
assert!(matches!(
|
|
router.route(&surface, note(ButtonEdge::Press)),
|
|
Some(MidiIntent::ToggleMute { .. })
|
|
));
|
|
|
|
let cc = |value| MidiMessage::ControlChange {
|
|
channel: channel(1),
|
|
number: data(33),
|
|
value: data(value),
|
|
};
|
|
assert!(matches!(
|
|
router.route(&surface, cc(127)),
|
|
Some(MidiIntent::ToggleMute { .. })
|
|
));
|
|
assert_eq!(router.route(&surface, cc(64)), None);
|
|
assert_eq!(router.route(&surface, cc(0)), None);
|
|
assert!(matches!(
|
|
router.route(&surface, cc(1)),
|
|
Some(MidiIntent::ToggleMute { .. })
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn unknown_controls_and_incompatible_bindings_emit_nothing() {
|
|
let bindings = Bindings::new([(
|
|
key("desk", MessageKind::Note, 1),
|
|
binding("invalid-note-volume", BindingAction::Volume),
|
|
)])
|
|
.unwrap();
|
|
let mut router = MidiRouter::new(bindings);
|
|
let surface = SurfaceId::new("desk").unwrap();
|
|
assert_eq!(
|
|
router.route(
|
|
&surface,
|
|
MidiMessage::ControlChange {
|
|
channel: channel(1),
|
|
number: data(99),
|
|
value: data(100),
|
|
}
|
|
),
|
|
None
|
|
);
|
|
assert_eq!(
|
|
router.route(
|
|
&surface,
|
|
MidiMessage::Note {
|
|
channel: channel(1),
|
|
number: data(1),
|
|
velocity: data(100),
|
|
edge: ButtonEdge::Press,
|
|
}
|
|
),
|
|
None
|
|
);
|
|
}
|