int main() { AudioGraphRef graph = new AudioGraph(); /*------------------------------------------------------------------------ * Create a simple envelope-modulated triangle wave. *-----------------------------------------------------------------------*/ NodeRef triangle = new Triangle(1000); NodeRef envelope = new ASR(0.01, 0.0, 0.1); NodeRef output = triangle * envelope; /*------------------------------------------------------------------------ * Pan the output across the stereo field. *-----------------------------------------------------------------------*/ NodeRef panned = new Pan(2, output); graph->add_output(panned); graph->start(); while (true) { /*------------------------------------------------------------------------ * Periodically, retrigger the envelope, panned to a random location. *-----------------------------------------------------------------------*/ usleep(random_integer(1e4, 1e6)); panned->set_input("pan", random_uniform(-1, 1)); envelope->trigger(); } }
NodeRef Synth::instantiate(NodeDefinition *nodedef) { /*------------------------------------------------------------------------ * Recursively instantiate the subgraph specified in NodeDefinition. * Does not currently support graphs that route one node to multiple * inputs. *-----------------------------------------------------------------------*/ NodeRegistry *registry = NodeRegistry::global(); Node *node = registry->create(nodedef->name); NodeRef noderef = NodeRef(node); /*------------------------------------------------------------------------ * Update the synth's internal collection of node refs. *-----------------------------------------------------------------------*/ this->nodes.insert(noderef); for (auto param : nodedef->params) { std::string param_name = param.first; NodeRef param_node = this->instantiate(param.second); noderef->set_input(param_name, param_node); } if (nodedef->is_constant) { Constant *constant = (Constant *) node; constant->value = nodedef->value; } if (!nodedef->input_name.empty()) { this->inputs[nodedef->input_name] = noderef; } return noderef; }