Skip to content

Synthesis

Sound generators using DaisySP, STK, PolyBLEP, and fxdsp backends.

Usage examples

Basic oscillators

from nanodsp import synthesis

# Sine wave at 440 Hz, 1 second
tone = synthesis.oscillator(frames=48000, freq=440.0, waveform="sine")

# Sawtooth with reduced amplitude
saw = synthesis.oscillator(frames=48000, freq=220.0, amp=0.5, waveform="saw")

# Square wave with pulse width
sq = synthesis.oscillator(frames=48000, freq=330.0, waveform="square", pw=0.3)

# Band-limited oscillator (DaisySP)
bl = synthesis.bl_oscillator(frames=48000, freq=440.0, waveform="saw")

FM synthesis

# Two-operator FM: carrier at 440 Hz, modulator at 2x carrier, mod index 1.5
fm = synthesis.fm2(frames=48000, freq=440.0, ratio=2.0, index=1.5)

# Formant oscillator
formant = synthesis.formant_oscillator(
    frames=48000, carrier_freq=440.0, formant_freq=1000.0
)

Band-limited oscillators

# PolyBLEP -- efficient, 14 waveforms
saw = synthesis.polyblep(frames=48000, freq=440.0, waveform="sawtooth")
sq = synthesis.polyblep(frames=48000, freq=440.0, waveform="square")

# BLIT -- configurable harmonics
blit = synthesis.blit_saw(frames=48000, freq=220.0, harmonics=20)
blit_sq = synthesis.blit_square(frames=48000, freq=220.0)

# DPW -- differentiated parabolic wave
dpw = synthesis.dpw_saw(frames=48000, freq=440.0)
pulse = synthesis.dpw_pulse(frames=48000, freq=440.0, duty=0.3)

# MinBLEP -- highest antialiasing quality
mb = synthesis.minblep(frames=48000, freq=440.0, waveform="saw")
mb_sq = synthesis.minblep(frames=48000, freq=440.0, waveform="square", pulse_width=0.3)

Noise generators

noise = synthesis.white_noise(frames=48000, amp=0.5)
clocked = synthesis.clocked_noise(frames=48000, freq=1000.0)
impulses = synthesis.dust(frames=48000, density=100.0)

Drum synthesis

kick = synthesis.analog_bass_drum(frames=48000, freq=60.0, decay=0.5, accent=0.8)
snare = synthesis.analog_snare_drum(frames=48000, freq=200.0, snappy=0.7)
hat = synthesis.hihat(frames=48000, freq=3000.0, decay=0.3, noisiness=0.8)

# Synthesis-focused variants with extra controls
syn_kick = synthesis.synthetic_bass_drum(
    frames=48000, freq=60.0, dirtiness=0.3, fm_env_amount=0.5
)
syn_snare = synthesis.synthetic_snare_drum(
    frames=48000, freq=200.0, fm_amount=0.3
)

Physical modeling

from nanodsp.buffer import AudioBuffer

# Karplus-Strong plucked string (excites a noise burst through a filtered delay)
excitation = AudioBuffer.noise(frames=48000, seed=42)
plucked = synthesis.karplus_strong(excitation, freq_hz=440.0, brightness=0.5)

# Simple pluck
p = synthesis.pluck(frames=48000, freq=440.0, decay=0.95, damp=0.9)

# Modal voice (resonant body)
modal = synthesis.modal_voice(frames=48000, freq=440.0, structure=0.5)

# Bowed string
bowed = synthesis.string_voice(frames=48000, freq=220.0, brightness=0.6)

# Water drop
drop = synthesis.drip(frames=48000, dettack=0.01)

STK instruments

# Single note
clarinet = synthesis.synth_note("clarinet", freq=440.0, duration=1.0, velocity=0.8)
flute = synthesis.synth_note("flute", freq=880.0, duration=0.5)

# Sequence of notes
melody = synthesis.synth_sequence("flute", notes=[
    (440.0, 0.0, 0.5),    # (freq, start_time, duration)
    (554.37, 0.5, 0.5),
    (659.26, 1.0, 1.0),
])

Available instruments: clarinet, flute, brass, bowed, plucked, sitar, stifkarp, saxofony, recorder, blowbotl, blowhole, whistle.

API reference

