abcjs 6.0.2 → 6.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/README.md +6 -0
- package/RELEASE.md +60 -0
- package/dist/abcjs-basic-min.js +2 -2
- package/dist/abcjs-basic.js +1344 -401
- package/dist/abcjs-basic.js.map +1 -1
- package/dist/abcjs-plugin-min.js +2 -2
- package/index.js +2 -0
- package/package.json +2 -2
- package/src/const/key-accidentals.js +53 -0
- package/src/const/relative-major.js +92 -0
- package/src/parse/abc_parse.js +20 -9
- package/src/parse/abc_parse_book.js +3 -2
- package/src/parse/abc_parse_header.js +3 -3
- package/src/parse/abc_parse_key_voice.js +9 -150
- package/src/parse/abc_parse_music.js +2 -2
- package/src/parse/abc_tokenizer.js +8 -8
- package/src/parse/abc_transpose.js +31 -69
- package/src/parse/all-notes.js +22 -0
- package/src/parse/transpose-chord.js +80 -0
- package/src/str/output.js +433 -0
- package/src/synth/abc_midi_flattener.js +1 -1
- package/src/synth/create-note-map.js +4 -0
- package/src/synth/place-note.js +1 -1
- package/src/test/abc_parser_lint.js +1 -1
- package/src/write/abc_abstract_engraver.js +15 -0
- package/src/write/abc_beam_element.js +24 -0
- package/src/write/abc_decoration.js +13 -0
- package/src/write/abc_glissando_element.js +7 -0
- package/src/write/abc_tie_element.js +11 -0
- package/src/write/draw/glissando.js +75 -0
- package/src/write/draw/voice.js +4 -0
- package/src/write/format-jazz-chord.js +1 -1
- package/src/write/top-text.js +1 -1
- package/types/index.d.ts +12 -8
- package/version.js +1 -1
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
// abc_transpose.js: Handles the automatic transposition of key signatures, chord symbols, and notes.
|
|
2
2
|
|
|
3
|
+
var allNotes = require("./all-notes");
|
|
4
|
+
var transposeChordName = require("../parse/transpose-chord")
|
|
5
|
+
var keyAccidentals = require('../const/key-accidentals');
|
|
3
6
|
var transpose = {};
|
|
4
7
|
|
|
5
8
|
var keyIndex = {
|
|
@@ -24,13 +27,13 @@ var keyIndex = {
|
|
|
24
27
|
var newKey = ['C', 'Db', 'D', 'Eb', 'E', 'F', 'F#', 'G', 'Ab', 'A', 'Bb', 'B'];
|
|
25
28
|
var newKeyMinor = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'Bb', 'B'];
|
|
26
29
|
|
|
27
|
-
transpose.keySignature = function(multilineVars,
|
|
28
|
-
if (multilineVars.clef.type === "perc")
|
|
29
|
-
return { accidentals:
|
|
30
|
+
transpose.keySignature = function(multilineVars, keyName, root, acc, localTranspose) {
|
|
31
|
+
if (multilineVars.clef.type === "perc" || multilineVars.clef.type === "none")
|
|
32
|
+
return { accidentals: keyAccidentals(keyName), root: root, acc: acc };
|
|
30
33
|
if (!localTranspose) localTranspose = 0;
|
|
31
34
|
multilineVars.localTransposeVerticalMovement = 0;
|
|
32
35
|
multilineVars.localTransposePreferFlats = false;
|
|
33
|
-
var k =
|
|
36
|
+
var k = keyAccidentals(keyName);
|
|
34
37
|
if (!k) return multilineVars.key; // If the key isn't in the list, it is non-standard. We won't attempt to transpose it.
|
|
35
38
|
multilineVars.localTranspose = (multilineVars.globalTranspose ? multilineVars.globalTranspose : 0) + localTranspose;
|
|
36
39
|
|
|
@@ -53,7 +56,7 @@ transpose.keySignature = function(multilineVars, keys, keyName, root, acc, local
|
|
|
53
56
|
if (index > 11) index = index % 12;
|
|
54
57
|
var newKeyName = (keyName[0] === 'm' ? newKeyMinor[index] : newKey[index]);
|
|
55
58
|
var transposedKey = newKeyName + keyName;
|
|
56
|
-
var newKeySig =
|
|
59
|
+
var newKeySig = keyAccidentals(transposedKey);
|
|
57
60
|
if (newKeySig.length > 0 && newKeySig[0].acc === 'flat')
|
|
58
61
|
multilineVars.localTransposePreferFlats = true;
|
|
59
62
|
var distance = transposedKey.charCodeAt(0) - baseKey.charCodeAt(0);
|
|
@@ -87,69 +90,8 @@ transpose.keySignature = function(multilineVars, keys, keyName, root, acc, local
|
|
|
87
90
|
return { accidentals: newKeySig, root: newKeyName[0], acc: newKeyName.length > 1 ? newKeyName[1] : "" };
|
|
88
91
|
};
|
|
89
92
|
|
|
90
|
-
var sharpChords = [ 'C', 'C♯', 'D', "D♯", 'E', 'F', "F♯", 'G', 'G♯', 'A', 'A♯', 'B'];
|
|
91
|
-
var flatChords = [ 'C', 'D♭', 'D', 'E♭', 'E', 'F', 'G♭', 'G', 'A♭', 'A', 'B♭', 'B'];
|
|
92
|
-
var sharpChordsFree = [ 'C', 'C#', 'D', "D#", 'E', 'F', "F#", 'G', 'G#', 'A', 'A#', 'B'];
|
|
93
|
-
var flatChordsFree = [ 'C', 'Db', 'D', 'Eb', 'E', 'F', 'Gb', 'G', 'Ab', 'A', 'Bb', 'B'];
|
|
94
|
-
|
|
95
93
|
transpose.chordName = function(multilineVars, chord) {
|
|
96
|
-
|
|
97
|
-
var transposeFactor = multilineVars.localTranspose;
|
|
98
|
-
while (transposeFactor < 0) transposeFactor += 12;
|
|
99
|
-
if (transposeFactor > 11) transposeFactor = transposeFactor % 12;
|
|
100
|
-
if (multilineVars.freegchord) {
|
|
101
|
-
chord = chord.replace(/Cb/g, "`~11`");
|
|
102
|
-
chord = chord.replace(/Db/g, "`~1`");
|
|
103
|
-
chord = chord.replace(/Eb/g, "`~3`");
|
|
104
|
-
chord = chord.replace(/Fb/g, "`~4`");
|
|
105
|
-
chord = chord.replace(/Gb/g, "`~6`");
|
|
106
|
-
chord = chord.replace(/Ab/g, "`~8`");
|
|
107
|
-
chord = chord.replace(/Bb/g, "`~10`");
|
|
108
|
-
chord = chord.replace(/C#/g, "`~1`");
|
|
109
|
-
chord = chord.replace(/D#/g, "`~3`");
|
|
110
|
-
chord = chord.replace(/E#/g, "`~5`");
|
|
111
|
-
chord = chord.replace(/F#/g, "`~6`");
|
|
112
|
-
chord = chord.replace(/G#/g, "`~8`");
|
|
113
|
-
chord = chord.replace(/A#/g, "`~10`");
|
|
114
|
-
chord = chord.replace(/B#/g, "`~0`");
|
|
115
|
-
} else {
|
|
116
|
-
chord = chord.replace(/C♭/g, "`~11`");
|
|
117
|
-
chord = chord.replace(/D♭/g, "`~1`");
|
|
118
|
-
chord = chord.replace(/E♭/g, "`~3`");
|
|
119
|
-
chord = chord.replace(/F♭/g, "`~4`");
|
|
120
|
-
chord = chord.replace(/G♭/g, "`~6`");
|
|
121
|
-
chord = chord.replace(/A♭/g, "`~8`");
|
|
122
|
-
chord = chord.replace(/B♭/g, "`~10`");
|
|
123
|
-
chord = chord.replace(/C♯/g, "`~1`");
|
|
124
|
-
chord = chord.replace(/D♯/g, "`~3`");
|
|
125
|
-
chord = chord.replace(/E♯/g, "`~5`");
|
|
126
|
-
chord = chord.replace(/F♯/g, "`~6`");
|
|
127
|
-
chord = chord.replace(/G♯/g, "`~8`");
|
|
128
|
-
chord = chord.replace(/A♯/g, "`~10`");
|
|
129
|
-
chord = chord.replace(/B♯/g, "`~0`");
|
|
130
|
-
}
|
|
131
|
-
chord = chord.replace(/C/g, "`~0`");
|
|
132
|
-
chord = chord.replace(/D/g, "`~2`");
|
|
133
|
-
chord = chord.replace(/E/g, "`~4`");
|
|
134
|
-
chord = chord.replace(/F/g, "`~5`");
|
|
135
|
-
chord = chord.replace(/G/g, "`~7`");
|
|
136
|
-
chord = chord.replace(/A/g, "`~9`");
|
|
137
|
-
chord = chord.replace(/B/g, "`~11`");
|
|
138
|
-
var arr = chord.split("`");
|
|
139
|
-
for (var i = 0; i < arr.length; i++) {
|
|
140
|
-
if (arr[i][0] === '~') {
|
|
141
|
-
var chordNum = parseInt(arr[i].substr(1),10);
|
|
142
|
-
chordNum += transposeFactor;
|
|
143
|
-
if (chordNum > 11) chordNum -= 12;
|
|
144
|
-
if (multilineVars.freegchord)
|
|
145
|
-
arr[i] = multilineVars.localTransposePreferFlats ? flatChordsFree[chordNum] : sharpChordsFree[chordNum];
|
|
146
|
-
else
|
|
147
|
-
arr[i] = multilineVars.localTransposePreferFlats ? flatChords[chordNum] : sharpChords[chordNum];
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
chord = arr.join("");
|
|
151
|
-
}
|
|
152
|
-
return chord;
|
|
94
|
+
return transposeChordName(chord, multilineVars.localTranspose, multilineVars.localTransposePreferFlats, multilineVars.freegchord)
|
|
153
95
|
};
|
|
154
96
|
|
|
155
97
|
var pitchToLetter = [ 'c', 'd', 'e', 'f', 'g', 'a', 'b' ];
|
|
@@ -196,19 +138,39 @@ var accidentals2 = {
|
|
|
196
138
|
"1": "sharp",
|
|
197
139
|
"2": "dblsharp"
|
|
198
140
|
};
|
|
141
|
+
var accidentals3 = {
|
|
142
|
+
"-2": "__",
|
|
143
|
+
"-1": "_",
|
|
144
|
+
"0": "=",
|
|
145
|
+
"1": "^",
|
|
146
|
+
"2": "^^"
|
|
147
|
+
};
|
|
148
|
+
var count = 0
|
|
199
149
|
transpose.note = function(multilineVars, el) {
|
|
200
|
-
// the "el" that is passed in has el.accidental, and el.pitch. "pitch" is the vertical position (0=middle C)
|
|
150
|
+
// the "el" that is passed in has el.name, el.accidental, and el.pitch. "pitch" is the vertical position (0=middle C)
|
|
201
151
|
// localTranspose is the number of half steps
|
|
202
152
|
// localTransposeVerticalMovement is the vertical distance to move.
|
|
153
|
+
//console.log(count++,multilineVars.localTranspose, el)
|
|
203
154
|
if (!multilineVars.localTranspose || multilineVars.clef.type === "perc")
|
|
204
155
|
return;
|
|
205
156
|
var origPitch = el.pitch;
|
|
206
|
-
|
|
157
|
+
if (multilineVars.localTransposeVerticalMovement) {
|
|
158
|
+
el.pitch = el.pitch + multilineVars.localTransposeVerticalMovement;
|
|
159
|
+
if (el.name) {
|
|
160
|
+
var actual = el.accidental ? el.name.substring(1) : el.name
|
|
161
|
+
var acc = el.accidental ? el.name[0] : ''
|
|
162
|
+
var p = allNotes.pitchIndex(actual)
|
|
163
|
+
el.name = acc + allNotes.noteName(p+multilineVars.localTransposeVerticalMovement)
|
|
164
|
+
}
|
|
165
|
+
}
|
|
207
166
|
|
|
208
167
|
if (el.accidental) {
|
|
209
168
|
var ret = accidentalChange(origPitch, el.pitch, el.accidental, multilineVars.globalTransposeOrigKeySig, multilineVars.targetKey);
|
|
210
169
|
el.pitch = ret[0];
|
|
211
170
|
el.accidental = accidentals2[ret[1]];
|
|
171
|
+
if (el.name) {
|
|
172
|
+
el.name = accidentals3[ret[1]] + el.name.replace(/[_^=]/g,'');
|
|
173
|
+
}
|
|
212
174
|
}
|
|
213
175
|
|
|
214
176
|
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
var allNotes = {};
|
|
2
|
+
|
|
3
|
+
const allPitches = [
|
|
4
|
+
'C,,,', 'D,,,', 'E,,,', 'F,,,', 'G,,,', 'A,,,', 'B,,,',
|
|
5
|
+
'C,,', 'D,,', 'E,,', 'F,,', 'G,,', 'A,,', 'B,,',
|
|
6
|
+
'C,', 'D,', 'E,', 'F,', 'G,', 'A,', 'B,',
|
|
7
|
+
'C', 'D', 'E', 'F', 'G', 'A', 'B',
|
|
8
|
+
'c', 'd', 'e', 'f', 'g', 'a', 'b',
|
|
9
|
+
"c'", "d'", "e'", "f'", "g'", "a'", "b'",
|
|
10
|
+
"c''", "d''", "e''", "f''", "g''", "a''", "b''",
|
|
11
|
+
"c'''", "d'''", "e'''", "f'''", "g'''", "a'''", "b'''",
|
|
12
|
+
];
|
|
13
|
+
|
|
14
|
+
allNotes.pitchIndex = function(noteName) {
|
|
15
|
+
return allPitches.indexOf(noteName)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
allNotes.noteName = function(pitchIndex) {
|
|
19
|
+
return allPitches[pitchIndex]
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
module.exports = allNotes;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
var sharpChords = ['C', 'C♯', 'D', "D♯", 'E', 'F', "F♯", 'G', 'G♯', 'A', 'A♯', 'B'];
|
|
2
|
+
var flatChords = ['C', 'D♭', 'D', 'E♭', 'E', 'F', 'G♭', 'G', 'A♭', 'A', 'B♭', 'B'];
|
|
3
|
+
var sharpChordsFree = ['C', 'C#', 'D', "D#", 'E', 'F', "F#", 'G', 'G#', 'A', 'A#', 'B'];
|
|
4
|
+
var flatChordsFree = ['C', 'Db', 'D', 'Eb', 'E', 'F', 'Gb', 'G', 'Ab', 'A', 'Bb', 'B'];
|
|
5
|
+
|
|
6
|
+
function transposeChordName(chord, steps, preferFlats, freeGCchord) {
|
|
7
|
+
if (!steps || (steps % 12 === 0)) // The chords are the same if it is an exact octave change.
|
|
8
|
+
return chord;
|
|
9
|
+
|
|
10
|
+
// There are two things in the chord that might need to be transposed:
|
|
11
|
+
// The chord will start with a letter from A-G, and might have one accidental after it.
|
|
12
|
+
// That accidental might be an actual sharp or flat char, or it might be a pound sign or lower case "b".
|
|
13
|
+
// Then there is a bunch of stuff that isn't transposed and should just be copied. That is stuff like "7" and more complicated chords.
|
|
14
|
+
// But there is one other exception: right after a slash there will be a bass note and possibly an accidental. That should also be transposed.
|
|
15
|
+
|
|
16
|
+
while (steps < 0) steps += 12;
|
|
17
|
+
if (steps > 11) steps = steps % 12;
|
|
18
|
+
|
|
19
|
+
// (chord name w/accidental) (a bunch of stuff) (/) (bass note) (anything else)
|
|
20
|
+
var match = chord.match(/^([A-G][b#♭♯]?)([^\/]+)?\/?([A-G][b#♭♯]?)?(.+)?/)
|
|
21
|
+
if (!match)
|
|
22
|
+
return chord; // We don't recognize the format of the chord, so skip it.
|
|
23
|
+
var name = match[1]
|
|
24
|
+
var extra1 = match[2]
|
|
25
|
+
var bass = match[3]
|
|
26
|
+
var extra2 = match[4]
|
|
27
|
+
var index = sharpChords.indexOf(name)
|
|
28
|
+
if (index < 0)
|
|
29
|
+
index = flatChords.indexOf(name)
|
|
30
|
+
if (index < 0)
|
|
31
|
+
index = sharpChordsFree.indexOf(name)
|
|
32
|
+
if (index < 0)
|
|
33
|
+
index = flatChordsFree.indexOf(name)
|
|
34
|
+
if (index < 0)
|
|
35
|
+
return chord; // This should never happen, but if we can't find the chord just bail.
|
|
36
|
+
|
|
37
|
+
index += steps
|
|
38
|
+
index = index % 12
|
|
39
|
+
|
|
40
|
+
if (preferFlats) {
|
|
41
|
+
if (freeGCchord) chord = flatChordsFree[index]
|
|
42
|
+
else chord = flatChords[index]
|
|
43
|
+
} else {
|
|
44
|
+
if (freeGCchord) chord = sharpChordsFree[index]
|
|
45
|
+
else chord = sharpChords[index]
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (extra1)
|
|
49
|
+
chord += extra1
|
|
50
|
+
|
|
51
|
+
if (bass) {
|
|
52
|
+
var index = sharpChords.indexOf(bass)
|
|
53
|
+
if (index < 0)
|
|
54
|
+
index = flatChords.indexOf(bass)
|
|
55
|
+
if (index < 0)
|
|
56
|
+
index = sharpChordsFree.indexOf(bass)
|
|
57
|
+
if (index < 0)
|
|
58
|
+
index = flatChordsFree.indexOf(bass)
|
|
59
|
+
chord += '/'
|
|
60
|
+
if (index >= 0) {
|
|
61
|
+
index += steps
|
|
62
|
+
index = index % 12
|
|
63
|
+
if (preferFlats) {
|
|
64
|
+
if (freeGCchord) chord += flatChordsFree[index]
|
|
65
|
+
else chord += flatChords[index]
|
|
66
|
+
} else {
|
|
67
|
+
if (freeGCchord) chord += sharpChordsFree[index]
|
|
68
|
+
else chord += sharpChords[index]
|
|
69
|
+
}
|
|
70
|
+
} else
|
|
71
|
+
chord += bass; // Don't know what to do so do nothing
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (extra2)
|
|
75
|
+
chord += extra2
|
|
76
|
+
|
|
77
|
+
return chord;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
module.exports = transposeChordName
|
|
@@ -0,0 +1,433 @@
|
|
|
1
|
+
var keyAccidentals = require("../const/key-accidentals");
|
|
2
|
+
var { relativeMajor, transposeKey, relativeMode } = require("../const/relative-major");
|
|
3
|
+
var transposeChordName = require("../parse/transpose-chord")
|
|
4
|
+
|
|
5
|
+
var strTranspose;
|
|
6
|
+
|
|
7
|
+
(function () {
|
|
8
|
+
"use strict";
|
|
9
|
+
strTranspose = function (abc, abcTune, steps) {
|
|
10
|
+
if (abcTune === "TEST") // Backdoor way to get entry points for unit tests
|
|
11
|
+
return { keyAccidentals: keyAccidentals, relativeMajor: relativeMajor, transposeKey: transposeKey, relativeMode: relativeMode, transposeChordName: transposeChordName}
|
|
12
|
+
steps = parseInt(steps, 10)
|
|
13
|
+
var changes = [];
|
|
14
|
+
var i;
|
|
15
|
+
for (i = 0; i < abcTune.length; i++)
|
|
16
|
+
changes = changes.concat(transposeOneTune(abc, abcTune[i], steps))
|
|
17
|
+
|
|
18
|
+
// Reverse sort so that we are replacing strings from the end to the beginning so that the indexes aren't invalidated as we go.
|
|
19
|
+
// (Because voices can be written in different ways we can't count on the notes being encountered in the order they appear in the string.)
|
|
20
|
+
changes = changes.sort(function (a, b) {
|
|
21
|
+
return b.start - a.start
|
|
22
|
+
})
|
|
23
|
+
var output = abc.split('')
|
|
24
|
+
for (i = 0; i < changes.length; i++) {
|
|
25
|
+
var ch = changes[i]
|
|
26
|
+
output.splice(ch.start, ch.end - ch.start, ch.note)
|
|
27
|
+
}
|
|
28
|
+
return output.join('')
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function transposeOneTune(abc, abcTune, steps) {
|
|
32
|
+
var changes = []
|
|
33
|
+
|
|
34
|
+
// Don't transpose bagpipe music - that is a special case and is always a particular key
|
|
35
|
+
var key = abcTune.getKeySignature()
|
|
36
|
+
if (key.root === 'Hp' || key.root === "HP")
|
|
37
|
+
return changes;
|
|
38
|
+
|
|
39
|
+
changes = changes.concat(changeAllKeySigs(abc, steps))
|
|
40
|
+
|
|
41
|
+
for (var i = 0; i < abcTune.lines.length; i++) {
|
|
42
|
+
var staves = abcTune.lines[i].staff
|
|
43
|
+
if (staves) {
|
|
44
|
+
for (var j = 0; j < staves.length; j++) {
|
|
45
|
+
var staff = staves[j]
|
|
46
|
+
if (staff.clef.type !== "perc")
|
|
47
|
+
changes = changes.concat(transposeVoices(abc, staff.voices, staff.key, steps))
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return changes
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function changeAllKeySigs(abc, steps) {
|
|
55
|
+
var changes = [];
|
|
56
|
+
var arr = abc.split("K:")
|
|
57
|
+
// now each line except the first one will start with whatever is right after "K:"
|
|
58
|
+
var count = arr[0].length
|
|
59
|
+
for (var i = 1; i < arr.length; i++) {
|
|
60
|
+
var segment = arr[i]
|
|
61
|
+
var match = segment.match(/^( *)([A-G])([#b]?)(\w*)/)
|
|
62
|
+
if (match) {
|
|
63
|
+
var start = count + 2 + match[1].length // move past the 'K:' and optional white space
|
|
64
|
+
var key = match[2] + match[3] + match[4] // key name, accidental, and mode
|
|
65
|
+
var destinationKey = newKey({ root: match[2], acc: match[3], mode: match[4] }, steps)
|
|
66
|
+
var dest = destinationKey.root + destinationKey.acc + destinationKey.mode
|
|
67
|
+
changes.push({ start: start, end: start + key.length, note: dest })
|
|
68
|
+
}
|
|
69
|
+
count += segment.length + 2
|
|
70
|
+
}
|
|
71
|
+
return changes
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function transposeVoices(abc, voices, key, steps) {
|
|
75
|
+
var changes = [];
|
|
76
|
+
var destinationKey = newKey(key, steps)
|
|
77
|
+
for (var i = 0; i < voices.length; i++) {
|
|
78
|
+
changes = changes.concat(transposeVoice(abc, voices[i], key.root, createKeyAccidentals(key), destinationKey, steps))
|
|
79
|
+
}
|
|
80
|
+
return changes
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function createKeyAccidentals(key) {
|
|
84
|
+
var ret = {}
|
|
85
|
+
for (var i = 0; i < key.accidentals.length; i++) {
|
|
86
|
+
var acc = key.accidentals[i];
|
|
87
|
+
if (acc.acc === 'flat')
|
|
88
|
+
ret[acc.note.toUpperCase()] = '_'
|
|
89
|
+
else if (acc.acc === 'sharp')
|
|
90
|
+
ret[acc.note.toUpperCase()] = '^'
|
|
91
|
+
}
|
|
92
|
+
return ret
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function setLetterDistance(destinationKey, keyRoot, steps) {
|
|
96
|
+
var letterDistance = letters.indexOf(destinationKey.root) - letters.indexOf(keyRoot)
|
|
97
|
+
if (keyRoot === "none")
|
|
98
|
+
letterDistance = letters.indexOf(destinationKey.root)
|
|
99
|
+
if (letterDistance === 0) {
|
|
100
|
+
// This could either be a half step (like Eb => E) or almost an octave (like E => Eb)
|
|
101
|
+
if (steps > 2) // If it is a large leap, then we are going up an octave
|
|
102
|
+
letterDistance += 7
|
|
103
|
+
else if (steps === -12) // If it is a large leap, then we are going down an octave
|
|
104
|
+
letterDistance -= 7
|
|
105
|
+
} else if (steps > 0 && letterDistance < 0)
|
|
106
|
+
letterDistance += 7
|
|
107
|
+
else if (steps < 0 && letterDistance > 0)
|
|
108
|
+
letterDistance -= 7
|
|
109
|
+
|
|
110
|
+
if (steps > 12)
|
|
111
|
+
letterDistance += 7
|
|
112
|
+
else if (steps < -12)
|
|
113
|
+
letterDistance -= 7
|
|
114
|
+
|
|
115
|
+
return letterDistance
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function transposeVoice(abc, voice, keyRoot, keyAccidentals, destinationKey, steps) {
|
|
119
|
+
var changes = []
|
|
120
|
+
var letterDistance = setLetterDistance(destinationKey, keyRoot, steps)
|
|
121
|
+
|
|
122
|
+
var measureAccidentals = {}
|
|
123
|
+
var transposedMeasureAccidentals = {}
|
|
124
|
+
for (var i = 0; i < voice.length; i++) {
|
|
125
|
+
var el = voice[i];
|
|
126
|
+
if (el.chord) {
|
|
127
|
+
for (var c = 0; c < el.chord.length; c++) {
|
|
128
|
+
var ch = el.chord[c]
|
|
129
|
+
if (ch.position === 'default') {
|
|
130
|
+
var prefersFlats = destinationKey.accidentals.length && destinationKey.accidentals[0].acc === 'flat'
|
|
131
|
+
var newChord = transposeChordName(ch.name, steps, prefersFlats, true)
|
|
132
|
+
newChord = newChord.replace(/♭/g, "b").replace(/♯/g, "#")
|
|
133
|
+
if (newChord !== ch.name) // If we didn't recognize the chord the input is returned unchanged and there is nothing to replace
|
|
134
|
+
changes.push(replaceChord(abc, el.startChar, el.endChar, newChord))
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
if (el.el_type === 'note' && el.pitches) {
|
|
139
|
+
for (var j = 0; j < el.pitches.length; j++) {
|
|
140
|
+
var note = parseNote(el.pitches[j].name, keyRoot, keyAccidentals, measureAccidentals)
|
|
141
|
+
if (note.acc)
|
|
142
|
+
measureAccidentals[note.name.toUpperCase()] = note.acc
|
|
143
|
+
var newPitch = transposePitch(note, destinationKey, letterDistance, transposedMeasureAccidentals)
|
|
144
|
+
if (newPitch.acc)
|
|
145
|
+
transposedMeasureAccidentals[newPitch.upper] = newPitch.acc
|
|
146
|
+
changes.push(replaceNote(abc, el.startChar, el.endChar, newPitch.acc + newPitch.name, j))
|
|
147
|
+
}
|
|
148
|
+
if (el.gracenotes) {
|
|
149
|
+
for (var g = 0; g < el.gracenotes.length; g++) {
|
|
150
|
+
var grace = parseNote(el.gracenotes[g].name, keyRoot, keyAccidentals, measureAccidentals)
|
|
151
|
+
if (grace.acc)
|
|
152
|
+
measureAccidentals[grace.name.toUpperCase()] = grace.acc
|
|
153
|
+
var newGrace = transposePitch(grace, destinationKey, letterDistance, measureAccidentals)
|
|
154
|
+
if (newGrace.acc)
|
|
155
|
+
transposedMeasureAccidentals[newGrace.upper] = newGrace.acc
|
|
156
|
+
changes.push(replaceGrace(abc, el.startChar, el.endChar, newGrace.acc + newGrace.name, g))
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
} else if (el.el_type === "bar") {
|
|
160
|
+
measureAccidentals = {}
|
|
161
|
+
transposedMeasureAccidentals = {}
|
|
162
|
+
} else if (el.el_type === "keySignature") {
|
|
163
|
+
keyRoot = el.root
|
|
164
|
+
keyAccidentals = createKeyAccidentals(el)
|
|
165
|
+
destinationKey = newKey(el, steps)
|
|
166
|
+
letterDistance = setLetterDistance(destinationKey, keyRoot, steps)
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return changes
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
var letters = "CDEFGAB"
|
|
173
|
+
var octaves = [",,,,", ",,,", ",,", ",", "", "'", "''", "'''", "''''"]
|
|
174
|
+
|
|
175
|
+
function newKey(key, steps) {
|
|
176
|
+
if (key.root === "none") {
|
|
177
|
+
return { root: transposeKey("C", steps), mode: "", acc: "", accidentals: [] }
|
|
178
|
+
}
|
|
179
|
+
var major = relativeMajor(key.root + key.acc + key.mode)
|
|
180
|
+
var newMajor = transposeKey(major, steps)
|
|
181
|
+
var newMode = relativeMode(newMajor, key.mode)
|
|
182
|
+
var acc = keyAccidentals(newMajor)
|
|
183
|
+
return { root: newMode[0], mode: key.mode, acc: newMode.length > 1 ? newMode[1] : '', accidentals: acc }
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function transposePitch(note, key, letterDistance, measureAccidentals) {
|
|
187
|
+
// Depending on what the current note and new note are, the octave might have changed
|
|
188
|
+
// The letterDistance is how far the change is to see if we passed "C" when transposing.
|
|
189
|
+
|
|
190
|
+
var pitch = note.pitch
|
|
191
|
+
var origDistFromC = letters.indexOf(note.name)
|
|
192
|
+
var root = letters.indexOf(key.root)
|
|
193
|
+
var index = (root + pitch) % 7
|
|
194
|
+
// if the note crosses "c" then the octave changes, so that is true of "B" when going up one step, "A" and "B" when going up two steps, etc., and reverse when going down.
|
|
195
|
+
var newDistFromC = origDistFromC + letterDistance
|
|
196
|
+
var oct = note.oct
|
|
197
|
+
while (newDistFromC > 6) {
|
|
198
|
+
oct++
|
|
199
|
+
newDistFromC -= 7
|
|
200
|
+
}
|
|
201
|
+
while (newDistFromC < 0) {
|
|
202
|
+
oct--
|
|
203
|
+
newDistFromC += 7
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
var name = letters[index]
|
|
207
|
+
|
|
208
|
+
var acc = '';
|
|
209
|
+
var adj = note.adj
|
|
210
|
+
// the amount of adjustment depends on the key - if there is a sharp in the key sig, then -1 is a natural, if there isn't, then -1 is a flat.
|
|
211
|
+
var keyAcc = '=';
|
|
212
|
+
for (var i = 0; i < key.accidentals.length; i++) {
|
|
213
|
+
if (key.accidentals[i].note.toLowerCase() === name.toLowerCase()) {
|
|
214
|
+
adj = adj + (key.accidentals[i].acc === 'flat' ? -1 : 1)
|
|
215
|
+
keyAcc = (key.accidentals[i].acc === 'flat' ? '_' : '^')
|
|
216
|
+
break;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
switch (adj) {
|
|
220
|
+
case -2: acc = "__"; break;
|
|
221
|
+
case -1: acc = "_"; break;
|
|
222
|
+
case 0: acc = "="; break;
|
|
223
|
+
case 1: acc = "^"; break;
|
|
224
|
+
case 2: acc = "^^"; break;
|
|
225
|
+
case -3:
|
|
226
|
+
// This requires a triple flat, so bump down the pitch and try again
|
|
227
|
+
var newNote = {}
|
|
228
|
+
newNote.pitch = note.pitch - 1
|
|
229
|
+
newNote.oct = note.oct
|
|
230
|
+
newNote.name = letters[letters.indexOf(note.name) - 1]
|
|
231
|
+
if (!newNote.name) {
|
|
232
|
+
newNote.name = "B"
|
|
233
|
+
newNote.oct--
|
|
234
|
+
}
|
|
235
|
+
if (newNote.name === "B" || newNote.name === "E")
|
|
236
|
+
newNote.adj = note.adj + 1;
|
|
237
|
+
else
|
|
238
|
+
newNote.adj = note.adj + 2;
|
|
239
|
+
return transposePitch(newNote, key, letterDistance + 1, measureAccidentals)
|
|
240
|
+
case 3:
|
|
241
|
+
// This requires a triple sharp, so bump up the pitch and try again
|
|
242
|
+
var newNote = {}
|
|
243
|
+
newNote.pitch = note.pitch + 1
|
|
244
|
+
newNote.oct = note.oct
|
|
245
|
+
newNote.name = letters[letters.indexOf(note.name) + 1]
|
|
246
|
+
if (!newNote.name) {
|
|
247
|
+
newNote.name = "C"
|
|
248
|
+
newNote.oct++
|
|
249
|
+
}
|
|
250
|
+
if (newNote.name === "C" || newNote.name === "F")
|
|
251
|
+
newNote.adj = note.adj - 1;
|
|
252
|
+
else
|
|
253
|
+
newNote.adj = note.adj - 2;
|
|
254
|
+
return transposePitch(newNote, key, letterDistance + 1, measureAccidentals)
|
|
255
|
+
}
|
|
256
|
+
if ((measureAccidentals[name] === acc || (!measureAccidentals[name] && acc === keyAcc)) && !note.courtesy)
|
|
257
|
+
acc = ""
|
|
258
|
+
|
|
259
|
+
switch (oct) {
|
|
260
|
+
case 0: name = name + ",,,"; break;
|
|
261
|
+
case 1: name = name + ",,"; break;
|
|
262
|
+
case 2: name = name + ","; break;
|
|
263
|
+
// case 3: it is already correct
|
|
264
|
+
case 4: name = name.toLowerCase(); break;
|
|
265
|
+
case 5: name = name.toLowerCase() + "'"; break;
|
|
266
|
+
case 6: name = name.toLowerCase() + "''"; break;
|
|
267
|
+
case 7: name = name.toLowerCase() + "'''"; break;
|
|
268
|
+
case 8: name = name.toLowerCase() + "''''"; break;
|
|
269
|
+
}
|
|
270
|
+
if (oct > 4)
|
|
271
|
+
name = name.toLowerCase();
|
|
272
|
+
|
|
273
|
+
return { acc: acc, name: name, upper: name.toUpperCase() }
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
var regPitch = /([_^=]*)([A-Ga-g])([,']*)/
|
|
277
|
+
var regNote = /([_^=]*[A-Ga-g][,']*)(\d*\/*\d*)([\>\<\-\)\.\s\\]*)/
|
|
278
|
+
var regOptionalNote = /([_^=]*[A-Ga-g][,']*)?(\d*\/*\d*)?([\>\<\-\)]*)?/
|
|
279
|
+
var regSpace = /(\s*)$/
|
|
280
|
+
|
|
281
|
+
// This the relationship of the note to the tonic and an octave. So what is returned is a distance in steps from the tonic and the amount of adjustment from
|
|
282
|
+
// a normal scale. That is - in the key of D an F# is two steps from the tonic and no adjustment. A G# is three steps from the tonic and one half-step higher.
|
|
283
|
+
// I don't think there is any adjustment needed for minor keys since the adjustment is based on the key signature and the accidentals.
|
|
284
|
+
function parseNote(note, keyRoot, keyAccidentals, measureAccidentals) {
|
|
285
|
+
var root = keyRoot === "none" ? 0 : letters.indexOf(keyRoot)
|
|
286
|
+
var reg = note.match(regPitch)
|
|
287
|
+
// reg[1] : "__", "_", "", "=", "^", or "^^"
|
|
288
|
+
// reg[2] : A-G a-g
|
|
289
|
+
// reg[3] : commas or apostrophes
|
|
290
|
+
var name = reg[2].toUpperCase()
|
|
291
|
+
var pos = letters.indexOf(name) - root;
|
|
292
|
+
if (pos < 0) pos += 7
|
|
293
|
+
var oct = octaves.indexOf(reg[3])
|
|
294
|
+
if (name === reg[2]) // See if it is a capital letter and subtract an octave if so.
|
|
295
|
+
oct--;
|
|
296
|
+
var currentAcc = measureAccidentals[name] || keyAccidentals[name] || "=" // use the key accidentals if they exist, but override with the measure accidentals, and if neither of them exist, use a natural.
|
|
297
|
+
return { acc: reg[1], name: name, pitch: pos, oct: oct, adj: calcAdjustment(reg[1], keyAccidentals[name], measureAccidentals[name]), courtesy: reg[1] === currentAcc }
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
function replaceNote(abc, start, end, newPitch, index) {
|
|
301
|
+
// There may be more than just the note between the start and end - there could be spaces, there could be a chord symbol, there could be a decoration.
|
|
302
|
+
// This could also be a part of a chord. If so, then the particular note needs to be teased out.
|
|
303
|
+
var note = abc.substring(start, end)
|
|
304
|
+
var match = note.match(new RegExp(regNote.source + regSpace.source), '')
|
|
305
|
+
if (match) {
|
|
306
|
+
// This will match a single note
|
|
307
|
+
var noteLen = match[1].length
|
|
308
|
+
var trailingLen = match[2].length + match[3].length + match[4].length
|
|
309
|
+
var leadingLen = end - start - noteLen - trailingLen
|
|
310
|
+
start += leadingLen
|
|
311
|
+
end -= trailingLen
|
|
312
|
+
} else {
|
|
313
|
+
// I don't know how to capture more than one note, so I'm separating them. There is a limit of the number of notes in a chord depending on the repeats I have here, but it is unlikely to happen in real music.
|
|
314
|
+
var regPreBracket = /([^\[]*)/
|
|
315
|
+
var regOpenBracket = /\[/
|
|
316
|
+
var regCloseBracket = /\-?](\d*\/*\d*)?([\>\<\-\)]*)/
|
|
317
|
+
match = note.match(new RegExp(regPreBracket.source + regOpenBracket.source + regOptionalNote.source +
|
|
318
|
+
regOptionalNote.source + regOptionalNote.source + regOptionalNote.source +
|
|
319
|
+
regOptionalNote.source + regOptionalNote.source + regOptionalNote.source +
|
|
320
|
+
regOptionalNote.source + regCloseBracket.source + regSpace.source))
|
|
321
|
+
|
|
322
|
+
if (match) {
|
|
323
|
+
// This will match a chord
|
|
324
|
+
// Get the number of chars used by the previous notes in this chord
|
|
325
|
+
var count = 1 + match[1].length // one character for the open bracket
|
|
326
|
+
for (var i = 0; i < index; i++) { // index is the iteration through the chord. This function gets called for each one.
|
|
327
|
+
if (match[i * 3 + 2])
|
|
328
|
+
count += match[i * 3 + 2].length
|
|
329
|
+
if (match[i * 3 + 3])
|
|
330
|
+
count += match[i * 3 + 3].length
|
|
331
|
+
if (match[i * 3 + 4])
|
|
332
|
+
count += match[i * 3 + 4].length
|
|
333
|
+
}
|
|
334
|
+
start += count
|
|
335
|
+
var endLen = match[index * 3 + 2] ? match[index * 3 + 2].length : 0
|
|
336
|
+
// endLen += match[index * 3 + 3] ? match[index * 3 + 3].length : 0
|
|
337
|
+
// endLen += match[index * 3 + 4] ? match[index * 3 + 4].length : 0
|
|
338
|
+
|
|
339
|
+
end = start + endLen
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
return { start: start, end: end, note: newPitch }
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
function replaceGrace(abc, start, end, newGrace, index) {
|
|
346
|
+
var note = abc.substring(start, end)
|
|
347
|
+
// I don't know how to capture more than one note, so I'm separating them. There is a limit of the number of notes in a chord depending on the repeats I have here, but it is unlikely to happen in real music.
|
|
348
|
+
var regOpenBrace = /\{/
|
|
349
|
+
var regCloseBrace = /\}/
|
|
350
|
+
var regPreBrace = /([^\{]*)/
|
|
351
|
+
var regPreNote = /(\/*)/
|
|
352
|
+
var match = note.match(new RegExp(regPreBrace.source + regOpenBrace.source + regPreNote.source + regOptionalNote.source +
|
|
353
|
+
regPreNote.source + regOptionalNote.source + regPreNote.source + regOptionalNote.source + regPreNote.source + regOptionalNote.source +
|
|
354
|
+
regPreNote.source + regOptionalNote.source + regPreNote.source + regOptionalNote.source + regPreNote.source + regOptionalNote.source +
|
|
355
|
+
regPreNote.source + regOptionalNote.source + regCloseBrace.source))
|
|
356
|
+
if (match) {
|
|
357
|
+
// This will match all notes inside a grace symbol
|
|
358
|
+
// Get the number of chars used by the previous graces
|
|
359
|
+
var count = 1 + match[1].length // one character for the open brace, and whatever comes before the brace
|
|
360
|
+
for (var i = 0; i < index; i++) { // index is the iteration through the chord. This function gets called for each one.
|
|
361
|
+
if (match[i * 3 + 2])
|
|
362
|
+
count += match[i * 3 + 2].length
|
|
363
|
+
if (match[i * 3 + 3])
|
|
364
|
+
count += match[i * 3 + 3].length
|
|
365
|
+
if (match[i * 3 + 4])
|
|
366
|
+
count += match[i * 3 + 4].length
|
|
367
|
+
if (match[i * 3 + 5])
|
|
368
|
+
count += match[i * 3 + 5].length
|
|
369
|
+
}
|
|
370
|
+
if (match[index * 3 + 2])
|
|
371
|
+
count += match[i * 3 + 2].length
|
|
372
|
+
start += count
|
|
373
|
+
var endLen = match[index * 3 + 3] ? match[index * 3 + 3].length : 0
|
|
374
|
+
endLen += match[index * 3 + 4] ? match[index * 3 + 4].length : 0
|
|
375
|
+
endLen += match[index * 3 + 5] ? match[index * 3 + 5].length : 0
|
|
376
|
+
|
|
377
|
+
end = start + endLen
|
|
378
|
+
}
|
|
379
|
+
return { start: start, end: end, note: newGrace }
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
function replaceChord(abc, start, end, newChord) {
|
|
383
|
+
// Isolate the chord and just replace that
|
|
384
|
+
var match = abc.substring(start, end).match(/([^"]+)?(".+")+/)
|
|
385
|
+
if (match[1])
|
|
386
|
+
start += match[1].length
|
|
387
|
+
end = start + match[2].length
|
|
388
|
+
// leave the quote in, so skip one more
|
|
389
|
+
return { start: start + 1, end: end - 1, note: newChord }
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
function calcAdjustment(thisAccidental, keyAccidental, measureAccidental) {
|
|
393
|
+
if (!thisAccidental && measureAccidental) {
|
|
394
|
+
// There was no accidental on this note, but there was earlier in the measure, so we'll use that
|
|
395
|
+
thisAccidental = measureAccidental
|
|
396
|
+
}
|
|
397
|
+
if (!thisAccidental)
|
|
398
|
+
return 0; // there is no deviation from the key.
|
|
399
|
+
|
|
400
|
+
switch (keyAccidental) {
|
|
401
|
+
case undefined:
|
|
402
|
+
switch (thisAccidental) {
|
|
403
|
+
case '__': return -2;
|
|
404
|
+
case '_': return -1;
|
|
405
|
+
case '=': return 0;
|
|
406
|
+
case '^': return 1;
|
|
407
|
+
case '^^': return 2;
|
|
408
|
+
default: return 0; // this should never happen
|
|
409
|
+
}
|
|
410
|
+
case '_':
|
|
411
|
+
switch (thisAccidental) {
|
|
412
|
+
case '__': return -1;
|
|
413
|
+
case '_': return 0;
|
|
414
|
+
case '=': return 1;
|
|
415
|
+
case '^': return 2;
|
|
416
|
+
case '^^': return 3;
|
|
417
|
+
default: return 0; // this should never happen
|
|
418
|
+
}
|
|
419
|
+
case '^':
|
|
420
|
+
switch (thisAccidental) {
|
|
421
|
+
case '__': return -3;
|
|
422
|
+
case '_': return -2;
|
|
423
|
+
case '=': return -1;
|
|
424
|
+
case '^': return 0;
|
|
425
|
+
case '^^': return 1;
|
|
426
|
+
default: return 0; // this should never happen
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
return 0// this should never happen
|
|
430
|
+
}
|
|
431
|
+
})();
|
|
432
|
+
|
|
433
|
+
module.exports = strTranspose;
|