forked from ddycai/chord-transposer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
203 lines (203 loc) · 7.36 KB
/
index.js
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.transpose = exports.Transposer = void 0;
const KeySignatures_1 = require("./KeySignatures");
const Chord_1 = require("./Chord");
const N_KEYS = 12;
/** Fluent API for transposing text containing chords. */
class Transposer {
constructor(text) {
if (typeof text === "string") {
this.tokens = tokenize(text);
}
else if (text instanceof Array) {
this.tokens = text;
}
else {
throw new Error("Invalid argument (must be text or parsed text).");
}
}
static transpose(text) {
return new Transposer(text);
}
/** Get the key of the text. If not explicitly set, it will be guessed from the first chord. */
getKey() {
if (this.currentKey) {
return this.currentKey;
}
for (const line of this.tokens) {
for (const token of line) {
if (token instanceof Chord_1.Chord) {
return KeySignatures_1.guessKeySignature(token);
}
}
}
throw new Error("Given text has no chords");
}
fromKey(key) {
this.currentKey =
key instanceof KeySignatures_1.KeySignature ? key : KeySignatures_1.KeySignatures.valueOf(key);
return this;
}
up(semitones) {
const key = this.getKey();
const newKey = transposeKey(key, semitones);
const tokens = transposeTokens(this.tokens, key, newKey);
return new Transposer(tokens).fromKey(newKey);
}
down(semitones) {
return this.up(-semitones);
}
toKey(toKey) {
const key = this.getKey();
const newKey = KeySignatures_1.KeySignatures.valueOf(toKey);
const tokens = transposeTokens(this.tokens, key, newKey);
return new Transposer(tokens).fromKey(newKey);
}
/** Returns a string representation of the text. */
toString() {
return this.tokens
.map((line) => line.map((token) => token.toString()).join(''))
.join("\n");
}
}
exports.Transposer = Transposer;
/**
* Finds the key that is a specified number of semitones above/below the current
* key.
*/
function transposeKey(currentKey, semitones) {
const newRank = (currentKey.rank + semitones + N_KEYS) % N_KEYS;
return KeySignatures_1.KeySignatures.forRank(newRank);
}
/** Tokenize the given text into chords.
*
* The ratio of chords to non-chord tokens in each line must be greater than
* the given threshold in order for the line to be transposed. The threshold
* is set to 0.5 by default.
*/
function tokenize(text, threshold) {
if (threshold === undefined) {
threshold = 0.5;
}
const lines = text.split("\n");
const newText = [];
for (const line of lines) {
const newLine = [];
let chordCount = 0;
let tokenCount = 0;
const tokens = line.split(/(\s+|-)/g);
let lastTokenWasString = false;
for (const token of tokens) {
const isTokenEmpty = token.trim() === "";
if (!isTokenEmpty && Chord_1.isChord(token)) {
const chord = Chord_1.Chord.parse(token);
newLine.push(chord);
chordCount++;
lastTokenWasString = false;
}
else {
if (lastTokenWasString) {
newLine.push(newLine.pop() + token);
}
else {
newLine.push(token);
}
if (!isTokenEmpty) {
tokenCount++;
}
lastTokenWasString = true;
}
}
if (chordCount / tokenCount >= threshold) {
newText.push(newLine);
}
else {
newText.push([line]);
}
}
return newText;
}
/**
* Transposes the given parsed text (by the parse() function) to another key.
*/
function transposeTokens(tokens, fromKey, toKey) {
const transpositionMap = createTranspositionMap(fromKey, toKey);
const result = [];
for (const line of tokens) {
const accumulator = [];
let spaceDebt = 0;
line.forEach((token, i) => {
if (typeof token === "string") {
if (spaceDebt > 0) {
const numSpaces = token.search(/\S|$/);
// Keep at least one space.
const spacesToTake = Math.min(spaceDebt, numSpaces, token.length - 1);
const truncatedToken = token.substring(spacesToTake);
accumulator.push(truncatedToken);
spaceDebt = 0;
}
else if (typeof accumulator[accumulator.length - 1] === "string") {
accumulator.push(accumulator.pop() + token);
}
else {
accumulator.push(token);
}
}
else {
const transposedChord = new Chord_1.Chord(transpositionMap.get(token.root), token.suffix, transpositionMap.get(token.bass));
const originalChordLen = token.toString().length;
const transposedChordLen = transposedChord.toString().length;
// Handle length differences between chord and transposed chord.
if (originalChordLen > transposedChordLen) {
// Pad right with spaces.
accumulator.push(transposedChord);
if (i < line.length - 1) {
accumulator.push(" ".repeat(originalChordLen - transposedChordLen));
}
}
else if (originalChordLen < transposedChordLen) {
// Remove spaces from the right (if possible).
spaceDebt += transposedChordLen - originalChordLen;
accumulator.push(transposedChord);
}
else {
accumulator.push(transposedChord);
}
}
});
result.push(accumulator);
}
return result;
}
/**
* Given the current key and the number of semitones to transpose, returns a
* mapping from each note to a transposed note.
*/
function createTranspositionMap(currentKey, newKey) {
const map = new Map();
const semitones = semitonesBetween(currentKey, newKey);
const scale = newKey.chromaticScale;
for (const [chord, rank] of Chord_1.CHORD_RANKS.entries()) {
const newRank = (rank + semitones + N_KEYS) % N_KEYS;
map.set(chord, scale[newRank]);
}
return map;
}
/** Finds the number of semitones between the given keys. */
function semitonesBetween(a, b) {
return b.rank - a.rank;
}
exports.transpose = (text) => new Transposer(text);
var Chord_2 = require("./Chord");
Object.defineProperty(exports, "Chord", { enumerable: true, get: function () { return Chord_2.Chord; } });
var KeySignatures_2 = require("./KeySignatures");
Object.defineProperty(exports, "KeySignature", { enumerable: true, get: function () { return KeySignatures_2.KeySignature; } });
Object.defineProperty(exports, "KeySignatures", { enumerable: true, get: function () { return KeySignatures_2.KeySignatures; } });
exports.default = {
transpose: exports.transpose,
Chord: Chord_1.Chord,
KeySignature: KeySignatures_1.KeySignature,
KeySignatures: KeySignatures_1.KeySignatures,
Transposer,
};