synthesis

Sound synthesis: oscillators, noise, drums, physical modeling, STK instruments.

oscillator

oscillator(
    frames: int,
    freq: float = 440.0,
    amp: float = 1.0,
    waveform: int | str = "sine",
    pw: float = 0.5,
    sample_rate: float = 48000.0,
) -> AudioBuffer

Generate a waveform using DaisySP Oscillator.

PARAMETER DESCRIPTION
freq

Frequency in Hz, > 0 and < Nyquist.

TYPE: float DEFAULT: 440.0

amp

Output amplitude, >= 0. 1.0 = full scale.

TYPE: float DEFAULT: 1.0

waveform

Waveform constant or name: "sine", "tri", "saw", "ramp", "square", "polyblep_tri", "polyblep_saw", "polyblep_square".

TYPE: int or str DEFAULT: 'sine'

pw

Pulse width (for square/pulse waveforms), 0.0--1.0.

TYPE: float DEFAULT: 0.5

fm2

fm2(
    frames: int,
    freq: float = 440.0,
    ratio: float = 2.0,
    index: float = 1.0,
    sample_rate: float = 48000.0,
) -> AudioBuffer

Generate 2-operator FM synthesis.

PARAMETER DESCRIPTION
freq

Carrier frequency in Hz, > 0 and < Nyquist.

TYPE: float DEFAULT: 440.0

ratio

Modulator/carrier frequency ratio, > 0. Integer ratios produce harmonic timbres.

TYPE: float DEFAULT: 2.0

index

Modulation index, >= 0. Higher = more sidebands. Typical: 0--10.

TYPE: float DEFAULT: 1.0

formant_oscillator

formant_oscillator(
    frames: int,
    carrier_freq: float = 440.0,
    formant_freq: float = 1000.0,
    phase_shift: float = 0.0,
    sample_rate: float = 48000.0,
) -> AudioBuffer

Generate formant oscillator signal.

PARAMETER DESCRIPTION
carrier_freq

Carrier frequency in Hz, > 0 and < Nyquist.

TYPE: float DEFAULT: 440.0

formant_freq

Formant frequency in Hz, > 0 and < Nyquist.

TYPE: float DEFAULT: 1000.0

phase_shift

Phase shift in radians. Typical: 0 to 2*pi.

TYPE: float DEFAULT: 0.0

bl_oscillator

bl_oscillator(
    frames: int,
    freq: float = 440.0,
    amp: float = 1.0,
    waveform: int | str = "saw",
    pw: float = 0.5,
    sample_rate: float = 48000.0,
) -> AudioBuffer

Generate a band-limited waveform using DaisySP BlOsc.

PARAMETER DESCRIPTION
waveform

Waveform constant or name: "triangle"/"tri", "saw", "square", "off".

TYPE: int or str DEFAULT: 'saw'

white_noise

white_noise(
    frames: int,
    amp: float = 1.0,
    sample_rate: float = 48000.0,
) -> AudioBuffer

Generate white noise.

clocked_noise

clocked_noise(
    frames: int,
    freq: float = 1000.0,
    sample_rate: float = 48000.0,
) -> AudioBuffer

Generate clocked (sample-and-hold) noise.

dust

dust(
    frames: int,
    density: float = 1.0,
    sample_rate: float = 48000.0,
) -> AudioBuffer

Generate Dust (random impulses at given density).

analog_bass_drum

analog_bass_drum(
    frames: int,
    freq: float = 60.0,
    tone: float = 0.5,
    decay: float = 0.5,
    accent: float = 0.5,
    sustain: bool = False,
    attack_fm: float = 0.5,
    self_fm: float = 0.5,
    sample_rate: float = 48000.0,
) -> AudioBuffer

Generate an analog bass drum hit (triggered at sample 0).

analog_snare_drum

analog_snare_drum(
    frames: int,
    freq: float = 200.0,
    tone: float = 0.5,
    decay: float = 0.5,
    snappy: float = 0.5,
    accent: float = 0.5,
    sustain: bool = False,
    sample_rate: float = 48000.0,
) -> AudioBuffer

Generate an analog snare drum hit (triggered at sample 0).

hihat

