#buffer #audio #dsp

audio

A library for working with audio buffers

2 releases

0.2.0-alpha.3 Apr 8, 2021
0.1.0 May 21, 2020
0.0.14 May 21, 2020
0.0.2 Nov 21, 2014

#49 in Audio

Download history 3/week @ 2020-12-23 2/week @ 2020-12-30 11/week @ 2021-01-06 2/week @ 2021-01-13 9/week @ 2021-01-20 4/week @ 2021-01-27 17/week @ 2021-02-03 68/week @ 2021-02-10 41/week @ 2021-02-17 1/week @ 2021-02-24 11/week @ 2021-03-03 1/week @ 2021-03-10 10/week @ 2021-03-17 19/week @ 2021-03-24 13/week @ 2021-03-31 59/week @ 2021-04-07

66 downloads per month
Used in 2 crates

MIT/Apache

255KB
4.5K SLoC

audio

Documentation Crates Actions Status

A library for working with audio buffers

The buffer is constructed similarly to a Vec<Vec<T>>, except the interior vector has a fixed size. And the buffer makes no attempt to clear data which is freed when using functions such as Dynamic::resize.

Formats and topologies

The following are the three canonical audio formats which are supported by this library:

  • dynamic - where each channel is stored in its own heap-allocated buffer.
  • interleaved - where each channel is interleaved, like 0:0, 1:0, 1:0, 1:1.
  • sequential - where each channel is stored in a linear buffer, one after another. Like 0:0, 0:1, 1:0, 1:0.

These all implement the Channels and ChannelsMut traits, allowing library authors to abstract over any one specific format. The exact channel and frame count of a buffer is known as its topology.

use audio::ChannelsMut as _;

let mut dynamic = audio::dynamic![[0i16; 4]; 2];
let mut interleaved = audio::interleaved![[0i16; 4]; 2];
let mut sequential = audio::sequential![[0i16; 4]; 2];

dynamic.channel_mut(0).copy_from_iter(0i16..);
interleaved.channel_mut(0).copy_from_iter(0i16..);
sequential.channel_mut(0).copy_from_iter(0i16..);

We also support wrapping external buffers so that they can interoperate like other audio buffers.

Example: play-mp3

Play an mp3 file with minimp3-rs, cpal, and rubato for resampling.

This example can handle with any channel and sample rate configuration.

cargo run --release --package audio-examples --bin play-mp3 -- path/to/file.mp3

Examples

use rand::Rng as _;

let mut buffer = audio::Dynamic::<f32>::new();

buffer.resize_channels(2);
buffer.resize(2048);

/// Fill both channels with random noise.
let mut rng = rand::thread_rng();
rng.fill(&mut buffer[0]);
rng.fill(&mut buffer[1]);

For convenience we also provide several macros for constructing various forms of dynamic audio buffers. These should mostly be used for testing.

let mut buf = audio::Dynamic::<f32>::with_topology(4, 8);

for channel in &mut buf {
    for f in channel {
        *f = 2.0;
    }
}

assert_eq! {
    buf,
    audio::dynamic![[2.0; 8]; 4],
};

assert_eq! {
    buf,
    audio::dynamic![[2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0]; 4],
};

License: MIT/Apache-2.0

Dependencies