Remade the NodeInfo type
This commit is contained in:
parent
4128fc69a0
commit
f07bb6b87b
2 changed files with 107 additions and 157 deletions
210
src/dsp/mod.rs
210
src/dsp/mod.rs
|
@ -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,24 +1627,12 @@ 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 {
|
|
||||||
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,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl $variant {
|
|
||||||
#[allow(unused_mut)]
|
|
||||||
pub fn new() -> Self {
|
|
||||||
let mut input_help = vec![$(crate::dsp::$variant::$para,)*];
|
let mut input_help = vec![$(crate::dsp::$variant::$para,)*];
|
||||||
$(input_help.push(crate::dsp::$variant::$atom);)*
|
$(input_help.push(crate::dsp::$variant::$atom);)*
|
||||||
|
|
||||||
Self {
|
crate::dsp::NodeInfo {
|
||||||
|
node_id,
|
||||||
inputs: vec![$(stringify!($para),)*],
|
inputs: vec![$(stringify!($para),)*],
|
||||||
atoms: vec![$(stringify!($atom),)*],
|
atoms: vec![$(stringify!($atom),)*],
|
||||||
outputs: vec![$(stringify!($out),)*],
|
outputs: vec![$(stringify!($out),)*],
|
||||||
|
@ -1629,8 +1641,35 @@ macro_rules! make_node_info_enum {
|
||||||
output_help: vec![$(crate::dsp::$variant::$out,)*],
|
output_help: vec![$(crate::dsp::$variant::$out,)*],
|
||||||
node_help: crate::dsp::$variant::HELP,
|
node_help: crate::dsp::$variant::HELP,
|
||||||
node_desc: crate::dsp::$variant::DESC,
|
node_desc: crate::dsp::$variant::DESC,
|
||||||
|
node_name: stringify!($variant),
|
||||||
|
|
||||||
|
norm_v:
|
||||||
|
std::rc::Rc::new(|i, x|
|
||||||
|
match i {
|
||||||
|
$($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,
|
||||||
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
)+
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
impl NodeInfo {
|
||||||
|
pub fn from(s: &str) -> Self {
|
||||||
|
match s {
|
||||||
|
$(stringify!($str) => crate::dsp::ni::$variant(NodeId::$variant(0)),)+
|
||||||
|
_ => NodeInfo::from_node_id(NodeId::Nop),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn name(&self) -> &'static str { self.node_name }
|
||||||
|
|
||||||
pub fn in_name(&self, in_idx: usize) -> Option<&'static str> {
|
pub fn in_name(&self, in_idx: usize) -> Option<&'static str> {
|
||||||
if let Some(s) = self.inputs.get(in_idx) {
|
if let Some(s) = self.inputs.get(in_idx) {
|
||||||
|
@ -1657,17 +1696,11 @@ macro_rules! make_node_info_enum {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn norm(&self, in_idx: usize, x: f32) -> f32 {
|
pub fn norm(&self, in_idx: usize, x: f32) -> f32 {
|
||||||
match in_idx {
|
(*self.norm_v)(in_idx, x)
|
||||||
$($in_idx => crate::dsp::norm_v::$variant::$para(x),)+
|
|
||||||
_ => x,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn denorm(&self, in_idx: usize, x: f32) -> f32 {
|
pub fn denorm(&self, in_idx: usize, x: f32) -> f32 {
|
||||||
match in_idx {
|
(*self.denorm_v)(in_idx, x)
|
||||||
$($in_idx => crate::dsp::denorm_v::$variant::$para(x),)+
|
|
||||||
_ => x,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn desc(&self) -> &'static str { self.node_desc }
|
pub fn desc(&self) -> &'static str { self.node_desc }
|
||||||
|
@ -1676,70 +1709,8 @@ macro_rules! make_node_info_enum {
|
||||||
pub fn out_count(&self) -> usize { self.outputs.len() }
|
pub fn out_count(&self) -> usize { self.outputs.len() }
|
||||||
pub fn in_count(&self) -> usize { self.inputs.len() }
|
pub fn in_count(&self) -> usize { self.inputs.len() }
|
||||||
pub fn at_count(&self) -> usize { self.atoms.len() }
|
pub fn at_count(&self) -> usize { self.atoms.len() }
|
||||||
}
|
|
||||||
)+
|
|
||||||
}
|
|
||||||
|
|
||||||
impl NodeInfo {
|
pub fn to_id(&self) -> NodeId { self.node_id }
|
||||||
pub fn from(s: &str) -> Self {
|
|
||||||
match s {
|
|
||||||
stringify!($s1) => NodeInfo::$v1,
|
|
||||||
$(stringify!($str) =>
|
|
||||||
NodeInfo::$variant(
|
|
||||||
(NodeId::$variant(0),
|
|
||||||
crate::dsp::ni::$variant::new()))),+,
|
|
||||||
_ => NodeInfo::Nop,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn in_name(&self, idx: usize) -> Option<&'static str> {
|
|
||||||
match self {
|
|
||||||
NodeInfo::$v1 => None,
|
|
||||||
$(NodeInfo::$variant((_, ni)) => ni.in_name(idx)),+
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn out_name(&self, idx: usize) -> Option<&'static str> {
|
|
||||||
match self {
|
|
||||||
NodeInfo::$v1 => None,
|
|
||||||
$(NodeInfo::$variant((_, ni)) => ni.out_name(idx)),+
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn in_help(&self, idx: usize) -> Option<&'static str> {
|
|
||||||
match self {
|
|
||||||
NodeInfo::$v1 => None,
|
|
||||||
$(NodeInfo::$variant((_, ni)) => ni.in_help(idx)),+
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn out_help(&self, idx: usize) -> Option<&'static str> {
|
|
||||||
match self {
|
|
||||||
NodeInfo::$v1 => None,
|
|
||||||
$(NodeInfo::$variant((_, ni)) => ni.out_help(idx)),+
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn to_id(&self) -> NodeId {
|
|
||||||
match self {
|
|
||||||
NodeInfo::$v1 => NodeId::$v1,
|
|
||||||
$(NodeInfo::$variant((id, _)) => *id),+
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn at_count(&self) -> usize {
|
|
||||||
match self {
|
|
||||||
NodeInfo::$v1 => 0,
|
|
||||||
$(NodeInfo::$variant(n) => n.1.at_count()),+
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn in_count(&self) -> usize {
|
|
||||||
match self {
|
|
||||||
NodeInfo::$v1 => 0,
|
|
||||||
$(NodeInfo::$variant(n) => n.1.in_count()),+
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn default_output(&self) -> Option<u8> {
|
pub fn default_output(&self) -> Option<u8> {
|
||||||
if self.out_count() > 0 {
|
if self.out_count() > 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()),+
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 _ =
|
||||||
|
|
Loading…
Reference in a new issue