abcjs 6.0.3 → 6.1.1
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 +8 -0
- package/RELEASE.md +53 -1
- package/SECURITY.md +9 -0
- package/dist/abcjs-basic-min.js +2 -2
- package/dist/abcjs-basic.js +1378 -484
- 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/api/abc_tunebook_svg.js +2 -98
- 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_key_voice.js +1 -148
- package/src/parse/abc_parse_music.js +2 -2
- package/src/parse/abc_transpose.js +9 -68
- 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/test/abc_parser_lint.js +1 -1
- package/src/write/abc_abstract_engraver.js +12 -0
- package/src/write/abc_beam_element.js +24 -0
- package/src/write/abc_decoration.js +13 -0
- package/src/write/abc_engraver_controller.js +64 -1
- package/src/write/abc_glissando_element.js +7 -0
- package/src/write/draw/draw.js +8 -0
- package/src/write/draw/glissando.js +75 -0
- package/src/write/draw/staff-group.js +5 -5
- package/src/write/draw/voice.js +4 -0
- package/src/write/selection.js +48 -12
- package/src/write/top-text.js +1 -1
- package/types/index.d.ts +56 -8
- package/version.js +1 -1
- package/temp.txt +0 -16
|
@@ -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;
|
|
@@ -630,7 +630,7 @@ var pitchesToPerc = require('./pitches-to-perc');
|
|
|
630
630
|
if (name && percmap[name])
|
|
631
631
|
actualPitch = percmap[name].sound;
|
|
632
632
|
}
|
|
633
|
-
var p = { cmd: 'note', pitch: actualPitch, volume: velocity, start: timeToRealTime(elem.time), duration: durationRounded(note.duration), instrument: currentInstrument };
|
|
633
|
+
var p = { cmd: 'note', pitch: actualPitch, volume: velocity, start: timeToRealTime(elem.time), duration: durationRounded(note.duration), instrument: currentInstrument, startChar: elem.elem.startChar, endChar: elem.elem.endChar};
|
|
634
634
|
p = adjustForMicroTone(p);
|
|
635
635
|
if (elem.gracenotes) {
|
|
636
636
|
p.duration = p.duration / 2;
|
|
@@ -29,6 +29,10 @@ var createNoteMap = function(sequence) {
|
|
|
29
29
|
end: Math.round((ev.start + len - gap) * 1000000)/1000000,
|
|
30
30
|
volume: ev.volume
|
|
31
31
|
};
|
|
32
|
+
if (ev.startChar)
|
|
33
|
+
obj.startChar = ev.startChar;
|
|
34
|
+
if (ev.endChar)
|
|
35
|
+
obj.endChar = ev.endChar;
|
|
32
36
|
if (ev.style)
|
|
33
37
|
obj.style = ev.style;
|
|
34
38
|
if (ev.cents)
|
|
@@ -57,7 +57,7 @@ var ParserLint = function() {
|
|
|
57
57
|
"trill", "lowermordent", "uppermordent", "mordent", "pralltriller", "accent",
|
|
58
58
|
"fermata", "invertedfermata", "tenuto", "0", "1", "2", "3", "4", "5", "+", "wedge",
|
|
59
59
|
"open", "thumb", "snap", "turn", "roll", "irishroll", "breath", "shortphrase", "mediumphrase", "longphrase",
|
|
60
|
-
"segno", "coda", "D.S.", "D.C.", "fine", "crescendo(", "crescendo)", "diminuendo(", "diminuendo)",
|
|
60
|
+
"segno", "coda", "D.S.", "D.C.", "fine", "crescendo(", "crescendo)", "diminuendo(", "diminuendo)", "glissando(", "glissando)",
|
|
61
61
|
"p", "pp", "f", "ff", "mf", "mp", "ppp", "pppp", "fff", "ffff", "sfz", "repeatbar", "repeatbar2", "slide",
|
|
62
62
|
"upbow", "downbow", "staccato", "trem1", "trem2", "trem3", "trem4",
|
|
63
63
|
"/", "//", "///", "////", "turnx", "invertedturn", "invertedturnx", "arpeggio", "trill(", "trill)", "xstem",
|
|
@@ -390,6 +390,13 @@ AbstractEngraver.prototype.createABCElement = function(isFirstStaff, isSingleLin
|
|
|
390
390
|
var beamelem = new BeamElem(this.stemHeight * this.voiceScale, this.stemdir, this.flatBeams, elems[0]);
|
|
391
391
|
if (hint) beamelem.setHint();
|
|
392
392
|
for (var i = 0; i < elems.length; i++) {
|
|
393
|
+
// Do a first pass to figure out the stem direction before creating the notes, so that staccatos and other decorations can be placed correctly.
|
|
394
|
+
beamelem.runningDirection(elems[i])
|
|
395
|
+
}
|
|
396
|
+
beamelem.setStemDirection()
|
|
397
|
+
var tempStemDir = this.stemdir
|
|
398
|
+
this.stemdir = beamelem.stemsUp ? 'up' : 'down'
|
|
399
|
+
for (i = 0; i < elems.length; i++) {
|
|
393
400
|
var elem = elems[i];
|
|
394
401
|
var abselem = this.createNote(elem, true, isSingleLineStaff, voice);
|
|
395
402
|
abselemset.push(abselem);
|
|
@@ -402,6 +409,7 @@ AbstractEngraver.prototype.createABCElement = function(isFirstStaff, isSingleLin
|
|
|
402
409
|
}
|
|
403
410
|
beamelem.calcDir();
|
|
404
411
|
voice.addBeam(beamelem);
|
|
412
|
+
this.stemdir = tempStemDir
|
|
405
413
|
return abselemset;
|
|
406
414
|
};
|
|
407
415
|
|
|
@@ -1004,6 +1012,10 @@ AbstractEngraver.prototype.createBarLine = function (voice, elem, isFirstStaff)
|
|
|
1004
1012
|
// Add a little space to the left of the bar line so that nothing can crowd it.
|
|
1005
1013
|
abselem.extraw -= 5;
|
|
1006
1014
|
|
|
1015
|
+
if (elem.chord !== undefined) {
|
|
1016
|
+
var ret3 = addChord(this.getTextSize, abselem, elem, 0, 0, 0, false);
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1007
1019
|
return abselem;
|
|
1008
1020
|
|
|
1009
1021
|
};
|
|
@@ -42,6 +42,15 @@ var BeamElem = function BeamElem(stemHeight, type, flat, firstElement) {
|
|
|
42
42
|
this.hint = true;
|
|
43
43
|
};
|
|
44
44
|
|
|
45
|
+
BeamElem.prototype.runningDirection = function (abcelem) {
|
|
46
|
+
var pitch = abcelem.averagepitch;
|
|
47
|
+
if (pitch === undefined) return; // don't include elements like spacers in beams
|
|
48
|
+
this.total = Math.round(this.total+pitch);
|
|
49
|
+
if (!this.count)
|
|
50
|
+
this.count = 0;
|
|
51
|
+
this.count++
|
|
52
|
+
};
|
|
53
|
+
|
|
45
54
|
BeamElem.prototype.add = function(abselem) {
|
|
46
55
|
var pitch = abselem.abcelem.averagepitch;
|
|
47
56
|
if (pitch === undefined) return; // don't include elements like spacers in beams
|
|
@@ -62,6 +71,21 @@ var BeamElem = function BeamElem(stemHeight, type, flat, firstElement) {
|
|
|
62
71
|
this.beams.push(beam);
|
|
63
72
|
};
|
|
64
73
|
|
|
74
|
+
BeamElem.prototype.setStemDirection = function() {
|
|
75
|
+
// Have to figure this out before the notes are placed because placing the notes also places the decorations.
|
|
76
|
+
this.average = calcAverage(this.total, this.count);
|
|
77
|
+
if (this.forceup) {
|
|
78
|
+
this.stemsUp = true;
|
|
79
|
+
} else if (this.forcedown) {
|
|
80
|
+
this.stemsUp = false;
|
|
81
|
+
} else {
|
|
82
|
+
var middleLine = 6; // hardcoded 6 is B
|
|
83
|
+
this.stemsUp = this.average < middleLine; // true is up, false is down;
|
|
84
|
+
}
|
|
85
|
+
delete this.count;
|
|
86
|
+
this.total = 0;
|
|
87
|
+
};
|
|
88
|
+
|
|
65
89
|
BeamElem.prototype.calcDir = function() {
|
|
66
90
|
this.average = calcAverage(this.total, this.elems.length);
|
|
67
91
|
if (this.forceup) {
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
var DynamicDecoration = require('./abc_dynamic_decoration');
|
|
4
4
|
var CrescendoElem = require('./abc_crescendo_element');
|
|
5
|
+
var GlissandoElem = require('./abc_glissando_element');
|
|
5
6
|
var glyphs = require('./abc_glyphs');
|
|
6
7
|
var RelativeElement = require('./abc_relative_element');
|
|
7
8
|
var TieElem = require('./abc_tie_element');
|
|
@@ -277,6 +278,7 @@ var Decoration = function Decoration() {
|
|
|
277
278
|
Decoration.prototype.dynamicDecoration = function(voice, decoration, abselem, positioning) {
|
|
278
279
|
var diminuendo;
|
|
279
280
|
var crescendo;
|
|
281
|
+
var glissando;
|
|
280
282
|
for (var i=0;i<decoration.length; i++) {
|
|
281
283
|
switch(decoration[i]) {
|
|
282
284
|
case "diminuendo(":
|
|
@@ -295,6 +297,14 @@ var Decoration = function Decoration() {
|
|
|
295
297
|
crescendo = { start: this.startCrescendoX, stop: abselem};
|
|
296
298
|
this.startCrescendoX = undefined;
|
|
297
299
|
break;
|
|
300
|
+
case "glissando(":
|
|
301
|
+
this.startGlissandoX = abselem;
|
|
302
|
+
glissando = undefined;
|
|
303
|
+
break;
|
|
304
|
+
case "glissando)":
|
|
305
|
+
glissando = { start: this.startGlissandoX, stop: abselem};
|
|
306
|
+
this.startGlissandoX = undefined;
|
|
307
|
+
break;
|
|
298
308
|
}
|
|
299
309
|
}
|
|
300
310
|
if (diminuendo) {
|
|
@@ -303,6 +313,9 @@ var Decoration = function Decoration() {
|
|
|
303
313
|
if (crescendo) {
|
|
304
314
|
voice.addOther(new CrescendoElem(crescendo.start, crescendo.stop, "<", positioning));
|
|
305
315
|
}
|
|
316
|
+
if (glissando) {
|
|
317
|
+
voice.addOther(new GlissandoElem(glissando.start, glissando.stop));
|
|
318
|
+
}
|
|
306
319
|
};
|
|
307
320
|
|
|
308
321
|
Decoration.prototype.createDecoration = function(voice, decoration, pitch, width, abselem, roomtaken, dir, minPitch, positioning, hasVocals) {
|