@playbykey/theory
@playbykey/theory is a zero-dependency npm package for computing scales, modes, chords, progressions, transposition, MIDI/frequency conversions, key relationships, and note display maps.
The music theory engine powering PlayByKey, a free interactive music theory learning tool.
Music theory relationships, resolved deterministically through fixed interval and scale-degree relationships.
Note notation
Section titled “Note notation”The package represents notes with one canonical sharp spelling (C#, not Db) to keep the Note type safe and unambiguous. Flats are fully supported:
- Input:
parseNote/parseNoteTokenaccept flat-spelled note names (Db,Eb,Gb,Ab,Bb), normalized to the canonical sharp spelling. - Output:
getFlats/getEnharmonicLabelsrespell computed output as flats or combined enharmonic labels.
Installation
Section titled “Installation”npm install @playbykey/theorypnpm add @playbykey/theoryyarn add @playbykey/theorybun add @playbykey/theoryInstall @playbykey/theory from npm and add it to my project. Docs: https://theory-engine.docs.playbykey.com. Functions: getModeNotes, getParentScaleModes, getModalRoot (modes); getRelativeMinorKey, getRelativeMajorKey, getKeySignatureCount (accepts an optional quality: 'major'|'minor' param, default major), getCircleOfFifthsOrder (key relationships); getSemitoneDistance (note utilities); getScaleNotes, getScaleDegrees, getScaleDegree, isNoteInScale, buildNoteMap (scales); resolveIntervalEndpoints, getIntervalSemitones, INTERVAL_DEFINITIONS (intervals - half_step/whole_step are scale motion, minor_2nd/major_2nd are from root); parseNote, parseNoteToken, parseModeName, isNote, isModeName (type guards/parsers, case-insensitive; parseNote and parseNoteToken also accept flat note names like Db); getSharps, getFlats, getEnharmonicLabels (respell notes as sharps/flats/combined enharmonic labels); getBluesNotes, getHarmonicMinorNotes, getPentatonicNotes (derived scales); getChordNotes, getDiatonicChords, getChordByDegree, getAvailableInversions, getChordInversion, detectChords (22 chord types - triads, 7ths, 6ths, suspended, 9ths, 11ths, 13ths); getProgressionInKey, getRomanNumeral (7 catalog progressions, roman numeral labeling); getMelodicMinorNotes, getMelodicMinorModeNotes, getHarmonicMinorModeNotes, getBebopScaleNotes (melodic minor + 7 modes, harmonic minor's Phrygian dominant, 3 bebop variants); transpose (shift notes between keys); noteToMidi, midiToNote, noteToFrequency (MIDI number and Hz frequency conversion). Zero dependencies, strict types with no any, sharps-only notation with flat-name input and output support. Or use @playbykey/theory-mcp for MCP tool calling.Quickstart
Section titled “Quickstart”Written in TypeScript with strict mode - every export is fully typed, no any. Import types directly: import type { Note, ModeName } from '@playbykey/theory';
import { getModeNotes, getParentScaleModes } from '@playbykey/theory';
// Get all notes in D Dorianconst notes = getModeNotes('D', 'dorian');console.log(notes); // ['D', 'E', 'F', 'G', 'A', 'B', 'C']
// Find the parent major keyconst parentModes = getParentScaleModes('D', 'dorian');const parent = parentModes.find((m) => m.mode === 'ionian');console.log(parent?.root); // 'C'import { getModeNotes, getFlats, getEnharmonicLabels, getSharps,} from '@playbykey/theory';
// Sharp-spelled by defaultconst notes = getModeNotes('C#', 'aeolian');console.log(notes); // ['C#', 'D#', 'E', 'F#', 'G#', 'A', 'B']
// Respell as flats or combined enharmonic labelsconsole.log(getFlats(notes)); // ['Db', 'Eb', 'E', 'Gb', 'Ab', 'A', 'B']console.log(getEnharmonicLabels(notes)); // ['Db/C#', 'Eb/D#', 'E', 'Gb/F#', 'Ab/G#', 'A', 'B']
// getSharps normalizes flat-spelled notes back to canonical sharpsconsole.log(getSharps(['Db', 'Eb', 'Gb'])); // ['C#', 'D#', 'F#']What’s in the package
Section titled “What’s in the package”| Category | What it covers | Page |
|---|---|---|
| Keys & Modes | Scales, modes, key relationships, note utilities | Keys & Modes |
| Intervals | Interval catalog, semitone lookup, endpoint resolution | Intervals |
| Scales | Blues, harmonic minor, melodic minor, pentatonic, bebop, derived scales | Scales |
| Chords | Chord notes, diatonic triads, inversions, chord detection (22 chord types) | Chords |
| Progressions | 7 catalog progressions, roman numeral labeling | Progressions |
| Transposition | Shift a set of notes between keys | Transposition |
| MIDI & Frequency | Convert between notes, MIDI numbers, and Hz frequencies | MIDI & Frequency |
| Constants | All exported constants and their values | Constants |