elemaudio_rs/authoring/
mc.rs1use crate::graph::Node;
2use crate::{create_node, unpack, ElemNode};
3
4fn channels_and_props(mut props: serde_json::Value) -> (usize, serde_json::Value) {
5 let channels = props
6 .get("channels")
7 .and_then(|value| value.as_u64())
8 .expect("mc helpers require a positive `channels` prop") as usize;
9
10 if let serde_json::Value::Object(map) = &mut props {
11 map.remove("channels");
12 }
13
14 (channels, props)
15}
16
17fn unpack_mc(
18 kind: &str,
19 props: serde_json::Value,
20 children: impl IntoIterator<Item = ElemNode>,
21) -> Vec<Node> {
22 let (channels, props) = channels_and_props(props);
23 unpack(create_node(kind, props, children), channels)
24}
25
26pub fn sample(props: serde_json::Value, gate: impl Into<ElemNode>) -> Vec<Node> {
35 unpack_mc("mc.sample", props, [gate.into()])
36}
37
38pub fn sampleseq(props: serde_json::Value, time: impl Into<ElemNode>) -> Vec<Node> {
44 unpack_mc("mc.sampleseq", props, [time.into()])
45}
46
47pub fn sampleseq2(props: serde_json::Value, time: impl Into<ElemNode>) -> Vec<Node> {
52 unpack_mc("mc.sampleseq2", props, [time.into()])
53}
54
55pub fn table(props: serde_json::Value, t: impl Into<ElemNode>) -> Vec<Node> {
57 unpack_mc("mc.table", props, [t.into()])
58}
59
60pub fn capture(
62 props: serde_json::Value,
63 g: impl Into<ElemNode>,
64 args: impl IntoIterator<Item = ElemNode>,
65) -> Vec<Node> {
66 let children = std::iter::once(g.into()).chain(args);
67 unpack_mc("mc.capture", props, children)
68}