@hum2music/music-theory 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +14 -4
- package/src/chords.ts +80 -0
- package/src/index.ts +4 -0
- package/src/key-profiles.ts +12 -0
- package/src/pitch-class.ts +15 -0
- package/src/scales.ts +18 -0
- package/README.md +0 -3
package/package.json
CHANGED
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hum2music/music-theory",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Pitch-class, chord, scale, and key-profile primitives used across Hum2Music.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"homepage": "https://github.com/hum2music/hum2music",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/hum2music/hum2music.git",
|
|
10
|
+
"directory": "packages/music-theory"
|
|
11
|
+
},
|
|
12
|
+
"type": "module",
|
|
13
|
+
"main": "./src/index.ts",
|
|
14
|
+
"types": "./src/index.ts",
|
|
15
|
+
"files": ["src"]
|
|
16
|
+
}
|
package/src/chords.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { NOTE_NAMES, pitchClassOf } from "./pitch-class";
|
|
2
|
+
|
|
3
|
+
export type ChordQuality = "major" | "minor" | "dominant7" | "major7" | "minor7" | "sus2" | "sus4";
|
|
4
|
+
|
|
5
|
+
export const CHORD_QUALITIES: ChordQuality[] = [
|
|
6
|
+
"major",
|
|
7
|
+
"minor",
|
|
8
|
+
"dominant7",
|
|
9
|
+
"major7",
|
|
10
|
+
"minor7",
|
|
11
|
+
"sus2",
|
|
12
|
+
"sus4",
|
|
13
|
+
];
|
|
14
|
+
|
|
15
|
+
/** Semitone offsets from the root, per PRD §8.2's initial chord-type set. */
|
|
16
|
+
export const CHORD_INTERVALS: Record<ChordQuality, number[]> = {
|
|
17
|
+
major: [0, 4, 7],
|
|
18
|
+
minor: [0, 3, 7],
|
|
19
|
+
dominant7: [0, 4, 7, 10],
|
|
20
|
+
major7: [0, 4, 7, 11],
|
|
21
|
+
minor7: [0, 3, 7, 10],
|
|
22
|
+
sus2: [0, 2, 7],
|
|
23
|
+
sus4: [0, 5, 7],
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const QUALITY_SUFFIX: Record<ChordQuality, string> = {
|
|
27
|
+
major: "",
|
|
28
|
+
minor: "m",
|
|
29
|
+
dominant7: "7",
|
|
30
|
+
major7: "maj7",
|
|
31
|
+
minor7: "m7",
|
|
32
|
+
sus2: "sus2",
|
|
33
|
+
sus4: "sus4",
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export function chordPitchClasses(root: number, quality: ChordQuality): number[] {
|
|
37
|
+
const rootPitchClass = pitchClassOf(root);
|
|
38
|
+
return CHORD_INTERVALS[quality].map((interval) => pitchClassOf(rootPitchClass + interval));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function chordSymbol(root: number, quality: ChordQuality): string {
|
|
42
|
+
return `${NOTE_NAMES[pitchClassOf(root)]}${QUALITY_SUFFIX[quality]}`;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const PITCH_CLASS_BY_NOTE_NAME: Record<string, number> = {
|
|
46
|
+
C: 0,
|
|
47
|
+
"C#": 1,
|
|
48
|
+
Db: 1,
|
|
49
|
+
D: 2,
|
|
50
|
+
"D#": 3,
|
|
51
|
+
Eb: 3,
|
|
52
|
+
E: 4,
|
|
53
|
+
F: 5,
|
|
54
|
+
"F#": 6,
|
|
55
|
+
Gb: 6,
|
|
56
|
+
G: 7,
|
|
57
|
+
"G#": 8,
|
|
58
|
+
Ab: 8,
|
|
59
|
+
A: 9,
|
|
60
|
+
"A#": 10,
|
|
61
|
+
Bb: 10,
|
|
62
|
+
B: 11,
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const QUALITY_BY_SUFFIX: Record<string, ChordQuality> = Object.fromEntries(
|
|
66
|
+
CHORD_QUALITIES.map((quality) => [QUALITY_SUFFIX[quality], quality]),
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
/** Inverse of chordSymbol; returns null for a symbol this chord-type set can't represent (e.g. an unsupported suffix). */
|
|
70
|
+
export function parseChordSymbol(symbol: string): { root: number; quality: ChordQuality } | null {
|
|
71
|
+
const match = symbol.trim().match(/^([A-G][#b]?)(.*)$/);
|
|
72
|
+
if (!match) return null;
|
|
73
|
+
|
|
74
|
+
const [, rootName, suffix] = match;
|
|
75
|
+
const root = PITCH_CLASS_BY_NOTE_NAME[rootName];
|
|
76
|
+
const quality = QUALITY_BY_SUFFIX[suffix];
|
|
77
|
+
if (root === undefined || quality === undefined) return null;
|
|
78
|
+
|
|
79
|
+
return { root, quality };
|
|
80
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ScaleMode } from "./scales";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Krumhansl-Kessler key profiles: empirically measured perceived stability
|
|
5
|
+
* of each pitch class relative to a tonic, indexed by semitone above that
|
|
6
|
+
* tonic. Used to correlate a melody's pitch-class distribution against all
|
|
7
|
+
* 24 candidate keys (Krumhansl & Kessler, 1982).
|
|
8
|
+
*/
|
|
9
|
+
export const KEY_PROFILES: Record<ScaleMode, number[]> = {
|
|
10
|
+
major: [6.35, 2.23, 3.48, 2.33, 4.38, 4.09, 2.52, 5.19, 2.39, 3.66, 2.29, 2.88],
|
|
11
|
+
minor: [6.33, 2.68, 3.52, 5.38, 2.6, 3.53, 2.54, 4.75, 3.98, 2.69, 3.34, 3.17],
|
|
12
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export const NOTE_NAMES = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"] as const;
|
|
2
|
+
|
|
3
|
+
export function pitchClassOf(midiNote: number): number {
|
|
4
|
+
return ((midiNote % 12) + 12) % 12;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function pitchClassName(pitchClass: number): string {
|
|
8
|
+
return NOTE_NAMES[pitchClassOf(pitchClass)];
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** Signed distance in [-6, 6] from `a` to `b` around the 12-pitch-class circle. */
|
|
12
|
+
export function pitchClassDistance(a: number, b: number): number {
|
|
13
|
+
const raw = pitchClassOf(b - a);
|
|
14
|
+
return raw > 6 ? raw - 12 : raw;
|
|
15
|
+
}
|
package/src/scales.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { pitchClassOf } from "./pitch-class";
|
|
2
|
+
|
|
3
|
+
export type ScaleMode = "major" | "minor";
|
|
4
|
+
|
|
5
|
+
/** Natural major and natural minor, per PRD §8.1 ("MVPでは長調・短調を中心とする"). */
|
|
6
|
+
export const SCALE_INTERVALS: Record<ScaleMode, number[]> = {
|
|
7
|
+
major: [0, 2, 4, 5, 7, 9, 11],
|
|
8
|
+
minor: [0, 2, 3, 5, 7, 8, 10],
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export function scalePitchClasses(tonic: number, mode: ScaleMode): Set<number> {
|
|
12
|
+
const tonicPitchClass = pitchClassOf(tonic);
|
|
13
|
+
return new Set(SCALE_INTERVALS[mode].map((interval) => pitchClassOf(tonicPitchClass + interval)));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function isDiatonic(pitchClass: number, tonic: number, mode: ScaleMode): boolean {
|
|
17
|
+
return scalePitchClasses(tonic, mode).has(pitchClassOf(pitchClass));
|
|
18
|
+
}
|
package/README.md
DELETED