mirror of
https://github.com/kuhyx/utils.git
synced 2026-11-26 06:47:23 +01:00
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
39 lines
1.2 KiB
Python
39 lines
1.2 KiB
Python
# Copyright (c) 2026 Krzysztof Rudnicki
|
|
import json
|
|
from typing import TYPE_CHECKING
|
|
|
|
import pytest
|
|
|
|
from music_theory import __version__, compose, manifest, wav
|
|
|
|
if TYPE_CHECKING:
|
|
from pathlib import Path
|
|
|
|
|
|
def test_entry_records_recipe_version_and_hash(tmp_path: Path) -> None:
|
|
recipe = compose.Recipe(bars=1)
|
|
signal = compose.compose(recipe)
|
|
path = wav.write(tmp_path / "bed.wav", signal, recipe.sample_rate)
|
|
record = manifest.entry(recipe, path, signal)
|
|
assert record["file"] == "bed.wav"
|
|
assert record["music_theory_version"] == __version__
|
|
assert record["sha256"] == wav.sha256(path)
|
|
assert record["frames"] == len(signal)
|
|
assert record["recipe"] == recipe.to_dict()
|
|
|
|
|
|
def test_write_and_read(tmp_path: Path) -> None:
|
|
path = tmp_path / "deep" / "m.json"
|
|
manifest.write(path, {"b": {"x": 1}, "a": {"y": 2}})
|
|
text = path.read_text()
|
|
assert text.endswith("\n")
|
|
assert text.index('"a"') < text.index('"b"')
|
|
assert manifest.read(path) == {"a": {"y": 2}, "b": {"x": 1}}
|
|
|
|
|
|
def test_read_rejects_non_object(tmp_path: Path) -> None:
|
|
path = tmp_path / "m.json"
|
|
path.write_text(json.dumps([1, 2]))
|
|
with pytest.raises(TypeError, match="manifest object"):
|
|
manifest.read(path)
|