Remade the NodeInfo type

This commit is contained in:
Weird Constructor 2021-10-03 21:27:53 +02:00
parent 4128fc69a0
commit f07bb6b87b
2 changed files with 107 additions and 157 deletions

View file

@ -1008,6 +1008,36 @@ pub fn get_rand_node_id(count: usize, sel: RandNodeSelector) -> Vec<NodeId> {
out out
} }
/// Holds information about the node type that was allocated.
/// It stores the names of inputs, output and atoms for uniform
/// access.
///
/// The [crate::NodeConfigurator] allocates and holds instances
/// of this type for access by [NodeId].
/// See also [crate::NodeConfigurator::node_by_id] and
/// [crate::Matrix::info_for].
#[derive(Clone)]
pub struct NodeInfo {
node_id: NodeId,
inputs: Vec<&'static str>,
atoms: Vec<&'static str>,
outputs: Vec<&'static str>,
input_help: Vec<&'static str>,
output_help: Vec<&'static str>,
node_help: &'static str,
node_desc: &'static str,
node_name: &'static str,
norm_v: std::rc::Rc<dyn Fn(usize, f32) -> f32>,
denorm_v: std::rc::Rc<dyn Fn(usize, f32) -> f32>,
}
impl std::fmt::Debug for NodeInfo {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
f.debug_struct("NodeInfo")
.field("node_id", &self.node_id)
.finish()
}
}
macro_rules! make_node_info_enum { macro_rules! make_node_info_enum {
($s1: ident => $v1: ident, ($s1: ident => $v1: ident,
@ -1023,29 +1053,28 @@ macro_rules! make_node_info_enum {
$([$out_idx: literal $out: ident])* $([$out_idx: literal $out: ident])*
,)+ ,)+
) => { ) => {
/// Holds information about the node type that was allocated.
/// It stores the names of inputs, output and atoms for uniform
/// access.
///
/// The [crate::NodeConfigurator] allocates and holds instances
/// of this type for access by [NodeId].
/// See also [crate::NodeConfigurator::node_by_id] and
/// [crate::Matrix::info_for].
#[derive(Debug, Clone)]
pub enum NodeInfo {
$v1,
$($variant((NodeId, crate::dsp::ni::$variant))),+
}
impl NodeInfo { impl NodeInfo {
/// Allocates a new [NodeInfo] from a [NodeId]. /// Allocates a new [NodeInfo] from a [NodeId].
/// Usually you access [NodeInfo] in the UI thread via /// Usually you access [NodeInfo] in the UI thread via
/// [crate::NodeConfigurator::node_by_id] /// [crate::NodeConfigurator::node_by_id]
/// or [crate::Matrix::info_for]. /// or [crate::Matrix::info_for].
pub fn from_node_id(nid: NodeId) -> NodeInfo { pub fn from_node_id(nid: NodeId) -> Self {
match nid { match nid {
NodeId::$v1 => NodeInfo::$v1, NodeId::$v1 => NodeInfo {
$(NodeId::$variant(_) => NodeInfo::$variant((nid, crate::dsp::ni::$variant::new()))),+ node_id: crate::dsp::NodeId::Nop,
inputs: vec![],
atoms: vec![],
outputs: vec![],
input_help: vec![],
output_help: vec![],
node_help: "Nop Help",
node_desc: "Nop Desc",
node_name: "Nop",
norm_v: std::rc::Rc::new(|i, x| x),
denorm_v: std::rc::Rc::new(|i, x| x),
},
$(NodeId::$variant(_) => crate::dsp::ni::$variant(nid)),+
} }
} }
} }
@ -1277,12 +1306,7 @@ macro_rules! make_node_info_enum {
} }
} }
pub fn from_node_info(ni: &NodeInfo) -> NodeId { pub fn from_node_info(ni: &NodeInfo) -> NodeId { ni.to_id() }
match ni {
NodeInfo::$v1 => NodeId::$v1,
$(NodeInfo::$variant(_) => NodeId::$variant(0)),+
}
}
pub fn label(&self) -> &'static str { pub fn label(&self) -> &'static str {
match self { match self {
@ -1603,144 +1627,91 @@ macro_rules! make_node_info_enum {
mod ni { mod ni {
$( $(
#[derive(Debug, Clone)] pub fn $variant(node_id: crate::dsp::NodeId) -> crate::dsp::NodeInfo {
pub struct $variant { let mut input_help = vec![$(crate::dsp::$variant::$para,)*];
inputs: Vec<&'static str>, $(input_help.push(crate::dsp::$variant::$atom);)*
atoms: Vec<&'static str>,
outputs: Vec<&'static str>,
input_help: Vec<&'static str>,
output_help: Vec<&'static str>,
node_help: &'static str,
node_desc: &'static str,
}
impl $variant { crate::dsp::NodeInfo {
#[allow(unused_mut)] node_id,
pub fn new() -> Self { inputs: vec![$(stringify!($para),)*],
let mut input_help = vec![$(crate::dsp::$variant::$para,)*]; atoms: vec![$(stringify!($atom),)*],
$(input_help.push(crate::dsp::$variant::$atom);)* outputs: vec![$(stringify!($out),)*],
Self { input_help,
inputs: vec![$(stringify!($para),)*], output_help: vec![$(crate::dsp::$variant::$out,)*],
atoms: vec![$(stringify!($atom),)*], node_help: crate::dsp::$variant::HELP,
outputs: vec![$(stringify!($out),)*], node_desc: crate::dsp::$variant::DESC,
node_name: stringify!($variant),
input_help, norm_v:
output_help: vec![$(crate::dsp::$variant::$out,)*], std::rc::Rc::new(|i, x|
node_help: crate::dsp::$variant::HELP, match i {
node_desc: crate::dsp::$variant::DESC, $($in_idx => crate::dsp::norm_v::$variant::$para(x),)+
} _ => x,
}),
denorm_v:
std::rc::Rc::new(|i, x|
match i {
$($in_idx => crate::dsp::denorm_v::$variant::$para(x),)+
_ => x,
}),
} }
pub fn in_name(&self, in_idx: usize) -> Option<&'static str> {
if let Some(s) = self.inputs.get(in_idx) {
Some(*s)
} else {
Some(*(self.atoms.get(in_idx)?))
}
}
pub fn at_name(&self, in_idx: usize) -> Option<&'static str> {
Some(*(self.atoms.get(in_idx)?))
}
pub fn out_name(&self, out_idx: usize) -> Option<&'static str> {
Some(*(self.outputs.get(out_idx)?))
}
pub fn in_help(&self, in_idx: usize) -> Option<&'static str> {
Some(*self.input_help.get(in_idx)?)
}
pub fn out_help(&self, out_idx: usize) -> Option<&'static str> {
Some(*(self.output_help.get(out_idx)?))
}
pub fn norm(&self, in_idx: usize, x: f32) -> f32 {
match in_idx {
$($in_idx => crate::dsp::norm_v::$variant::$para(x),)+
_ => x,
}
}
pub fn denorm(&self, in_idx: usize, x: f32) -> f32 {
match in_idx {
$($in_idx => crate::dsp::denorm_v::$variant::$para(x),)+
_ => x,
}
}
pub fn desc(&self) -> &'static str { self.node_desc }
pub fn help(&self) -> &'static str { self.node_help }
pub fn out_count(&self) -> usize { self.outputs.len() }
pub fn in_count(&self) -> usize { self.inputs.len() }
pub fn at_count(&self) -> usize { self.atoms.len() }
} }
)+ )+
} }
impl NodeInfo { impl NodeInfo {
pub fn from(s: &str) -> Self { pub fn from(s: &str) -> Self {
match s { match s {
stringify!($s1) => NodeInfo::$v1, $(stringify!($str) => crate::dsp::ni::$variant(NodeId::$variant(0)),)+
$(stringify!($str) => _ => NodeInfo::from_node_id(NodeId::Nop),
NodeInfo::$variant(
(NodeId::$variant(0),
crate::dsp::ni::$variant::new()))),+,
_ => NodeInfo::Nop,
} }
} }
pub fn in_name(&self, idx: usize) -> Option<&'static str> { pub fn name(&self) -> &'static str { self.node_name }
match self {
NodeInfo::$v1 => None, pub fn in_name(&self, in_idx: usize) -> Option<&'static str> {
$(NodeInfo::$variant((_, ni)) => ni.in_name(idx)),+ if let Some(s) = self.inputs.get(in_idx) {
Some(*s)
} else {
Some(*(self.atoms.get(in_idx)?))
} }
} }
pub fn out_name(&self, idx: usize) -> Option<&'static str> { pub fn at_name(&self, in_idx: usize) -> Option<&'static str> {
match self { Some(*(self.atoms.get(in_idx)?))
NodeInfo::$v1 => None,
$(NodeInfo::$variant((_, ni)) => ni.out_name(idx)),+
}
} }
pub fn in_help(&self, idx: usize) -> Option<&'static str> { pub fn out_name(&self, out_idx: usize) -> Option<&'static str> {
match self { Some(*(self.outputs.get(out_idx)?))
NodeInfo::$v1 => None,
$(NodeInfo::$variant((_, ni)) => ni.in_help(idx)),+
}
} }
pub fn out_help(&self, idx: usize) -> Option<&'static str> { pub fn in_help(&self, in_idx: usize) -> Option<&'static str> {
match self { Some(*self.input_help.get(in_idx)?)
NodeInfo::$v1 => None,
$(NodeInfo::$variant((_, ni)) => ni.out_help(idx)),+
}
} }
pub fn to_id(&self) -> NodeId { pub fn out_help(&self, out_idx: usize) -> Option<&'static str> {
match self { Some(*(self.output_help.get(out_idx)?))
NodeInfo::$v1 => NodeId::$v1,
$(NodeInfo::$variant((id, _)) => *id),+
}
} }
pub fn at_count(&self) -> usize { pub fn norm(&self, in_idx: usize, x: f32) -> f32 {
match self { (*self.norm_v)(in_idx, x)
NodeInfo::$v1 => 0,
$(NodeInfo::$variant(n) => n.1.at_count()),+
}
} }
pub fn in_count(&self) -> usize { pub fn denorm(&self, in_idx: usize, x: f32) -> f32 {
match self { (*self.denorm_v)(in_idx, x)
NodeInfo::$v1 => 0,
$(NodeInfo::$variant(n) => n.1.in_count()),+
}
} }
pub fn desc(&self) -> &'static str { self.node_desc }
pub fn help(&self) -> &'static str { self.node_help }
pub fn out_count(&self) -> usize { self.outputs.len() }
pub fn in_count(&self) -> usize { self.inputs.len() }
pub fn at_count(&self) -> usize { self.atoms.len() }
pub fn to_id(&self) -> NodeId { self.node_id }
pub fn default_output(&self) -> Option<u8> { pub fn default_output(&self) -> Option<u8> {
if self.out_count() > 0 { if self.out_count() > 0 {
Some(0) Some(0)
@ -1756,27 +1727,6 @@ macro_rules! make_node_info_enum {
None None
} }
} }
pub fn out_count(&self) -> usize {
match self {
NodeInfo::$v1 => 0,
$(NodeInfo::$variant(n) => n.1.out_count()),+
}
}
pub fn desc(&self) -> &'static str {
match self {
NodeInfo::$v1 => "",
$(NodeInfo::$variant(n) => n.1.desc()),+
}
}
pub fn help(&self) -> &'static str {
match self {
NodeInfo::$v1 => "",
$(NodeInfo::$variant(n) => n.1.help()),+
}
}
} }
} }
} }

