Track

Enum Track 

Source
pub enum Track {
    Midi(MidiTrack),
    Audio(AudioTrack),
}
Expand description

Wrapper type allowing processors to operate on MIDI or audio data.

Variants§

Implementations§

Source§

impl Track

Source

pub fn from_midi<P: AsRef<Path>>(path: P) -> Result<Self>

Load a MIDI track from a lightweight text representation where each line contains note velocity.

Source

pub fn from_audio(samples: Vec<f32>, sample_rate: u32) -> Self

Construct an audio track from samples and a sample rate.

Examples found in repository?
examples/audio_gain.rs (line 5)
2fn main() -> humster::Result<()> {
3    use humster::prelude::*;
4
5    let mut track = Track::from_audio(vec![0.1, -0.3, 0.5], 48_000);
6
7    struct Gain(f32);
8
9    impl Processor for Gain {
10        fn process_audio(&self, track: &mut AudioTrack) {
11            for sample in track.samples.iter_mut() {
12                *sample *= self.0;
13            }
14        }
15    }
16
17    let gain = Gain(1.5);
18    track.apply(&gain);
19
20    if let Some(audio) = track.as_audio() {
21        println!("Sample rate: {}", audio.sample_rate);
22        for (idx, sample) in audio.samples.iter().enumerate() {
23            println!("Sample #{idx}: {sample:.3}");
24        }
25    }
26
27    Ok(())
28}
Source

pub fn apply<P: Processor>(&mut self, processor: &P)

Apply a processor to the underlying track representation.

Examples found in repository?
examples/audio_gain.rs (line 18)
2fn main() -> humster::Result<()> {
3    use humster::prelude::*;
4
5    let mut track = Track::from_audio(vec![0.1, -0.3, 0.5], 48_000);
6
7    struct Gain(f32);
8
9    impl Processor for Gain {
10        fn process_audio(&self, track: &mut AudioTrack) {
11            for sample in track.samples.iter_mut() {
12                *sample *= self.0;
13            }
14        }
15    }
16
17    let gain = Gain(1.5);
18    track.apply(&gain);
19
20    if let Some(audio) = track.as_audio() {
21        println!("Sample rate: {}", audio.sample_rate);
22        for (idx, sample) in audio.samples.iter().enumerate() {
23            println!("Sample #{idx}: {sample:.3}");
24        }
25    }
26
27    Ok(())
28}
More examples
Hide additional examples
examples/midi_input.rs (line 22)
2fn main() -> humster::Result<()> {
3    use humster::prelude::*;
4
5    let mut track = Track::Midi(MidiTrack::new(vec![
6        MidiEvent::new(60, 80),
7        MidiEvent::new(64, 90),
8    ]));
9
10    struct VelocityBoost(f32);
11
12    impl Processor for VelocityBoost {
13        fn process_midi(&self, track: &mut MidiTrack) {
14            for event in track.events.iter_mut() {
15                let boosted = (event.velocity as f32 * self.0).round() as u32;
16                event.velocity = boosted.min(127) as u8;
17            }
18        }
19    }
20
21    let boost = VelocityBoost(1.25);
22    track.apply(&boost);
23
24    if let Some(midi) = track.as_midi() {
25        for (idx, event) in midi.events().iter().enumerate() {
26            println!("Event #{idx}: note {}, velocity {}", event.note, event.velocity);
27        }
28    }
29
30    Ok(())
31}
Source

pub fn export<P: AsRef<Path>>(&self, path: P) -> Result<()>

Export a track to the lightweight text formats understood by from_midi and from_audio.

Source

pub fn as_midi(&self) -> Option<&MidiTrack>

Returns the contained MIDI track if present.

Examples found in repository?
examples/midi_input.rs (line 24)
2fn main() -> humster::Result<()> {
3    use humster::prelude::*;
4
5    let mut track = Track::Midi(MidiTrack::new(vec![
6        MidiEvent::new(60, 80),
7        MidiEvent::new(64, 90),
8    ]));
9
10    struct VelocityBoost(f32);
11
12    impl Processor for VelocityBoost {
13        fn process_midi(&self, track: &mut MidiTrack) {
14            for event in track.events.iter_mut() {
15                let boosted = (event.velocity as f32 * self.0).round() as u32;
16                event.velocity = boosted.min(127) as u8;
17            }
18        }
19    }
20
21    let boost = VelocityBoost(1.25);
22    track.apply(&boost);
23
24    if let Some(midi) = track.as_midi() {
25        for (idx, event) in midi.events().iter().enumerate() {
26            println!("Event #{idx}: note {}, velocity {}", event.note, event.velocity);
27        }
28    }
29
30    Ok(())
31}
Source

pub fn as_midi_mut(&mut self) -> Option<&mut MidiTrack>

Returns the contained MIDI track mutably if present.

Source

pub fn as_audio(&self) -> Option<&AudioTrack>

Returns the contained audio track if present.

Examples found in repository?
examples/audio_gain.rs (line 20)
2fn main() -> humster::Result<()> {
3    use humster::prelude::*;
4
5    let mut track = Track::from_audio(vec![0.1, -0.3, 0.5], 48_000);
6
7    struct Gain(f32);
8
9    impl Processor for Gain {
10        fn process_audio(&self, track: &mut AudioTrack) {
11            for sample in track.samples.iter_mut() {
12                *sample *= self.0;
13            }
14        }
15    }
16
17    let gain = Gain(1.5);
18    track.apply(&gain);
19
20    if let Some(audio) = track.as_audio() {
21        println!("Sample rate: {}", audio.sample_rate);
22        for (idx, sample) in audio.samples.iter().enumerate() {
23            println!("Sample #{idx}: {sample:.3}");
24        }
25    }
26
27    Ok(())
28}
Source

pub fn as_audio_mut(&mut self) -> Option<&mut AudioTrack>

Returns the contained audio track mutably if present.

Trait Implementations§

Source§

impl Clone for Track

Source§

fn clone(&self) -> Track

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 Track

Source§

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

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

impl PartialEq for Track

Source§

fn eq(&self, other: &Track) -> 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 StructuralPartialEq for Track

Auto Trait Implementations§

§

impl Freeze for Track

§

impl RefUnwindSafe for Track

§

impl Send for Track

§

impl Sync for Track

§

impl Unpin for Track

§

impl UnwindSafe for Track

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.