wavegraph-compiler 0.1.0

Compile named textual audio patches to WaveGraph signal bytecode
Documentation
# wavegraph-compiler

[![crates.io][crate-badge]][crate]
[![docs.rs][docs-badge]][docs]
[![Build status][ci-badge]][ci]
[![Project Chat][chat-image]][chat-link]

Compile a WaveGraph Language (WGL) patch (`.wg`) to signal bytecode. The
caller supplies a catalog of native object types and their named signal ports.
The compiler returns the bytecode, the native object declarations in binding
order, and the left and right output bus IDs. It supports `no_std` with `alloc`.

This patch averages a stereo input on the left output and sends the original
right channel to the right output. The [render test](tests/render.rs) compiles
the [patch file](tests/fixtures/stereo.wg) and checks both channels through the
`wavegraph-dsp` interpreter.

```text
node input stereo
gain halfLeft input.left 64
gain halfRight input.right 64
mix mono halfLeft.out halfRight.out
output left mono.out
output right input.right
```

## Language

Write one statement per line. Whitespace separates tokens. A `#` anywhere
starts a comment that runs to the end of the line. Blank lines are ignored.
Object aliases start with an ASCII letter and can contain ASCII letters,
digits and underscores. Aliases and port names are case-sensitive. In a port
reference, the first dot separates the alias from the catalog's port name.

| Statement | Meaning |
| --- | --- |
| `node NAME TYPE [KEY=VALUE ...]` | Declare a native object. The caller interprets its constructor options. |
| `connect SOURCE.OUTLET DESTINATION.INLET` | Connect a named signal outlet to a named signal inlet. Each inlet takes one source. An outlet can feed several inlets. |
| `gain NAME SOURCE.OUTLET LEVEL` | Scale a signal by LEVEL/128 (0–255). The result is `NAME.out`. |
| `mix NAME A.OUTLET B.OUTLET` | Sum two signals with saturation. The result is `NAME.out`. |
| `output left SOURCE.OUTLET`, `output right SOURCE.OUTLET` | Bind the two physical outputs. Both can use the same bus. |

Connections may precede declarations. Native object IDs follow declaration
order. The compiler schedules producers before consumers, breaking ties by
declaration order. An invalid connection, an undriven inlet, or a signal cycle
produces a diagnostic with a source line. The bytecode can address 256 native
objects and 256 signal buses.

On a `node` line, each option occupies one token. Its first `=` separates a
nonempty key and value. Each key may appear once per node. The compiler passes
options to the host in source order. For example, the desktop sequencer reads
`steps=36!+39,42,-` as an accented 36 together with 39, then 42, then a rest.
Both `left` and `right` outputs must be bound exactly once per patch.

See the [grammar design notes](GRAMMAR.md) for the rationale behind the syntax
and the PEG parser.

```rust
use wavegraph_compiler::{NodeSpec, compile};

let source = "node source input\ngain half source.out 64\noutput left source.out\noutput right half.out";
let patch = compile(source, |kind| {
    (kind == "input").then_some(NodeSpec { inputs: &[], outputs: &["out"] })
})?;
assert_eq!(patch.nodes[0].alias, "source");
assert_eq!(patch.outputs, [0, 1]);
# Ok::<(), wavegraph_compiler::CompileError>(())
```

## License

The Rust code is licensed under either of

- [Apache License, Version 2.0][apache]
- [MIT License][mit]

at your option.

### Contribution

Unless you explicitly state otherwise, you license any contribution you
intentionally submit for inclusion in this work (as defined in the Apache-2.0
license) under both licenses above, without additional terms or conditions.

[crate-badge]: https://img.shields.io/crates/v/wavegraph-compiler?logo=rust
[crate]: https://crates.io/crates/wavegraph-compiler
[docs-badge]: https://docs.rs/wavegraph-compiler/badge.svg
[docs]: https://docs.rs/wavegraph-compiler/
[ci-badge]: https://github.com/fm1-rust/wavegraph/actions/workflows/wavegraph-compiler.yml/badge.svg?branch=main&event=push
[ci]: https://github.com/fm1-rust/wavegraph/actions/workflows/wavegraph-compiler.yml
[chat-image]: https://img.shields.io/badge/discord-join_chat-blue.svg
[chat-link]: https://discord.gg/bq8NYQWzm5
[apache]: https://github.com/fm1-rust/wavegraph/blob/main/wavegraph-compiler/LICENSE-APACHE
[mit]: https://github.com/fm1-rust/wavegraph/blob/main/wavegraph-compiler/LICENSE-MIT