-
Notifications
You must be signed in to change notification settings - Fork 223
/
Copy pathindex.ts
162 lines (144 loc) · 3.63 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { rotate } from "@tonaljs/collection";
import { simplify, transposeFifths } from "@tonaljs/interval";
import { EmptyPcset, Pcset } from "@tonaljs/pcset";
import { transpose } from "@tonaljs/pitch-distance";
import { NoteName } from "@tonaljs/pitch-note";
import { get as getType } from "@tonaljs/scale-type";
const MODES = [
[0, 2773, 0, "ionian", "", "Maj7", "major"],
[1, 2902, 2, "dorian", "m", "m7"],
[2, 3418, 4, "phrygian", "m", "m7"],
[3, 2741, -1, "lydian", "", "Maj7"],
[4, 2774, 1, "mixolydian", "", "7"],
[5, 2906, 3, "aeolian", "m", "m7", "minor"],
[6, 3434, 5, "locrian", "dim", "m7b5"],
] as const;
type ModeDatum = (typeof MODES)[number];
export interface Mode extends Pcset {
readonly name: string;
readonly modeNum: number;
readonly alt: number; // number of alterations === number of fifths
readonly triad: string;
readonly seventh: string;
readonly aliases: string[];
}
const NoMode: Mode = {
...EmptyPcset,
name: "",
alt: 0,
modeNum: NaN,
triad: "",
seventh: "",
aliases: [],
};
const modes: Mode[] = MODES.map(toMode);
const index: Record<string, Mode> = {};
modes.forEach((mode) => {
index[mode.name] = mode;
mode.aliases.forEach((alias) => {
index[alias] = mode;
});
});
type ModeLiteral = string | { name: string };
/**
* Get a Mode by it's name
*
* @example
* get('dorian')
* // =>
* // {
* // intervals: [ '1P', '2M', '3m', '4P', '5P', '6M', '7m' ],
* // modeNum: 1,
* // chroma: '101101010110',
* // normalized: '101101010110',
* // name: 'dorian',
* // setNum: 2902,
* // alt: 2,
* // triad: 'm',
* // seventh: 'm7',
* // aliases: []
* // }
*/
export function get(name: ModeLiteral): Mode {
return typeof name === "string"
? index[name.toLowerCase()] || NoMode
: name && name.name
? get(name.name)
: NoMode;
}
/** @deprecated */
export const mode = get;
/**
* Get a list of all modes
*/
export function all() {
return modes.slice();
}
/** @deprecated */
export const entries = all;
/**
* Get a list of all mode names
*/
export function names() {
return modes.map((mode) => mode.name);
}
function toMode(mode: ModeDatum): Mode {
const [modeNum, setNum, alt, name, triad, seventh, alias] = mode;
const aliases = alias ? [alias] : [];
const chroma = Number(setNum).toString(2);
const intervals = getType(name).intervals;
return {
empty: false,
intervals,
modeNum,
chroma,
normalized: chroma,
name,
setNum,
alt,
triad,
seventh,
aliases,
};
}
export function notes(modeName: ModeLiteral, tonic: NoteName) {
return get(modeName).intervals.map((ivl) => transpose(tonic, ivl));
}
function chords(chords: string[]) {
return (modeName: ModeLiteral, tonic: NoteName) => {
const mode = get(modeName);
if (mode.empty) return [];
const triads = rotate(mode.modeNum, chords);
const tonics = mode.intervals.map((i) => transpose(tonic, i));
return triads.map((triad, i) => tonics[i] + triad);
};
}
export const triads = chords(MODES.map((x) => x[4]));
export const seventhChords = chords(MODES.map((x) => x[5]));
export function distance(destination: ModeLiteral, source: ModeLiteral) {
const from = get(source);
const to = get(destination);
if (from.empty || to.empty) return "";
return simplify(transposeFifths("1P", to.alt - from.alt));
}
export function relativeTonic(
destination: ModeLiteral,
source: ModeLiteral,
tonic: NoteName,
) {
return transpose(tonic, distance(destination, source));
}
/** @deprecated */
export default {
get,
names,
all,
distance,
relativeTonic,
notes,
triads,
seventhChords,
// deprecated
entries,
mode,
};