[][src]Crate twang

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 paramter representing the frequency counter. You can use the frequency counter to generate continuous pitched waveforms.

A4 (440 Hz) Organ Example

use twang::gen::{Generator, Saw};
use fon::{
    mono::Mono64,
    ops::{Add, Sine},
    Audio, Hz,
};

/// First ten harmonic volumes of a piano sample (sounds like electric piano).
const HARMONICS: [f64; 10] = [
    0.700, 0.243, 0.229, 0.095, 0.139, 0.087, 0.288, 0.199, 0.124, 0.090,
];
/// The three pitches in a perfectly tuned A3 minor chord
const PITCHES: [f64; 3] = [220.0, 220.0 * 32.0 / 27.0, 220.0 * 3.0 / 2.0];

let mut gen;

// Five seconds of 48 KHz Audio
let mut chord = Audio::with_silence(48_000, 48_000 * 5);
let mut temp;

// Synthesize an A minor chord.
let volume = 0.25; // To avoid clipping
for pitch in PITCHES.iter().cloned() {
    // Add note to chord
    for (i, harmonic) in HARMONICS.iter().cloned().enumerate() {
        let i: f64 = (i as i32).into();
        gen = Saw::new(Hz(pitch * i));
        temp = Audio::<Mono64>::with_silence(48_000, 48_000 * 5);
        gen.generate(&mut temp);
        temp.blend_sample(Mono64::new(harmonic * volume), Sine);
        // Add harmonic to chord
        chord.blend_audio(&temp, Add);
    }
}

Structs

Fc

Frequency counter.

Pink

Pink Noise Generator using algorithm described in research paper A New Shade of Pink.

Signal

A signed digital audio signal that can be routed through processing components. This differs from Mono64 in that the values are not clamped between -1 and 1.

Synth

A streaming synthesizer into an audio Sink. Rather than implementing Stream, which has it's own associated sample rate, Synth generates the audio directly at the destination sample rate.

White

White Noise Generator using Middle Square Weyl Sequence PRNG.

Traits

Mix

Trait for synthesizing multiple sounds together.