SamplePlayer

Struct SamplePlayer 

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

Plays pre-decoded audio samples.

This node plays audio that’s already been decoded into memory as f32 samples. It reports its native sample rate, so Klingt will automatically handle resampling if the output device runs at a different rate.

§Example

use symphonium::SymphoniumLoader;
use klingt::{Klingt, nodes::SamplePlayer};

let decoded = SymphoniumLoader::new().load_f32("music.ogg", None)?;
let mut player = SamplePlayer::new(
    decoded.as_interleaved(),
    decoded.channels(),
    decoded.sample_rate,
);
player.set_looping(true);

let mut klingt = Klingt::default_output()?;
let handle = klingt.add(player);
klingt.output(&handle);

§Large Files

For very large files, loading everything into memory may not be ideal. Consider implementing a streaming player using a ring buffer fed by a decoder thread.

Implementations§

Source§

impl SamplePlayer

Source

pub fn new(samples: Vec<f32>, channels: usize, sample_rate: u32) -> Self

Create a player from interleaved audio samples.

§Arguments
  • samples - Interleaved audio data (L, R, L, R, … for stereo)
  • channels - Number of channels in the audio data
  • sample_rate - Sample rate of the audio data in Hz

Playback starts immediately. Use PlayerMessage::Pause to start paused.

Source

pub fn set_looping(&mut self, looping: bool)

Enable or disable looping.

When enabled, playback restarts from the beginning when it reaches the end.

Source

pub fn sample_rate(&self) -> u32

Get the source sample rate in Hz.

Source

pub fn channels(&self) -> usize

Get the number of audio channels.

Source

pub fn duration_secs(&self) -> f64

Get the total duration in seconds.

Source

pub fn position_secs(&self) -> f64

Get the current playback position in seconds.

Source

pub fn is_playing(&self) -> bool

Check if playback is currently active.

Trait Implementations§

Source§

impl AudioNode for SamplePlayer

Source§

type Message = PlayerMessage

Message type for parameter updates. Read more
Source§

fn process( &mut self, _ctx: &ProcessContext, messages: impl Iterator<Item = PlayerMessage>, _inputs: &[Input], outputs: &mut [Buffer], )

Process one block of audio. Read more
Source§

fn num_inputs(&self) -> usize

Number of audio input channels (0 for sources).
Source§

fn num_outputs(&self) -> usize

Number of audio output channels.
Source§

fn native_sample_rate(&self) -> Option<u32>

Native sample rate of this node, if it has one. 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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<S> FromSample<S> for S

Source§

fn from_sample_(s: S) -> S

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, U> ToSample<U> for T
where U: FromSample<T>,

Source§

fn to_sample_(self) -> U

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.
Source§

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,