AudioBuffer¶
The central data type. A 2D float32 numpy array with shape [channels, frames] plus metadata (sample_rate, channel_layout, label).
Usage examples¶
Construction¶
import numpy as np
from nanodsp.buffer import AudioBuffer
# From a numpy array
arr = np.zeros((2, 44100), dtype=np.float32)
buf = AudioBuffer(arr, sample_rate=44100)
# From file
buf = AudioBuffer.from_file("input.wav")
# Factory methods
buf = AudioBuffer.sine(440.0, channels=1, frames=44100, sample_rate=44100)
buf = AudioBuffer.noise(channels=2, frames=44100, seed=42)
buf = AudioBuffer.impulse(channels=1, frames=1024)
buf = AudioBuffer.zeros(channels=1, frames=4096, sample_rate=48000)
buf = AudioBuffer.ones(channels=2, frames=1024)
Inspecting properties¶
buf = AudioBuffer.sine(440.0, frames=48000, sample_rate=48000)
buf.channels # 1
buf.frames # 48000
buf.sample_rate # 48000.0
buf.duration # 1.0 (seconds)
buf.channel_layout # 'mono'
buf.data.shape # (1, 48000)
Channel operations¶
stereo = AudioBuffer.noise(channels=2, frames=44100)
# Downmix
mono = stereo.to_mono("mean") # average channels
mono = stereo.to_mono("left") # take left channel
# Upmix
stereo = mono.to_channels(2) # duplicate mono to stereo
# Split into individual channels
left, right = stereo.split()
# Stack channels
merged = AudioBuffer.concat_channels(left, right)
Arithmetic¶
buf = AudioBuffer.sine(440.0, frames=4096)
quiet = buf * 0.5 # scale amplitude
boosted = buf.gain_db(6.0) # +6 dB
inverted = -buf # phase invert
mixed = buf_a + buf_b # sum two buffers
diff = buf_a - buf_b # difference
Pipeline processing¶
from nanodsp.effects import filters, dynamics
result = (
AudioBuffer.from_file("input.wav")
.pipe(filters.highpass, cutoff_hz=80.0)
.pipe(filters.lowpass, cutoff_hz=12000.0)
.pipe(dynamics.compress, threshold=-18.0, ratio=4.0)
)
result.write("output.wav")
Slicing¶
buf = AudioBuffer.sine(440.0, frames=48000, sample_rate=48000)
# Time slice (view, no copy)
first_half = buf.slice(0, 24000)
# Channel indexing
ch0 = buf[0] # 1D numpy array (channel 0)
samples = buf[0, 100:200] # numpy slice of channel 0, frames 100-199
API reference¶
AudioBuffer
¶
AudioBuffer(
data: ArrayLike | AudioBuffer,
sample_rate: float = 48000.0,
channel_layout: str | None = None,
label: str | None = None,
)
A 2D [channels, frames] float32 audio buffer with metadata.
| PARAMETER | DESCRIPTION |
|---|---|
data
|
Audio samples. 1D input is normalised to
TYPE:
|
sample_rate
|
Sample rate in Hz. Must be positive (> 0).
TYPE:
|
channel_layout
|
E.g.
TYPE:
|
label
|
Free-form label carried as metadata.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If data is not 1D or 2D, or if sample_rate is not positive. |
zeros
classmethod
¶
ones
classmethod
¶
impulse
classmethod
¶
impulse(
channels: int = 1,
frames: int = 1024,
sample_rate: float = 48000.0,
**kw,
) -> AudioBuffer
Unit impulse at frame 0 in every channel.
sine
classmethod
¶
sine(
freq: float,
channels: int = 1,
frames: int = 4096,
sample_rate: float = 48000.0,
**kw,
) -> AudioBuffer
noise
classmethod
¶
noise(
channels: int = 1,
frames: int = 4096,
sample_rate: float = 48000.0,
seed: int | None = None,
**kw,
) -> AudioBuffer
from_numpy
classmethod
¶
Explicit alias for the constructor.
from_file
classmethod
¶
Read an audio file (WAV/FLAC, detected by extension).
write
¶
Write this buffer to an audio file (WAV/FLAC, detected by extension).
to_mono
¶
Mix down to mono.
method: 'mean' (default), 'left', 'right', 'sum'.
to_channels
¶
Upmix mono to n channels by copying, or error if incompatible.
concat_channels
staticmethod
¶
Stack channels from multiple AudioBuffers.
slice
¶
Return a view (no copy) of frames [start_frame:end_frame].
pipe
¶
Chain a DSP function: buf.pipe(dsp.lowpass, 5000).
Calls fn(self, *args, **kwargs) and validates the return type.
ensure_1d
¶
Return a contiguous 1D float32 view for 1D C++ bindings.