Instrument

Struct Instrument 

Source
pub struct Instrument<M, NFG>
where NFG: NoteFreqGenerator,
{ pub mode: M, pub voices: Vec<Voice<NFG::NoteFreq>>, pub detune: f32, pub note_freq_gen: NFG, pub attack_ms: Ms, pub release_ms: Ms, }
Expand description

A performable Instrument type that converts Note events into a sequence of voices, each with their own unique amplitude and frequency per frame. This is useful for driving the playback of instrument types like synthesisers or samplers.

Instrument handles the following logic:

  • Playback mode: Legato, Retrigger or Polyphonic.
  • Note on detuning.
  • Note on “interoplation” / frequency generation: Legato or Constant.
  • Sustained note warping:
  • Multi-channel audio processing.

Fields§

§mode: M

The mode of note playback.

§voices: Vec<Voice<NFG::NoteFreq>>

The stack of Voices used by the Instrument.

  • If the Instrument is in Mono mode, it will play one voice at a time.
  • If the Instrument is in Poly mode, it will play all voices at once.
§detune: f32

The amount each voice’s note_on should be detuned.

§note_freq_gen: NFG

Note on “interoplation” / frequency generation: Legato or Constant.

§attack_ms: Ms

A duration in frames over which the amplitude of each note will fade in after note_on.

§release_ms: Ms

A duration in frames over which the amplitude of each note will fade out after note_off.

Implementations§

Source§

impl<M, NFG> Instrument<M, NFG>
where NFG: NoteFreqGenerator,

Source

pub fn new(mode: M, note_freq_gen: NFG) -> Self

Construct a new Instrument of the given mode using the given note frequency generator.

Source

pub fn num_voices(self, num_voices: usize) -> Self

Build the Instrument with the given number of voices.

Source

pub fn fade<A, R>(self, attack: A, release: R) -> Self
where A: Into<Ms>, R: Into<Ms>,

Set the note fades for the Instrument in frames.

Source

pub fn attack<A>(self, attack: A) -> Self
where A: Into<Ms>,

Set the attack.

Source

pub fn release<R>(self, release: R) -> Self
where R: Into<Ms>,

Set the release.

Source

pub fn detune(self, detune: f32) -> Self

Set the Instrument’s note_on detune amount.

Source

pub fn note_freq_generator(self, generator: NFG) -> Self

Convert Self into a new Instrument with the given NoteFreqGenerator.

Generates new NoteFreqs for each of the currently active Voices.

Source

pub fn set_num_voices(&mut self, num_voices: usize)

Set the number of voices that the Instrument shall use.

Source

pub fn is_active(&self) -> bool

Return whether or not there are any currently active voices.

Source

pub fn note_on<T>(&mut self, note_hz: T, note_vel: NoteVelocity)
where M: Mode, T: Into<Hz>,

Begin playback of a note. Instrument will try to use a free Voice to do this.

If no Voices are free, the one playing the oldest note will be chosen to play the new note instead.

Source

pub fn note_off<T>(&mut self, note_hz: T)
where M: Mode, T: Into<Hz>,

Stop playback of the note that was triggered with the matching frequency.

Source

pub fn stop(&mut self)
where M: Mode,

Stop playback and clear the current notes.

Source

pub fn frames(&mut self, sample_hz: SampleHz) -> Frames<'_, NFG::NoteFreq>

Provides an Iterator like type that consecutively yields the next_frame_per_voice for the given sample_hz.

Source

pub fn frame_per_voice( &mut self, sample_hz: SampleHz, ) -> FramePerVoice<'_, NFG::NoteFreq>

Produces an Iterator that yields the amplitude and frequency of each voice for the next frame.

Trait Implementations§

Source§

impl<M: Clone, NFG> Clone for Instrument<M, NFG>
where NFG: NoteFreqGenerator + Clone, NFG::NoteFreq: Clone,

Source§

fn clone(&self) -> Instrument<M, NFG>

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<M: Debug, NFG> Debug for Instrument<M, NFG>
where NFG: NoteFreqGenerator + Debug, NFG::NoteFreq: Debug,

Source§

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

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

impl<M: PartialEq, NFG> PartialEq for Instrument<M, NFG>

Source§

fn eq(&self, other: &Instrument<M, NFG>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<M, NFG> StructuralPartialEq for Instrument<M, NFG>
where NFG: NoteFreqGenerator,

Auto Trait Implementations§

§

impl<M, NFG> Freeze for Instrument<M, NFG>
where M: Freeze, NFG: Freeze,

§

impl<M, NFG> RefUnwindSafe for Instrument<M, NFG>

§

impl<M, NFG> Send for Instrument<M, NFG>
where M: Send, NFG: Send, <NFG as NoteFreqGenerator>::NoteFreq: Send,

§

impl<M, NFG> Sync for Instrument<M, NFG>
where M: Sync, NFG: Sync, <NFG as NoteFreqGenerator>::NoteFreq: Sync,

§

impl<M, NFG> Unpin for Instrument<M, NFG>
where M: Unpin, NFG: Unpin, <NFG as NoteFreqGenerator>::NoteFreq: Unpin,

§

impl<M, NFG> UnwindSafe for Instrument<M, NFG>

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.