hihat(
    frames: int,
    freq: float = 3000.0,
    tone: float = 0.5,
    decay: float = 0.3,
    noisiness: float = 0.8,
    accent: float = 0.5,
    sustain: bool = False,
    sample_rate: float = 48000.0,
) -> AudioBuffer

Generate a hi-hat hit (triggered at sample 0).

synthetic_bass_drum

synthetic_bass_drum(
    frames: int,
    freq: float = 60.0,
    tone: float = 0.5,
    decay: float = 0.5,
    dirtiness: float = 0.3,
    fm_env_amount: float = 0.5,
    fm_env_decay: float = 0.3,
    accent: float = 0.5,
    sustain: bool = False,
    sample_rate: float = 48000.0,
) -> AudioBuffer

Generate a synthetic bass drum hit (triggered at sample 0).

synthetic_snare_drum

synthetic_snare_drum(
    frames: int,
    freq: float = 200.0,
    decay: float = 0.5,
    snappy: float = 0.5,
    fm_amount: float = 0.3,
    accent: float = 0.5,
    sustain: bool = False,
    sample_rate: float = 48000.0,
) -> AudioBuffer

Generate a synthetic snare drum hit (triggered at sample 0).

karplus_strong

karplus_strong(
    buf: AudioBuffer,
    freq_hz: float = 440.0,
    brightness: float = 0.5,
    damping: float = 0.5,
    non_linearity: float = 0.0,
) -> AudioBuffer

Karplus-Strong string model (excitation input, per channel).

modal_voice

modal_voice(
    frames: int,
    freq: float = 440.0,
    accent: float = 0.5,
    structure: float = 0.5,
    brightness: float = 0.5,
    damping: float = 0.5,
    sustain: bool = False,
    sample_rate: float = 48000.0,
) -> AudioBuffer

Generate a modal voice hit (triggered at sample 0).

string_voice

string_voice(
    frames: int,
    freq: float = 440.0,
    accent: float = 0.5,
    structure: float = 0.5,
    brightness: float = 0.5,
    damping: float = 0.5,
    sustain: bool = False,
    sample_rate: float = 48000.0,
) -> AudioBuffer

Generate a string voice hit (triggered at sample 0).

pluck

pluck(
    frames: int,
    freq: float = 440.0,
    amp: float = 0.8,
    decay: float = 0.95,
    damp: float = 0.9,
    sample_rate: float = 48000.0,
) -> AudioBuffer

Generate a plucked string sound (triggered at sample 0).

drip

drip(
    frames: int,
    dettack: float = 0.01,
    sample_rate: float = 48000.0,
) -> AudioBuffer

Generate a water-drip sound (triggered at sample 0).

synth_note

synth_note(
    instrument: str,
    freq: float = 440.0,
    duration: float = 1.0,
    velocity: float = 0.8,
    release: float = 0.1,
    sample_rate: float = 48000.0,
) -> AudioBuffer

Synthesize a single note using an STK physical model.

PARAMETER DESCRIPTION
instrument

Instrument name: 'clarinet', 'flute', 'brass', 'bowed', 'plucked', 'sitar', 'stifkarp', 'saxofony', 'recorder', 'blowbotl', 'blowhole', 'whistle'.

TYPE: str

freq

Note frequency in Hz.

TYPE: float DEFAULT: 440.0

duration

Note duration in seconds (sustain portion before release).

TYPE: float DEFAULT: 1.0

velocity

Note velocity / amplitude (0.0 to 1.0).

TYPE: float DEFAULT: 0.8

release

Release time in seconds after note-off.

TYPE: float DEFAULT: 0.1

sample_rate

Output sample rate.

TYPE: float DEFAULT: 48000.0

polyblep

polyblep(
    frames: int,
    freq: float = 440.0,
    waveform: str = "sawtooth",
    pulse_width: float = 0.5,
    sample_rate: float = 48000.0,
) -> AudioBuffer

Generate a band-limited waveform using PolyBLEP anti-aliasing.

PARAMETER DESCRIPTION
frames

Number of samples to generate.

TYPE: int

freq

Frequency in Hz.

TYPE: float DEFAULT: 440.0

waveform

One of: 'sine', 'cosine', 'triangle'/'tri', 'square', 'rectangle'/'rect', 'sawtooth'/'saw', 'ramp', 'modified_triangle', 'modified_square', 'half_wave_rectified_sine', 'full_wave_rectified_sine', 'triangular_pulse', 'trapezoid_fixed', 'trapezoid_variable'.

