use crate::juce_var::Value;
use crate::names::DeviceModel;
use crate::valuetree::Node;
pub const MIX_COUNT_PER_SOURCE: u8 = 13;
#[derive(Debug, Clone, PartialEq)]
struct Singleton {
idx: Option<u32>,
}
impl Singleton {
fn path(&self) -> Option<Vec<u32>> {
self.idx.map(|p| vec![p])
}
fn is_path(&self, path: &[u32]) -> bool {
matches!((self.idx, path), (Some(i), [p]) if *p == i)
}
}
#[derive(Debug, Clone, PartialEq)]
struct Indexed {
first: Option<u32>,
count: u8,
}
impl Indexed {
fn path(&self, n: u8) -> Option<Vec<u32>> {
let first = self.first?;
if n >= self.count {
return None;
}
Some(vec![first + n as u32])
}
fn index_from_path(&self, path: &[u32]) -> Option<u8> {
let first = self.first?;
if path.len() != 1 {
return None;
}
let p = path[0];
if p < first {
return None;
}
let n = p - first;
if n >= self.count as u32 {
return None;
}
Some(n as u8)
}
}
#[derive(Debug, Clone, PartialEq)]
struct TwoLevel {
parent: Option<u32>,
first: u32,
count: u8,
}
impl TwoLevel {
fn path(&self, n: u8) -> Option<Vec<u32>> {
let parent = self.parent?;
if n >= self.count {
return None;
}
Some(vec![parent, self.first + n as u32])
}
fn index_from_path(&self, path: &[u32]) -> Option<u8> {
let parent = self.parent?;
if path.len() != 2 || path[0] != parent {
return None;
}
let p = path[1];
if p < self.first {
return None;
}
let n = p - self.first;
if n >= self.count as u32 {
return None;
}
Some(n as u8)
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Layout {
model: DeviceModel,
channel: Indexed,
first_mix: u32,
source_count: u8,
fader: TwoLevel,
input_source: Indexed,
headphone: Indexed,
effects: Indexed,
master_channel: Singleton,
output: Singleton,
ducker: Singleton,
recorder: Singleton,
player: Singleton,
gui: Singleton,
system: Singleton,
pad: TwoLevel,
sip_calling: Singleton,
sip_advanced: Singleton,
sip_call_slots: Indexed,
sip_registration: TwoLevel,
test: Singleton,
pad_recorder: Indexed,
fx_preset: TwoLevel,
network: Singleton,
audio: Singleton,
build: Singleton,
app: Singleton,
theme: Singleton,
current_show: Singleton,
show_control: Singleton,
recordings: Singleton,
radio: Singleton,
show: TwoLevel,
recording: TwoLevel,
storage_volume: Indexed,
radio_tx: Indexed,
radio_rx: Indexed,
wifi_scan_result: Indexed,
streamerx_mix_preset: Indexed,
streamerx_stream_mix: Indexed,
rcsync_mix: Indexed,
mix_minuses: Indexed,
}
macro_rules! impl_singleton {
($field:ident, $path_fn:ident, $is_path_fn:ident) => {
pub fn $path_fn(&self) -> Option<Vec<u32>> {
self.$field.path()
}
pub fn $is_path_fn(&self, path: &[u32]) -> bool {
self.$field.is_path(path)
}
};
}
macro_rules! impl_run {
($field:ident, $path_fn:ident, $index_fn:ident, $count_fn:ident) => {
pub fn $path_fn(&self, n: u8) -> Option<Vec<u32>> {
self.$field.path(n)
}
pub fn $index_fn(&self, path: &[u32]) -> Option<u8> {
self.$field.index_from_path(path)
}
pub fn $count_fn(&self) -> u8 {
self.$field.count
}
};
}
impl Layout {
pub fn from_full_sync(root: &Node) -> Result<Layout, BuildError> {
let model = detect_model(root);
let physical_interface_idx = position_of_named_child(root, "PHYSICALINTERFACE")
.ok_or(BuildError::MissingNode("PHYSICALINTERFACE under root"))?;
let phys = &root.children[physical_interface_idx as usize];
let first_fader_in_phys = position_of_named_child(phys, "FADER")
.ok_or(BuildError::MissingNode("FADER under PHYSICALINTERFACE"))?;
let fader = TwoLevel {
parent: Some(physical_interface_idx),
first: first_fader_in_phys,
count: count_consecutive_named(phys, first_fader_in_phys, "FADER"),
};
let first_channel = position_of_named_child(root, "CHANNEL")
.ok_or(BuildError::MissingNode("CHANNEL under root"))?;
let channel = Indexed {
first: Some(first_channel),
count: count_consecutive_named(root, first_channel, "CHANNEL"),
};
let first_mix = position_of_named_child(root, "MIX")
.ok_or(BuildError::MissingNode("MIX under root"))?;
let mix_total = count_consecutive_named_u32(root, first_mix, "MIX");
if mix_total == 0 || !mix_total.is_multiple_of(MIX_COUNT_PER_SOURCE as u32) {
return Err(BuildError::ShapeMismatch {
what: "MIX",
detail: format!(
"expected mix_total divisible by MIX_COUNT_PER_SOURCE={}, got {}",
MIX_COUNT_PER_SOURCE, mix_total
),
});
}
let source_count = u8::try_from(mix_total / MIX_COUNT_PER_SOURCE as u32).map_err(|_| {
BuildError::ShapeMismatch {
what: "MIX",
detail: format!("source_count overflows u8 (mix_total={mix_total})"),
}
})?;
let input_source = discover_run(root, "INPUTSOURCE");
let headphone = discover_run(root, "HEADPHONE");
let effects = discover_run(root, "EFFECTS_PARAMETERS");
let master_channel = Singleton {
idx: position_of_named_child(root, "MASTERCHANNEL"),
};
let output = Singleton {
idx: position_of_named_child(root, "OUTPUT"),
};
let ducker = Singleton {
idx: position_of_named_child(root, "DUCKER"),
};
let recorder = Singleton {
idx: position_of_named_child(root, "RECORDER"),
};
let player = Singleton {
idx: position_of_named_child(root, "PLAYER"),
};
let gui = Singleton {
idx: position_of_named_child(root, "GUI"),
};
let system = Singleton {
idx: position_of_named_child(root, "SYSTEM"),
};
let test = Singleton {
idx: position_of_named_child(root, "TEST"),
};
let pad_recorder = discover_run(root, "PADRECORDER");
let fx_preset = discover_two_level(root, "FXPRESETS", "FXPRESET");
let sip_calling = Singleton {
idx: position_of_named_child(root, "SIPCALLING"),
};
let sip_advanced = Singleton {
idx: position_of_named_child(root, "SIPADVANCED"),
};
let sip_call_slots = discover_run(root, "SIPCALLSLOTS");
let sip_registration = discover_two_level(root, "SIPCALLING", "SIPREGISTRATION");
let pad = discover_two_level(root, "SOUNDPADS", "PAD");
let network = Singleton {
idx: position_of_named_child(root, "NETWORK"),
};
let audio = Singleton {
idx: position_of_named_child(root, "AUDIO"),
};
let build = Singleton {
idx: position_of_named_child(root, "BUILD"),
};
let app = Singleton {
idx: position_of_named_child(root, "APP"),
};
let theme = Singleton {
idx: position_of_named_child(root, "THEME"),
};
let current_show = Singleton {
idx: position_of_named_child(root, "CURRENTSHOW"),
};
let show_control = Singleton {
idx: position_of_named_child(root, "SHOWCONTROL"),
};
let recordings = Singleton {
idx: position_of_named_child(root, "RECORDINGS"),
};
let radio = Singleton {
idx: position_of_named_child(root, "RADIO"),
};
let show = discover_two_level(root, "SHOWS", "SHOW");
let recording = discover_two_level(root, "RECORDINGS", "RECORDING");
let storage_volume = discover_run(root, "STORAGEVOLUME");
let radio_tx = discover_run(root, "RADIOTX");
let radio_rx = discover_run(root, "RADIORX");
let wifi_scan_result = discover_run(root, "WIFISCANRESULT");
let streamerx_mix_preset = discover_run(root, "STREAMERXMIXPRESET");
let streamerx_stream_mix = discover_run(root, "STREAMERXSTREAMMIX");
let rcsync_mix = discover_run(root, "RCSYNCMIX");
let mix_minuses = discover_run(root, "MIXMINUSES");
Ok(Layout {
model,
channel,
first_mix,
source_count,
fader,
input_source,
headphone,
effects,
master_channel,
output,
ducker,
recorder,
player,
gui,
system,
pad,
sip_calling,
sip_advanced,
sip_call_slots,
sip_registration,
test,
pad_recorder,
fx_preset,
network,
audio,
build,
app,
theme,
current_show,
show_control,
recordings,
radio,
show,
recording,
storage_volume,
radio_tx,
radio_rx,
wifi_scan_result,
streamerx_mix_preset,
streamerx_stream_mix,
rcsync_mix,
mix_minuses,
})
}
pub fn model(&self) -> DeviceModel {
self.model
}
pub fn physical_interface_idx(&self) -> u32 {
self.fader
.parent
.expect("PHYSICALINTERFACE present in every built Layout")
}
pub fn first_fader_in_phys(&self) -> u32 {
self.fader.first
}
pub fn fader_count(&self) -> u8 {
self.fader.count
}
pub fn first_channel(&self) -> u32 {
self.channel
.first
.expect("CHANNEL present in every built Layout")
}
pub fn channel_count(&self) -> u8 {
self.channel.count
}
pub fn first_mix(&self) -> u32 {
self.first_mix
}
pub fn source_count(&self) -> u8 {
self.source_count
}
pub fn mix_count_per_source(&self) -> u8 {
MIX_COUNT_PER_SOURCE
}
pub fn first_input_source(&self) -> Option<u32> {
self.input_source.first
}
pub fn input_source_count(&self) -> u8 {
self.input_source.count
}
pub fn master_channel(&self) -> Option<u32> {
self.master_channel.idx
}
pub fn output(&self) -> Option<u32> {
self.output.idx
}
pub fn ducker(&self) -> Option<u32> {
self.ducker.idx
}
pub fn recorder(&self) -> Option<u32> {
self.recorder.idx
}
pub fn player(&self) -> Option<u32> {
self.player.idx
}
pub fn first_headphone(&self) -> Option<u32> {
self.headphone.first
}
pub fn headphone_count(&self) -> u8 {
self.headphone.count
}
pub fn first_effects(&self) -> Option<u32> {
self.effects.first
}
pub fn effects_count(&self) -> u8 {
self.effects.count
}
pub fn gui(&self) -> Option<u32> {
self.gui.idx
}
pub fn system(&self) -> Option<u32> {
self.system.idx
}
pub fn soundpads(&self) -> Option<u32> {
self.pad.parent
}
pub fn first_pad(&self) -> u32 {
self.pad.first
}
pub fn pad_count(&self) -> u8 {
self.pad.count
}
pub fn channel_path(&self, n: u8) -> Option<Vec<u32>> {
self.channel.path(n)
}
pub fn channel_index_from_path(&self, path: &[u32]) -> Option<u8> {
self.channel.index_from_path(path)
}
pub fn fader_path(&self, n: u8) -> Option<Vec<u32>> {
self.fader.path(n)
}
pub fn fader_index_from_path(&self, path: &[u32]) -> Option<u8> {
self.fader.index_from_path(path)
}
pub fn pad_path(&self, n: u8) -> Option<Vec<u32>> {
self.pad.path(n)
}
pub fn pad_index_from_path(&self, path: &[u32]) -> Option<u8> {
self.pad.index_from_path(path)
}
pub fn input_source_path(&self, n: u8) -> Option<Vec<u32>> {
self.input_source.path(n)
}
pub fn input_source_index_from_path(&self, path: &[u32]) -> Option<u8> {
self.input_source.index_from_path(path)
}
pub fn headphone_path(&self, n: u8) -> Option<Vec<u32>> {
self.headphone.path(n)
}
pub fn headphone_index_from_path(&self, path: &[u32]) -> Option<u8> {
self.headphone.index_from_path(path)
}
pub fn effects_path(&self, n: u8) -> Option<Vec<u32>> {
self.effects.path(n)
}
pub fn effects_index_from_path(&self, path: &[u32]) -> Option<u8> {
self.effects.index_from_path(path)
}
pub fn mix_cell_path(&self, source: u8, mix: u8) -> Option<Vec<u32>> {
if source >= self.source_count || mix >= MIX_COUNT_PER_SOURCE {
return None;
}
Some(vec![
self.first_mix + source as u32 * MIX_COUNT_PER_SOURCE as u32 + mix as u32,
])
}
pub fn mix_cell_from_path(&self, path: &[u32]) -> Option<(u8, u8)> {
if path.len() != 1 {
return None;
}
let p = path[0];
if p < self.first_mix {
return None;
}
let offset = p - self.first_mix;
let span = self.source_count as u32 * MIX_COUNT_PER_SOURCE as u32;
if offset >= span {
return None;
}
let source = (offset / MIX_COUNT_PER_SOURCE as u32) as u8;
let mix = (offset % MIX_COUNT_PER_SOURCE as u32) as u8;
Some((source, mix))
}
impl_singleton!(master_channel, master_channel_path, is_master_channel_path);
impl_singleton!(output, output_path, is_output_path);
impl_singleton!(ducker, ducker_path, is_ducker_path);
impl_singleton!(recorder, recorder_path, is_recorder_path);
impl_singleton!(player, player_path, is_player_path);
impl_singleton!(gui, gui_path, is_gui_path);
impl_singleton!(system, system_path, is_system_path);
impl_singleton!(sip_calling, sip_calling_path, is_sip_calling_path);
impl_singleton!(sip_advanced, sip_advanced_path, is_sip_advanced_path);
impl_singleton!(test, test_path, is_test_path);
impl_singleton!(network, network_path, is_network_path);
impl_singleton!(audio, audio_path, is_audio_path);
impl_singleton!(build, build_path, is_build_path);
impl_singleton!(app, app_path, is_app_path);
impl_singleton!(theme, theme_path, is_theme_path);
impl_singleton!(current_show, current_show_path, is_current_show_path);
impl_singleton!(show_control, show_control_path, is_show_control_path);
impl_singleton!(recordings, recordings_path, is_recordings_path);
impl_singleton!(radio, radio_path, is_radio_path);
impl_run!(
sip_call_slots,
sip_call_slots_path,
sip_call_slots_index_from_path,
sip_call_slots_count
);
impl_run!(
sip_registration,
sip_registration_path,
sip_registration_index_from_path,
sip_registration_count
);
impl_run!(
pad_recorder,
pad_recorder_path,
pad_recorder_index_from_path,
pad_recorder_count
);
impl_run!(
fx_preset,
fx_preset_path,
fx_preset_index_from_path,
fx_preset_count
);
impl_run!(show, show_path, show_index_from_path, show_count);
impl_run!(
recording,
recording_path,
recording_index_from_path,
recording_count
);
impl_run!(
storage_volume,
storage_volume_path,
storage_volume_index_from_path,
storage_volume_count
);
impl_run!(
radio_tx,
radio_tx_path,
radio_tx_index_from_path,
radio_tx_count
);
impl_run!(
radio_rx,
radio_rx_path,
radio_rx_index_from_path,
radio_rx_count
);
impl_run!(
wifi_scan_result,
wifi_scan_result_path,
wifi_scan_result_index_from_path,
wifi_scan_result_count
);
impl_run!(
streamerx_mix_preset,
streamerx_mix_preset_path,
streamerx_mix_preset_index_from_path,
streamerx_mix_preset_count
);
impl_run!(
streamerx_stream_mix,
streamerx_stream_mix_path,
streamerx_stream_mix_index_from_path,
streamerx_stream_mix_count
);
impl_run!(
rcsync_mix,
rcsync_mix_path,
rcsync_mix_index_from_path,
rcsync_mix_count
);
impl_run!(
mix_minuses,
mix_minuses_path,
mix_minuses_index_from_path,
mix_minuses_count
);
}
fn detect_model(root: &Node) -> DeviceModel {
let sys = root.children.iter().find(|c| c.name == "SYSTEM");
let board_type = sys.and_then(|s| {
s.properties
.iter()
.find(|p| p.name == "boardType")
.and_then(|p| p.value.as_int())
});
let system_name = sys.and_then(|s| {
s.properties
.iter()
.find(|p| p.name == "systemName")
.and_then(|p| match &p.value {
Value::String(name) => Some(name.as_str()),
_ => None,
})
});
DeviceModel::detect(board_type, system_name)
}
fn discover_run(parent: &Node, name: &str) -> Indexed {
match position_of_named_child(parent, name) {
Some(first) => Indexed {
first: Some(first),
count: count_consecutive_named(parent, first, name),
},
None => Indexed {
first: None,
count: 0,
},
}
}
fn discover_two_level(root: &Node, container_name: &str, child_name: &str) -> TwoLevel {
match position_of_named_child(root, container_name) {
Some(parent) => {
let container = &root.children[parent as usize];
match position_of_named_child(container, child_name) {
Some(first) => TwoLevel {
parent: Some(parent),
first,
count: count_consecutive_named(container, first, child_name),
},
None => TwoLevel {
parent: Some(parent),
first: 0,
count: 0,
},
}
}
None => TwoLevel {
parent: None,
first: 0,
count: 0,
},
}
}
fn position_of_named_child(parent: &Node, name: &str) -> Option<u32> {
parent
.children
.iter()
.position(|c| c.name == name)
.map(|i| i as u32)
}
fn count_consecutive_named(parent: &Node, start: u32, name: &str) -> u8 {
let mut n: u8 = 0;
for child in parent.children.iter().skip(start as usize) {
if child.name != name {
break;
}
n = n.saturating_add(1);
}
n
}
fn count_consecutive_named_u32(parent: &Node, start: u32, name: &str) -> u32 {
let mut n: u32 = 0;
for child in parent.children.iter().skip(start as usize) {
if child.name != name {
break;
}
n = n.saturating_add(1);
}
n
}
#[derive(Debug, Clone, PartialEq)]
pub enum BuildError {
MissingNode(&'static str),
ShapeMismatch { what: &'static str, detail: String },
}
impl std::fmt::Display for BuildError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
BuildError::MissingNode(what) => {
write!(f, "fullSync missing required node: {what}")
}
BuildError::ShapeMismatch { what, detail } => {
write!(f, "fullSync shape mismatch for {what}: {detail}")
}
}
}
}
impl std::error::Error for BuildError {}
#[cfg(test)]
#[path = "layout_tests.rs"]
mod tests;