sample
A crate providing the fundamentals for working with PCM (pulse-code modulation)
DSP (digital signal processing). In other words, sample
provides a suite of
low-level, high-performance tools including types, traits and functions for
working with digital audio signals.
The sample
crate requires no dynamic allocations1 and has no
dependencies. The goal is to design a library akin to the std, but for audio
DSP; keeping the focus on portable and fast fundamentals.
1: Besides the Signal::bus
method, which is only necessary when
converting a Signal
tree into a directed acyclic graph.
Find the API documentation here.
Features
Use the Sample trait to convert between and remain generic over any bit-depth in an optimal, performance-sensitive manner. Implementations are provided for all signed integer, unsigned integer and floating point primitive types along with some custom types including 11, 20, 24 and 48-bit signed and unsigned unpacked integers. For example:
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Use the Frame trait to remain generic over the number of channels at a discrete moment in time. Implementations are provided for all fixed-size arrays up to 32 elements in length.
let foo = ;
let bar = foo.scale_amp;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
let foo = ;
let bar: = foo.map;
assert_eq!;
Use the Signal trait for working with infinite-iterator-like types that
yield Frame
s. Signal provides methods for adding, scaling, offsetting,
multiplying, clipping, generating, monitoring and buffering streams of Frame
s.
Working with Signals allows for easy, readable creation of rich and complex
DSP graphs with a simple and familiar API.
// Clip to an amplitude of 0.9.
let frames = ;
let clipped: = from_iter.clip_amp.take.collect;
assert_eq!;
// Add `a` with `b` and yield the result.
let a = ;
let b = ;
let a_signal = from_iter;
let b_signal = from_iter;
let added: = a_signal.add_amp.take.collect;
assert_eq!;
// Scale the playback rate by `0.5`.
let foo = ;
let mut source = from_iter;
let interp = from_source;
let frames: = source.scale_hz.take.collect;
assert_eq!;
// Convert a signal to its RMS.
let signal = rate.const_hz.sine;;
let ring_buffer = from;
let mut rms_signal = signal.rms;
The signal module also provides a series of Signal source types, including:
FromIterator
FromInterleavedSamplesIterator
Equilibrium
(silent signal)Phase
Sine
Saw
Square
Noise
NoiseSimplex
Gen
(generate frames from a Fn() -> F)GenMut
(generate frames from a FnMut() -> F)
Use the slice module functions for processing chunks of Frame
s.
Conversion functions are provided for safely converting between slices of
interleaved Sample
s and slices of Frame
s without requiring any allocation.
For example:
let frames = &;
let samples = to_sample_slice;
assert_eq!;
let samples = &;
let frames = to_frame_slice;
assert_eq!;
let samples = &;
let frames = to_frame_slice;
assert_eq!;
The conv module provides pure functions and traits for more specific conversions. A function is provided for converting between every possible pair of sample types. Traits include:
FromSample
,ToSample
,Duplex
,FromSampleSlice
,ToSampleSlice
,DuplexSampleSlice
,FromSampleSliceMut
,ToSampleSliceMut
,DuplexSampleSliceMut
,FromFrameSlice
,ToFrameSlice
,DuplexFrameSlice
,FromFrameSliceMut
,ToFrameSliceMut
,DuplexFrameSliceMut
,DuplexSlice
,DuplexSliceMut
,
The interpolate module provides a Converter type, for converting and interpolating the rate of Signals. This can be useful for both sample rate conversion and playback rate multiplication. Converters can use a range of interpolation methods, with Floor, Linear, and Sinc interpolation provided in the library. (NB: Sinc interpolation currently requires heap allocation, as it uses VecDeque.)
The ring_buffer module provides generic Fixed and Bounded ring buffer types, both of which may be used with owned, borrowed, stack and allocated buffers.
The peak module can be used for monitoring the peak of a signal. Provided
peak rectifiers include full_wave
, positive_half_wave
and
negative_half_wave
.
The rms module provides a flexible Rms type that can be used for RMS (root mean square) detection. Any Fixed ring buffer can be used as the window for the RMS detection.
The envelope module provides a Detector type (also known as a Follower) that allows for detecting the envelope of a signal. Detector is generic over the type of Detection - Rms and Peak detection are provided. For example:
let signal = rate.const_hz.sine;
let attack = 1.0;
let release = 1.0;
let detector = peak;
let mut envelope = signal.detect_envelope;
assert_eq!;
Using in a no_std
environment
This crate is largely dependency free, even of things outside core
. The
no_std
cargo feature will enable using sample
in these environments.
Currently, only nightly is supported, because it explicitly depends on the
alloc
and collections
for datastructures and core_intrinsics
for some of
the math. If this restriction is onerous for you, it can be lifted with minor
loss of functionality (the Signal::bus
method), so open an issue!
Contributions
If the sample crate is missing types, conversions or other fundamental functionality that you wish it had, feel free to open an issue or pull request! The more hands on deck, the merrier :)
License
Licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contributions
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.