SPB Git

Repository Public

architecture.md 7.1 KB 148 lines markdown

1<!--2ORIX Sonata3File: Documentation/manual/architecture.md4Author: Simon-Pierre Boucher5Contact: contact@spboucher.ai6-->7# Architecture89This chapter explains what happens between the text you write and the sound you hear. It is10for the curious, and for anyone who wants to build on Sonata.1112## The pipeline1314```text15source text16  → tokens              (lexer: every token knows its place in the file)17  → syntax tree         (parser: recovers from errors and reports several at once)18  → resolved program    (names, modules, kinds, argument counts)19  → Musical IR          (the piece: notes with exact times, tempo map, automation, routing)20  → Audio DSP IR        (the mixing graph for one sample rate, with latencies)21  → execution plan      (instruments, effects and buffers prepared in memory)22  → audio               (played in real time, or rendered to a file)23```2425Three things follow from this shape.2627**One compiler.** The app, the command line and the editor services all call the same28compiler. The Studio does not parse or time anything itself: it draws what the compiler29returns. That is why the timeline, the score, a MIDI export and a render always agree.3031**The program is run at compile time.** Loops, functions and random choices are executed32while compiling; the result, the Musical IR, is a plain list of events. Playback never33executes your code, so it cannot stall the audio.3435**An execution plan, not machine code.** The compiled piece is a prepared graph that the36runtime interprets block by block. Sonata 0.1 does not generate machine code.3738You can look at every stage: `sonata build piece.sonata --emit ast`, `--emit ir`,39`--emit graph`, or the **Compiler** space ([Compiler](compiler.md)).4041## The Musical IR4243A compiled composition (a `Program`) contains:4445* the tempo map and the time signatures;46* tracks, each with its instrument (engine and parameter values), its mixer settings, its47  effects and its notes;48* for each note: its exact musical start and duration, the same in seconds, its pitch (a49  fractional MIDI number), its velocity, its voice, its articulation, any pitch bend, and the50  place in the source that produced it;51* automation lanes, buses, sends, the master settings and markers.5253The source position kept with each note is what lets a click on a note in the Studio or the54Score take you to the right line of code.5556## Time5758Musical positions are exact fractions of a whole note, held as two 64-bit integers. They are59never rounded: a thousand triplets end exactly where they should. An overflow is an error60(Q6005), never a silent wrap.6162Seconds come from the tempo map. For a constant tempo, a whole note lasts `240 / bpm`63seconds. A tempo ramp moves the *duration of a whole note* in a straight line between its two64ends, and the time it takes is the area under that line (a trapezoid).6566A time in seconds becomes a sample index by one rule, used by the renderer, the real-time67engine and the rulers alike:6869```text70sample = floor(seconds × sample_rate + 0.5)71```7273A quarter note at 120 bpm starts 0.5 s after the previous one: sample 24 000 at 48 kHz,7422 050 at 44.1 kHz. The piece is the same at every sample rate; only the rounding to samples75differs.7677## The mixing graph7879For a given sample rate the compiler turns the tracks, buses and master into a graph of nodes:80instrument sources, effects, faders, sends, sums, the limiter and the output. It checks that81the graph has no cycle, orders the nodes, computes the latency along every path and inserts82pure delays where paths of different latency meet, so that everything is aligned at each sum.83`sonata inspect` prints the number of nodes and the total latency.8485## The audio engine8687The engine has two halves. The **processor** runs on the audio thread and produces sound. The88**controller** is used by everything else — the transport, the mixer, the editor — and talks89to the processor through lock-free queues.9091The audio thread follows strict rules, because a late block is an audible click:9293* it never allocates or frees memory — voices, buffers and delay lines are created when the94  piece is prepared;95* it never waits for a lock, reads a file, writes a log or compiles;96* it works in chunks of at most 64 frames, cut at every note and automation boundary, so97  events are exact to the sample whatever the block size of the audio device;98* it checks its own output: a value that is not a finite number is replaced by silence and99  counted; the output to the device is clamped as a last resort.100101These rules are enforced by tests. One of them counts every memory allocation during102thousands of audio callbacks, with every effect inserted and commands and program swaps103arriving, and requires the count to be zero.104105**Playback and export are the same code.** The offline renderer drives the same processor, so106a render equals what the engine plays: the tests compare renders made with block sizes of 32,10764, 128, 256, 1 024 frames and one single block, and require identical samples.108109## Hot reload110111When you edit while the piece plays, the new version is compiled and prepared away from the112audio thread. The finished program is handed to the processor, which switches at a block113boundary with a 30 ms equal-power crossfade, keeps the transport position and the mixer114state, and restarts the notes that should be sounding at that position. The old program is115handed back and freed elsewhere. If the new code has an error, nothing is swapped: the last116valid program keeps playing.117118## The registry119120Engines, effects and patch nodes describe themselves: names, parameters, units, ranges,121defaults, presets, latency. This description — the registry — is the single source for three122things: the compiler validates your code against it, the inspectors of the app are built from123it, and the reference chapters of this manual are generated from it124(`Scripts/gen-reference.sh`; a test fails when they are out of date). A parameter cannot125exist in one place and not in the others.126127## Safety of a composition128129A Sonata file is data, not a script with access to your machine. It can import modules and130name audio files, both confined to its project. There is no file, network or process access131in the language, loops are bounded and resources have quotas132([Algorithmic composition](algorithmic-composition.md#limits)). Opening a piece from someone133else can make noise and nothing more — and the master limiter is on by default for that.134135## Where things are136137| Part | Location |138|---|---|139| Shared definitions: exact time, diagnostics, Musical IR, DSP IR, registry types | `crates/sonata-ir` |140| Lexer, parser and formatter, semantics, compiler | `crates/sonata-lexer`, `-parser`, `-semantics`, `-compiler` |141| DSP building blocks, effects, analysis (FFT, loudness, pitch) | `crates/sonata-dsp` |142| Instrument engines | `crates/sonata-instruments` |143| Real-time engine, offline renderer and file formats | `crates/sonata-runtime`, `crates/sonata-render` |144| MIDI and MusicXML | `crates/sonata-interop` |145| Command line, C interface for the app | `crates/sonata-cli`, `crates/sonata-ffi` |146| The app | `Apps/ORIXSonata`, `Packages/SonataUI`, `SonataBridge`, `SonataProject` |147| Standard library, in Sonata | `StandardLibrary/std` |148
Files main
Menu

Theme