utils/music_theory/music_theory/manifest.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

63 lines
1.9 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, Protocol
from music_theory import __version__, wav
if TYPE_CHECKING:
from pathlib import Path
from music_theory.synth import Signal
Entry = dict[str, object]
class Recordable(Protocol):
"""Anything rendered from plain data: a ``compose.Recipe``, an ``sfx.Sfx``."""
@property
def sample_rate(self) -> int:
"""Rate the file was rendered at."""
def to_dict(self) -> dict[str, object]:
"""The full recipe as JSON-ready data."""
def entry(recipe: Recordable, 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