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

69 lines
2.0 KiB
Python

# Copyright (c) 2026 Krzysztof Rudnicki
"""WAV: write what was rendered so a person can hear it, and fingerprint it.
16-bit mono PCM is what Godot's ``AudioStreamWAV`` and ``aplay`` both take
without conversion. The SHA-256 is what a manifest records so a re-render
can prove it reproduced the committed file byte for byte.
"""
from __future__ import annotations
import hashlib
from typing import TYPE_CHECKING
import wave
import numpy as np
from music_theory import synth
if TYPE_CHECKING:
from pathlib import Path
_BYTES_PER_SAMPLE = 2
_CHANNELS = 1
def write(
path: Path, signal: synth.Signal, sample_rate: int = synth.SAMPLE_RATE
) -> Path:
"""Write a float signal as 16-bit mono PCM and return the path."""
path.parent.mkdir(parents=True, exist_ok=True)
with wave.Wave_write(str(path)) as handle:
handle.setnchannels(_CHANNELS)
handle.setsampwidth(_BYTES_PER_SAMPLE)
handle.setframerate(sample_rate)
handle.writeframes(synth.to_int16(signal).tobytes())
return path
def read(path: Path) -> tuple[synth.Signal, int]:
"""Read a 16-bit mono WAV back into a float signal and its sample rate."""
with wave.Wave_read(str(path)) as handle:
if (
handle.getsampwidth() != _BYTES_PER_SAMPLE
or handle.getnchannels() != _CHANNELS
):
msg = f"{path} is not 16-bit mono"
raise ValueError(msg)
rate = handle.getframerate()
raw = handle.readframes(handle.getnframes())
samples = np.frombuffer(raw, dtype=np.int16).astype(np.float64) / 32767.0
return samples, rate
def sha256(path: Path) -> str:
"""Hex digest of the file's bytes."""
return hashlib.sha256(path.read_bytes()).hexdigest()
def duration_seconds(
signal: synth.Signal, sample_rate: int = synth.SAMPLE_RATE
) -> float:
"""Length of a signal in seconds."""
return len(signal) / sample_rate
def play_command(path: Path) -> str:
"""The one line a person runs to hear the file."""
return f"aplay -q {path}"