pub struct AudioFrame<'a> { /* private fields */ }Expand description
A frame of audio samples with associated sample rate.
AudioFrame is the standard audio input type across the WaveKat ecosystem.
It stores samples as f32 normalized to [-1.0, 1.0], regardless of the
original input format.
Construct via AudioFrame::new, which accepts both &[f32] (zero-copy)
and &[i16] (converts once) through the IntoSamples trait.
§Examples
use wavekat_core::AudioFrame;
// f32 input — zero-copy via Cow::Borrowed
let samples = [0.1f32, -0.2, 0.3];
let frame = AudioFrame::new(&samples, 16000);
assert_eq!(frame.samples(), &[0.1, -0.2, 0.3]);
// i16 input — normalized to f32 [-1.0, 1.0]
let samples = [i16::MAX, 0, i16::MIN];
let frame = AudioFrame::new(&samples, 16000);
assert!((frame.samples()[0] - 1.0).abs() < 0.001);Implementations§
Source§impl<'a> AudioFrame<'a>
impl<'a> AudioFrame<'a>
Sourcepub fn new(samples: impl IntoSamples<'a>, sample_rate: u32) -> Self
pub fn new(samples: impl IntoSamples<'a>, sample_rate: u32) -> Self
Create a new audio frame from any supported sample type.
Accepts &[f32] (zero-copy) or &[i16] (converts to normalized f32).
Sourcepub fn sample_rate(&self) -> u32
pub fn sample_rate(&self) -> u32
Sample rate in Hz (e.g. 16000).
Sourcepub fn duration_secs(&self) -> f64
pub fn duration_secs(&self) -> f64
Duration of this frame in seconds.
Sourcepub fn into_owned(self) -> AudioFrame<'static>
pub fn into_owned(self) -> AudioFrame<'static>
Consume the frame and return the owned samples.
Source§impl AudioFrame<'static>
impl AudioFrame<'static>
Sourcepub fn from_vec(samples: Vec<f32>, sample_rate: u32) -> Self
pub fn from_vec(samples: Vec<f32>, sample_rate: u32) -> Self
Construct an owned frame directly from a Vec<f32>.
Zero-copy — wraps the vec as Cow::Owned without cloning.
Intended for audio producers (TTS, ASR) that generate owned data.
§Example
use wavekat_core::AudioFrame;
let samples = vec![0.5f32, -0.5, 0.3];
let frame = AudioFrame::from_vec(samples, 24000);
assert_eq!(frame.sample_rate(), 24000);
assert_eq!(frame.len(), 3);Source§impl AudioFrame<'_>
impl AudioFrame<'_>
Sourcepub fn resample(
&self,
target_rate: u32,
) -> Result<AudioFrame<'static>, CoreError>
pub fn resample( &self, target_rate: u32, ) -> Result<AudioFrame<'static>, CoreError>
Resample this frame to a different sample rate.
Returns a new owned AudioFrame at target_rate. If the frame is
already at the target rate, returns a clone without touching the
resampler.
Uses high-quality sinc interpolation via rubato.
§Errors
Returns [CoreError::Audio] if the resampler cannot be constructed
(e.g. zero sample rate) or if processing fails.
§Example
use wavekat_core::AudioFrame;
let frame = AudioFrame::from_vec(vec![0.0f32; 4410], 44100);
let resampled = frame.resample(16000).unwrap();
assert_eq!(resampled.sample_rate(), 16000);Source§impl AudioFrame<'_>
impl AudioFrame<'_>
Sourcepub fn write_wav(&self, path: impl AsRef<Path>) -> Result<(), CoreError>
pub fn write_wav(&self, path: impl AsRef<Path>) -> Result<(), CoreError>
Write this frame to a WAV file at path.
Always writes mono f32 PCM at the frame’s native sample rate.
§Example
use wavekat_core::AudioFrame;
let frame = AudioFrame::from_vec(vec![0.0f32; 16000], 16000);
frame.write_wav("output.wav").unwrap();Source§impl AudioFrame<'static>
impl AudioFrame<'static>
Sourcepub fn from_wav(path: impl AsRef<Path>) -> Result<Self, CoreError>
pub fn from_wav(path: impl AsRef<Path>) -> Result<Self, CoreError>
Read a mono WAV file and return an owned AudioFrame.
Accepts both f32 and i16 WAV files. i16 samples are normalised to
[-1.0, 1.0] (divided by 32768).
§Example
use wavekat_core::AudioFrame;
let frame = AudioFrame::from_wav("input.wav").unwrap();
println!("{} Hz, {} samples", frame.sample_rate(), frame.len());Trait Implementations§
Source§impl<'a> Clone for AudioFrame<'a>
impl<'a> Clone for AudioFrame<'a>
Source§fn clone(&self) -> AudioFrame<'a>
fn clone(&self) -> AudioFrame<'a>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more