Library for pure Rust advanced audio synthesis.
Most audio DSP (Digital Signal Processing) libraries have a concept of an audio graph which connects sources to destinations. Twang uses a simplified model: a synthesis tree. Twang doesn't deal with having speakers as a node on a graph, as it's only focus is synthesis. A synthesis tree can do all of the things that an audio graph can do, but it's simpler and much easier to learn.
To start, first you need to construct a synthesizer
(Synth
). Then you need a type that implements the
Sink
trait. Audio
buffers have a sink
method you can use to get a
Sink
. Once you have those, you can synthesize audio with a closure that
has one parameter representing the frequency counter. You can use the
frequency counter to generate continuous pitched waveforms.
A3 (220 Hz) Minor Piano Example
This example uses the first ten piano harmonics to generate a sound that
sounds like an electric piano. This is an example of additive synthesis,
since it uses the Mix
trait.
use ;
use ;
// Target sample rate set to 48 KHz
const S_RATE: u32 = 48_000;
/// First ten harmonic volumes of a piano sample (sounds like electric piano).
const HARMONICS: = ;
/// The three pitches in a perfectly tuned A3 minor chord
const PITCHES: = ;
/// Volume of the piano
const VOLUME: f64 = 0.1;