@tty-pt/pitch
Universal Node.js and Browser bindings forlibpitch, the pure-C musical chord detection, key signature analysis, semitone transposition, and lyrics column-alignment engine. Canonical C header:<ttypt/pitch.h>.
๐ Full C & Music Theory Documentation: See the main libpitch GitHub README for the chord grammar specification, enharmonic spelling rules, and C AST (pitch_song_t) API.
1. Overview & Domain Concepts
libpitch (@tty-pt/pitch) parses, analyzes, and transposes plain-text chord-over-lyric song charts with zero external chord dictionaries:
- Grammar-Based Chord Detection:
- Identifies chord lines vs. lyric lines via a single-pass left-to-right grammar (
root+ accidental + quality/extension/suspension/alteration + optional/bass), handling complex jazz extensions (Cmaj9#11/G,F#m7b5) without false positives on ordinary words.
- Automatic Key Signature Inference (
getDetectedKey):
- Infers the song's chromatic tonic (
0..11) from progression weights to select musically natural enharmonic spellings during transposition.
- Column-Preserving Lyric Alignment:
- Adjusts horizontal spacing when chord widths expand or shrink (e.g.,
C$\rightarrow$C#orDo$\rightarrow$Reb), keeping chords anchored above their exact syllable.
- Notation & Rendering Flags:
- Supports Anglo-American (
C D E F G A B) and Latin/Romance solfege (Do Re Mi Fa Sol La SiviaPITCH_LATIN), flats vs. sharps (PITCH_BEMOL), HTML output (PITCH_HTML), chord-only or lyric-only views, and slash-chord line breaking.
2. Installation
npm config set @tty-pt:registry https://tty.pt/npm/
npm install @tty-pt/pitch
Native OS & Mobile Packages (tty.pt): Prebuilt native C packages (libpitch) for Linux (apt, apk, pacman, dnf), macOS (brew), Windows (winget, MSYS2), and OpenBSD (pkg_add), plus static mobile sysroots for Android (https://tty.pt/android/) and iOS (https://tty.pt/ios/ โ libpitch.xcframework) are available via the tty.pt package repositories.
3. Quickstart
import {
transposeText,
getDetectedKey,
getShiftTable,
PITCH_HTML,
PITCH_BEMOL,
PITCH_LATIN
} from "@tty-pt/pitch";
const chart = [
"C G/B Am F",
"Singing in the morning sun"
].join("\n");
// 1. Transpose up 2 semitones and render HTML
const html = transposeText(chart, 2, PITCH_HTML);
console.log(html);
// <div><b>D A/C# Bm G</b></div><div>Singing in the morning sun</div>
// 2. Transpose to flat enharmonics in Latin solfege (Do, Re, Mi...)
const solfege = transposeText(chart, 3, PITCH_BEMOL | PITCH_LATIN);
console.log(solfege);
// 3. Inspect detected chromatic key (0 = C, 2 = D, ...) & key shift options
console.log("Detected key index:", getDetectedKey());
console.log("Key shift table HTML:", getShiftTable(0));
4. JavaScript / TypeScript API Reference
transposeText(text: string, semitones: number, flags?: number): string
Parses text, shifts all detected chords by semitones (positive or negative), and formats the output according to flags.
Key Analysis & Shift Table Helpers
getDetectedKey(): number: Returns the detected chromatic tonic (0..11, where0 = C, or-1if no chords were found).resetKey(): void: Resets the sticky key state on the shared pitch context so the next chart re-detects its key from scratch.getShiftTable(latin?: number): string: Renders<option>tags for all 12 semitone transpositions relative to the detected key (latin = 1forDo Re Milabels).
Formatting Flags Bitmask
| Constant | Value | Description |
|---|---|---|
PITCH_HIDE_CHORDS | 0x01 | Strip chord lines and emit lyrics only |
PITCH_HIDE_LYRICS | 0x02 | Strip lyric lines and emit chords only |
PITCH_HTML | 0x04 | Wrap chord (<div><b>...</b></div>) and lyric lines in HTML |
PITCH_BEMOL | 0x08 | Prefer flat enharmonic spellings (Bb, Eb) over sharps |
PITCH_REMOVE_COMMENTS | 0x10 | Strip {...} / [...] performance comment annotations |
PITCH_BREAK_SLASH | 0x20 | Break slash bass notes onto a secondary alignment line |
PITCH_LATIN | 0x80 | Render chord roots in Romance/Latin solfege (Do, Re, Mi, ...) |
getPitchWasm(): WebAssembly.Instance
Returns the initialized pitch.wasm reactor instance on @tty-pt/stele's shared linear memory substrate.
5. C Development Headers & Native Integration
CFLAGS += -I./node_modules/@tty-pt/pitch/include
LDLIBS += -lpitch
License
BSD-2-Clause โ Copyright (c) 2026, Paulo Quirino <quirinpa@gmail.com>. See LICENSE.