Audio Processor Traits
Traits for audio processor types and audio buffer types. Heavily subject to change.
Primer
This is very much exploration based and I'm still finding out the best API to express in Rust types.
I think I've found a good abstraction to handle AudioBuffer and basic FX AudioProcessor, but there's quite a lot more to uncover.
Motivation
It'd be great to have generic traits for structs that process audio.
These may be 1 or more abstractions on top of audio processing which enable higher-level systems to manipulate audio processing nodes.
At its core, audio processing may be defined as a function:
input
would be your input/output buffer and you could perform mutable operations on it. However, this does not
encode several audio processing concerns, such as:
- The buffer won't be always
f32
, it might bef64
- The buffer channel count isn't expressed
- The buffer layout for multi-channel data isn't expressed (e.g. Interleaved data or otherwise)
- Sample rate isn't expressed
- Dry/wet configurations aren't expressed
- MIDI & other concerns aren't expressed
We'd like some abstraction that covers some of these issues. Without thinking about external system problems (such as MIDI, state & dry/wet), a basic audio processor trait can solve buffer/sample conversion issues.
AudioBuffer
The first part is the AudioBuffer
trait.
It provides an abstraction to get the size of the buffer and modify it as well as a helper read-only iterator implementation.
There're 3 main implementations of this trait in this crate.
InterleavedAudioBuffer
This implementation provides compatibility with interleaved buffers, where multi-channel samples are interleaved with one another.
This provides compatibility of the AudioBuffer
trait with cpal
.
VSTAudioBuffer
This implementation provides compatibility with the VST API and the rust-vst
/vst
crate.
In vst
, the channels are separate slices of continuous samples & input/output are separate pointers.
SliceAudioBuffer
This is unused at the moment, but provides support for other audio buffers that have similar layout than VST, where each channel is a slice of floats.
AudioProcessor
The AudioProcessor trait is only two methods:
It provides a prepare callback, where channel & sample rate configuration will be provided and a process callback where
a generic AudioBuffer
is provided.
Design notes
SampleType associated type
The SampleType
is provided as an associated type to both the AudioBuffer
and the AudioProcessor
traits. This
enables implementors to use generic SampleType
types in their processors.
For example, this is the SilenceAudioProcessor
implementation in this crate, which should work for any num::Float
type and any AudioBuffer
implementation:
;
Pending work
- MIDI trait
- Richer API for applications
- State management guidelines, using a background ref-counting garbage-collector & immutable 'state handle' references (while still allowing the internal state of a processor to be mutable)
- Automatic implementation of the VST API for all trait implementors
- Automatic implementation of a "stand-alone"
cpal
based App for all trait implementors (seeaudio-processor-standalone
in this repository) - An audio-graph implementation
- Testing tools