Generator

Trait Generator 

Source
pub trait Generator {
    type Sample;

    // Required method
    fn sample(&mut self) -> Self::Sample;

    // Provided methods
    fn iter(self) -> Iter<Self>
       where Self: Sized { ... }
    fn amplitude(self, amplitude: f32) -> Amplitude<Self>
       where Self: Sized { ... }
}
Expand description

The trait for an audio generator.

Required Associated Types§

Source

type Sample

The sample that is generated.

Required Methods§

Source

fn sample(&mut self) -> Self::Sample

Generate the next sample from the generator. Advances the underlying generator to the next sample.

§Examples
use audio_generator::{Generator, Sin};

let mut g = Sin::new(440.0, 44100.0);
assert_eq!(g.sample(), 0.0);
assert!(g.sample() > 0.0);

Provided Methods§

Source

fn iter(self) -> Iter<Self>
where Self: Sized,

Construct an iterator from this generator.

use audio_generator::{Generator, Sin};

let mut g = Sin::new(440.0, 44100.0).amplitude(0.5);
let samples = g.iter().take(10).collect::<Vec<f32>>();

assert_eq!(samples.len(), 10);
Source

fn amplitude(self, amplitude: f32) -> Amplitude<Self>
where Self: Sized,

Modify the amplitude of the sample.

§Examples
use audio_generator::{Generator, Sin};

let mut a = Sin::new(440.0, 44100.0).amplitude(0.1);
let mut b = Sin::new(440.0, 44100.0).amplitude(0.1);

for _ in 0..100 {
    assert!(a.sample().abs() <= b.sample().abs());
}

Implementors§