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:
cutoff
: as a fraction of sample rate, can be obtained fromcutoff_from_frequency(cutoff, sample_rate)
. (eg. for a lowpass filter frequencies belowsample_rate
/cutoff
are preserved)band
: transition band as a fraction of the sample rate. This determines how the cutoff “blends”, or how harsh a cutoff this is.
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 |
Functions
add | Performs elementwise addition of two |
bandpass_filter | Creates a low-pass filter. Frequencies between |
bandreject_filter | Creates a low-pass filter. Frequencies outside of |
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. |