@tonaljs/core 4.6.5 → 4.6.11

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/dist/index.es.js DELETED
@@ -1,345 +0,0 @@
1
- /**
2
- * Fill a string with a repeated character
3
- *
4
- * @param character
5
- * @param repetition
6
- */
7
- const fillStr = (s, n) => Array(Math.abs(n) + 1).join(s);
8
- function deprecate(original, alternative, fn) {
9
- return function (...args) {
10
- // tslint:disable-next-line
11
- console.warn(`${original} is deprecated. Use ${alternative}.`);
12
- return fn.apply(this, args);
13
- };
14
- }
15
-
16
- function isNamed(src) {
17
- return src !== null && typeof src === "object" && typeof src.name === "string"
18
- ? true
19
- : false;
20
- }
21
-
22
- function isPitch(pitch) {
23
- return pitch !== null &&
24
- typeof pitch === "object" &&
25
- typeof pitch.step === "number" &&
26
- typeof pitch.alt === "number"
27
- ? true
28
- : false;
29
- }
30
- // The number of fifths of [C, D, E, F, G, A, B]
31
- const FIFTHS = [0, 2, 4, -1, 1, 3, 5];
32
- // The number of octaves it span each step
33
- const STEPS_TO_OCTS = FIFTHS.map((fifths) => Math.floor((fifths * 7) / 12));
34
- function encode(pitch) {
35
- const { step, alt, oct, dir = 1 } = pitch;
36
- const f = FIFTHS[step] + 7 * alt;
37
- if (oct === undefined) {
38
- return [dir * f];
39
- }
40
- const o = oct - STEPS_TO_OCTS[step] - 4 * alt;
41
- return [dir * f, dir * o];
42
- }
43
- // We need to get the steps from fifths
44
- // Fifths for CDEFGAB are [ 0, 2, 4, -1, 1, 3, 5 ]
45
- // We add 1 to fifths to avoid negative numbers, so:
46
- // for ["F", "C", "G", "D", "A", "E", "B"] we have:
47
- const FIFTHS_TO_STEPS = [3, 0, 4, 1, 5, 2, 6];
48
- function decode(coord) {
49
- const [f, o, dir] = coord;
50
- const step = FIFTHS_TO_STEPS[unaltered(f)];
51
- const alt = Math.floor((f + 1) / 7);
52
- if (o === undefined) {
53
- return { step, alt, dir };
54
- }
55
- const oct = o + 4 * alt + STEPS_TO_OCTS[step];
56
- return { step, alt, oct, dir };
57
- }
58
- // Return the number of fifths as if it were unaltered
59
- function unaltered(f) {
60
- const i = (f + 1) % 7;
61
- return i < 0 ? 7 + i : i;
62
- }
63
-
64
- const NoNote = { empty: true, name: "", pc: "", acc: "" };
65
- const cache$1 = new Map();
66
- const stepToLetter = (step) => "CDEFGAB".charAt(step);
67
- const altToAcc = (alt) => alt < 0 ? fillStr("b", -alt) : fillStr("#", alt);
68
- const accToAlt = (acc) => acc[0] === "b" ? -acc.length : acc.length;
69
- /**
70
- * Given a note literal (a note name or a note object), returns the Note object
71
- * @example
72
- * note('Bb4') // => { name: "Bb4", midi: 70, chroma: 10, ... }
73
- */
74
- function note(src) {
75
- const cached = cache$1.get(src);
76
- if (cached) {
77
- return cached;
78
- }
79
- const value = typeof src === "string"
80
- ? parse$1(src)
81
- : isPitch(src)
82
- ? note(pitchName$1(src))
83
- : isNamed(src)
84
- ? note(src.name)
85
- : NoNote;
86
- cache$1.set(src, value);
87
- return value;
88
- }
89
- const REGEX$1 = /^([a-gA-G]?)(#{1,}|b{1,}|x{1,}|)(-?\d*)\s*(.*)$/;
90
- /**
91
- * @private
92
- */
93
- function tokenizeNote(str) {
94
- const m = REGEX$1.exec(str);
95
- return [m[1].toUpperCase(), m[2].replace(/x/g, "##"), m[3], m[4]];
96
- }
97
- /**
98
- * @private
99
- */
100
- function coordToNote(noteCoord) {
101
- return note(decode(noteCoord));
102
- }
103
- const mod = (n, m) => ((n % m) + m) % m;
104
- const SEMI = [0, 2, 4, 5, 7, 9, 11];
105
- function parse$1(noteName) {
106
- const tokens = tokenizeNote(noteName);
107
- if (tokens[0] === "" || tokens[3] !== "") {
108
- return NoNote;
109
- }
110
- const letter = tokens[0];
111
- const acc = tokens[1];
112
- const octStr = tokens[2];
113
- const step = (letter.charCodeAt(0) + 3) % 7;
114
- const alt = accToAlt(acc);
115
- const oct = octStr.length ? +octStr : undefined;
116
- const coord = encode({ step, alt, oct });
117
- const name = letter + acc + octStr;
118
- const pc = letter + acc;
119
- const chroma = (SEMI[step] + alt + 120) % 12;
120
- const height = oct === undefined
121
- ? mod(SEMI[step] + alt, 12) - 12 * 99
122
- : SEMI[step] + alt + 12 * (oct + 1);
123
- const midi = height >= 0 && height <= 127 ? height : null;
124
- const freq = oct === undefined ? null : Math.pow(2, (height - 69) / 12) * 440;
125
- return {
126
- empty: false,
127
- acc,
128
- alt,
129
- chroma,
130
- coord,
131
- freq,
132
- height,
133
- letter,
134
- midi,
135
- name,
136
- oct,
137
- pc,
138
- step,
139
- };
140
- }
141
- function pitchName$1(props) {
142
- const { step, alt, oct } = props;
143
- const letter = stepToLetter(step);
144
- if (!letter) {
145
- return "";
146
- }
147
- const pc = letter + altToAcc(alt);
148
- return oct || oct === 0 ? pc + oct : pc;
149
- }
150
-
151
- const NoInterval = { empty: true, name: "", acc: "" };
152
- // shorthand tonal notation (with quality after number)
153
- const INTERVAL_TONAL_REGEX = "([-+]?\\d+)(d{1,4}|m|M|P|A{1,4})";
154
- // standard shorthand notation (with quality before number)
155
- const INTERVAL_SHORTHAND_REGEX = "(AA|A|P|M|m|d|dd)([-+]?\\d+)";
156
- const REGEX = new RegExp("^" + INTERVAL_TONAL_REGEX + "|" + INTERVAL_SHORTHAND_REGEX + "$");
157
- /**
158
- * @private
159
- */
160
- function tokenizeInterval(str) {
161
- const m = REGEX.exec(`${str}`);
162
- if (m === null) {
163
- return ["", ""];
164
- }
165
- return m[1] ? [m[1], m[2]] : [m[4], m[3]];
166
- }
167
- const cache = {};
168
- /**
169
- * Get interval properties. It returns an object with:
170
- *
171
- * - name: the interval name
172
- * - num: the interval number
173
- * - type: 'perfectable' or 'majorable'
174
- * - q: the interval quality (d, m, M, A)
175
- * - dir: interval direction (1 ascending, -1 descending)
176
- * - simple: the simplified number
177
- * - semitones: the size in semitones
178
- * - chroma: the interval chroma
179
- *
180
- * @param {string} interval - the interval name
181
- * @return {Object} the interval properties
182
- *
183
- * @example
184
- * import { interval } from '@tonaljs/core'
185
- * interval('P5').semitones // => 7
186
- * interval('m3').type // => 'majorable'
187
- */
188
- function interval(src) {
189
- return typeof src === "string"
190
- ? cache[src] || (cache[src] = parse(src))
191
- : isPitch(src)
192
- ? interval(pitchName(src))
193
- : isNamed(src)
194
- ? interval(src.name)
195
- : NoInterval;
196
- }
197
- const SIZES = [0, 2, 4, 5, 7, 9, 11];
198
- const TYPES = "PMMPPMM";
199
- function parse(str) {
200
- const tokens = tokenizeInterval(str);
201
- if (tokens[0] === "") {
202
- return NoInterval;
203
- }
204
- const num = +tokens[0];
205
- const q = tokens[1];
206
- const step = (Math.abs(num) - 1) % 7;
207
- const t = TYPES[step];
208
- if (t === "M" && q === "P") {
209
- return NoInterval;
210
- }
211
- const type = t === "M" ? "majorable" : "perfectable";
212
- const name = "" + num + q;
213
- const dir = num < 0 ? -1 : 1;
214
- const simple = num === 8 || num === -8 ? num : dir * (step + 1);
215
- const alt = qToAlt(type, q);
216
- const oct = Math.floor((Math.abs(num) - 1) / 7);
217
- const semitones = dir * (SIZES[step] + alt + 12 * oct);
218
- const chroma = (((dir * (SIZES[step] + alt)) % 12) + 12) % 12;
219
- const coord = encode({ step, alt, oct, dir });
220
- return {
221
- empty: false,
222
- name,
223
- num,
224
- q,
225
- step,
226
- alt,
227
- dir,
228
- type,
229
- simple,
230
- semitones,
231
- chroma,
232
- coord,
233
- oct,
234
- };
235
- }
236
- /**
237
- * @private
238
- *
239
- * forceDescending is used in the case of unison (#243)
240
- */
241
- function coordToInterval(coord, forceDescending) {
242
- const [f, o = 0] = coord;
243
- const isDescending = f * 7 + o * 12 < 0;
244
- const ivl = forceDescending || isDescending ? [-f, -o, -1] : [f, o, 1];
245
- return interval(decode(ivl));
246
- }
247
- function qToAlt(type, q) {
248
- return (q === "M" && type === "majorable") ||
249
- (q === "P" && type === "perfectable")
250
- ? 0
251
- : q === "m" && type === "majorable"
252
- ? -1
253
- : /^A+$/.test(q)
254
- ? q.length
255
- : /^d+$/.test(q)
256
- ? -1 * (type === "perfectable" ? q.length : q.length + 1)
257
- : 0;
258
- }
259
- // return the interval name of a pitch
260
- function pitchName(props) {
261
- const { step, alt, oct = 0, dir } = props;
262
- if (!dir) {
263
- return "";
264
- }
265
- const calcNum = step + 1 + 7 * oct;
266
- // this is an edge case: descending pitch class unison (see #243)
267
- const num = calcNum === 0 ? step + 1 : calcNum;
268
- const d = dir < 0 ? "-" : "";
269
- const type = TYPES[step] === "M" ? "majorable" : "perfectable";
270
- const name = d + num + altToQ(type, alt);
271
- return name;
272
- }
273
- function altToQ(type, alt) {
274
- if (alt === 0) {
275
- return type === "majorable" ? "M" : "P";
276
- }
277
- else if (alt === -1 && type === "majorable") {
278
- return "m";
279
- }
280
- else if (alt > 0) {
281
- return fillStr("A", alt);
282
- }
283
- else {
284
- return fillStr("d", type === "perfectable" ? alt : alt + 1);
285
- }
286
- }
287
-
288
- /**
289
- * Transpose a note by an interval.
290
- *
291
- * @param {string} note - the note or note name
292
- * @param {string} interval - the interval or interval name
293
- * @return {string} the transposed note name or empty string if not valid notes
294
- * @example
295
- * import { tranpose } from "@tonaljs/core"
296
- * transpose("d3", "3M") // => "F#3"
297
- * transpose("D", "3M") // => "F#"
298
- * ["C", "D", "E", "F", "G"].map(pc => transpose(pc, "M3)) // => ["E", "F#", "G#", "A", "B"]
299
- */
300
- function transpose(noteName, intervalName) {
301
- const note$1 = note(noteName);
302
- const interval$1 = interval(intervalName);
303
- if (note$1.empty || interval$1.empty) {
304
- return "";
305
- }
306
- const noteCoord = note$1.coord;
307
- const intervalCoord = interval$1.coord;
308
- const tr = noteCoord.length === 1
309
- ? [noteCoord[0] + intervalCoord[0]]
310
- : [noteCoord[0] + intervalCoord[0], noteCoord[1] + intervalCoord[1]];
311
- return coordToNote(tr).name;
312
- }
313
- /**
314
- * Find the interval distance between two notes or coord classes.
315
- *
316
- * To find distance between coord classes, both notes must be coord classes and
317
- * the interval is always ascending
318
- *
319
- * @param {Note|string} from - the note or note name to calculate distance from
320
- * @param {Note|string} to - the note or note name to calculate distance to
321
- * @return {string} the interval name or empty string if not valid notes
322
- *
323
- */
324
- function distance(fromNote, toNote) {
325
- const from = note(fromNote);
326
- const to = note(toNote);
327
- if (from.empty || to.empty) {
328
- return "";
329
- }
330
- const fcoord = from.coord;
331
- const tcoord = to.coord;
332
- const fifths = tcoord[0] - fcoord[0];
333
- const octs = fcoord.length === 2 && tcoord.length === 2
334
- ? tcoord[1] - fcoord[1]
335
- : -Math.floor((fifths * 7) / 12);
336
- // If it's unison and not pitch class, it can be descending interval (#243)
337
- const forceDescending = to.height === from.height &&
338
- to.midi !== null &&
339
- from.midi !== null &&
340
- from.step > to.step;
341
- return coordToInterval([fifths, octs], forceDescending).name;
342
- }
343
-
344
- export { accToAlt, altToAcc, coordToInterval, coordToNote, decode, deprecate, distance, encode, fillStr, interval, isNamed, isPitch, note, stepToLetter, tokenizeInterval, tokenizeNote, transpose };
345
- //# sourceMappingURL=index.es.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.es.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -1,27 +0,0 @@
1
- import { IntervalLiteral, IntervalName } from "./interval";
2
- import { NoteLiteral, NoteName } from "./note";
3
- /**
4
- * Transpose a note by an interval.
5
- *
6
- * @param {string} note - the note or note name
7
- * @param {string} interval - the interval or interval name
8
- * @return {string} the transposed note name or empty string if not valid notes
9
- * @example
10
- * import { tranpose } from "@tonaljs/core"
11
- * transpose("d3", "3M") // => "F#3"
12
- * transpose("D", "3M") // => "F#"
13
- * ["C", "D", "E", "F", "G"].map(pc => transpose(pc, "M3)) // => ["E", "F#", "G#", "A", "B"]
14
- */
15
- export declare function transpose(noteName: NoteLiteral, intervalName: IntervalLiteral): NoteName;
16
- /**
17
- * Find the interval distance between two notes or coord classes.
18
- *
19
- * To find distance between coord classes, both notes must be coord classes and
20
- * the interval is always ascending
21
- *
22
- * @param {Note|string} from - the note or note name to calculate distance from
23
- * @param {Note|string} to - the note or note name to calculate distance to
24
- * @return {string} the interval name or empty string if not valid notes
25
- *
26
- */
27
- export declare function distance(fromNote: NoteLiteral, toNote: NoteLiteral): IntervalName;
@@ -1,59 +0,0 @@
1
- import { Named } from "./named";
2
- import { Direction, IntervalCoordinates, Pitch, PitchCoordinates } from "./pitch";
3
- export declare type IntervalName = string;
4
- export declare type IntervalLiteral = IntervalName | Pitch | Named;
5
- declare type Quality = "dddd" | "ddd" | "dd" | "d" | "m" | "M" | "P" | "A" | "AA" | "AAA" | "AAAA";
6
- declare type Type = "perfectable" | "majorable";
7
- export interface Interval extends Pitch, Named {
8
- readonly empty: boolean;
9
- readonly name: IntervalName;
10
- readonly num: number;
11
- readonly q: Quality;
12
- readonly type: Type;
13
- readonly step: number;
14
- readonly alt: number;
15
- readonly dir: Direction;
16
- readonly simple: number;
17
- readonly semitones: number;
18
- readonly chroma: number;
19
- readonly coord: IntervalCoordinates;
20
- readonly oct: number;
21
- }
22
- export interface NoInterval extends Partial<Interval> {
23
- readonly empty: true;
24
- readonly name: "";
25
- readonly acc: "";
26
- }
27
- declare type IntervalTokens = [string, string];
28
- /**
29
- * @private
30
- */
31
- export declare function tokenizeInterval(str?: IntervalName): IntervalTokens;
32
- /**
33
- * Get interval properties. It returns an object with:
34
- *
35
- * - name: the interval name
36
- * - num: the interval number
37
- * - type: 'perfectable' or 'majorable'
38
- * - q: the interval quality (d, m, M, A)
39
- * - dir: interval direction (1 ascending, -1 descending)
40
- * - simple: the simplified number
41
- * - semitones: the size in semitones
42
- * - chroma: the interval chroma
43
- *
44
- * @param {string} interval - the interval name
45
- * @return {Object} the interval properties
46
- *
47
- * @example
48
- * import { interval } from '@tonaljs/core'
49
- * interval('P5').semitones // => 7
50
- * interval('m3').type // => 'majorable'
51
- */
52
- export declare function interval(src: IntervalLiteral): Interval | NoInterval;
53
- /**
54
- * @private
55
- *
56
- * forceDescending is used in the case of unison (#243)
57
- */
58
- export declare function coordToInterval(coord: PitchCoordinates, forceDescending?: boolean): Interval;
59
- export {};
@@ -1,11 +0,0 @@
1
- export interface Named {
2
- readonly name: string;
3
- }
4
- export interface NamedFound {
5
- readonly empty: false;
6
- }
7
- export interface NotFound extends Named {
8
- readonly empty: true;
9
- readonly name: "";
10
- }
11
- export declare function isNamed(src: any): src is Named;
@@ -1,43 +0,0 @@
1
- import { Named } from "./named";
2
- import { Pitch, PitchCoordinates } from "./pitch";
3
- export declare type NoteWithOctave = string;
4
- export declare type PcName = string;
5
- export declare type NoteName = NoteWithOctave | PcName;
6
- export declare type NoteLiteral = NoteName | Pitch | Named;
7
- export interface Note extends Pitch, Named {
8
- readonly empty: boolean;
9
- readonly name: NoteName;
10
- readonly letter: string;
11
- readonly acc: string;
12
- readonly pc: PcName;
13
- readonly chroma: number;
14
- readonly height: number;
15
- readonly coord: PitchCoordinates;
16
- readonly midi: number | null;
17
- readonly freq: number | null;
18
- }
19
- export interface NoNote extends Partial<Note> {
20
- empty: true;
21
- name: "";
22
- pc: "";
23
- acc: "";
24
- }
25
- export declare const stepToLetter: (step: number) => string;
26
- export declare const altToAcc: (alt: number) => string;
27
- export declare const accToAlt: (acc: string) => number;
28
- /**
29
- * Given a note literal (a note name or a note object), returns the Note object
30
- * @example
31
- * note('Bb4') // => { name: "Bb4", midi: 70, chroma: 10, ... }
32
- */
33
- export declare function note(src: NoteLiteral): Note | NoNote;
34
- declare type NoteTokens = [string, string, string, string];
35
- /**
36
- * @private
37
- */
38
- export declare function tokenizeNote(str: string): NoteTokens;
39
- /**
40
- * @private
41
- */
42
- export declare function coordToNote(noteCoord: PitchCoordinates): Note;
43
- export {};
@@ -1,25 +0,0 @@
1
- declare type Fifths = number;
2
- declare type Octaves = number;
3
- export declare type Direction = 1 | -1;
4
- export declare type PitchClassCoordinates = [Fifths];
5
- export declare type NoteCoordinates = [Fifths, Octaves];
6
- export declare type IntervalCoordinates = [Fifths, Octaves, Direction];
7
- export declare type PitchCoordinates = PitchClassCoordinates | NoteCoordinates | IntervalCoordinates;
8
- /**
9
- * Pitch properties
10
- *
11
- * - {number} step - The step number: 0 = C, 1 = D, ... 6 = B
12
- * - {number} alt - Number of alterations: -2 = 'bb', -1 = 'b', 0 = '', 1 = '#', ...
13
- * - {number} [oct] = The octave (undefined when is a coord class)
14
- * - {number} [dir] = Interval direction (undefined when is not an interval)
15
- */
16
- export interface Pitch {
17
- readonly step: number;
18
- readonly alt: number;
19
- readonly oct?: number;
20
- readonly dir?: Direction;
21
- }
22
- export declare function isPitch(pitch: any): pitch is Pitch;
23
- export declare function encode(pitch: Pitch): PitchCoordinates;
24
- export declare function decode(coord: PitchCoordinates): Pitch;
25
- export {};
@@ -1,8 +0,0 @@
1
- /**
2
- * Fill a string with a repeated character
3
- *
4
- * @param character
5
- * @param repetition
6
- */
7
- export declare const fillStr: (s: string, n: number) => string;
8
- export declare function deprecate<ResultFn extends (this: any, ...newArgs: any[]) => ReturnType<ResultFn>>(original: string, alternative: string, fn: ResultFn): (this: unknown, ...args: unknown[]) => ReturnType<ResultFn>;