pub struct Waveform<BitDepth: Clone> { /* private fields */ }
Expand description
A structure that represent a waveform.
Implementations§
source§impl<BitDepth: Clone> Waveform<BitDepth>
impl<BitDepth: Clone> Waveform<BitDepth>
sourcepub fn new(sample_rate: f32) -> Self
pub fn new(sample_rate: f32) -> Self
Construct a new empty waveform.
At construction the sampling rate of the waveform must be specified.
Examples
use waver::Waveform;
let wf = Waveform::<i16>::new(44100.0);
sourcepub fn with_wave(sample_rate: f32, wave: Wave) -> Self
pub fn with_wave(sample_rate: f32, wave: Wave) -> Self
Construct a new waveform with a single underlying wave component.
This is identical to using new()
and then superpose()
.
Examples
use waver::{Waveform, Wave};
let wf = Waveform::<i16>::with_wave(44100.0,
Wave { frequency: 4000.0, ..Default::default() });
sourcepub fn superpose(&mut self, wave: Wave) -> &mut Self
pub fn superpose(&mut self, wave: Wave) -> &mut Self
Add a wave component.
Examples
use waver::{Waveform, Wave};
let mut wf = Waveform::<i16>::new(44100.0);
wf.superpose(Wave { frequency: 6000.0, amplitude: 0.25, ..Default::default() })
.superpose(Wave { frequency: 5500.0, amplitude: 0.75, ..Default::default() });
sourcepub fn normalize_amplitudes(&mut self) -> &mut Self
pub fn normalize_amplitudes(&mut self) -> &mut Self
Normalize amplitude weights of all underlying waves.
When superposing waves with a combined amplitude-weights that exceed 100% normally waves would be clipped at the highest quantization level.
In waver, overshoot causes quantization to become numerically unstable which manifests in an iterator stop.
Use this method to normalize all weights to equal shares of the amplitude.
Examples
use waver::{Waveform, Wave};
// Two waves with an amplitude weights of 150%.
let mut wf = Waveform::<i16>::with_wave(44100.0,
Wave { frequency: 3000.0, amplitude: 1.0, ..Default::default() });
wf.superpose(Wave { frequency: 4000.0, amplitude: 0.5, ..Default::default() }).normalize_amplitudes();
sourcepub fn iter(&self) -> WaveformIterator<'_, BitDepth> ⓘ
pub fn iter(&self) -> WaveformIterator<'_, BitDepth> ⓘ
An infinite iterator for the superposition of all underlying waveform components.
The iterator will produce a quantization of the superposition of waveform components at the waveform sampling frequency.
Examples
use std::f32::consts::PI;
use std::vec::Vec;
use waver::{Waveform, Wave};
let mut wf = Waveform::<i16>::new(44100.0);
let res: Vec<i16> = wf.superpose(Wave { frequency: 4000.0, phase: PI / 2.0, ..Default::default() })
.iter().take(10).collect();