TYPE: str DEFAULT: 'sawtooth'

pulse_width

Pulse width for rectangle/variable waveforms (0.0 to 1.0).

TYPE: float DEFAULT: 0.5

sample_rate

Output sample rate.

TYPE: float DEFAULT: 48000.0

blit_saw

blit_saw(
    frames: int,
    freq: float = 220.0,
    harmonics: int = 0,
    sample_rate: float = 48000.0,
) -> AudioBuffer

Generate a BLIT (Band-Limited Impulse Train) sawtooth.

PARAMETER DESCRIPTION
frames

Number of samples to generate.

TYPE: int

freq

Frequency in Hz.

TYPE: float DEFAULT: 220.0

harmonics

Number of harmonics (0 = maximum up to Nyquist).

TYPE: int DEFAULT: 0

sample_rate

Output sample rate.

TYPE: float DEFAULT: 48000.0

blit_square

blit_square(
    frames: int,
    freq: float = 220.0,
    harmonics: int = 0,
    sample_rate: float = 48000.0,
) -> AudioBuffer

Generate a BLIT square wave with DC blocker.

PARAMETER DESCRIPTION
frames

Number of samples to generate.

TYPE: int

freq

Frequency in Hz.

TYPE: float DEFAULT: 220.0

harmonics

Number of harmonics (0 = maximum up to Nyquist).

TYPE: int DEFAULT: 0

sample_rate

Output sample rate.

TYPE: float DEFAULT: 48000.0

dpw_saw

dpw_saw(
    frames: int,
    freq: float = 440.0,
    sample_rate: float = 48000.0,
) -> AudioBuffer

Generate a DPW (Differentiated Parabolic Wave) sawtooth.

PARAMETER DESCRIPTION
frames

Number of samples to generate.

TYPE: int

freq

Frequency in Hz.

TYPE: float DEFAULT: 440.0

sample_rate

Output sample rate.

TYPE: float DEFAULT: 48000.0

dpw_pulse

dpw_pulse(
    frames: int,
    freq: float = 440.0,
    duty: float = 0.5,
    sample_rate: float = 48000.0,
) -> AudioBuffer

Generate a DPW pulse with variable duty cycle.

PARAMETER DESCRIPTION
frames

Number of samples to generate.

TYPE: int

freq

Frequency in Hz.

TYPE: float DEFAULT: 440.0

duty

Duty cycle (0.01 to 0.99, default 0.5 for square).

TYPE: float DEFAULT: 0.5

sample_rate

Output sample rate.

TYPE: float DEFAULT: 48000.0

minblep

minblep(
    frames: int,
    freq: float = 440.0,
    waveform: str = "saw",
    pulse_width: float = 0.5,
    sample_rate: float = 48000.0,
) -> AudioBuffer

Generate a band-limited waveform using MinBLEP anti-aliasing.

PARAMETER DESCRIPTION
frames

Number of samples to generate.

TYPE: int

freq

Frequency in Hz.

TYPE: float DEFAULT: 440.0

waveform

One of: 'saw'/'sawtooth', 'rsaw', 'square', 'triangle'/'tri'.

TYPE: str DEFAULT: 'saw'

pulse_width

Pulse width for square waveform (0.01 to 0.99).

TYPE: float DEFAULT: 0.5

sample_rate

Output sample rate.

TYPE: float DEFAULT: 48000.0

synth_sequence

synth_sequence(
    instrument: str,
    notes: list[tuple[float, float, float]],
    sample_rate: float = 48000.0,
    release: float = 0.1,
    velocity: float = 0.8,
) -> AudioBuffer

Synthesize a sequence of notes.

PARAMETER DESCRIPTION
instrument

Instrument name (see :func:synth_note).

TYPE: str

notes

Each tuple is (frequency, start_time, duration) in seconds.

TYPE: list of (freq_hz, start_time_s, duration_s)

sample_rate

Output sample rate.

TYPE: float DEFAULT: 48000.0

release

Release time for each note in seconds.

TYPE: float DEFAULT: 0.1

velocity

Default velocity for all notes.

TYPE: float DEFAULT: 0.8