NibbleRealm / twang

Library for pure Rust advanced audio synthesis.
https://nibblerealm.com/twang/
Apache License 2.0
125 stars 8 forks source link

Re-work API #16

Open AldaronLau opened 2 years ago

AldaronLau commented 2 years ago

Currently the API requires the user to pass a closure for synthesis.

I'd like to re-work this to be data-oriented (savable and loadable as a file), and synthesis constructable with the builder pattern. Users must be able to pass in their own variables, and still be able to get step events in custom closures for flexibility and backwards-compatibility.

AldaronLau commented 1 year ago

Update

I have thought about the right way to do this probably too way much without writing code. I want to avoid writing a VM within Twang due to performance concerns. So how should a synthesis format work?

API

The new API will heavily use the type system.

use twang::Synth::{Sig, Mix};

let mut audio = Audio::<Ch16, 2>::with_silence(48_000, 48_000 * 5);
// Two sine oscillators, an octave apart
let oscs = [
    Sig(440.0).sine(),
    Sig(880.0).sine(),
];
// Mix the two together
let synth = Mix(&oscs);

synth.stream(audio.sink());

Format

For the format, twang will attempt to use postcard (as an optional feature). An intermediate MuON based format for human-readable/writeable will be able to be used with it.