Struct usfx::Sample

source ·
pub struct Sample { /* private fields */ }
Expand description

Audio sample that procedurally generates it’s sound.

Plug this into the Mixer object to play the sound.

// Generate a sine wave at 2khz
let mut sine_wave = usfx::Sample::default();
sine_wave.osc_frequency(2000);
sine_wave.osc_type(usfx::OscillatorType::Sine);

// Add it to the mixer
let mut mixer = usfx::Mixer::default();
mixer.play(sine_wave);

// Plug it into a audio library, see the examples for a cpal & SDL2 implementation
// ...
// Call the generator to get a buffer for the audio library
mixer.generate(&mut buffer);

Implementations§

source§

impl Sample

source

pub fn volume(&mut self, volume: f32) -> &mut Self

Set the volume which is a multiplier of the result.

A range from 0.0-1.0 will result in proper behavior, but you can experiment with other values.

source

pub fn osc_frequency(&mut self, frequency: usize) -> &mut Self

Set the frequency of the oscillator in hertz.

When using the noise oscillator type this will be the seed. A range from 1-20000 is allowed.

source

pub fn osc_type(&mut self, oscillator: OscillatorType) -> &mut Self

Set the type of the oscillator.

See the OscillatorType enum for supported wave types.

source

pub fn osc_duty_cycle(&mut self, duty_cycle: DutyCycle) -> &mut Self

Set the length of the pulse, this only applies when you use a square wave.

Changing of the duty cycle mainly results in a different timbre. A range from 0.0-1.0 will have results, other values won’t do anything.

source

pub fn env_attack(&mut self, attack: f32) -> &mut Self

Set the time until the first envelope slope reaches it’s maximum height.

A range from 0.0-1.0 will result in proper behavior, but you can experiment with other values.

source

pub fn env_decay(&mut self, decay: f32) -> &mut Self

Set the time it takes from the maximum height to go into the main plateau.

A range from 0.0-1.0 will result in proper behavior, but you can experiment with other values.

source

pub fn env_sustain(&mut self, sustain: f32) -> &mut Self

Set the height of the main plateau.

A range from 0.0-1.0 will result in proper behavior, but you can experiment with other values.

source

pub fn env_release(&mut self, release: f32) -> &mut Self

Set the time it takes to go from the end of the plateau to zero.

A range from 0.0-1.0 will result in proper behavior, but you can experiment with other values.

source

pub fn dis_crunch(&mut self, crunch: f32) -> &mut Self

Overdrive that adds hard clipping.

A range from 0.0-1.0 will result in proper behavior, but you can experiment with other values.

source

pub fn dis_drive(&mut self, drive: f32) -> &mut Self

Overdrive with soft clipping.

A range from 0.0-1.0 will result in proper behavior, but you can experiment with other values.

Trait Implementations§

source§

impl Clone for Sample

source§

fn clone(&self) -> Sample

Returns a copy 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 Sample

source§

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

Formats the value using the given formatter. Read more
source§

impl Default for Sample

source§

fn default() -> Self

The default is a sinewave of 441 hz.

source§

impl Copy for Sample

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> 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,

§

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>,

§

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>,

§

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.