utils/music_theory/music_theory/tests/test_synth.py
Krzysztof kuhy Rudnicki 26c55675dd music_theory: sfx module -- noise, sweeps, biquads and 22 spellcode presets
Code-generated sound effects for spellcode (no AI audio, no sfxr port):
seeded white/pink noise and grain gates (noise), a phase-accumulating
oscillator over glide/vibrato/arpeggio/jump/retrigger pitch curves plus
ring mod (sweep), exponential and punch envelopes (envelopes), resonant
RBJ low/high/band-pass with a moving cutoff (biquad), and an sfxr-style
layered parameter set rendered at 44.1 kHz by default (sfx).

Presets: cast/hit/impact for arcane, fire, water, lightning, earth and
wind, plus dodge, parry_normal, parry_perfect and fizzle, following R1's
per-element recipes (arcane's is new). `python -m music_theory sfx --out
DIR` writes 16-bit mono WAVs and a SHA-256 manifest; manifest.entry now
takes any Recordable (Recipe or Sfx). analysis gains band_energy and
spectral_slope, which the tests use as the oracle.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016o4CqWFZwpfiDj8csAPgLa
2026-10-19 11:27:49 +02:00

90 lines
3.0 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
def test_waveshape_duty_sets_the_square_balance() -> None:
phase = np.linspace(0.0, 1.0, 1000, endpoint=False)
narrow = synth.waveshape(phase, "square", duty=0.25)
assert float(np.mean(narrow)) == pytest.approx(-0.5)
assert np.array_equal(synth.waveshape(phase, "saw"), 2.0 * phase - 1.0)
with pytest.raises(ValueError, match="unknown waveform"):
synth.waveshape(phase, cast("synth.Waveform", "noise"))