| Crates.io | audioadapter-buffers |
| lib.rs | audioadapter-buffers |
| version | 2.0.0 |
| created_at | 2025-10-24 19:58:38.353856+00 |
| updated_at | 2025-12-08 21:21:26.088755+00 |
| description | A selection of wrappers and buffers for audio data |
| homepage | |
| repository | https://github.com/HEnquist/audioadapter-buffers-rs |
| max_upload_size | |
| id | 1899216 |
| size | 132,493 |
This crate is part of the audioadapter family.
The audioadapter family consists of three crates:
audioadapter traits.Read and Write traits.This crate provides a selection of wrappers and buffers
that implement the audioadapter traits.
The [owned] and [direct] modules contain implementations that pass the sample values on unchanged. These are used when the sample data is already in a useable format, and only the data layout needs to be handled.
use audioadapter::Adapter;
use audioadapter_buffers::direct::InterleavedSlice;
// Make a vector with some dummy data.
// 2 channels * 3 frames => 6 samples
let data: Vec<i16> = vec![1, 2, 3, 4, 5, 6];
// Wrap it with an interleaved adapter
let adapter = InterleavedSlice::new(&data[..], 2, 3).unwrap();
// Loop over all samples and print their values
for channel in 0..adapter.channels() {
for frame in 0..adapter.frames() {
let value = adapter.read_sample(channel, frame).unwrap();
println!("Channel: {}, frame: {}, value {}", channel, frame, value);
}
}
Audio is often exchanged as buffers of raw bytes, and it is up to each application to treat those bytes as samples of the correct format. The [number_to_float] module is desgined to help with this.
This shows how to read 24-bit integer format from raw bytes while converting them to f32:
use audioadapter_buffers::number_to_float::InterleavedNumbers;
use audioadapter::Adapter;
use audioadapter_sample::sample::I24_LE;
// make a vector with some dummy data.
// 2 channels * 3 frames * 3 bytes per sample => 18 bytes
let data: Vec<u8> = vec![
1, 1, 1, //frame 1, left
2, 2, 2, //frame 1, right
3, 3, 3, //frame 2, left
4, 4, 4, //frame 2, right
5, 5, 5, //frame 3, left
6, 6, 6 //frame 3, right
];
// wrap the data
let buffer = InterleavedNumbers::<&[I24_LE], f32>::new_from_bytes(&data, 2, 3).unwrap();
// Loop over all samples and print their values
for channel in 0..buffer.channels() {
for frame in 0..buffer.frames() {
let value = buffer.read_sample(channel, frame).unwrap();
println!(
"Channel: {}, frame: {}, value: {}",
channel, frame, value
);
}
}
Note that the example uses I24_LE, which means 24-bit samples
stored as 3 bytes in little-endian order without padding.
24-bit samples are also commonly stored with a padding byte, so that each sample takes up four bytes.
This is handled by selecting I24_4RJ_LE or I24_4LJ_LE as the format.
This crate can be used in no_std environments if the std Cargo feature is disabled.
This feature is enabled by default.
Disabling it also disables all functionality that depends on the standard library,
such as the buffers in the [owned] module since they are based on [std::vec::Vec].