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
impl SamplePlayer
Sourcepub fn new(samples: Vec<f32>, channels: usize, sample_rate: u32) -> Self
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 datasample_rate- Sample rate of the audio data in Hz
Playback starts immediately. Use PlayerMessage::Pause to start paused.
Sourcepub fn set_looping(&mut self, looping: bool)
pub fn set_looping(&mut self, looping: bool)
Enable or disable looping.
When enabled, playback restarts from the beginning when it reaches the end.
Sourcepub fn sample_rate(&self) -> u32
pub fn sample_rate(&self) -> u32
Get the source sample rate in Hz.
Sourcepub fn duration_secs(&self) -> f64
pub fn duration_secs(&self) -> f64
Get the total duration in seconds.
Sourcepub fn position_secs(&self) -> f64
pub fn position_secs(&self) -> f64
Get the current playback position in seconds.
Sourcepub fn is_playing(&self) -> bool
pub fn is_playing(&self) -> bool
Check if playback is currently active.