Module synthrs::filter[][src]

A collection of signal filters.

To filter a bunch of samples, first create the filter and samples.

There are two types of filters: stateless, and stateful filters. Stateless filters can be used to convolve samples, while stateful filters transform individual samples.

Stateless filters

Stateless filters are pure functions and are used in conjunction with the convolve function:

use synthrs::filter::{convolve, cutoff_from_frequency, lowpass_filter};
use synthrs::synthesizer::{quantize_samples, make_samples};
use synthrs::wave::sine_wave;

// Generate a bunch of samples at two different frequencies
let samples = make_samples(0.5, 44_100, |t: f64| -> f64 {
    0.5 * (sine_wave(6000.0)(t) + sine_wave(80.0)(t))
});

// Create a lowpass filter, using a cutoff of 400Hz at a 44_100Hz sample rate (ie. filter out frequencies >400Hz)
let lowpass = lowpass_filter(cutoff_from_frequency(400.0, 44_100), 0.01);

// Apply convolution to filter out high frequencies
let lowpass_samples = quantize_samples::<i16>(&convolve(&lowpass, &samples));

Common stateless filter arguments:

Stateful filters

Stateful filters are structs which hold some state, such as DelayLine which has to keep in memory historical samples.

They can be used to transform a bunch of samples using map.

use synthrs::filter::Comb;
use synthrs::synthesizer::{quantize_samples, make_samples};
use synthrs::wave::sine_wave;

// Creates a comb filter with
// * 0.2 second delay
// * 44100Hz,
// * 0.5 dampening inverse factor
// * 0.5 dampening factor
// * 0.5 feedback factor
let mut comb = Comb::new(0.2, 44_100, 0.5, 0.5, 0.5);

let samples = make_samples(0.5, 44_100, |t: f64| -> f64 { sine_wave(440.0)(t) });

let filtered_raw: Vec<f64> = samples
    .into_iter()
    .map(|sample| comb.tick(sample))
    .collect();
let filtered_quantized = quantize_samples::<i16>(&filtered_raw);

See: examples/filters.rs

An all-poss filter is implemented as a generator in crate::wave::allpass.

Structs

AllPass

A stateful all-pass filter.

Comb

A stateful comb filter.

DelayLine

A stateful delay line. Samples are delayed for delay_length seconds.

Functions

add

Performs elementwise addition of two Vec<f64>s. Can be used to combine filters together (eg. combining a low-pass filter with a high-pass filter to create a band-pass filter)

bandpass_filter

Creates a low-pass filter. Frequencies between low_frequency and high_frequency are preserved when samples are convolved with this filter.

bandreject_filter

Creates a low-pass filter. Frequencies outside of low_frequency and high_frequency are preserved when samples are convolved with this filter.

blackman_window

Creates a Blackman window filter of a given size.

convolve
cutoff_from_frequency

Returns the cutoff fraction for a given cutoff frequency at a sample rate, which can be used for filter creation.

envelope

Simple linear attack/decay envelope. No sustain or release.

highpass_filter

Creates a high-pass filter. Frequencies above the cutoff are preserved when samples are convolved with this filter.

lowpass_filter

Creates a low-pass filter. Frequencies below the cutoff are preserved when samples are convolved with this filter.

spectral_invert

Given a filter, inverts it. For example, inverting a low-pass filter will result in a high-pass filter with the same cutoff frequency.