Files
pytheory/examples/midi_converter.py
kennethreitz 9da3ac8b28 Add 12 example scripts showcasing pytheory features
- circle_of_fifths.py — visualize keys around the circle
- chord_identifier.py — identify chords from notes and fingerings
- key_explorer.py — explore keys, signatures, progressions, borrowed chords
- temperament_comparison.py — compare equal, Pythagorean, and meantone
- chord_tension.py — analyze tension, consonance, and voice leading
- world_scales.py — scales from 6 musical traditions
- fretboard_explorer.py — instruments, tunings, capo transposition
- midi_converter.py — MIDI ↔ note ↔ frequency reference
- progression_writer.py — famous progressions, Nashville numbers, random generation
- interval_trainer.py — interval names, songs, and consonance ranking
- overtone_series.py — harmonics and why chords sound good
- key_detection.py — detect keys from melodies and chord progressions

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-22 20:27:18 -04:00

36 lines
1.3 KiB
Python

"""Convert between MIDI note numbers, frequencies, and note names."""
from pytheory import Tone
print("MIDI ↔ Note ↔ Frequency Reference")
print("=" * 50)
print()
print(f"{'MIDI':>5s} {'Note':>5s} {'Freq (Hz)':>10s} {'Octave':>6s}")
print(f"{'' * 5} {'' * 5} {'' * 10} {'' * 6}")
# Show all notes from C2 to C7
for midi in range(36, 97):
tone = Tone.from_midi(midi)
freq = tone.frequency
print(f"{midi:>5d} {tone.full_name:>5s} {freq:>10.2f} {tone.octave:>6d}")
# Useful reference points
print()
print("Key Reference Points:")
print(f" Lowest piano note: A0 = MIDI {Tone.from_string('A0', system='western').midi}")
print(f" Middle C: C4 = MIDI {Tone.from_string('C4', system='western').midi}")
print(f" Concert A: A4 = MIDI {Tone.from_string('A4', system='western').midi}")
print(f" Highest piano note: C8 = MIDI {Tone.from_string('C8', system='western').midi}")
# Round-trip demo
print()
print("Round-trip conversions:")
for start in ["C4", "A4", "F#3", "Bb5"]:
tone = Tone.from_string(start, system="western")
midi = tone.midi
freq = tone.frequency
from_midi = Tone.from_midi(midi)
from_freq = Tone.from_frequency(freq)
print(f" {start:4s} → MIDI {midi}{from_midi.full_name:4s} | "
f"{start:4s}{freq:.2f} Hz → {from_freq.full_name}")