Spaces:
Sleeping
Sleeping
Download sim/chord_engine.py from adisha-education-lab/Mus_Sim_1-beta: direct link, hf CLI and curl.
- Browser
- Download file 5.37 kB
-
https://huggingface.co/spaces/adisha-education-lab/Mus_Sim_1-beta/resolve/main/sim/chord_engine.py
- Command line
-
hf download hf://spaces/adisha-education-lab/Mus_Sim_1-beta/sim/chord_engine.py
-
curl -L -o chord_engine.py https://huggingface.co/spaces/adisha-education-lab/Mus_Sim_1-beta/resolve/main/sim/chord_engine.py
5.37 kB
| from __future__ import annotations | |
| __version__ = "1.5.5" | |
| from dataclasses import dataclass | |
| from typing import Dict, List, Tuple | |
| NOTE_NAMES_SHARP = ["C","C#","D","D#","E","F","F#","G","G#","A","A#","B"] | |
| NOTE_NAMES_FLAT = ["C","Db","D","Eb","E","F","Gb","G","Ab","A","Bb","B"] | |
| NOTE_TO_PC = {n:i for i,n in enumerate(NOTE_NAMES_SHARP)} | |
| NOTE_TO_PC.update({n:i for i,n in enumerate(NOTE_NAMES_FLAT)}) | |
| PC_TO_SHARP = {i:n for i,n in enumerate(NOTE_NAMES_SHARP)} | |
| PC_TO_FLAT = {i:n for i,n in enumerate(NOTE_NAMES_FLAT)} | |
| QUALITY_INTERVALS: Dict[str, List[int]] = { | |
| # Triads | |
| "maj": [0,4,7], | |
| "min": [0,3,7], | |
| "dim": [0,3,6], | |
| "aug": [0,4,8], | |
| "sus2": [0,2,7], | |
| "sus4": [0,5,7], | |
| # 6ths | |
| "6": [0,4,7,9], | |
| "m6": [0,3,7,9], | |
| # 7ths | |
| "maj7": [0,4,7,11], | |
| "7": [0,4,7,10], | |
| "m7": [0,3,7,10], | |
| "m(maj7)": [0,3,7,11], | |
| # Extensions | |
| "add9": [0,4,7,14], | |
| "madd9": [0,3,7,14], | |
| "maj9": [0,4,7,11,14], | |
| "9": [0,4,7,10,14], | |
| "m9": [0,3,7,10,14], | |
| "11": [0,4,7,10,14,17], | |
| "13": [0,4,7,10,14,17,21], | |
| "ø7": [0,3,6,10], | |
| "dim7": [0,3,6,9], | |
| "aug7": [0,4,8,10], | |
| } | |
| # Public list of chord qualities used by the UI (stable ordering) | |
| CHORD_TYPES = list(QUALITY_INTERVALS.keys()) | |
| QUALITY_TENSION = { | |
| "maj": 15, "min": 18, "sus2": 25, "sus4": 25, | |
| "dim": 55, "aug": 50, | |
| "6": 22, "m6": 24, | |
| "maj7": 35, "7": 38, "m7": 36, "m(maj7)": 45, | |
| "ø7": 60, "dim7": 70, "aug7": 55 | |
| } | |
| def normalize_note_name(note: str, prefer_sharps: bool) -> str: | |
| """Normalise enharmonic spelling to a single display style (sharp/flat).""" | |
| pc = NOTE_TO_PC.get(note) | |
| if pc is None: | |
| return note | |
| return PC_TO_SHARP[pc] if prefer_sharps else PC_TO_FLAT[pc] | |
| def note_pc(note: str) -> int: | |
| if note not in NOTE_TO_PC: | |
| raise ValueError(f"Unknown note name: {note}") | |
| return NOTE_TO_PC[note] | |
| def pc_to_note(pc: int, prefer_sharps: bool) -> str: | |
| pc = pc % 12 | |
| return PC_TO_SHARP[pc] if prefer_sharps else PC_TO_FLAT[pc] | |
| def midi_for(pc: int, octave: int) -> int: | |
| # MIDI note: C4 = 60. Octave mapping: C0 = 12. | |
| return 12 + octave*12 + (pc % 12) | |
| def invert_midi(midi_notes: List[int], inversion: str) -> List[int]: | |
| notes = sorted(midi_notes) | |
| if inversion == "Root position": | |
| return notes | |
| steps = {"1st inversion": 1, "2nd inversion": 2, "3rd inversion": 3}.get(inversion, 0) | |
| for _ in range(steps): | |
| if notes: | |
| notes[0] += 12 | |
| notes = sorted(notes) | |
| return notes | |
| def chord_label(root: str, quality: str) -> str: | |
| # Simple naming convention | |
| q = quality | |
| if q == "maj": | |
| return f"{root}" | |
| if q == "min": | |
| return f"{root}m" | |
| if q == "dim": | |
| return f"{root}dim" | |
| if q == "aug": | |
| return f"{root}aug" | |
| return f"{root}{q}" | |
| def build_chord(root_note: str, quality: str, inversion: str, prefer_sharps: bool, base_octave: int = 4) -> Dict: | |
| if quality not in QUALITY_INTERVALS: | |
| raise ValueError(f"Unsupported quality: {quality}") | |
| root_pc = note_pc(normalize_note_name(root_note, prefer_sharps)) | |
| intervals = QUALITY_INTERVALS[quality] | |
| pcs = [(root_pc + iv) % 12 for iv in intervals] | |
| # Choose a musically reasonable MIDI placement near base_octave | |
| root_midi = midi_for(root_pc, base_octave) | |
| midi = [root_midi + iv for iv in intervals] | |
| midi = invert_midi(midi, inversion) | |
| notes = [pc_to_note(pc, prefer_sharps) for pc in pcs] | |
| name = chord_label(pc_to_note(root_pc, prefer_sharps), quality) | |
| tension = int(min(100, max(0, QUALITY_TENSION.get(quality, 25)))) | |
| # Simple role labels aligned to common harmony pedagogy | |
| roles = [] | |
| for iv in intervals: | |
| if iv == 0: | |
| roles.append("Root") | |
| elif iv in (3,4): | |
| roles.append("3rd") | |
| elif iv in (6,7,8): | |
| roles.append("5th") | |
| elif iv in (9,10,11): | |
| roles.append("7th/6th") | |
| elif iv in (14,): | |
| roles.append("9th") | |
| elif iv in (17,): | |
| roles.append("11th") | |
| elif iv in (21,): | |
| roles.append("13th") | |
| else: | |
| roles.append("Tone") | |
| return { | |
| "name": name, | |
| "root": pc_to_note(root_pc, prefer_sharps), | |
| "quality": quality, | |
| "intervals": intervals, | |
| "pitch_classes": pcs, | |
| "notes": notes, | |
| "midi": midi, | |
| "tension": tension, | |
| "roles": roles, | |
| } | |
| def diatonic_degrees_major(key_tonic: str) -> Dict[str, str]: | |
| """Return diatonic triad roots for a major key using pitch classes (simple, enharmonic-light).""" | |
| tonic_pc = note_pc(key_tonic) | |
| # Major scale intervals | |
| scale = [0,2,4,5,7,9,11] | |
| pcs = [(tonic_pc + s) % 12 for s in scale] | |
| # Degrees in common notation (triad qualities implied: I ii iii IV V vi vii°) | |
| return { | |
| "I": PC_TO_SHARP[pcs[0]], | |
| "ii": PC_TO_SHARP[pcs[1]], | |
| "iii": PC_TO_SHARP[pcs[2]], | |
| "IV": PC_TO_SHARP[pcs[3]], | |
| "V": PC_TO_SHARP[pcs[4]], | |
| "vi": PC_TO_SHARP[pcs[5]], | |
| "vii°": PC_TO_SHARP[pcs[6]], | |
| } | |
| def degree_name(degree: str) -> str: | |
| return { | |
| "I": "Tonic", | |
| "ii": "Supertonic", | |
| "iii": "Mediant", | |
| "IV": "Subdominant", | |
| "V": "Dominant", | |
| "vi": "Submediant", | |
| "vii°": "Leading-tone", | |
| }.get(degree, "Degree") | |