Expand description
Chunk-based audio streaming for Rust.
Two traits drive the whole crate:
AudioStream— pull source.fill_chunkfills a pre-allocatedAudioSamplesbuffer and returns the frame count, orNonewhen exhausted.AudioSink— push destination.write_chunkaccepts the same buffer;finalizemust be called when writing is complete.
§Sources and sinks
| Type | Direction | Feature |
|---|---|---|
WavFileStream | source | wav |
WavFileSink | sink | wav |
FlacFileStream | source | flac |
DeviceCapture | source | device |
DevicePlayback | sink | device |
RodioSource | source adapter | rodio |
AsyncAudioStream | stream adapter | async |
No features are enabled by default.
§Quick start
Read input.wav chunk by chunk and write it to output.wav, using
pipeline::run to drive the loop. Insert per-chunk processing between
source and sink by writing a manual loop instead — see the manual_loop example.
use std::num::{NonZeroU32, NonZeroUsize};
use audio_samples::AudioSamples;
use audio_samples_streaming::{WavFileStream, WavFileSink, pipeline};
let mut source = WavFileStream::<_, f32>::open("input.wav").unwrap();
let mut sink = WavFileSink::<_, f32>::create("output.wav",
source.num_channels(), source.sample_rate()).unwrap();
let mut buffer = AudioSamples::<f32>::zeros_multi_channel(
NonZeroU32::new(source.num_channels() as u32).unwrap(),
NonZeroUsize::new(1024).unwrap(),
NonZeroU32::new(source.sample_rate()).unwrap(),
);
pipeline::run(&mut source, &mut sink, &mut buffer).unwrap();
sink.finalize().unwrap();§Device I/O
Enable the device feature for real-time capture and playback. Both types use a
lock-free ring buffer and condvar notification — no spin-sleep, no hot-path allocation.
Pass a Duration to request a specific callback period; None
uses the driver default.
use std::time::Duration;
use audio_samples_streaming::{DeviceCapture, DevicePlayback};
let capture = DeviceCapture::from_default_input(Some(Duration::from_millis(5))).unwrap();
let playback = DevicePlayback::from_default_output(Some(Duration::from_millis(5))).unwrap();
println!("Capture: {} Hz {} ch", capture.sample_rate(), capture.channels());
println!("Playback: {} Hz {} ch", playback.sample_rate(), playback.channels());Re-exports§
pub use error::StreamingError;pub use error::StreamingResult;pub use traits::AudioSink;pub use traits::AudioStream;pub use sinks::WavFileSink;pub use sources::FlacFileStream;pub use sources::WavFileStream;pub use sinks::DevicePlayback;pub use sources::DeviceCapture;pub use interop::AsyncAudioStream;pub use interop::RodioSource;