Compare commits

...

10 Commits

Author SHA1 Message Date
kennethreitz 0b98f7bd77 Mellotron, hard sync, ring mod, wavefold, drift synths + analog presets — v0.40.9
Five new synth waveforms: tape-replay Mellotron (strings/flute/choir
tapes with wow, flutter, saturation, 8s fadeout), hard sync oscillator,
ring modulation, wavefolding, and analog drift VCO with pitch
instability. 14 new instrument presets for Score.part(). Synth kwargs
now pass through play()/save()/_render(). 808 bass envelope fixed
from pluck to piano.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-02 23:14:18 -04:00
kennethreitz e0a1ce9d18 Fix hold() inflating Part.total_beats and Score.duration_ms — v0.40.8
Note.beats now returns 0.0 for held notes (_hold=True), matching the
renderer which already skipped advancing the beat position. Previously
every hold() call added its full duration to the part's total, causing
duration reports to be 2-3x too long on tracks with drone notes.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-01 13:27:08 -04:00
kennethreitz de7575fe0a Expose rhodes, wurlitzer, vibraphone, pipe organ, choir in Synth enum — v0.40.7
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-01 01:59:11 -04:00
kennethreitz 665a6f5de5 Remove lowpass/vel_to_filter from sax presets, let wave shape its own tone — v0.40.6
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-01 00:44:01 -04:00
kennethreitz 63362df697 Saxophone synth overhaul: reed clipping, formants, breath noise — v0.40.5
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-01 00:38:10 -04:00
kennethreitz 755b33a63b Fix test: update Synth enum count 42 → 46
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-31 07:12:43 -04:00
kennethreitz 40901d603d Multi-stage distortion: preamp, power amp, asymmetric clipping — v0.40.4
Single tanh was too mild. Now chains preamp gain → power amp clip →
asymmetric rectifier sag for proper overdrive/fuzz character.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-31 07:08:10 -04:00
kennethreitz 9b3cbd9065 Add crotales, tingsha, rain stick, ocean drum, cabasa, wind chimes, finger cymbal — v0.40.3
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-30 14:50:03 -04:00
kennethreitz 0911947971 v0.40.2 — dial back master compressor
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-30 04:43:53 -04:00
kennethreitz c2f748d5f3 Dial back master compressor: raise threshold, cap makeup gain
Threshold 0.5 → 0.7 so more dynamics survive. Makeup gain capped
at 3x so sparse arrangements (solo singing bowl, etc.) don't get
over-amplified to clipping.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-30 04:42:52 -04:00
20 changed files with 1552 additions and 69 deletions
+80
View File
@@ -2,6 +2,86 @@
All notable changes to PyTheory are documented here.
## 0.40.9
- **Mellotron synth** — tape-replay keyboard with wow/flutter, tape saturation,
bandwidth limiting, hiss, and 8-second tape fadeout. Three tape banks via the
`tape` parameter: `"strings"` (default), `"flute"`, and `"choir"`.
- **Analog oscillator synths** — four new waveform generators for fat, alive,
analog-style sounds:
- `Synth.HARD_SYNC` — slave oscillator hard-synced to a master (Prophet-5
leads). `slave_ratio` parameter controls harmonic content.
- `Synth.RING_MOD` — two oscillators multiplied for metallic, bell-like
inharmonic tones. `mod_ratio` parameter.
- `Synth.WAVEFOLD` — west coast wavefolding (Buchla-style). `folds` parameter
sweeps from warm to gnarly.
- `Synth.DRIFT` — analog VCO with pitch drift, jitter, and noise floor.
`shape` parameter (`"saw"`, `"square"`, `"triangle"`, `"pulse"`) and
`drift_amount` for instability level.
- **Synth kwargs passthrough** — `play()`, `save()`, and `_render()` now accept
`**synth_kw` for forwarding parameters to synth wave functions (e.g.
`play(tone, synth=Synth.MELLOTRON, tape="choir")`).
- **14 new instrument presets** — `mellotron`, `mellotron_strings`,
`mellotron_flute`, `mellotron_choir`, `sync_lead`, `sync_lead_bright`,
`ring_mod_bell`, `ring_mod_metallic`, `wavefold_warm`, `wavefold_gnarly`,
`drift_saw`, `drift_square`, `analog_pad`, `analog_bass`.
- **808 bass envelope fix** — changed from `pluck` (zero sustain, wrong for 808)
to `piano` (sharp attack with long decay tail).
## 0.40.8
- **Fix hold() inflating duration** — `Note.beats` was returning the full
duration for held notes (`_hold=True`), causing `Part.total_beats` and
`Score.duration_ms` to overcount. A part with `hold(Sa, WHOLE * 4)` followed
by `add(Pa, QUARTER)` would report 17 beats instead of 1. Now held notes
return 0 beats, matching the renderer which already skipped advancing the
timeline for held notes.
## 0.40.7
- **Expose missing Synth enum entries** — rhodes, wurlitzer, vibraphone,
pipe organ, and choir wave functions were already implemented but not
accessible via the Synth enum. Now available as `Synth.RHODES`,
`Synth.WURLITZER`, `Synth.VIBRAPHONE`, `Synth.PIPE_ORGAN`, `Synth.CHOIR`.
## 0.40.6
- **Saxophone presets cleaned up** — removed lowpass filters and vel_to_filter
from all sax instrument presets (saxophone, alto_sax, tenor_sax, bari_sax).
The saxophone wave function already shapes its own spectrum; the extra
filters were dulling the tone.
## 0.40.5
- **Saxophone synth overhaul** — reed nonlinearity (asymmetric soft clipping),
conical bore formant resonances, breath noise with attack envelope, separate
reed buzz, key click transient, and sub-harmonic warmth. Vibrato dialed back
to subtle, delayed onset.
## 0.40.4
- **Distortion overhaul** — multi-stage clipping (preamp → power amp →
asymmetric rectifier) replaces single-stage tanh. Crunch, distorted,
orange crunch, and metal guitar presets now sound properly driven.
## 0.40.3
- **Crotales synth** — tuned bronze discs with long ring and bright harmonics
- **Tingsha synth** — paired Tibetan cymbals with beating from two detuned discs
- **Rain stick** — cascading pebbles (steep and slow/shallow variants)
- **Ocean drum** — steel beads rolling inside a frame drum, surf wash
- **Cabasa** — metal bead chain on cylinder, bright metallic scrape
- **Wind chimes** — multiple suspended metal tubes ringing at random offsets
- **Finger cymbal** — single zill tap, bright metallic ping
- `crotales`, `tingsha`, `singing_bowl`, `singing_bowl_ring` instrument presets
- Audio demos in docs for all new sounds
## 0.40.2
- **Master compressor dialed back** — threshold raised from 0.5 to 0.7,
makeup gain capped at 3x. Sparse arrangements no longer get
over-amplified to clipping.
## 0.40.1
- **Singing bowl synth** — two variants: strike (mallet hit with chirp
+207
View File
@@ -0,0 +1,207 @@
"""Demo the 5 new synths: Mellotron, Hard Sync, Ring Mod, Wavefold, Drift.
Each synth gets a short musical phrase — not just a scale run — with
reverb and rhythmic variety to show off its character.
"""
from pytheory import Score, Duration, play_score
EIGHTH = Duration.EIGHTH
QUARTER = Duration.QUARTER
HALF = Duration.HALF
DOTTED_Q = Duration.DOTTED_QUARTER
WHOLE = Duration.WHOLE
# ── Mellotron Strings ────────────────────────────────────────────────────────
# Strawberry Fields vibes — slow, haunted, with rests that breathe.
print("=== MELLOTRON STRINGS ===")
s = Score("4/4", bpm=72)
p = s.part("tape", instrument="mellotron_strings",
reverb=0.45, reverb_type="cathedral", reverb_decay=2.0)
p.add("G4", HALF).add("B4", QUARTER).add("D5", QUARTER)
p.add("C5", DOTTED_Q).add("B4", EIGHTH).add("A4", HALF)
p.rest(QUARTER)
p.add("G4", DOTTED_Q).add("F#4", EIGHTH).add("G4", WHOLE)
play_score(s)
# ── Mellotron Flute ──────────────────────────────────────────────────────────
# Lonely, breathy, with space between phrases.
print("\n=== MELLOTRON FLUTE ===")
s = Score("3/4", bpm=84)
p = s.part("flute", instrument="mellotron_flute",
reverb=0.5, reverb_type="taj_mahal", reverb_decay=2.5)
p.add("E5", HALF).add("D5", QUARTER)
p.add("C5", DOTTED_Q).add("B4", EIGHTH).rest(QUARTER)
p.add("A4", HALF).add("G4", QUARTER)
p.add("A4", HALF).rest(QUARTER)
p.add("E5", QUARTER).add("D5", QUARTER).add("C5", QUARTER)
p.add("B4", HALF + QUARTER)
play_score(s)
# ── Mellotron Choir ──────────────────────────────────────────────────────────
# Ghostly pad — slow chords, big reverb.
print("\n=== MELLOTRON CHOIR ===")
s = Score("4/4", bpm=60)
p = s.part("choir", instrument="mellotron_choir",
reverb=0.6, reverb_type="cathedral", reverb_decay=3.0)
p.add("C4", WHOLE)
p.add("E4", HALF).add("G4", HALF)
p.add("A4", DOTTED_Q).add("G4", EIGHTH).add("F4", HALF)
p.add("E4", WHOLE)
play_score(s)
# ── Hard Sync Lead ───────────────────────────────────────────────────────────
# Aggressive, punchy — fast 16ths and syncopation.
print("\n=== HARD SYNC LEAD ===")
s = Score("4/4", bpm=128)
p = s.part("sync", instrument="sync_lead",
reverb=0.25, reverb_type="plate")
p.add("E4", EIGHTH).add("E4", EIGHTH).rest(EIGHTH).add("G4", EIGHTH)
p.add("A4", QUARTER).add("G4", EIGHTH).add("E4", EIGHTH)
p.add("D4", EIGHTH).rest(EIGHTH).add("E4", EIGHTH).add("G4", EIGHTH)
p.add("A4", HALF)
p.rest(QUARTER).add("B4", EIGHTH).add("A4", EIGHTH)
p.add("G4", QUARTER).add("E4", QUARTER).add("D4", HALF)
play_score(s)
# ── Hard Sync Bright ─────────────────────────────────────────────────────────
# Higher slave ratio — more harmonics, screaming lead.
print("\n=== HARD SYNC BRIGHT ===")
s = Score("4/4", bpm=138)
p = s.part("sync2", instrument="sync_lead_bright",
reverb=0.2, reverb_type="plate")
p.add("A4", EIGHTH).add("C5", EIGHTH).add("D5", QUARTER)
p.rest(EIGHTH).add("E5", EIGHTH).add("D5", EIGHTH).add("C5", EIGHTH)
p.add("A4", QUARTER).rest(QUARTER).add("G4", EIGHTH).add("A4", EIGHTH)
p.add("C5", HALF)
play_score(s)
# ── Ring Mod Bell ────────────────────────────────────────────────────────────
# Shimmery, metallic — sparse hits with long reverb tail.
print("\n=== RING MOD BELL ===")
s = Score("4/4", bpm=66)
p = s.part("bell", instrument="ring_mod_bell",
reverb=0.6, reverb_type="cave", reverb_decay=3.0)
p.add("C5", HALF).rest(QUARTER).add("G4", QUARTER)
p.rest(HALF).add("E5", HALF)
p.add("D5", QUARTER).rest(QUARTER).add("C5", HALF)
p.rest(WHOLE)
p.add("G4", QUARTER).add("A4", QUARTER).add("C5", HALF)
play_score(s)
# ── Ring Mod Metallic ────────────────────────────────────────────────────────
# Alien, inharmonic — atonal stabs.
print("\n=== RING MOD METALLIC ===")
s = Score("4/4", bpm=100)
p = s.part("metal", instrument="ring_mod_metallic",
reverb=0.4, reverb_type="parking_garage", reverb_decay=2.0)
p.add("F4", EIGHTH).rest(EIGHTH).add("Ab4", EIGHTH).add("F4", EIGHTH)
p.rest(QUARTER).add("Db5", QUARTER).rest(QUARTER)
p.add("C5", EIGHTH).add("Ab4", EIGHTH).rest(QUARTER).add("F4", HALF)
p.rest(HALF).add("Db5", QUARTER).add("C5", QUARTER)
play_score(s)
# ── Wavefold Warm ────────────────────────────────────────────────────────────
# Gentle folds — round and musical, like a filtered saw with overtones.
print("\n=== WAVEFOLD WARM ===")
s = Score("4/4", bpm=108)
p = s.part("fold", instrument="wavefold_warm",
reverb=0.3, reverb_type="plate")
p.add("A3", QUARTER).add("C4", QUARTER).add("E4", QUARTER).add("A4", QUARTER)
p.add("G4", DOTTED_Q).add("E4", EIGHTH).add("C4", HALF)
p.add("D4", QUARTER).add("F4", QUARTER).add("A4", HALF)
p.add("G4", WHOLE)
play_score(s)
# ── Wavefold Gnarly ──────────────────────────────────────────────────────────
# Cranked folds — buzzy, aggressive, with syncopation.
print("\n=== WAVEFOLD GNARLY ===")
s = Score("4/4", bpm=130)
p = s.part("gnarly", instrument="wavefold_gnarly",
reverb=0.2, reverb_type="spring")
p.add("E3", EIGHTH).add("E3", EIGHTH).rest(EIGHTH).add("G3", EIGHTH)
p.add("A3", EIGHTH).rest(EIGHTH).add("B3", EIGHTH).add("A3", EIGHTH)
p.add("E3", QUARTER).add("G3", EIGHTH).add("A3", EIGHTH).add("B3", QUARTER)
p.rest(QUARTER)
p.add("E4", EIGHTH).add("D4", EIGHTH).add("B3", QUARTER).add("A3", HALF)
play_score(s)
# ── Drift Saw ────────────────────────────────────────────────────────────────
# Warm, alive analog saw — the Minimoog pad.
print("\n=== DRIFT SAW (vintage VCO) ===")
s = Score("4/4", bpm=88)
p = s.part("drift", instrument="drift_saw",
reverb=0.35, reverb_type="taj_mahal", reverb_decay=2.0)
p.add("D4", HALF).add("F4", HALF)
p.add("A4", DOTTED_Q).add("G4", EIGHTH).add("F4", QUARTER).rest(QUARTER)
p.add("D4", QUARTER).add("E4", QUARTER).add("F4", HALF)
p.add("D4", WHOLE)
play_score(s)
# ── Drift Square ─────────────────────────────────────────────────────────────
# Hollow, wobbly — 8-bit with analog soul.
print("\n=== DRIFT SQUARE ===")
s = Score("4/4", bpm=110)
p = s.part("dsq", instrument="drift_square",
reverb=0.25, reverb_type="plate")
p.add("C4", EIGHTH).add("E4", EIGHTH).add("G4", QUARTER).add("E4", QUARTER)
p.rest(QUARTER)
p.add("A4", EIGHTH).add("G4", EIGHTH).add("E4", QUARTER).add("C4", HALF)
p.add("D4", QUARTER).add("F4", EIGHTH).add("G4", EIGHTH).add("A4", HALF)
p.add("G4", WHOLE)
play_score(s)
# ── Analog Pad ───────────────────────────────────────────────────────────────
# Slow, drifting chords — Juno-style lushness.
print("\n=== ANALOG PAD ===")
s = Score("4/4", bpm=70)
p = s.part("pad", instrument="analog_pad",
reverb=0.5, reverb_type="taj_mahal", reverb_decay=3.0)
p.add("A3", WHOLE)
p.add("C4", HALF).add("E4", HALF)
p.add("F4", WHOLE)
p.add("E4", HALF).add("D4", HALF)
p.add("C4", WHOLE)
play_score(s)
# ── Analog Bass ──────────────────────────────────────────────────────────────
# Tight, punchy — Moog bass with filter sweep.
print("\n=== ANALOG BASS ===")
s = Score("4/4", bpm=120)
p = s.part("bass", instrument="analog_bass",
reverb=0.1, reverb_type="plate")
p.add("E2", EIGHTH).add("E2", EIGHTH).rest(EIGHTH).add("G2", EIGHTH)
p.add("A2", QUARTER).rest(QUARTER)
p.add("E2", EIGHTH).rest(EIGHTH).add("B2", EIGHTH).add("A2", EIGHTH)
p.add("G2", QUARTER).add("E2", QUARTER).rest(HALF)
p.add("E2", EIGHTH).add("E2", EIGHTH).add("G2", EIGHTH).add("A2", EIGHTH)
p.add("B2", QUARTER).add("A2", QUARTER).add("E2", HALF)
play_score(s)
print("\nDone!")
BIN
View File
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+70
View File
@@ -858,6 +858,68 @@ def gen_synth_granular():
render("synth_granular", score)
def gen_synth_crotales():
score = Score("4/4", bpm=60)
p = score.part("demo", synth="crotales_synth", envelope="none",
volume=0.5, reverb=0.3)
for n in ["C6", "E6", "G6", "C7", "G6", "E6", "C6"]:
p.add(n, Duration.HALF, velocity=80)
render("synth_crotales", score)
def gen_synth_tingsha():
score = Score("4/4", bpm=40)
p = score.part("demo", synth="tingsha_synth", envelope="none",
volume=0.5, reverb=0.4)
for n in ["E5", "A5", "E6", "A5"]:
p.add(n, Duration.WHOLE, velocity=75)
render("synth_tingsha", score)
def gen_rainstick():
score = Score("4/4", bpm=60)
p = score.part("demo", synth="sine", volume=1.0)
p.hit(DrumSound.RAINSTICK, Duration.WHOLE * 3, velocity=90)
render("rainstick", score)
def gen_rainstick_slow():
score = Score("4/4", bpm=60)
p = score.part("demo", synth="sine", volume=1.0)
p.hit(DrumSound.RAINSTICK_SLOW, Duration.WHOLE * 4, velocity=85)
render("rainstick_slow", score)
def gen_ocean_drum():
score = Score("4/4", bpm=60)
p = score.part("demo", synth="sine", volume=1.0)
p.hit(DrumSound.OCEAN_DRUM, Duration.WHOLE * 3, velocity=85)
render("ocean_drum", score)
def gen_cabasa():
score = Score("4/4", bpm=100)
p = score.part("demo", synth="sine", volume=1.0)
for _ in range(16):
p.hit(DrumSound.CABASA, Duration.EIGHTH, velocity=100)
render("cabasa", score)
def gen_wind_chimes():
score = Score("4/4", bpm=60)
p = score.part("demo", synth="sine", volume=1.0)
p.hit(DrumSound.WIND_CHIMES, Duration.WHOLE * 3, velocity=85)
render("wind_chimes", score)
def gen_finger_cymbal():
score = Score("4/4", bpm=80)
p = score.part("demo", synth="sine", volume=1.0)
for _ in range(8):
p.hit(DrumSound.FINGER_CYMBAL, Duration.QUARTER, velocity=85)
render("finger_cymbal", score)
def gen_synth_singing_bowl_strike():
score = Score("4/4", bpm=40)
p = score.part("demo", synth="singing_bowl_strike_synth", envelope="none",
@@ -1078,8 +1140,16 @@ GENERATORS = [
gen_synth_mandolin,
gen_synth_ukulele,
gen_synth_granular,
gen_synth_crotales,
gen_synth_tingsha,
gen_synth_singing_bowl_strike,
gen_synth_singing_bowl_ring,
gen_rainstick,
gen_rainstick_slow,
gen_ocean_drum,
gen_cabasa,
gen_wind_chimes,
gen_finger_cymbal,
gen_arpeggio,
gen_legato_glide,
gen_acid_house,
+16
View File
@@ -44,6 +44,22 @@ Optional parameters for synth, envelope, and temperament:
play(Tone.from_string("C4"), synth=Synth.SAW, envelope=Envelope.PLUCK, t=1_000)
play(Tone.from_string("C4"), temperament="pythagorean", t=1_000)
Synth-specific parameters are passed through as keyword arguments:
.. code-block:: python
# Mellotron with flute tape
play(Tone.from_string("C4"), synth=Synth.MELLOTRON, tape="choir", t=2_000)
# Hard sync with custom slave ratio
play(Tone.from_string("C4"), synth=Synth.HARD_SYNC, slave_ratio=2.5)
# Wavefolding with 4 folds
play(Tone.from_string("C4"), synth=Synth.WAVEFOLD, folds=4.0)
# Drift oscillator with square shape
play(Tone.from_string("C4"), synth=Synth.DRIFT, shape="square")
play_score() -- Full Arrangements
---------------------------------
+263 -5
View File
@@ -1,7 +1,7 @@
Synthesizers
============
PyTheory includes 41 built-in waveforms and 10 ADSR envelope presets.
PyTheory includes 56 built-in waveforms and 10 ADSR envelope presets.
Every sound is generated from scratch -- no samples or external audio
files needed.
@@ -249,6 +249,114 @@ produces a natural chorus/vibrato effect built into the waveform itself.
<audio controls style="width:100%;margin:0.3em 0 0.5em"><source src="../_static/audio/synth_pwm_fast.wav" type="audio/wav"></audio>
Analog Synthesis
----------------
These waveforms model the behavior of real analog hardware — the
imperfections, interactions, and nonlinearities that make a room full
of vintage synths sound so much more alive than a room full of VSTs.
Each one is a different approach to the same question: how do you make
a digital oscillator sound like it has a soul?
Hard Sync
~~~~~~~~~
A "slave" oscillator is forced to restart its cycle every time a
"master" oscillator completes one. The abrupt restart creates bright
formant peaks that sweep as the slave ratio changes. This is THE sound
of the Prophet-5, Moog Prodigy, and every screaming analog lead since
1978.
**Use for:** aggressive leads, formant sweeps, cutting solos.
.. code-block:: python
lead = score.part("lead", synth="hard_sync", envelope="pluck")
# Higher slave ratio = more harmonics, brighter
from pytheory import play, Synth, Tone
play(Tone.from_string("C4"), synth=Synth.HARD_SYNC, slave_ratio=2.5)
Ring Modulation
~~~~~~~~~~~~~~~
Two oscillators multiplied together, producing sum and difference
frequencies. Unlike FM, ring mod outputs only sidebands — no carrier
or modulator fundamental. The result is metallic, bell-like, and often
inharmonic. Classic Dalek voice, Stockhausen, and every sci-fi
soundtrack.
**Use for:** metallic bells, alien textures, inharmonic percussion.
.. code-block:: python
bells = score.part("bells", instrument="ring_mod_bell",
reverb=0.5, reverb_type="cave")
# Non-integer ratios = more inharmonic
play(Tone.from_string("C4"), synth=Synth.RING_MOD, mod_ratio=2.1)
Wavefolding
~~~~~~~~~~~
The heart of west coast synthesis (Buchla, Make Noise, Verbos). A sine
wave is amplified past ±1.0, then "folded" — the overflow bounces back
instead of clipping. Each fold adds new harmonic pairs. At low fold
counts it's warm and round; crank it up and it gets buzzy, gnarly, and
alive.
This sounds completely different from subtractive synthesis — instead of
*removing* harmonics with a filter, you're *generating* them by shaping
the wave. Pairs beautifully with a lowpass filter after the fold.
**Use for:** complex leads, evolving textures, west coast basslines.
.. code-block:: python
# Warm, musical folding
warm = score.part("fold", instrument="wavefold_warm")
# Cranked and aggressive
gnarly = score.part("gnarly", instrument="wavefold_gnarly")
# Direct control over fold amount
play(Tone.from_string("C4"), synth=Synth.WAVEFOLD, folds=3.0)
Drift Oscillator
~~~~~~~~~~~~~~~~
Real analog oscillators are never perfectly stable. Capacitor charging,
thermal variations, and component tolerances make the pitch wander
slightly. This is what makes a Minimoog sound "fat" and a VST sound
"thin" — the constant micro-motion of imperfect hardware.
The drift oscillator models slow pitch drift (< 1 Hz wander), fast
jitter (per-cycle randomness), a soft analog noise floor, and slightly
rounded waveform edges. It turns any basic shape into something that
breathes.
**Use for:** analog-style pads, warm basses, vintage leads, any voice
that needs to feel "alive."
.. code-block:: python
# Vintage Minimoog-style saw
pad = score.part("pad", instrument="drift_saw",
reverb=0.35, reverb_type="taj_mahal")
# Hollow square with analog wobble
sq = score.part("sq", instrument="drift_square")
# Control the shape and instability directly
play(Tone.from_string("C4"), synth=Synth.DRIFT,
shape="triangle", drift_amount=0.25)
Drift amount controls how unstable the oscillator is:
- **0.05** = studio-grade (Sequential, Oberheim)
- **0.15** = classic vintage (Minimoog, ARP) — the default
- **0.30** = barely-holding-it-together (old SH-101)
ADSR Envelopes
--------------
@@ -440,11 +548,11 @@ Dedicated Instrument Synths
--------------------------
Beyond the classic and physical modeling waveforms, PyTheory includes
31 dedicated instrument synths. Each one uses tailored synthesis
36 dedicated instrument synths. Each one uses tailored synthesis
techniques -- additive harmonics, formant shaping, body resonance
modeling, and specialized envelopes -- to capture the character of a
specific acoustic instrument. These are the waveforms that bring the
total count to 41.
total count to 56.
Piano Synth
~~~~~~~~~~~
@@ -495,6 +603,38 @@ picked up by an electrostatic pickup. More nasal, reedy, and biting
<audio controls style="width:100%;margin:0.3em 0 0.5em"><source src="../_static/audio/synth_wurlitzer.wav" type="audio/wav"></audio>
Mellotron
~~~~~~~~~
The original "sampler" — a 1960s keyboard where each key triggers a
strip of magnetic tape with a pre-recorded instrument. The mechanical
tape transport gives it a haunted, lo-fi quality that no digital
emulation fully captures: pitch wobbles from uneven capstan speed,
bandwidth limited to 300 Hz6 kHz (like a worn cassette), soft tape
saturation, and tapes that physically run out after 8 seconds.
The Mellotron defined the sound of *Strawberry Fields Forever*,
*Stairway to Heaven*, and every prog rock record from 19691977.
Three tape banks are available via the ``tape`` parameter:
- ``"strings"`` (default) — the iconic MkII string section
- ``"flute"`` — breathy, haunting solo flute
- ``"choir"`` — ghostly vocal pad
**Use for:** prog rock, haunted textures, vintage orchestral color.
.. code-block:: python
# Use instrument presets (includes reverb)
strings = score.part("strings", instrument="mellotron_strings")
flute = score.part("flute", instrument="mellotron_flute")
choir = score.part("choir", instrument="mellotron_choir")
# Or select the tape directly
from pytheory import play, Synth, Tone
play(Tone.from_string("C4"), synth=Synth.MELLOTRON, tape="choir", t=3000)
Vibraphone Synth
~~~~~~~~~~~~~~~~
@@ -930,6 +1070,40 @@ Parameters (passed as synth kwargs):
<audio controls style="width:100%;margin:0.3em 0 0.5em"><source src="../_static/audio/synth_granular.wav" type="audio/wav"></audio>
Crotales
~~~~~~~~
Small tuned bronze discs (antique cymbals) struck with brass mallets.
Bright, crystalline, bell-like tone with strong upper harmonics that
rings for a long time. Nearly harmonic partials give crotales their
penetrating brilliance — they cut through any orchestra.
.. code-block:: python
crotales = score.part("crotales", synth="crotales_synth", envelope="none",
reverb=0.3)
.. raw:: html
<audio controls style="width:100%;margin:0.3em 0 0.5em"><source src="../_static/audio/synth_crotales.wav" type="audio/wav"></audio>
Tingsha
~~~~~~~
Two small Tibetan cymbals joined by a cord, clashed together. Both discs
ring at slightly different frequencies, producing a bright ping with
pronounced beating — the wavering interference between the two is the
whole character of the sound.
.. code-block:: python
tingsha = score.part("tingsha", synth="tingsha_synth", envelope="none",
reverb=0.4)
.. raw:: html
<audio controls style="width:100%;margin:0.3em 0 0.5em"><source src="../_static/audio/synth_tingsha.wav" type="audio/wav"></audio>
Singing Bowl (Strike)
~~~~~~~~~~~~~~~~~~~~~
@@ -963,6 +1137,76 @@ and out as the bowl resonates.
<audio controls style="width:100%;margin:0.3em 0 0.5em"><source src="../_static/audio/synth_singing_bowl_ring.wav" type="audio/wav"></audio>
Rain Stick
~~~~~~~~~~
Cascading pebbles through a cactus tube with internal pins. Two variants:
steep angle (fast cascade) and shallow angle (slow trickle).
.. code-block:: python
p.hit(DrumSound.RAINSTICK, Duration.WHOLE * 3) # steep — fast cascade
p.hit(DrumSound.RAINSTICK_SLOW, Duration.WHOLE * 4) # shallow — gentle trickle
.. raw:: html
<audio controls style="width:100%;margin:0.3em 0 0.5em"><source src="../_static/audio/rainstick.wav" type="audio/wav"></audio>
<audio controls style="width:100%;margin:0.3em 0 0.5em"><source src="../_static/audio/rainstick_slow.wav" type="audio/wav"></audio>
Ocean Drum
~~~~~~~~~~
Steel beads rolling inside a frame drum — tilting produces a smooth surf wash.
.. code-block:: python
p.hit(DrumSound.OCEAN_DRUM, Duration.WHOLE * 3)
.. raw:: html
<audio controls style="width:100%;margin:0.3em 0 0.5em"><source src="../_static/audio/ocean_drum.wav" type="audio/wav"></audio>
Cabasa
~~~~~~
Metal bead chain scraped against a textured cylinder — brighter and
more metallic than a shaker.
.. code-block:: python
p.hit(DrumSound.CABASA, Duration.EIGHTH)
.. raw:: html
<audio controls style="width:100%;margin:0.3em 0 0.5em"><source src="../_static/audio/cabasa.wav" type="audio/wav"></audio>
Wind Chimes
~~~~~~~~~~~
Suspended metal tubes struck by hand or breeze. Each tube rings at
its own pitch with slight time offsets.
.. code-block:: python
p.hit(DrumSound.WIND_CHIMES, Duration.WHOLE * 3)
.. raw:: html
<audio controls style="width:100%;margin:0.3em 0 0.5em"><source src="../_static/audio/wind_chimes.wav" type="audio/wav"></audio>
Finger Cymbal
~~~~~~~~~~~~~
Single small cymbal tap (zill) — bright metallic ping.
.. code-block:: python
p.hit(DrumSound.FINGER_CYMBAL, Duration.HALF)
.. raw:: html
<audio controls style="width:100%;margin:0.3em 0 0.5em"><source src="../_static/audio/finger_cymbal.wav" type="audio/wav"></audio>
Analog Oscillator Drift
~~~~~~~~~~~~~~~~~~~~~~~~
@@ -1018,13 +1262,19 @@ distorted_guitar, orange_crunch, metal_guitar, bass_guitar, upright_bass,
harp, sitar, koto, banjo, mandolin, mandola, ukulele
**World/Exotic**: pedal_steel, theremin, kalimba, steel_drum, didgeridoo,
bagpipe, singing_bowl, singing_bowl_ring
bagpipe, singing_bowl, singing_bowl_ring, tingsha
**Synth**: synth_lead, synth_pad, synth_bass, acid_bass, 808_bass,
granular_pad, granular_texture, vocal, choir
**Mellotron**: mellotron, mellotron_strings, mellotron_flute, mellotron_choir
**Analog**: sync_lead, sync_lead_bright, ring_mod_bell, ring_mod_metallic,
wavefold_warm, wavefold_gnarly, drift_saw, drift_square, analog_pad,
analog_bass
**Percussion**: vibraphone, marimba, xylophone, glockenspiel, tubular_bells,
timpani
timpani, crotales
Explicit kwargs override preset defaults:
@@ -1065,3 +1315,11 @@ Some practical combos worth memorizing:
long reverb and you're scoring a nature documentary.
- ``saw`` + ``pluck`` = **funk stab.** Short, sharp, bright. The
sound of Nile Rodgers' right hand.
- ``hard_sync`` + ``pluck`` = **prophet lead.** Bright formant peak
that cuts through any mix. The opening riff of every 80s synth solo.
- ``wavefold`` + ``organ`` = **west coast bass.** Warm, harmonically
rich sine-derivative that pairs beautifully with a lowpass after.
- ``drift`` + ``pad`` = **analog pad.** A sawtooth that breathes and
wobbles like a real VCO. Add chorus and reverb for Juno vibes.
- ``mellotron_synth`` + ``organ`` = **prog strings.** Haunted tape
machine. Add cathedral reverb and you're in 1972.
+1 -1
View File
@@ -1,6 +1,6 @@
[project]
name = "pytheory"
version = "0.40.1"
version = "0.40.9"
description = "Music Theory for Humans"
readme = "README.md"
license = "MIT"
+1 -1
View File
@@ -1,6 +1,6 @@
"""PyTheory: Music Theory for Humans."""
__version__ = "0.40.1"
__version__ = "0.40.9"
from .tones import Tone, Interval
from .systems import System, SYSTEMS, TET
+779 -49
View File
@@ -203,6 +203,168 @@ def pwm_fast_wave(hz, peak=SAMPLE_PEAK, n_samples=SAMPLE_RATE):
return pwm_wave(hz, peak, n_samples, lfo_rate=3.0)
def hard_sync_wave(hz, peak=SAMPLE_PEAK, n_samples=SAMPLE_RATE,
slave_ratio=1.5):
"""Hard-sync oscillator — slave saw reset by master clock.
The quintessential analog lead sound. A "slave" oscillator runs at
a different frequency but is forced to restart its cycle every time
the "master" oscillator completes one. The abrupt restart creates
bright, harmonically complex formant peaks that sweep as the slave
ratio changes.
This is THE sound of the Prophet-5, Moog Prodigy, and every
screaming analog lead since 1978.
Args:
slave_ratio: Slave frequency as a multiple of master.
1.0 = unison (plain saw). 1.53.0 = sweet spot for leads.
Higher = more metallic, ring-mod-like.
"""
t = numpy.arange(n_samples, dtype=numpy.float64) / SAMPLE_RATE
# Master phase ramps 0→1 at hz
master_phase = (t * hz) % 1.0
# Detect master zero-crossings (phase resets)
resets = numpy.diff(master_phase, prepend=master_phase[0]) < -0.5
# Slave phase: runs at slave_ratio * hz, but resets with master
slave_phase = numpy.zeros(n_samples, dtype=numpy.float64)
phase = 0.0
slave_freq = hz * slave_ratio
dt = 1.0 / SAMPLE_RATE
for i in range(n_samples):
if resets[i]:
phase = 0.0
slave_phase[i] = phase
phase += slave_freq * dt
phase %= 1.0
# Slave is a sawtooth: 2*phase - 1
wave = 2.0 * slave_phase - 1.0
return (peak * wave).astype(numpy.int16)
def ring_mod_wave(hz, peak=SAMPLE_PEAK, n_samples=SAMPLE_RATE,
mod_ratio=1.5):
"""Ring modulation — two oscillators multiplied together.
Multiplying two signals produces sum and difference frequencies,
creating inharmonic, metallic, bell-like tones. Unlike FM, ring mod
produces only sidebands no carrier or modulator in the output.
Classic Dalek voice, Stockhausen elektronische Musik, and the
metallic clang of every sci-fi soundtrack.
Args:
mod_ratio: Modulator frequency as a multiple of carrier.
Integer ratios (2, 3) = harmonic (bell-like).
Non-integer (1.5, 2.1) = inharmonic (metallic, alien).
"""
t = numpy.arange(n_samples, dtype=numpy.float64) / SAMPLE_RATE
carrier = numpy.sin(2 * numpy.pi * hz * t)
modulator = numpy.sin(2 * numpy.pi * hz * mod_ratio * t)
wave = carrier * modulator
return (peak * wave).astype(numpy.int16)
def wavefold_wave(hz, peak=SAMPLE_PEAK, n_samples=SAMPLE_RATE,
folds=3.0):
"""Wavefolding — signal folded back on itself for complex harmonics.
The heart of west coast synthesis (Buchla, Make Noise, Verbos).
A sine wave is amplified past ±1.0, then "folded" the overflow
bounces back instead of clipping. Each fold adds a new pair of
harmonics. At low fold counts it's warm and round; crank it up
and it gets buzzy, gnarly, and alive.
Sounds completely different from subtractive synthesis instead
of removing harmonics with a filter, you're *generating* them
by shaping the wave. Pairs beautifully with a lowpass filter.
Args:
folds: Drive amount. 1.0 = clean sine. 24 = sweet spot.
6+ = harsh, buzzy territory.
"""
t = numpy.arange(n_samples, dtype=numpy.float64) / SAMPLE_RATE
wave = numpy.sin(2 * numpy.pi * hz * t) * folds
# Triangle-fold: repeatedly reflect at ±1
# Uses the mathematical identity for folding
wave = 4.0 * numpy.abs((wave / 4.0 + 0.25) % 1.0 - 0.5) - 1.0
return (peak * wave).astype(numpy.int16)
def drift_wave(hz, peak=SAMPLE_PEAK, n_samples=SAMPLE_RATE,
shape="saw", drift_amount=0.15):
"""Analog VCO with pitch drift, instability, and soft noise floor.
Real analog oscillators are never perfectly stable. Capacitor
charging, thermal variations, and component tolerances make the
pitch wander slightly. This is what makes a Minimoog sound "fat"
and a VST sound "thin" the constant micro-motion of imperfect
hardware.
Models:
- Slow pitch drift (< 1 Hz wander, like warming up)
- Fast jitter (subtle per-cycle randomness)
- Soft analog noise floor (faint hiss blended in)
- Slightly rounded edges (no mathematically perfect transitions)
Args:
shape: Base oscillator "saw", "square", "triangle", or "pulse".
drift_amount: How unstable the oscillator is.
0.05 = studio-grade (Sequential, Oberheim).
0.15 = classic vintage (Minimoog, ARP).
0.3 = barely-holding-it-together (old SH-101).
"""
t = numpy.arange(n_samples, dtype=numpy.float64) / SAMPLE_RATE
rng = numpy.random.default_rng(int(hz * 100) % 2**31)
# Slow pitch drift — 2 LFOs at sub-Hz rates with random phase
drift1 = drift_amount * 0.6 * numpy.sin(
2 * numpy.pi * 0.07 * t + rng.uniform(0, 2 * numpy.pi))
drift2 = drift_amount * 0.4 * numpy.sin(
2 * numpy.pi * 0.23 * t + rng.uniform(0, 2 * numpy.pi))
# Fast jitter — per-sample noise filtered to ~50 Hz bandwidth
jitter_raw = rng.normal(0, drift_amount * 0.08, n_samples)
# Simple one-pole lowpass for jitter smoothing
alpha = 2 * numpy.pi * 50.0 / SAMPLE_RATE
jitter = numpy.zeros(n_samples, dtype=numpy.float64)
jitter[0] = jitter_raw[0]
for i in range(1, n_samples):
jitter[i] = jitter[i-1] + alpha * (jitter_raw[i] - jitter[i-1])
# Instantaneous frequency with drift (in cents, converted to ratio)
cents_offset = drift1 + drift2 + jitter
freq_ratio = 2.0 ** (cents_offset / 1200.0)
# Accumulate phase with varying frequency
phase = numpy.cumsum(hz * freq_ratio / SAMPLE_RATE)
phase %= 1.0
# Generate waveform from phase
if shape == "square":
wave = numpy.where(phase < 0.5, 1.0, -1.0)
elif shape == "triangle":
wave = 4.0 * numpy.abs(phase - 0.5) - 1.0
elif shape == "pulse":
wave = numpy.where(phase < 0.25, 1.0, -1.0)
else: # saw
wave = 2.0 * phase - 1.0
# Soft edges — gentle lowpass to round off transitions
cutoff = min(16000, hz * 12)
bl, al = scipy.signal.butter(1, cutoff, btype='low', fs=SAMPLE_RATE)
wave = scipy.signal.lfilter(bl, al, wave)
# Subtle analog noise floor
noise = rng.normal(0, 0.005, n_samples)
wave = wave + noise
mx = numpy.abs(wave).max()
if mx > 0:
wave /= mx
return (peak * wave).astype(numpy.int16)
def pluck_wave(hz, peak=SAMPLE_PEAK, n_samples=SAMPLE_RATE):
"""Karplus-Strong plucked string synthesis.
@@ -534,6 +696,145 @@ def wurlitzer_wave(hz, peak=SAMPLE_PEAK, n_samples=SAMPLE_RATE):
return (peak * wave).astype(numpy.int16)
def mellotron_wave(hz, peak=SAMPLE_PEAK, n_samples=SAMPLE_RATE,
tape="strings"):
"""Mellotron — tape-replay keyboard from the 1960s.
Each key triggers a strip of magnetic tape with a pre-recorded
instrument the original "sampler." The mechanical transport gives
it a lo-fi, haunted quality that no digital emulation fully captures:
- Tape flutter: pitch wobbles from uneven capstan speed
- Limited bandwidth: 300 Hz6 kHz, like a worn cassette
- Tape saturation: soft compression, rounded transients
- 8-second limit: tapes physically run out (we model the fadeout)
- Head noise: faint hiss baked into the character
The Mellotron defined the sound of Strawberry Fields Forever,
Stairway to Heaven, and every prog rock record from 19691977.
Args:
tape: Which tape bank to simulate.
"strings" the iconic MkII string section
"flute" breathy, haunting solo flute
"choir" ghostly vocal pad
"""
t = numpy.arange(n_samples, dtype=numpy.float64) / SAMPLE_RATE
rng = numpy.random.default_rng(int(hz * 100) % 2**31)
# --- Tape flutter: slow wow + faster flutter ---
wow = 0.12 * numpy.sin(2 * numpy.pi * 0.4 * t + rng.uniform(0, 6.28))
flutter = 0.06 * numpy.sin(2 * numpy.pi * 6.3 * t + rng.uniform(0, 6.28))
flutter += 0.03 * numpy.sin(2 * numpy.pi * 9.7 * t + rng.uniform(0, 6.28))
# Cents of pitch deviation
pitch_cents = wow + flutter
freq_ratio = 2.0 ** (pitch_cents / 1200.0)
# Accumulate phase with flutter
inst_freq = hz * freq_ratio
phase = numpy.cumsum(inst_freq / SAMPLE_RATE)
# --- Generate the "tape" source ---
nyquist = SAMPLE_RATE / 2.0
wave = numpy.zeros(n_samples, dtype=numpy.float64)
if tape == "flute":
# Breathy flute: fundamental + weak odd harmonics + breath noise
wave += 0.7 * numpy.sin(2 * numpy.pi * phase)
if hz * 3 < nyquist:
wave += 0.12 * numpy.sin(2 * numpy.pi * 3 * phase + rng.uniform(0, 6.28))
if hz * 5 < nyquist:
wave += 0.04 * numpy.sin(2 * numpy.pi * 5 * phase + rng.uniform(0, 6.28))
# Breath noise — bandpass filtered around fundamental
breath = rng.normal(0, 0.25, n_samples)
bw = max(100, hz * 0.3)
lo = max(20, hz - bw)
hi = min(nyquist * 0.95, hz + bw)
bb, ab = scipy.signal.butter(2, [lo, hi], btype='band', fs=SAMPLE_RATE)
breath = scipy.signal.lfilter(bb, ab, breath)
wave += breath
elif tape == "choir":
# Ghostly vocal pad: formant-shaped harmonics with slow drift
formants = [
(800, 100), # first formant ~'ah'
(1200, 120), # second formant
(2500, 200), # third formant
]
n_harmonics = min(20, int(nyquist / hz))
for n in range(1, n_harmonics + 1):
f_n = hz * n
if f_n >= nyquist:
break
amp = 1.0 / n
# Shape by formant peaks
for f_center, f_bw in formants:
amp *= 1.0 + 1.5 * numpy.exp(-((f_n - f_center) / f_bw) ** 2)
p = rng.uniform(0, 2 * numpy.pi)
wave += amp * numpy.sin(2 * numpy.pi * n * phase + p)
# Slow ensemble drift between "voices"
drift = 0.005 * numpy.sin(2 * numpy.pi * 0.15 * t + rng.uniform(0, 6.28))
wave2 = numpy.zeros(n_samples, dtype=numpy.float64)
phase2 = numpy.cumsum(hz * (1.0 + drift) * freq_ratio / SAMPLE_RATE)
for n in range(1, min(8, int(nyquist / hz)) + 1):
f_n = hz * n
if f_n >= nyquist:
break
amp = 0.6 / n
for f_center, f_bw in formants:
amp *= 1.0 + 1.5 * numpy.exp(-((f_n - f_center) / f_bw) ** 2)
wave2 += amp * numpy.sin(2 * numpy.pi * n * phase2 + rng.uniform(0, 6.28))
wave += wave2
else:
# Strings (default): layered ensemble with detuned unison
n_harmonics = min(25, int(nyquist / hz))
# Two "sections" slightly detuned for ensemble width
for section_detune in [-1.5, 0, 1.5]:
section_hz = hz * (2 ** (section_detune / 1200.0))
section_phase = numpy.cumsum(section_hz * freq_ratio / SAMPLE_RATE)
for n in range(1, n_harmonics + 1):
f_n = section_hz * n
if f_n >= nyquist:
break
# String-like: 1/n rolloff, even harmonics slightly weaker
amp = 1.0 / n
if n % 2 == 0:
amp *= 0.8
p = rng.uniform(0, 2 * numpy.pi)
wave += amp * numpy.sin(2 * numpy.pi * n * section_phase + p)
# Normalize before processing
mx = numpy.abs(wave).max()
if mx > 0:
wave /= mx
# --- Tape bandwidth limiting: 300 Hz 6 kHz ---
lo_cut = min(300, hz * 0.9) # don't cut fundamental
hi_cut = min(6000, nyquist * 0.95)
if lo_cut < hi_cut and lo_cut > 0:
bb, ab = scipy.signal.butter(2, [lo_cut, hi_cut], btype='band', fs=SAMPLE_RATE)
wave = scipy.signal.lfilter(bb, ab, wave)
# --- Tape saturation: soft compression ---
wave = numpy.tanh(wave * 1.4) / 1.2
# --- Tape run-out: gentle fadeout after ~7 seconds ---
if n_samples > int(SAMPLE_RATE * 7):
fadeout_start = int(SAMPLE_RATE * 7)
fadeout_len = n_samples - fadeout_start
fade = numpy.linspace(1.0, 0.0, fadeout_len)
wave[fadeout_start:] *= fade
# --- Head noise / tape hiss ---
hiss = rng.normal(0, 0.008, n_samples)
wave += hiss
mx = numpy.abs(wave).max()
if mx > 0:
wave /= mx
return (peak * wave).astype(numpy.int16)
def vibraphone_wave(hz, peak=SAMPLE_PEAK, n_samples=SAMPLE_RATE):
"""Vibraphone — struck aluminum bars with motor-driven tremolo.
@@ -1187,62 +1488,131 @@ def timpani_wave(hz, peak=SAMPLE_PEAK, n_samples=SAMPLE_RATE):
def saxophone_wave(hz, peak=SAMPLE_PEAK, n_samples=SAMPLE_RATE):
"""Saxophone — single reed through a conical brass bore.
"""Saxophone — single reed driving a conical brass bore.
The conical bore produces all harmonics (like oboe), but the
brass body and larger mouthpiece give a warmer, fatter, more
vocal quality. The reed adds a slight buzz. Saxophone is
between clarinet (odd harmonics) and oboe (nasal even+odd) —
it has everything, with a strong fundamental and rich mids.
Models the key acoustic properties of a saxophone:
1. Reed-bore interaction nonlinear clipping creates the characteristic
bright, edgy tone (not just additive sines)
2. Conical bore formants vocal-like resonances at ~500, ~1400, ~2300,
~3200 Hz that give sax its singing quality
3. Breath noise turbulent airflow through the mouthpiece, strongest
at attack and blending into sustained tone
4. Sub-harmonic warmth the conical bore's coupling creates warmth
below the fundamental
5. Vibrato delayed onset, ~5 Hz, characteristic of jazz/classical sax
"""
import scipy.signal as _sig
t = numpy.arange(n_samples, dtype=numpy.float64) / SAMPLE_RATE
rng = numpy.random.default_rng(int(hz * 100) % 2**31)
# Vibrato develops after ~250ms, wider than flute
vib_onset = numpy.clip(t / 0.25, 0.0, 1.0)
vib = hz * 0.0012 * vib_onset * numpy.sin(2 * numpy.pi * 5.2 * t)
# --- Vibrato: delayed onset, subtle depth ---
vib_onset = numpy.clip((t - 0.3) / 0.3, 0.0, 1.0)
vib_rate = 5.0 + 0.15 * numpy.sin(2 * numpy.pi * 0.4 * t)
vib = hz * 0.0006 * vib_onset * numpy.sin(2 * numpy.pi * vib_rate * t)
# --- Core tone: sawtooth-like waveform with reed clipping ---
# Real sax reed creates a quasi-sawtooth pressure wave, not pure sines.
# Build from harmonics with sax-specific spectral envelope, then clip.
wave = numpy.zeros(n_samples, dtype=numpy.float64)
n_harmonics = min(20, int((SAMPLE_RATE / 2) / hz))
n_harmonics = min(25, int((SAMPLE_RATE / 2) / hz))
for n in range(1, n_harmonics + 1):
f_n = hz * n
if f_n >= SAMPLE_RATE / 2:
break
# Sax spectral shape: strong fundamental, broad mid peak (3-6),
# slower rolloff than oboe (brass body carries harmonics further)
# Saxophone spectral envelope from acoustic measurements:
# Strong fundamental, nearly-as-strong 2nd and 3rd harmonics,
# broad energy peak around harmonics 4-8 (the "body"), then
# gradual rolloff — but slower than other woodwinds (brass bore
# sustains upper partials).
if n == 1:
amp = 1.0
elif n <= 3:
amp = 0.6
elif n <= 6:
amp = 0.4 * numpy.exp(-0.1 * (n - 4) ** 2)
elif n == 2:
amp = 0.85
elif n == 3:
amp = 0.7
elif n <= 8:
# Broad mid peak — this is the sax "meat"
amp = 0.55 * numpy.exp(-0.06 * (n - 5) ** 2)
else:
amp = 0.2 / n
# Slower rolloff than oboe/clarinet
amp = 0.35 / (n ** 0.7)
# Slight even/odd asymmetry — conical bore has all harmonics
# but evens are ~10% weaker (midway between cylinder and cone)
if n % 2 == 0:
amp *= 0.9
phase = rng.uniform(0, 2 * numpy.pi)
wave += amp * numpy.sin(2 * numpy.pi * (f_n + vib * n) * t + phase)
# Reed buzz — more present than oboe but still warm
reed = rng.normal(0, 0.07, n_samples)
# Bandpass the reed noise around 1-3kHz (the "honk" range)
import scipy.signal as _sig
reed_lo = max(20, int(hz * 2))
reed_hi = min(SAMPLE_RATE // 2 - 1, int(hz * 6))
# --- Reed nonlinearity: soft clipping ---
# The reed closes against the mouthpiece, creating asymmetric clipping
# that adds brightness and "edge". This is what makes sax sound like
# sax and not a flute.
wave_max = numpy.abs(wave).max()
if wave_max > 0:
wave /= wave_max
# Asymmetric soft clip: positive peaks clip harder (reed closure)
wave = numpy.tanh(1.8 * wave) * 0.7 + numpy.tanh(2.5 * wave) * 0.3
# --- Formant resonances: conical bore creates vocal quality ---
# These fixed resonances are what make sax sound "vocal" — they
# emphasize certain frequency bands regardless of the note played.
formant_freqs = [520, 1380, 2300, 3200]
formant_bws = [120, 200, 280, 350]
formant_gains = [0.25, 0.18, 0.12, 0.08]
formant_sum = numpy.zeros(n_samples, dtype=numpy.float64)
for fc, bw, gain in zip(formant_freqs, formant_bws, formant_gains):
lo = max(20, int(fc - bw))
hi = min(SAMPLE_RATE // 2 - 1, int(fc + bw))
if lo < hi:
bf, af = _sig.butter(2, [lo, hi], btype='band', fs=SAMPLE_RATE)
formant_sum += _sig.lfilter(bf, af, wave) * gain
wave = wave * 0.7 + formant_sum
# --- Breath noise: turbulent air through the mouthpiece ---
# Strongest at the attack, then settles to a subtle constant hiss
# that gives the tone "life" and prevents it from sounding synthetic.
breath = rng.normal(0, 1.0, n_samples)
# Shape breath noise into the sax's "hiss" band (2-6 kHz)
breath_lo = max(20, 2000)
breath_hi = min(SAMPLE_RATE // 2 - 1, 6000)
if breath_lo < breath_hi:
bb, ab = _sig.butter(2, [breath_lo, breath_hi], btype='band', fs=SAMPLE_RATE)
breath = _sig.lfilter(bb, ab, breath)
# Attack envelope for breath — strong at onset, then quiet
breath_env = 0.15 * numpy.exp(-8.0 * t) + 0.03
wave += breath * breath_env
# --- Reed buzz: low-frequency interaction noise ---
# Different from breath — this is the "buzz" from reed vibration
# against the mouthpiece, centered around the playing frequency.
reed_noise = rng.normal(0, 1.0, n_samples)
reed_lo = max(20, int(hz * 0.8))
reed_hi = min(SAMPLE_RATE // 2 - 1, int(hz * 4))
if reed_lo < reed_hi:
br, ar = _sig.butter(2, [reed_lo, reed_hi], btype='band', fs=SAMPLE_RATE)
reed = _sig.lfilter(br, ar, reed).astype(numpy.float64) * 2.0
wave += reed
reed_noise = _sig.lfilter(br, ar, reed_noise) * 0.06
wave += reed_noise
# Brass body warmth — low-mid boost
center = min(1500, hz * 4)
bw = 500
lo = max(20, int(center - bw))
hi = min(SAMPLE_RATE // 2 - 1, int(center + bw))
if lo < hi:
bp, ap = _sig.butter(2, [lo, hi], btype='band', fs=SAMPLE_RATE)
body = _sig.lfilter(bp, ap, wave) * 0.2
wave += body
# --- Attack transient: key click + breath burst ---
attack_len = min(int(SAMPLE_RATE * 0.015), n_samples)
if attack_len > 0:
click = rng.uniform(-1.0, 1.0, attack_len)
click *= numpy.exp(-numpy.linspace(0, 8, attack_len))
wave[:attack_len] += click * 0.12
# --- Sub-harmonic warmth ---
# Conical bore coupling produces energy slightly below fundamental
if hz > 80: # only if there's room
sub = numpy.sin(2 * numpy.pi * (hz * 0.5) * t) * 0.04
sub *= numpy.clip(t / 0.1, 0.0, 1.0) # fade in gently
wave += sub
# --- Final shaping ---
mx = numpy.abs(wave).max()
if mx > 0:
wave /= mx
@@ -2048,6 +2418,99 @@ def sitar_wave(hz, peak=SAMPLE_PEAK, n_samples=SAMPLE_RATE):
return (peak * out).astype(numpy.int16)
def crotales_wave(hz, peak=SAMPLE_PEAK, n_samples=SAMPLE_RATE):
"""Crotales — small tuned bronze discs struck with brass mallets.
Antique cymbals. Bright, crystalline, bell-like tone that rings
for a very long time. The partials are nearly harmonic (closer
to a bell than a bar) with strong upper harmonics that give
crotales their penetrating brilliance. Played in the octave
above written they cut through any orchestra.
"""
t = numpy.arange(n_samples, dtype=numpy.float64) / SAMPLE_RATE
rng = numpy.random.default_rng(int(hz * 100) % 2**31)
wave = numpy.zeros(n_samples, dtype=numpy.float64)
# Bronze disc modes — nearly harmonic, very bright.
# Higher partials are stronger than in most percussion,
# which is what gives crotales their cutting brilliance.
# (ratio, amplitude, decay_rate)
disc_modes = [
(1.0, 1.0, 0.3), # fundamental — rings for ages
(2.0, 0.6, 0.4), # octave — strong
(3.01, 0.35, 0.6), # near-12th — slight inharmonicity
(4.03, 0.25, 0.9), # double octave
(5.06, 0.15, 1.3), # bright
(6.1, 0.08, 2.0), # shimmer
(8.15, 0.04, 3.0), # sparkle at the top
]
for ratio, amp, decay_rate in disc_modes:
f = hz * ratio
if f >= SAMPLE_RATE / 2:
break
phase = rng.uniform(0, 2 * numpy.pi)
mode_decay = numpy.exp(-decay_rate * t)
wave += amp * numpy.sin(2 * numpy.pi * f * t + phase) * mode_decay
# Hard mallet strike — brass on bronze, bright transient
strike_len = min(int(SAMPLE_RATE * 0.002), n_samples)
strike_t = numpy.linspace(0, 1, strike_len)
strike = 0.5 * numpy.sin(2 * numpy.pi * hz * 8 * strike_t) * numpy.exp(-strike_t * 25)
wave[:strike_len] += strike
mx = numpy.abs(wave).max()
if mx > 0:
wave /= mx
return (peak * wave).astype(numpy.int16)
def tingsha_wave(hz, peak=SAMPLE_PEAK, n_samples=SAMPLE_RATE):
"""Tingsha — two small Tibetan cymbals clashed together on a cord.
When the pair strikes, both discs ring simultaneously at slightly
different frequencies (no two are identical), producing a bright
ping with pronounced beating. The sound is thinner and higher
than a singing bowl a clear, cutting tone that fades over a
few seconds. The two-disc interference is the whole character.
"""
t = numpy.arange(n_samples, dtype=numpy.float64) / SAMPLE_RATE
rng = numpy.random.default_rng(int(hz * 100) % 2**31)
# Two discs at slightly different pitches — this IS the tingsha sound
detune = hz * 0.008 # ~14 cents apart, creates ~3-4 Hz beat at middle C
disc_a = numpy.sin(2 * numpy.pi * (hz - detune) * t)
disc_b = numpy.sin(2 * numpy.pi * (hz + detune) * t + rng.uniform(0, 2 * numpy.pi))
wave = (disc_a + disc_b) * 0.5
# Upper partials — both discs, slightly different inharmonicity
for ratio, amp, dec in [(2.72, 0.3, 5.0), (5.1, 0.12, 10.0), (8.3, 0.05, 18.0)]:
if hz * ratio >= SAMPLE_RATE / 2:
break
p1 = rng.uniform(0, 2 * numpy.pi)
p2 = rng.uniform(0, 2 * numpy.pi)
wave += amp * numpy.sin(2 * numpy.pi * hz * ratio * 0.998 * t + p1) * numpy.exp(-dec * t)
wave += amp * numpy.sin(2 * numpy.pi * hz * ratio * 1.002 * t + p2) * numpy.exp(-dec * t)
# Decay — medium ring, not as long as a singing bowl
decay = numpy.exp(-1.8 * t)
wave *= decay
# Clash transient — metal on metal, sharper than a mallet hit
clash_len = min(int(SAMPLE_RATE * 0.003), n_samples)
clash = rng.uniform(-0.4, 0.4, clash_len).astype(numpy.float64)
clash *= numpy.exp(-numpy.linspace(0, 20, clash_len))
wave[:clash_len] += clash
mx = numpy.abs(wave).max()
if mx > 0:
wave /= mx
return (peak * wave).astype(numpy.int16)
def singing_bowl_strike_wave(hz, peak=SAMPLE_PEAK, n_samples=SAMPLE_RATE):
"""Singing bowl strike — mallet hit that excites all modes at once.
@@ -2099,7 +2562,7 @@ def singing_bowl_strike_wave(hz, peak=SAMPLE_PEAK, n_samples=SAMPLE_RATE):
if mx > 0:
wave /= mx
return (peak * 0.8 * wave).astype(numpy.int16)
return (peak * wave).astype(numpy.int16)
def singing_bowl_ring_wave(hz, peak=SAMPLE_PEAK, n_samples=SAMPLE_RATE):
@@ -2146,7 +2609,7 @@ def singing_bowl_ring_wave(hz, peak=SAMPLE_PEAK, n_samples=SAMPLE_RATE):
if mx > 0:
wave /= mx
return (peak * 0.8 * wave).astype(numpy.int16)
return (peak * wave).astype(numpy.int16)
def _apply_envelope(samples, attack, decay, sustain, release, sample_rate=SAMPLE_RATE):
@@ -2287,8 +2750,20 @@ class Synth(Enum):
ACOUSTIC_GUITAR = "acoustic_guitar_synth"
SITAR = "sitar_synth"
ELECTRIC_GUITAR = "electric_guitar_synth"
CROTALES = "crotales_synth"
TINGSHA = "tingsha_synth"
SINGING_BOWL_STRIKE = "singing_bowl_strike_synth"
SINGING_BOWL_RING = "singing_bowl_ring_synth"
RHODES = "rhodes_synth"
WURLITZER = "wurlitzer_synth"
VIBRAPHONE = "vibraphone_synth"
PIPE_ORGAN = "pipe_organ_synth"
CHOIR = "choir_synth"
MELLOTRON = "mellotron_synth"
HARD_SYNC = "hard_sync"
RING_MOD = "ring_mod"
WAVEFOLD = "wavefold"
DRIFT = "drift"
def __call__(self, hz, **kwargs):
"""Make Synth members callable — dispatches to the wave function."""
@@ -2319,13 +2794,18 @@ _SYNTH_FUNCTIONS = {
"ukulele_synth": ukulele_wave,
"acoustic_guitar_synth": acoustic_guitar_wave,
"sitar_synth": sitar_wave, "electric_guitar_synth": electric_guitar_wave,
"crotales_synth": crotales_wave,
"tingsha_synth": tingsha_wave,
"singing_bowl_strike_synth": singing_bowl_strike_wave,
"singing_bowl_ring_synth": singing_bowl_ring_wave,
"mellotron_synth": mellotron_wave,
"hard_sync": hard_sync_wave, "ring_mod": ring_mod_wave,
"wavefold": wavefold_wave, "drift": drift_wave,
}
def _render(tone_or_chord, temperament="equal", synth=Synth.SINE, t=1_000,
envelope=Envelope.PIANO):
envelope=Envelope.PIANO, **synth_kw):
"""Render a tone or chord to a NumPy sample array.
Args:
@@ -2337,6 +2817,9 @@ def _render(tone_or_chord, temperament="equal", synth=Synth.SINE, t=1_000,
t: Duration in milliseconds.
envelope: ADSR envelope preset. Use ``Envelope.NONE`` for raw
output (old behavior).
**synth_kw: Extra keyword arguments forwarded to the synth wave
function (e.g. ``tape="flute"`` for Mellotron,
``slave_ratio=2.0`` for Hard Sync).
Returns:
A NumPy int16 array of audio samples.
@@ -2344,10 +2827,10 @@ def _render(tone_or_chord, temperament="equal", synth=Synth.SINE, t=1_000,
n_samples = int(SAMPLE_RATE * t / 1_000)
if isinstance(tone_or_chord, Tone):
waves = [synth(tone_or_chord.pitch(temperament=temperament), n_samples=n_samples)]
waves = [synth(tone_or_chord.pitch(temperament=temperament), n_samples=n_samples, **synth_kw)]
else:
waves = [
synth(tone.pitch(temperament=temperament), n_samples=n_samples)
synth(tone.pitch(temperament=temperament), n_samples=n_samples, **synth_kw)
for tone in tone_or_chord.tones
]
@@ -2362,7 +2845,7 @@ def _render(tone_or_chord, temperament="equal", synth=Synth.SINE, t=1_000,
def play(tone_or_chord, temperament="equal", synth=Synth.SINE, t=1_000,
envelope=Envelope.PIANO):
envelope=Envelope.PIANO, **synth_kw):
"""Play a tone or chord through the speakers.
Args:
@@ -2374,19 +2857,22 @@ def play(tone_or_chord, temperament="equal", synth=Synth.SINE, t=1_000,
t: Duration in milliseconds (default 1000).
envelope: ADSR envelope preset (default ``Envelope.PIANO``).
Use ``Envelope.NONE`` for raw waveform.
**synth_kw: Extra keyword arguments forwarded to the synth wave
function (e.g. ``tape="flute"`` for Mellotron).
Example::
>>> play(Tone.from_string("A4"), t=1_000)
>>> play(Chord.from_name("Am7"), synth=Synth.TRIANGLE, t=2_000)
>>> play(tone, envelope=Envelope.PAD, t=3_000)
>>> play(tone, synth=Synth.MELLOTRON, tape="choir", t=2_000)
"""
_play_for(_render(tone_or_chord, temperament=temperament, synth=synth,
t=t, envelope=envelope), ms=t)
t=t, envelope=envelope, **synth_kw), ms=t)
def save(tone_or_chord, path, temperament="equal", synth=Synth.SINE, t=1_000,
envelope=Envelope.PIANO):
envelope=Envelope.PIANO, **synth_kw):
"""Render a tone or chord and save it as a WAV file.
Args:
@@ -2396,6 +2882,8 @@ def save(tone_or_chord, path, temperament="equal", synth=Synth.SINE, t=1_000,
synth: Waveform type.
t: Duration in milliseconds (default 1000).
envelope: ADSR envelope preset (default ``Envelope.PIANO``).
**synth_kw: Extra keyword arguments forwarded to the synth wave
function.
Example::
@@ -2405,7 +2893,7 @@ def save(tone_or_chord, path, temperament="equal", synth=Synth.SINE, t=1_000,
import scipy.io.wavfile
samples = _render(tone_or_chord, temperament=temperament, synth=synth,
t=t, envelope=envelope)
t=t, envelope=envelope, **synth_kw)
normalized = samples.astype(numpy.float32) / SAMPLE_PEAK
# Convert to 16-bit PCM
pcm = (normalized * 32767).astype(numpy.int16)
@@ -3575,6 +4063,227 @@ def _synth_guiro(n_samples):
return wave
def _synth_rainstick_slow(n_samples):
"""Rain stick (shallow angle): slow trickle, longer cascade, sparser impacts."""
wave = numpy.zeros(n_samples, dtype=numpy.float32)
rng = numpy.random.default_rng(77)
cascade_len = min(n_samples, int(SAMPLE_RATE * 4.0))
n_pebbles = 800
# More uniform distribution — shallow angle means steadier flow
positions = rng.beta(1.2, 1.8, n_pebbles) * cascade_len
positions = positions.astype(int)
for pos in positions:
if pos >= n_samples - 100:
continue
peb_len = rng.integers(25, 90)
end = min(pos + peb_len, n_samples)
actual = end - pos
click = rng.uniform(-1.0, 1.0, actual).astype(numpy.float32)
click *= numpy.exp(-numpy.linspace(0, 10, actual).astype(numpy.float32))
click *= rng.uniform(0.03, 0.18)
wave[pos:end] += click
t = numpy.arange(cascade_len, dtype=numpy.float32) / SAMPLE_RATE
body = numpy.sin(2 * numpy.pi * 160 * t) * 0.04
body *= numpy.exp(-0.8 * t)
wave[:cascade_len] += body
full_env = numpy.ones(n_samples, dtype=numpy.float32)
fade_len = min(int(SAMPLE_RATE * 1.2), n_samples)
if fade_len > 0 and cascade_len > fade_len:
full_env[cascade_len - fade_len:cascade_len] = numpy.linspace(
1.0, 0.0, fade_len).astype(numpy.float32)
full_env[cascade_len:] = 0.0
wave *= full_env
mx = numpy.abs(wave).max()
if mx > 0:
wave /= mx * 1.5
return wave
def _synth_ocean_drum(n_samples):
"""Ocean drum: steel beads rolling inside a frame drum — surf wash.
Tilt the drum and the beads cascade across the internal head,
producing a smooth wash that sounds like ocean waves.
"""
wave = numpy.zeros(n_samples, dtype=numpy.float32)
rng = numpy.random.default_rng(55)
wash_len = min(n_samples, int(SAMPLE_RATE * 2.5))
t = numpy.arange(wash_len, dtype=numpy.float32) / SAMPLE_RATE
# Dense bead noise — smoother than rain stick (steel beads on drum head)
noise = rng.standard_normal(wash_len).astype(numpy.float32)
# Bandpass to ~1-6kHz — beads on mylar head
import scipy.signal as _sig
bp, ap = _sig.butter(2, [1000, 6000], btype='band', fs=SAMPLE_RATE)
noise = _sig.lfilter(bp, ap, noise).astype(numpy.float32)
# Swell envelope — wave comes in, peaks, recedes
swell = numpy.abs(numpy.sin(numpy.pi * t / t[-1])) ** 0.7 if wash_len > 0 else noise
noise *= swell * 0.5
# Drum body resonance
body = numpy.sin(2 * numpy.pi * 120 * t) * 0.08 * swell
wave[:wash_len] = noise + body
mx = numpy.abs(wave).max()
if mx > 0:
wave /= mx * 1.3
return wave
def _synth_cabasa(n_samples):
"""Cabasa: metal bead chain scraped against a cylinder.
Brighter and more metallic than a shaker the beads are steel
chain wrapped around a textured metal cylinder.
"""
n = min(n_samples, int(SAMPLE_RATE * 0.08))
t = numpy.arange(n, dtype=numpy.float32) / SAMPLE_RATE
rng = numpy.random.default_rng(33)
# Metallic noise — brighter than shaker
noise = rng.standard_normal(n).astype(numpy.float32)
# High-pass to emphasize the metallic chain sound
env = numpy.exp(-25 * t) + 0.4 * numpy.exp(-6 * t)
wave = noise * env * 0.5
# Metal bead resonances
wave += numpy.sin(2 * numpy.pi * 7500 * t) * 0.12 * numpy.exp(-30 * t)
wave += numpy.sin(2 * numpy.pi * 9200 * t) * 0.08 * numpy.exp(-35 * t)
out = numpy.zeros(n_samples, dtype=numpy.float32)
out[:n] = wave
return out
def _synth_wind_chimes(n_samples):
"""Wind chimes: multiple suspended metal tubes ringing at random intervals.
Each tube has its own pitch and decay. A hand strike or breeze
sets several ringing at once with slight time offsets.
"""
wave = numpy.zeros(n_samples, dtype=numpy.float32)
rng = numpy.random.default_rng(22)
chime_len = min(n_samples, int(SAMPLE_RATE * 3.0))
t = numpy.arange(chime_len, dtype=numpy.float32) / SAMPLE_RATE
# 6-8 tubes at different pitches — pentatonic-ish spread
tube_freqs = [1200, 1450, 1700, 2000, 2400, 2850, 3300]
for freq in tube_freqs:
# Each tube starts at a random offset (breeze hits them at different times)
offset = rng.integers(0, int(SAMPLE_RATE * 0.3))
if offset >= chime_len:
continue
tube_t = t[offset:]
tube_local = tube_t - tube_t[0]
# Tube mode with slight inharmonicity
tone = numpy.sin(2 * numpy.pi * freq * tube_local) * 0.2
tone += numpy.sin(2 * numpy.pi * freq * 2.73 * tube_local) * 0.06
# Each tube decays independently
decay = numpy.exp(-rng.uniform(2.0, 4.0) * tube_local)
tone *= decay
# Slight amplitude variation
tone *= rng.uniform(0.5, 1.0)
wave[offset:chime_len] += tone[:chime_len - offset].astype(numpy.float32)
mx = numpy.abs(wave).max()
if mx > 0:
wave /= mx
return wave
def _synth_finger_cymbal(n_samples):
"""Finger cymbal (zill): single small cymbal tap — bright metallic ping."""
n = min(n_samples, int(SAMPLE_RATE * 0.8))
t = numpy.arange(n, dtype=numpy.float32) / SAMPLE_RATE
rng = numpy.random.default_rng(11)
# High-pitched metallic modes
wave = numpy.sin(2 * numpy.pi * 3200 * t).astype(numpy.float32) * 0.5
wave += numpy.sin(2 * numpy.pi * 3210 * t).astype(numpy.float32) * 0.5 # beating pair
wave += numpy.sin(2 * numpy.pi * 7800 * t).astype(numpy.float32) * 0.15 * numpy.exp(-8 * t).astype(numpy.float32)
wave += numpy.sin(2 * numpy.pi * 12500 * t).astype(numpy.float32) * 0.06 * numpy.exp(-15 * t).astype(numpy.float32)
wave *= numpy.exp(-3.0 * t).astype(numpy.float32)
# Tap transient
tap_len = min(int(SAMPLE_RATE * 0.001), n)
wave[:tap_len] += rng.uniform(-0.2, 0.2, tap_len).astype(numpy.float32)
out = numpy.zeros(n_samples, dtype=numpy.float32)
out[:n] = wave
mx = numpy.abs(out).max()
if mx > 0:
out /= mx
return out
def _synth_rainstick(n_samples):
"""Rain stick: cascading pebbles through a cactus tube with internal pins.
Hundreds of tiny seed/pebble impacts falling through the tube,
each one a brief high-frequency click with a hint of resonance
from the hollow body. The density tapers off as gravity runs out.
"""
wave = numpy.zeros(n_samples, dtype=numpy.float32)
rng = numpy.random.default_rng(42)
# Duration of the cascade — up to 2.5 seconds
cascade_len = min(n_samples, int(SAMPLE_RATE * 2.5))
# Generate random pebble impacts — denser at the start, sparse at the end
n_pebbles = 800
# Positions weighted toward the beginning (gravity)
positions = rng.beta(1.5, 3.0, n_pebbles) * cascade_len
positions = positions.astype(int)
for pos in positions:
if pos >= n_samples - 100:
continue
# Each pebble: tiny noise click with random pitch resonance
peb_len = rng.integers(20, 80)
end = min(pos + peb_len, n_samples)
actual = end - pos
# Noise click
click = rng.uniform(-1.0, 1.0, actual).astype(numpy.float32)
# Fast decay
click *= numpy.exp(-numpy.linspace(0, 12, actual).astype(numpy.float32))
# Random amplitude — some pebbles louder than others
click *= rng.uniform(0.05, 0.25)
wave[pos:end] += click
# Tube body resonance — hollow cactus, low rumble underneath
t = numpy.arange(cascade_len, dtype=numpy.float32) / SAMPLE_RATE
body = numpy.sin(2 * numpy.pi * 180 * t) * 0.06
body *= numpy.exp(-1.5 * t)
# Modulate body resonance by the cascade density
env = numpy.exp(-1.2 * t)
body *= env
wave[:cascade_len] += body
# Overall envelope — smooth fade
full_env = numpy.ones(n_samples, dtype=numpy.float32)
fade_len = min(int(SAMPLE_RATE * 0.8), n_samples)
if fade_len > 0 and cascade_len > fade_len:
full_env[cascade_len - fade_len:cascade_len] = numpy.linspace(
1.0, 0.0, fade_len).astype(numpy.float32)
full_env[cascade_len:] = 0.0
wave *= full_env
mx = numpy.abs(wave).max()
if mx > 0:
wave /= mx * 1.5 # leave headroom
return wave
def _render_drum_hit(sound_value, n_samples):
"""Render a single drum sound to a float32 array.
@@ -3669,6 +4378,13 @@ def _render_drum_hit(sound_value, n_samples):
DrumSound.BASS_3.value: lambda n: _synth_march_bass(n, pitch=62),
DrumSound.BASS_4.value: lambda n: _synth_march_bass(n, pitch=52),
DrumSound.BASS_5.value: lambda n: _synth_march_bass(n, pitch=42),
# Effects / world
DrumSound.RAINSTICK.value: lambda n: _synth_rainstick(n),
DrumSound.RAINSTICK_SLOW.value: lambda n: _synth_rainstick_slow(n),
DrumSound.OCEAN_DRUM.value: lambda n: _synth_ocean_drum(n),
DrumSound.CABASA.value: lambda n: _synth_cabasa(n),
DrumSound.WIND_CHIMES.value: lambda n: _synth_wind_chimes(n),
DrumSound.FINGER_CYMBAL.value: lambda n: _synth_finger_cymbal(n),
}
renderer = _dispatch.get(sound_value, lambda n: _synth_clave(n))
@@ -4577,7 +5293,19 @@ def _apply_distortion(samples, drive=1.0, mix=1.0):
"""
if mix <= 0 or drive <= 0:
return samples
driven = numpy.tanh(samples * drive)
# Multi-stage gain + clipping like a real amp:
# Stage 1: preamp gain — push the signal hard
stage1 = numpy.tanh(samples * drive)
# Stage 2: power amp — clip again with more gain for sustain and grit
stage2 = numpy.tanh(stage1 * drive * 0.5)
# Stage 3: at high drive, add asymmetric clipping (tube rectifier sag)
if drive > 3.0:
# Positive peaks clip harder than negative — asymmetric harmonics
driven = numpy.where(stage2 > 0,
numpy.tanh(stage2 * 1.5),
numpy.tanh(stage2 * 1.2))
else:
driven = stage2
return samples * (1 - mix) + driven * mix
@@ -4679,7 +5407,7 @@ def _pan_to_stereo(mono, pan=0.0):
return stereo
def _master_compress(samples, threshold=0.5, ratio=4.0, attack=0.002,
def _master_compress(samples, threshold=0.7, ratio=4.0, attack=0.002,
release=0.05, makeup=True, limiter=True,
sample_rate=SAMPLE_RATE):
"""Master bus compressor with brick-wall limiter.
@@ -4734,11 +5462,13 @@ def _master_compress(samples, threshold=0.5, ratio=4.0, attack=0.002,
# Apply gain
compressed = samples * gain
# Makeup gain — bring the level back up
# Makeup gain — bring the level back up, but cap at 3x
# so sparse arrangements don't get over-amplified
if makeup:
peak = numpy.max(numpy.abs(compressed))
if peak > 0:
compressed = compressed / peak * 0.9
desired_gain = 0.9 / peak
compressed = compressed * min(desired_gain, 3.0)
# Brick-wall limiter — hard clip at 0.95
if limiter:
@@ -5164,8 +5894,8 @@ def render_score(score):
env_tuple = _resolve_envelope(part.envelope)
# Use part swing if set, otherwise score swing
effective_swing = part.swing if part.swing is not None else score.swing
# Build synth-specific kwargs (e.g. FM ratio/index)
synth_kwargs = {}
# Build synth-specific kwargs (e.g. FM ratio/index, tape, folds)
synth_kwargs = dict(getattr(part, 'synth_kw', None) or {})
if part.synth in ("fm",):
synth_kwargs["mod_ratio"] = part.fm_ratio
synth_kwargs["mod_index"] = part.fm_index
+6
View File
@@ -806,6 +806,12 @@ _INSTRUMENT_NAMES = [
# Synth presets
"synth_lead", "synth_pad", "synth_bass", "acid_bass",
"granular_pad", "vocal", "choir", "granular_texture", "808_bass",
# Mellotron
"mellotron", "mellotron_strings", "mellotron_flute", "mellotron_choir",
# Analog
"sync_lead", "sync_lead_bright", "ring_mod_bell", "ring_mod_metallic",
"wavefold_warm", "wavefold_gnarly", "drift_saw", "drift_square",
"analog_pad", "analog_bass",
# Percussion / Mallet
"vibraphone", "marimba", "xylophone", "glockenspiel", "tubular_bells", "timpani",
# Woodwinds (continued)
+125 -9
View File
@@ -265,6 +265,16 @@ INSTRUMENTS = {
"lowpass": 4500,
"humanize": 0.2,
},
"crotales": {
"synth": "crotales_synth", "envelope": "none",
"reverb": 0.3,
"humanize": 0.2,
},
"tingsha": {
"synth": "tingsha_synth", "envelope": "none",
"reverb": 0.4,
"humanize": 0.2,
},
"singing_bowl": {
"synth": "singing_bowl_strike_synth", "envelope": "none",
"reverb": 0.5,
@@ -332,12 +342,110 @@ INSTRUMENTS = {
"delay": 0.3, "delay_time": 0.4, "delay_feedback": 0.4,
},
"808_bass": {
"synth": "sine", "envelope": "pluck",
"synth": "sine", "envelope": "piano",
"distortion": 0.4, "distortion_drive": 2.5,
"lowpass": 200, "lowpass_q": 1.5,
"sub_osc": 0.5, "saturation": 0.2,
},
# ── Mellotron ──
"mellotron": {
"synth": "mellotron_synth", "envelope": "organ",
"reverb": 0.3, "reverb_type": "plate",
"humanize": 0.2,
},
"mellotron_strings": {
"synth": "mellotron_synth", "envelope": "organ",
"reverb": 0.3, "reverb_type": "plate",
"humanize": 0.2,
},
"mellotron_flute": {
"synth": "mellotron_synth", "envelope": "organ",
"synth_kw": {"tape": "flute"},
"reverb": 0.35, "reverb_type": "hall",
"humanize": 0.2,
},
"mellotron_choir": {
"synth": "mellotron_synth", "envelope": "organ",
"synth_kw": {"tape": "choir"},
"reverb": 0.4, "reverb_type": "cathedral",
"humanize": 0.2,
},
# ── Analog oscillator presets ──
"sync_lead": {
"synth": "hard_sync", "envelope": "pluck",
"synth_kw": {"slave_ratio": 1.5},
"detune": 8, "lowpass": 4000,
"filter_attack": 0.01, "filter_decay": 0.25,
"filter_sustain": 0.3, "filter_amount": 3000,
"delay": 0.15, "delay_time": 0.2, "delay_feedback": 0.25,
"analog": 0.3,
},
"sync_lead_bright": {
"synth": "hard_sync", "envelope": "pluck",
"synth_kw": {"slave_ratio": 2.5},
"detune": 10, "lowpass": 6000,
"filter_attack": 0.005, "filter_decay": 0.2,
"filter_sustain": 0.1, "filter_amount": 4000,
"analog": 0.3,
},
"ring_mod_bell": {
"synth": "ring_mod", "envelope": "bell",
"synth_kw": {"mod_ratio": 2.1},
"reverb": 0.4, "reverb_type": "plate",
},
"ring_mod_metallic": {
"synth": "ring_mod", "envelope": "mallet",
"synth_kw": {"mod_ratio": 3.7},
"reverb": 0.3, "reverb_type": "hall",
"delay": 0.2, "delay_time": 0.3, "delay_feedback": 0.3,
},
"wavefold_warm": {
"synth": "wavefold", "envelope": "organ",
"synth_kw": {"folds": 2.0},
"lowpass": 3000, "lowpass_q": 1.2,
"analog": 0.3,
},
"wavefold_gnarly": {
"synth": "wavefold", "envelope": "pluck",
"synth_kw": {"folds": 5.0},
"lowpass": 2000, "lowpass_q": 2.5,
"filter_attack": 0.01, "filter_decay": 0.3,
"filter_sustain": 0.1, "filter_amount": 4000,
"distortion": 0.3, "distortion_drive": 2.0,
"analog": 0.3,
},
"drift_saw": {
"synth": "drift", "envelope": "organ",
"synth_kw": {"shape": "saw", "drift_amount": 0.15},
"detune": 10,
"analog": 0.4,
},
"drift_square": {
"synth": "drift", "envelope": "organ",
"synth_kw": {"shape": "square", "drift_amount": 0.15},
"detune": 10,
"analog": 0.4,
},
"analog_pad": {
"synth": "drift", "envelope": "pad",
"synth_kw": {"shape": "saw", "drift_amount": 0.12},
"detune": 12, "spread": 0.5,
"chorus": 0.2,
"lowpass": 2500, "lowpass_q": 1.0,
"analog": 0.5,
},
"analog_bass": {
"synth": "drift", "envelope": "pluck",
"synth_kw": {"shape": "saw", "drift_amount": 0.1},
"lowpass": 600, "lowpass_q": 2.0,
"filter_attack": 0.005, "filter_decay": 0.15,
"filter_sustain": 0.0, "filter_amount": 2000,
"sub_osc": 0.4,
"analog": 0.3,
},
# ── Percussion / Mallet ──
"vibraphone": {
"synth": "vibraphone_synth", "envelope": "none",
@@ -370,22 +478,19 @@ INSTRUMENTS = {
# ── Woodwinds (continued) ──
"saxophone": {
"synth": "saxophone_synth", "envelope": "bowed",
"humanize": 0.15, "vel_to_filter": 1500,
"humanize": 0.15,
},
"alto_sax": {
"synth": "saxophone_synth", "envelope": "bowed",
"humanize": 0.15, "vel_to_filter": 1800,
"humanize": 0.15,
},
"tenor_sax": {
"synth": "saxophone_synth", "envelope": "bowed",
"lowpass": 3000,
"humanize": 0.15, "vel_to_filter": 1200,
"humanize": 0.15,
},
"bari_sax": {
"synth": "saxophone_synth", "envelope": "bowed",
"lowpass": 2000,
"humanize": 0.15, "vel_to_filter": 800,
"sub_osc": 0.15,
"humanize": 0.15, "sub_osc": 0.15,
},
}
@@ -468,6 +573,8 @@ class Note:
@property
def beats(self) -> float:
if self._hold:
return 0.0
return self.duration.value
@@ -595,6 +702,13 @@ class DrumSound(Enum):
BASS_3 = 126 # middle
BASS_4 = 127 # fourth
BASS_5 = 80 # lowest (biggest) bass drum
# Effects / world percussion
RAINSTICK = 81 # cascading pebbles through cactus tube (steep angle)
RAINSTICK_SLOW = 128 # gentle trickle (shallow angle)
OCEAN_DRUM = 82 # tilting drum with steel beads — surf wash
CABASA = 83 # metal bead chain wrapped around cylinder
WIND_CHIMES = 84 # suspended metal tubes struck by wind/hand
FINGER_CYMBAL = 85 # single small cymbal tap (zill)
class _DrumTone:
@@ -2838,7 +2952,8 @@ class Part:
analog: float = 0.0,
ensemble: int = 1,
fm_ratio: float = 2.0,
fm_index: float = 3.0):
fm_index: float = 3.0,
synth_kw: dict = None):
self.name = name
self.synth = synth
self.envelope = envelope
@@ -2886,6 +3001,7 @@ class Part:
self.ensemble = ensemble
self.fm_ratio = fm_ratio
self.fm_index = fm_index
self.synth_kw = synth_kw or {}
self._system = "western" # default, overridden by Score.part()
self._fretboard = None # set by Score.part(fretboard=...)
self.notes: list[Note] = []
+3 -3
View File
@@ -5333,7 +5333,7 @@ def test_supersaw_wave():
@needs_portaudio
def test_all_synths_in_enum():
from pytheory.play import Synth
assert len(Synth) == 42
assert len(Synth) == 56
for s in Synth:
wave = s(440, n_samples=1000)
assert len(wave) == 1000
@@ -6540,7 +6540,7 @@ def test_instrument_808_bass():
assert p.lowpass == 200
assert p.lowpass_q == 1.5
assert p.synth == "sine"
assert p.envelope == "pluck"
assert p.envelope == "piano"
# ── Non-12-TET / Microtonal systems ─────────────────────────────────────────
@@ -7155,7 +7155,7 @@ def test_score_system_propagates():
def test_synth_enum_count():
from pytheory.play import Synth
assert len(Synth) == 42
assert len(Synth) == 56
def test_all_synths_render_and_enum_match():
Generated
+1 -1
View File
@@ -690,7 +690,7 @@ wheels = [
[[package]]
name = "pytheory"
version = "0.40.1"
version = "0.40.9"
source = { editable = "." }
dependencies = [
{ name = "rich" },