utils/music_theory/music_theory/tests/test_synth.py
Krzysztof kuhy Rudnicki d5b3f2cff3 music_theory: music theory from first principles, as code you can hear
New subproject porting Luke Haas's "Music Theory for Programmers" section
by section into typed Python: pitch (MIDI <-> Hz), tuning (harmonic
series, the Pythagorean comma, brute-forced "why twelve"), scales as step
arrays with modes as rotations, chords stacked in thirds with Roman
numerals and the V7 -> I resolution, named progressions, a numpy synth
(waveforms, ADSR, low-pass), a bar-exact sequencer, and a seeded Recipe ->
WAV composer. `degrade` is the same piece ruined by a 0..1 amount, so an
ending can be the opening destroyed on the same seed; filtering runs LAST
because filtering first measured brighter than the clean render.

17 demos (`python -m music_theory demo all`) each print the article's
numbers -- the comma is 1.013643, the tempered fifth is 0.113 % flat,
twelve is the first acceptable division -- and the FFT of the render, so
the claims are tested rather than asserted. 157 tests, 100 % branch
coverage, ruff ALL, mypy strict, pylint 10.

Consumed next by roadside-assistance for its three world-state beds.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VC17zymxkeiUcHrMvt8W5W
2026-09-11 18:29:13 +02:00

81 lines
2.6 KiB
Python

# Copyright (c) 2026 Krzysztof Rudnicki
from typing import cast
import numpy as np
import pytest
from music_theory import analysis, synth
def test_time_axis_length_and_negative() -> None:
assert len(synth.time_axis(1.0, 100)) == 100
assert len(synth.time_axis(-1.0, 100)) == 0
@pytest.mark.parametrize("waveform", synth.WAVEFORMS)
def test_every_waveform_peaks_at_its_frequency(waveform: synth.Waveform) -> None:
signal = synth.oscillator(440.0, 1.0, waveform)
assert analysis.dominant_frequency(signal) == pytest.approx(440.0, abs=1.0)
assert np.max(np.abs(signal)) <= 1.0
def test_oscillator_rejects_unknown_waveform() -> None:
with pytest.raises(ValueError, match="unknown waveform"):
synth.oscillator(440.0, 0.1, cast("synth.Waveform", "noise"))
def test_square_is_two_valued() -> None:
assert set(np.unique(synth.oscillator(10.0, 0.5, "square"))) == {-1.0, 1.0}
def test_additive_stacks_harmonics() -> None:
signal = synth.additive(100.0, 1.0, [0.0, 1.0])
assert analysis.dominant_frequency(signal) == pytest.approx(200.0, abs=1.0)
def test_adsr_shape() -> None:
gain = synth.adsr(synth.Envelope(0.1, 0.1, 0.5, 0.1), 1.0, 100)
assert len(gain) == 100
assert gain[0] == 0.0
assert gain[10] == pytest.approx(1.0)
assert gain[50] == pytest.approx(0.5)
assert gain[-1] < 0.5
assert gain[-1] >= 0.0
def test_adsr_longer_than_the_note_is_clamped() -> None:
gain = synth.adsr(synth.Envelope(1.0, 1.0, 0.5, 1.0), 0.1, 100)
assert len(gain) == 10
assert np.all(gain >= 0.0)
assert synth.adsr(synth.Envelope(), -1.0, 100).size == 0
def test_lowpass_darkens_and_zero_cutoff_silences() -> None:
saw = synth.oscillator(220.0, 0.5, "saw")
dark = synth.lowpass(saw, 500.0)
assert analysis.spectral_centroid(dark) < analysis.spectral_centroid(saw)
assert not np.any(synth.lowpass(saw, 0.0))
def test_detune_by_cents() -> None:
assert synth.detune(440.0, 1200.0) == pytest.approx(880.0)
assert synth.detune(440.0, 0.0) == 440.0
def test_mix_pads_shorter_signals() -> None:
mixed = synth.mix(np.ones(3), np.ones(5))
assert list(mixed) == [2.0, 2.0, 2.0, 1.0, 1.0]
assert synth.mix().size == 0
def test_normalize_scales_peak_and_keeps_silence() -> None:
assert np.max(np.abs(synth.normalize(np.array([0.1, -0.2])))) == pytest.approx(0.8)
assert not np.any(synth.normalize(np.zeros(4)))
assert synth.normalize(np.zeros(0)).size == 0
def test_to_int16_clips() -> None:
out = synth.to_int16(np.array([2.0, -2.0, 0.0]))
assert list(out) == [32767, -32767, 0]
assert out.dtype == np.int16