View file

@ -266,7 +266,7 @@ impl SharedNodeConf {
impl NodeConfigurator { impl NodeConfigurator {
pub(crate) fn new() -> (Self, SharedNodeExec) { pub(crate) fn new() -> (Self, SharedNodeExec) {
let mut nodes = Vec::new(); let mut nodes = Vec::new();
nodes.resize_with(MAX_ALLOCATED_NODES, || (NodeInfo::Nop, None)); nodes.resize_with(MAX_ALLOCATED_NODES, || (NodeInfo::from_node_id(NodeId::Nop), None));
let (shared, shared_exec) = SharedNodeConf::new(); let (shared, shared_exec) = SharedNodeConf::new();
@ -696,7 +696,7 @@ impl NodeConfigurator {
pub fn delete_nodes(&mut self) { pub fn delete_nodes(&mut self) {
self.node2idx.clear(); self.node2idx.clear();
self.nodes.fill_with(|| (NodeInfo::Nop, None)); self.nodes.fill_with(|| (NodeInfo::from_node_id(NodeId::Nop), None));
self.params .clear(); self.params .clear();
self.param_values.clear(); self.param_values.clear();
self.param_modamt.clear(); self.param_modamt.clear();
@ -720,7 +720,7 @@ impl NodeConfigurator {
} }
for i in 0..self.nodes.len() { for i in 0..self.nodes.len() {
if let NodeInfo::Nop = self.nodes[i].0 { if let NodeId::Nop = self.nodes[i].0.to_id() {
index = Some(i); index = Some(i);
break; break;
@ -749,7 +749,7 @@ impl NodeConfigurator {
self.nodes.resize_with( self.nodes.resize_with(
(self.nodes.len() + 1) * 2, (self.nodes.len() + 1) * 2,
|| (NodeInfo::Nop, None)); || (NodeInfo::from_node_id(NodeId::Nop), None));
self.nodes[index] = (info, None); self.nodes[index] = (info, None);
let _ = let _ =