I've been modifying my Gakken SX-150 II, adding a microcontroller, DACs, digipots, and everything else required to fully digitally control it. What good is that control without some control signals though? We need sources of modulation.
Modulators
We have a class called "Modulator" that models these modulation sources. It contains things like LFOs and envelope generators. All class members have an output and a function that advances this output: "step()".
Modulating
Modulators need something to modulate, so we have another class: "Parameter". These work as summing nodes, collecting different modulation values. A parameter might be owned by a VCO, and specify something like the pulse width. Parameters can also be owned by modulators to control things like an LFO's rate.
Modulation
Now we need a way to connect modulators to parameters. For this we have another class, "Route", that works something like a patch cable. It specifies an origin modulator, a destination param, and a depth for the modulation. The depth is itself a param, so that it too can be modulated. Also, the route is a subclass of modulator. This allows routes to be chained through other routes.
![]() |
| An example configuration of modulators, routes and parameters |
Here's an example of an LFO being connected to a VCO's pitch parameter via a route. The depth of the modulation is being controlled by an ADSR via another route.
Getting Loopy
The ability to inter-modulate and chain modulators is powerful, but creates a mess for us to evaluate. Remember that the modulators have a function to advance their output, "step()". If we have 3 different LFOs that all modulate each other, in what order should we make them step? We have to think in terms of dependencies.
Let's use a simplified representation, with arrows to indicate modulation, and say our three LFOs are set up like this: A → B → C. C is dependent on B, while B is dependent on A, yet A is not dependent on anything. We should then step A first, followed by B, ending with C. This resolves all the dependencies in order.
If we went in the opposite order, stepping C, then B, then A, the data would be stale. The modulation that C receives comes from B who has not yet stepped (one step stale), and B receives modulation from A who has not yet stepped (another step of staleness). The modulation that C indirectly receives from A is now two steps stale.
Since the order does have an impact, how can we write a script to detect and handle these dependencies correctly? What if C went on to modulate A? How can we handle loops in our modulation?
A Theory
Our little letters-and-arrows representation of the modulators is actually a "graph". In graph theory the letters are called nodes, and the arrows are called edges. That might not seem relevant, but the ability to represent our data as a graph means we can use all the powerful tools of graph theory to solve our problem.
Kahn
The graph we have is more specifically a dependency graph (just with our arrows turned backwards) and Wikipedia tells us that: "If a dependency graph does not have any circular dependencies, it forms a directed acyclic graph, and an evaluation order may be found by topological sorting."
![]() |
| topological sort - leetcode |
Looking at the topological sort page gives us Kahn's algorithm. It plucks off nodes that have no dependencies, revealing new nodes that have no dependencies, plucking those, revealing, plucking... until there are no nodes left. The order of the pluck gives you a topological sort of the nodes. This works great unless there are loops (aka cycles) that by definition will not have any no-dependency nodes. That's what the above quote meant by "acyclic" graphs: no cycles. Well, how do we even know if we have cycles?
Bellman–Ford
The Bellman–Ford algorithm is a different option that's not a topological sort. It's intended to find the "cheapest" path through a cyclic/acyclic graph that has different costs per edge. If we make every edge have a cost of 1, then it's actually finding the shortest distance. If we make it -1, then it finds the furthest distance. This is equivalent to finding the depth, and we can use this depth to make a topological sort.
We can start it on a random node, assign a depth of 0, move to a child node, and assign it the parent depth +1 (unless the child already has a deeper parent). When we run out of children, start over on a random node, ultimately covering all nodes. With multiple passes like this, the depth continues to propagate forward, and eventually settles. The deepest parent will be found for each node, and each node will get that depth + 1. Unless there's a cycle.
Didn't we say this supported cyclic graphs though? Yes, but negative edge values break that support. After a certain number of passes the graph should have settled, but a cycle looks like infinitely many children, and simply continues to increase forever.
"If a graph contains a "negative cycle" (i.e. a cycle whose edges sum to a negative value) that is reachable from the source, then there is no cheapest path: any path that has a point on the negative cycle can be made cheaper by one more walk around the negative cycle. In such a case, the Bellman–Ford algorithm can detect and report the negative cycle." - Wikipedia
We've been foiled by cycles again, but at least we can detect them now
Tarjan
Onto Tarjan's algorithm. It also isn't a topo-sort, but Donald Knuth tells us that "...[Tarjan's] algorithm also does topological sorting as a byproduct." It's meant to find "strongly connected components" of a graph. What are those? They're a special, interconnected portion of a graph. For our purposes though, they're cycles (or even cycles of cycles).
![]() |
| SCC - Wikipedia |
It works by doing a depth-first search, meaning it tries to progress forward through a graph until it hits a dead-end, then backtracks and explores a new node. As it marches forward it assigns an increasing index number to each node, and keeps track of the lowest index that can be reached from that node by moving forward. This lowest-reachable-index is called the lowlink. Until we explore further, each node's lowlink is equal to its own index.
Any time the algorithm is able to connect to an old node and form a cycle, it has to reevaluate the lowlink value for the connecting node. This is because looping back to old nodes is the only way to reach a lower index node. Moving into new nodes will only ever create higher indexes.
When it finishes exploring a node and has to backtrack, the lowlink propagates backwards to the parent. Then the lowlink gets compared to the node's index. If they're equal, the node is considered a "root". That means it's either the start of a cycle, and it reaches itself through the other cycle members, or it's a "singleton", and it's part of an offshoot that doesn't form a cycle. Either way, we're done evaluating it and can put it/the cycle into our topological sort.
If instead the lowlink is lower than the index, that means we haven't backtracked to the start of a cycle yet. We continue passing the lowlink up through the parents until we find a match that indicates a root.
![]() |
| Example graph with indexes and lowlinks assigned |
Here's an example graph that's had indexes and lowlinks added. Nodes B, C, and D can all reach (directly or indirectly) to index 1 (node B), so they all have a lowlink of 1. Node B forms the root of the cycle since it has a matching index and lowlink. Node A also has an index and lowlink that match, making it a root, but of a singleton.
After we've explored every node once, we'll have found every root and created a (reverse) topological sort. Critically, cycles don't invalidate the sort, and the members of the cycle will have a usable order. Keep in mind there is no perfect order for a cycle, but the sort will return one that yields a single step of staleness for simple loops.
We've glossed over some details of how Tarjan's keeps track of the nodes it has seen and the nodes it's still processing. The gory details can be teased from this code fragment on pastebin or from this YouTube video.
Application
Now we need to apply Tarjan's algorithm to our data. It only works in terms of nodes and edges, so we need to translate our modulators, routes, and parameters. Modulators are nodes. Routes, by extension, are also nodes and critically provide the edges. Then there are parameters... are they nodes too? Not exactly.
As we've said, parameters have owners (VCOs, LFO, etc) and the owners are nodes. This makes sense of our A → B → C graph. A, B, and C are all LFOs, but you can't modulate an LFO as a whole; you can only modulate a parameter that it owns.
So, to make our graph, we need to replace parameters with their owners. Then the owners and any other modulators are the nodes. If we then assign numbers to all the nodes, we can shed the complexities of the different classes, and implement Tarjan's algo so that it uses simple numbers.
Once Tarjan's runs, we have an order to evaluate all owners/modulators in.
Order in the Court
Now that we have an order, what exactly are we doing? We have to do a pass of every node in the graph, and step each one.
We start with an owner/modulator that we want to step. We read any modulation that's sitting in its parameters. Then we step it, getting a new output value. We look at what params are targeted by the owner, and we add this new output to each one. This process is repeated for the rest of the nodes in order.
The catch is correctly managing the parameters. The fact that cycles have no perfect order means we have to accept some staleness. This staleness manifests as modulation that gets sent to parameters after the param owner has stepped. If we don't account for this, the modulation won't just be stale, it'll be missing.
The params are just summing nodes, and they need to be zeroed out at some point in the pass (else they increase infinitely). It's tempting to zero them at the start of a pass, but that wipes out any late modulation that didn't get read during the last pass.
The solution is simple: zero params immediately after the owner has read them. Then they retain any modulation that was added since the last time they were read.
Going further
We have a solution for sending modulation to a destination. What if we want to process that modulation though? So far all we can do is attenuate signals via the depth control of a route.
We can add more functionality to the routes beyond depth. Why not let them invert, rectify, slew limit? They could take more than one modulation as input and subtract them, ring modulate them...










