Struct PolyBlepOscillator

Source
pub struct PolyBlepOscillator { /* private fields */ }
Expand description

This is a band-limited oscillator based on the PolyBlep technique.

NOTE: You need to call crate::init_cos_tab.

Here is a quick example on how to use it:

 use synfx_dsp::{PolyBlepOscillator, rand_01, init_cos_tab};
 init_cos_tab();

 // Randomize the initial phase to make cancellation on summing less
 // likely:
 let mut osc =
     PolyBlepOscillator::new(rand_01() * 0.25);


 let freq   = 440.0; // Hz
 let israte = 1.0 / 44100.0; // Seconds per Sample
 let pw     = 0.2;   // Pulse-Width for the next_pulse()
 let waveform = 0;   // 0 being pulse in this example, 1 being sawtooth

 let mut block_of_samples = [0.0; 128];
 // in your process function:
 for output_sample in block_of_samples.iter_mut() {
    *output_sample =
        if waveform == 1 {
             osc.next_saw(freq, israte)
        } else {
             osc.next_pulse(freq, israte, pw)
        }
 }

Implementations§

Source§

impl PolyBlepOscillator

Source

pub fn new(init_phase: f32) -> Self

Create a new instance of PolyBlepOscillator.

  • init_phase - Initial phase of the oscillator. Range of this parameter is from 0.0 to 1.0. Passing a random value is advised for preventing phase cancellation when summing multiple oscillators.
 use synfx_dsp::*;

 let mut osc = PolyBlepOscillator::new(rand_01() * 0.25);
Source

pub fn reset(&mut self)

Reset the internal state of the oscillator as if you just called PolyBlepOscillator::new.

Source

pub fn next_sin(&mut self, freq: f32, israte: f32) -> f32

Creates the next sample of a sine wave.

  • freq - The frequency in Hz.
  • israte - The inverse sampling rate, or seconds per sample as in eg. 1.0 / 44100.0.
 use synfx_dsp::*;

 let mut osc = PolyBlepOscillator::new(rand_01() * 0.25);

 let freq   = 440.0; // Hz
 let israte = 1.0 / 44100.0; // Seconds per Sample

 // ...
 let sample = osc.next_sin(freq, israte);
 // ...
Source

pub fn next_tri(&mut self, freq: f32, israte: f32) -> f32

Creates the next sample of a triangle wave. Please note that the bandlimited waveform needs a few initial samples to swing in.

  • freq - The frequency in Hz.
  • israte - The inverse sampling rate, or seconds per sample as in eg. 1.0 / 44100.0.
 use synfx_dsp::*;

 let mut osc = PolyBlepOscillator::new(rand_01() * 0.25);

 let freq   = 440.0; // Hz
 let israte = 1.0 / 44100.0; // Seconds per Sample

 // ...
 let sample = osc.next_tri(freq, israte);
 // ...
Source

pub fn next_saw(&mut self, freq: f32, israte: f32) -> f32

Creates the next sample of a sawtooth wave.

  • freq - The frequency in Hz.
  • israte - The inverse sampling rate, or seconds per sample as in eg. 1.0 / 44100.0.
 use synfx_dsp::*;

 let mut osc = PolyBlepOscillator::new(rand_01() * 0.25);

 let freq   = 440.0; // Hz
 let israte = 1.0 / 44100.0; // Seconds per Sample

 // ...
 let sample = osc.next_saw(freq, israte);
 // ...
Source

pub fn next_pulse(&mut self, freq: f32, israte: f32, pw: f32) -> f32

Creates the next sample of a pulse wave. In comparison to PolyBlepOscillator::next_pulse_no_dc this version is DC compensated, so that you may add multiple different pulse oscillators for a unison effect without too big DC issues.

  • freq - The frequency in Hz.
  • israte - The inverse sampling rate, or seconds per sample as in eg. 1.0 / 44100.0.
  • pw - The pulse width. Use the value 0.0 for a square wave.
 use synfx_dsp::*;

 let mut osc = PolyBlepOscillator::new(rand_01() * 0.25);

 let freq   = 440.0; // Hz
 let israte = 1.0 / 44100.0; // Seconds per Sample
 let pw     = 0.0; // 0.0 is a square wave.

 // ...
 let sample = osc.next_pulse(freq, israte, pw);
 // ...
Source

pub fn next_pulse_no_dc(&mut self, freq: f32, israte: f32, pw: f32) -> f32

Creates the next sample of a pulse wave. In comparison to PolyBlepOscillator::next_pulse this version is not DC compensated. So be careful when adding multiple of this or generally using it in an audio context.

  • freq - The frequency in Hz.
  • israte - The inverse sampling rate, or seconds per sample as in eg. 1.0 / 44100.0.
  • pw - The pulse width. Use the value 0.0 for a square wave.
 use synfx_dsp::*;

 let mut osc = PolyBlepOscillator::new(rand_01() * 0.25);

 let freq   = 440.0; // Hz
 let israte = 1.0 / 44100.0; // Seconds per Sample
 let pw     = 0.0; // 0.0 is a square wave.

 // ...
 let sample = osc.next_pulse_no_dc(freq, israte, pw);
 // ...

Trait Implementations§

Source§

impl Clone for PolyBlepOscillator

Source§

fn clone(&self) -> PolyBlepOscillator

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PolyBlepOscillator

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.