SPB Git

Repository Public

performance.md 8.5 KB 174 lines markdown

1# Performance Guidelines23An automation runs next to live audio. ORIX DJ is built so that a script cannot disturb the sound, and it enforces fixed limits so that a script cannot run away. This chapter lists those limits, explains what happens when one is reached, and gives advice for writing scripts that stay far from them.45## Why a script cannot glitch the audio67- The script runtime runs on its own scheduler thread. It never runs on the audio thread.8- The scheduler wakes every 4 milliseconds, advances each running automation up to 15 milliseconds ahead of the master clock, and hands the resulting parameter changes and actions to the audio engine with the exact sample at which each must apply.9- The audio thread only applies those time-stamped commands. It does not wait for the scheduler.1011If a script does a lot of work in one step, the scheduler step takes longer; the audio continues. To keep even that under control, the runtime counts the work of every automation and stops one that exceeds its budget.1213## Limits checked before the script runs1415| Limit | Value | Code when exceeded |16| --- | --- | --- |17| Script size | 256 KB | E-LIMIT |18| Statements per script | 20000 | E-LIMIT |19| Nesting of blocks | 20 levels | E-LIMIT |20| Nesting of an expression | 20 levels | E-LIMIT |21| Turns of one loop (`repeat`, `for`, `every`) | 10000 | E-LIMIT |22| `while` limit | a constant from 1 to 10000 | E-UNBOUNDED |23| Shortest `every` period | 1/64 beat | E-LIMIT |24| Array length | 256 elements | E-LIMIT |25| Event handlers per automation | 32 | E-LIMIT |26| Variables per automation or function | 512 | E-LIMIT |27| String literal | 512 characters | E-LIMIT |28| A wait, ramp or position | 100000 beats | E-LIMIT |29| Recursion | not allowed | E-RECURSION |3031## Limits checked while the script runs3233| Limit | Value | Code when exceeded |34| --- | --- | --- |35| Operations in one scheduler step, without a wait | 20000 | E-BUDGET |36| Operations over the whole life of one automation | 5000000 | E-BUDGET |37| Parameter changes and actions within one beat (ramp updates not counted) | 512 | E-RATE |38| Pending tasks (scheduled blocks and handler runs) | 256 | E-LIMIT |39| Active ramps | 128 | E-LIMIT |40| Registered event handlers | 32 | E-LIMIT |41| Handler runs in progress; further events are dropped, not an error | 64 | – |42| Turns of one loop, when the count is computed | 10000 | E-LIMIT |43| Depth of function calls | 16 | E-LIMIT |44| A computed wait, ramp or position | 100000 beats | E-LIMIT |45| A string built with `+` | 2048 characters | E-LIMIT |46| Text of one `print` | cut at 2048 characters | – |4748These are the values of the standard configuration.4950### What counts as an operation5152Roughly: each statement executed, each turn of a loop, each arithmetic, comparison or logical operator evaluated, each read of a live value, and each call of a builtin. A typical transition uses a few dozen operations in total. A `bar` handler that tests two conditions uses fewer than ten per bar.5354## What happens when a run-time limit is reached5556`E-BUDGET`, `E-RATE` and the run-time `E-LIMIT` are run-time errors like any other:57581. The automation is cancelled immediately: all its tasks, ramps and handlers are removed.592. Every effect it had switched on and not switched off is switched off, and the log shows a `bypassed … after the failure` line for each.603. The error is logged with the line that was executing.6162Decks keep playing, other automations keep running, and the controls other than those effects stay where they were. The message for `E-RATE` says it plainly: the automation was stopped to protect the performance.6364```orixdj-error65var total = 066for a in 1...200 {67    for b in 1...200 {68        total = total + a * b69    }70}71print(total)72```7374```text75More than 20000 operations without a wait. Add a 'wait' inside the loop or reduce its work. [E-BUDGET]76```7778Dropped events are different: they do not stop anything. When an event fires while 64 runs of handlers are already in progress in that automation, that occurrence is skipped and counted. See [Events](events.md).7980## Emergency stop8182If something sounds wrong and you do not know why, use the emergency stop: `panic` in the console, or the control mapped to `automation.panic`. It stops every automation and bypasses every effect in every rack. Decks keep playing and faders are not touched.8384## Guidelines8586### Let time pass in loops8788A loop without a `wait` does all its turns in one scheduler step. That is fine for small counts (scheduling 16 blocks, summing 8 numbers). For anything that writes controls, each turn should wait.8990```orixdj91// Good: 32 steps spread over 2 bars92for step in 1...32 {93    deck.A.filter.highpass = hz(100 + step * 50)94    wait 1/4beat95}96deck.A.filter.highpass = 20Hz97```9899### Prefer a ramp to many small steps100101`over` is cheaper and smoother than a loop of assignments: the runtime computes the value at each scheduler step, the engine glides between them, and ramp updates are not counted against the commands-per-beat limit.102103```orixdj104// Better than the loop above when a continuous sweep is wanted105over 2 bars { deck.A.filter.highpass -> 1700Hz }106deck.A.filter.highpass = 20Hz107```108109### Choose the slowest event that does the job110111`on bar` fires once per bar, `on beat` four times as often, `on deck.X.beat` once per beat of each deck. To notice that a track is ending, a `bar` handler is more than enough.112113### Keep handlers short114115A handler run that lasts longer than the interval between its events overlaps with the next run. Use a flag variable to skip events while busy, or `once` when the handler is needed a single time.116117### Schedule what is known, compute what is not118119Positions known in advance are best written as `at` blocks at the start: they cost nothing until they fire. Do not poll for something that has a fixed position.120121### Do the setup before the bar122123Statements at the same instant are sent to the engine for the same sample, in order. Setting eight effect parameters and then calling `enable()` on the bar is fine. There is no need to spread them out.124125### Avoid many long-running automations126127Each running automation is advanced at every scheduler step. A handful of them costs nothing measurable; dozens of forgotten event-driven automations add up and, more importantly, make it hard to know what is acting on the mix. Check `status` and stop what you no longer need.128129### Mind what several automations do to one control130131Two automations ramping the same control fight: each writes its own value at every step and the control jitters between them. One automation should own a control at a time.132133### Use seconds sparingly134135Seconds are converted to beats with the tempo of that moment. A script written in beats and bars stays aligned whatever the tempo does.136137## Measuring138139ORIX DJ measures the scheduler while it runs. The figures available are:140141| Figure | Meaning |142| --- | --- |143| Ticks | Number of scheduler steps |144| Last, average and maximum step time | Time spent per step, in microseconds |145| Maximum gap between steps | Scheduler jitter, in milliseconds (the nominal gap is 4 ms) |146| Active automations, pending tasks, active ramps | Current load |147| Started, finished, run-time errors, validation errors | Counters since the last reset |148| Tracked commands | Commands whose arrival time was measured by the audio thread |149| Late commands, maximum and mean lateness | Commands that reached the audio thread after their sample, and by how many samples |150| Dropped control events | Commands that could not be queued to the audio thread |151152Per automation, the run statistics give: operations used, writes, ramp updates, actions, clamped values, dropped events, the largest number of tasks and of ramps at once, and the largest lateness in beats between the time a task was due and the time it ran.153154Signs that a script should be simplified:155156- late commands or dropped control events that appear only while it runs;157- a maximum step time that approaches the 4 ms interval;158- clamped values or dropped events above zero.159160The simulator reports the same per-automation statistics without any audio, so the cost of a script can be examined before it is used. The command-line tool `orix-dj-bench bench` measures the engine and the automation timing on your machine and prints the measured values.161162## The live log163164The log keeps the most recent 2000 lines, and the list of finished runs the most recent 50. A script that prints on every beat fills the log quickly; print on bars, or only when something changes.165166## Related167168- [Security Model](security.md)169- [Automation Scheduling](scheduling.md)170- [Loops](loops.md)171- [Events](events.md)172- [Error Handling](errors.md)173- [Debugging](debugger.md)174
Files main
Menu

Theme