mirror of
https://github.com/kuhyx/utils.git
synced 2026-11-27 23:29:14 +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
53 lines
1.6 KiB
Python
53 lines
1.6 KiB
Python
# Copyright (c) 2026 Krzysztof Rudnicki
|
|
"""Manifest: the recipe of every rendered file, so it can be rebuilt.
|
|
|
|
A committed audio file whose recipe lives only in a chat transcript is
|
|
unreproducible. An entry here holds the full recipe, the package version
|
|
that rendered it and the SHA-256 of the output, so a re-render can prove
|
|
it reproduced the shipped bytes -- or show exactly what changed.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from typing import TYPE_CHECKING
|
|
|
|
from music_theory import __version__, wav
|
|
|
|
if TYPE_CHECKING:
|
|
from pathlib import Path
|
|
|
|
from music_theory.compose import Recipe
|
|
from music_theory.synth import Signal
|
|
|
|
Entry = dict[str, object]
|
|
|
|
|
|
def entry(recipe: Recipe, path: Path, signal: Signal) -> Entry:
|
|
"""Provenance record for one rendered file."""
|
|
return {
|
|
"file": path.name,
|
|
"recipe": recipe.to_dict(),
|
|
"music_theory_version": __version__,
|
|
"sample_rate": recipe.sample_rate,
|
|
"frames": len(signal),
|
|
"seconds": wav.duration_seconds(signal, recipe.sample_rate),
|
|
"sha256": wav.sha256(path),
|
|
}
|
|
|
|
|
|
def write(path: Path, entries: dict[str, Entry]) -> Path:
|
|
"""Write entries as sorted, indented JSON with a trailing newline."""
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_text(json.dumps(entries, indent=2, sort_keys=True) + "\n")
|
|
return path
|
|
|
|
|
|
def read(path: Path) -> dict[str, Entry]:
|
|
"""Load a manifest written by ``write``."""
|
|
data = json.loads(path.read_text())
|
|
if not isinstance(data, dict):
|
|
msg = f"{path} does not hold a manifest object"
|
|
raise TypeError(msg)
|
|
return data
|