abcjs 6.0.0-beta.8 → 6.0.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/.github/workflows/tests.yml +29 -0
- package/CODE_OF_CONDUCT.md +76 -0
- package/CONTRIBUTING.md +1 -0
- package/LICENSE.md +1 -1
- package/README.md +92 -3
- package/RELEASE.md +959 -1
- package/abcjs-audio.css +14 -5
- package/dist/.gitignore +1 -2
- package/dist/abcjs-basic-min.js +3 -0
- package/dist/abcjs-basic-min.js.LICENSE +23 -0
- package/dist/abcjs-basic.js +28232 -0
- package/dist/abcjs-basic.js.map +1 -0
- package/dist/abcjs-plugin-min.js +3 -0
- package/dist/abcjs-plugin-min.js.LICENSE +23 -0
- package/dist/report-basic.html +37 -0
- package/dist/report-before-glyph-compress.html +37 -0
- package/dist/report-brown-ts-target-es5.html +37 -0
- package/dist/report-dev-orig-no-babel.html +37 -0
- package/dist/report-synth.html +37 -0
- package/docker-build.sh +1 -0
- package/glyphs.json +1 -0
- package/index.js +27 -2
- package/{static-wrappers/license.js → license.js} +1 -1
- package/package.json +26 -29
- package/{src/plugin/abc_plugin.js → plugin.js} +31 -19
- package/src/api/abc_animation.js +1 -17
- package/src/api/abc_tablatures.js +144 -0
- package/src/api/abc_timing_callbacks.js +239 -116
- package/src/api/abc_tunebook.js +18 -67
- package/src/api/abc_tunebook_svg.js +38 -46
- package/src/data/abc_tune.js +232 -972
- package/src/data/deline-tune.js +199 -0
- package/src/edit/abc_editarea.js +112 -0
- package/src/edit/abc_editor.js +95 -221
- package/src/midi/abc_midi_create.js +48 -50
- package/src/parse/abc_common.js +0 -14
- package/src/parse/abc_parse.js +167 -1321
- package/src/parse/abc_parse_book.js +62 -0
- package/src/parse/abc_parse_directive.js +164 -41
- package/src/parse/abc_parse_header.js +116 -145
- package/src/parse/abc_parse_key_voice.js +26 -20
- package/src/parse/abc_parse_music.js +1337 -0
- package/src/parse/abc_tokenizer.js +21 -15
- package/src/parse/abc_transpose.js +3 -15
- package/src/parse/tune-builder.js +896 -0
- package/src/parse/wrap_lines.js +205 -453
- package/src/synth/abc_midi_flattener.js +1292 -0
- package/src/{midi → synth}/abc_midi_renderer.js +44 -17
- package/src/synth/abc_midi_sequencer.js +648 -0
- package/src/synth/active-audio-context.js +3 -14
- package/src/synth/cents-to-factor.js +10 -0
- package/src/synth/create-note-map.js +21 -32
- package/src/synth/create-synth-control.js +20 -103
- package/src/synth/create-synth.js +185 -77
- package/src/synth/download-buffer.js +7 -21
- package/src/synth/get-midi-file.js +13 -20
- package/src/synth/images/{loading.svg → loading.svg.js} +4 -0
- package/src/synth/images/loop.svg.js +65 -0
- package/src/synth/images/pause.svg.js +10 -0
- package/src/synth/images/play.svg.js +9 -0
- package/src/synth/images/{reset.svg → reset.svg.js} +5 -1
- package/src/synth/instrument-index-to-name.js +1 -16
- package/src/synth/load-note.js +37 -76
- package/src/synth/pitch-to-note-name.js +0 -15
- package/src/synth/pitches-to-perc.js +64 -0
- package/src/synth/place-note.js +78 -68
- package/src/synth/play-event.js +17 -18
- package/src/synth/register-audio-context.js +11 -23
- package/src/synth/sounds-cache.js +0 -15
- package/src/synth/supports-audio.js +9 -23
- package/src/synth/synth-controller.js +80 -49
- package/src/synth/synth-sequence.js +20 -34
- package/src/tablatures/instruments/guitar/guitar-fonts.js +19 -0
- package/src/tablatures/instruments/guitar/guitar-patterns.js +23 -0
- package/src/tablatures/instruments/guitar/tab-guitar.js +50 -0
- package/src/tablatures/instruments/string-patterns.js +277 -0
- package/src/tablatures/instruments/string-tablature.js +56 -0
- package/src/tablatures/instruments/tab-note.js +282 -0
- package/src/tablatures/instruments/tab-notes.js +41 -0
- package/src/tablatures/instruments/violin/tab-violin.js +47 -0
- package/src/tablatures/instruments/violin/violin-fonts.js +19 -0
- package/src/tablatures/instruments/violin/violin-patterns.js +23 -0
- package/src/tablatures/tab-absolute-elements.js +310 -0
- package/src/tablatures/tab-common.js +29 -0
- package/src/tablatures/tab-renderer.js +243 -0
- package/src/tablatures/transposer.js +110 -0
- package/src/test/abc_midi_lint.js +5 -22
- package/src/test/abc_midi_sequencer_lint.js +11 -14
- package/src/test/abc_parser_lint.js +136 -32
- package/src/test/abc_vertical_lint.js +94 -32
- package/src/test/rendering-lint.js +38 -5
- package/src/write/abc_absolute_element.js +112 -120
- package/src/write/abc_abstract_engraver.js +102 -253
- package/src/write/abc_beam_element.js +30 -290
- package/src/write/abc_brace_element.js +12 -121
- package/src/write/abc_create_clef.js +21 -32
- package/src/write/abc_create_key_signature.js +8 -26
- package/src/write/abc_create_note_head.js +107 -0
- package/src/write/abc_create_time_signature.js +2 -21
- package/src/write/abc_crescendo_element.js +3 -50
- package/src/write/abc_decoration.js +7 -30
- package/src/write/abc_dynamic_decoration.js +3 -37
- package/src/write/abc_ending_element.js +1 -57
- package/src/write/abc_engraver_controller.js +111 -234
- package/src/write/abc_glyphs.js +8 -18
- package/src/write/abc_relative_element.js +57 -97
- package/src/write/abc_renderer.js +10 -832
- package/src/write/abc_spacing.js +0 -15
- package/src/write/abc_staff_group_element.js +14 -349
- package/src/write/abc_tempo_element.js +9 -117
- package/src/write/abc_tie_element.js +5 -68
- package/src/write/abc_triplet_element.js +6 -124
- package/src/write/abc_voice_element.js +7 -222
- package/src/write/add-chord.js +103 -0
- package/src/write/add-text-if.js +33 -0
- package/src/write/bottom-text.js +79 -0
- package/src/write/calcHeight.js +17 -0
- package/src/write/classes.js +100 -0
- package/src/write/draw/absolute.js +68 -0
- package/src/write/draw/beam.js +56 -0
- package/src/write/draw/brace.js +106 -0
- package/src/write/draw/crescendo.js +38 -0
- package/src/write/draw/debug-box.js +8 -0
- package/src/write/draw/draw.js +56 -0
- package/src/write/draw/dynamics.js +20 -0
- package/src/write/draw/ending.js +46 -0
- package/src/write/draw/group-elements.js +66 -0
- package/src/write/draw/horizontal-line.js +25 -0
- package/src/write/draw/non-music.js +50 -0
- package/src/write/draw/print-line.js +24 -0
- package/src/write/draw/print-path.js +7 -0
- package/src/write/draw/print-stem.js +30 -0
- package/src/write/draw/print-symbol.js +59 -0
- package/src/write/draw/print-vertical-line.js +18 -0
- package/src/write/draw/relative.js +77 -0
- package/src/write/draw/round-number.js +5 -0
- package/src/write/draw/selectables.js +59 -0
- package/src/write/draw/separator.js +16 -0
- package/src/write/draw/set-paper-size.js +45 -0
- package/src/write/{sprintf.js → draw/sprintf.js} +0 -0
- package/src/write/draw/staff-group.js +226 -0
- package/src/write/draw/staff-line.js +9 -0
- package/src/write/draw/staff.js +33 -0
- package/src/write/draw/tab-line.js +40 -0
- package/src/write/draw/tempo.js +45 -0
- package/src/write/draw/text.js +71 -0
- package/src/write/draw/tie.js +97 -0
- package/src/write/draw/triplet.js +46 -0
- package/src/write/draw/voice.js +102 -0
- package/src/write/format-jazz-chord.js +15 -0
- package/src/write/free-text.js +41 -0
- package/src/write/get-font-and-attr.js +37 -0
- package/src/write/get-text-size.js +56 -0
- package/src/write/highlight.js +11 -0
- package/src/write/layout/VoiceElements.js +121 -0
- package/src/write/layout/beam.js +213 -0
- package/src/write/layout/get-left-edge-of-staff.js +56 -0
- package/src/write/layout/getBarYAt.js +6 -0
- package/src/write/layout/layout.js +94 -0
- package/src/write/layout/setUpperAndLowerElements.js +232 -0
- package/src/write/layout/staffGroup.js +146 -0
- package/src/write/layout/triplet.js +75 -0
- package/src/write/layout/voice.js +137 -0
- package/src/write/selection.js +188 -70
- package/src/write/separator.js +10 -0
- package/src/write/set-class.js +21 -0
- package/src/write/subtitle.js +12 -0
- package/src/write/svg.js +95 -43
- package/src/write/top-text.js +54 -0
- package/src/write/unhighlight.js +11 -0
- package/temp.txt +15256 -0
- package/test.js +27 -64
- package/types/index.d.ts +1095 -0
- package/version.js +1 -1
- package/.babelrc +0 -5
- package/.eslintrc +0 -3
- package/.gitmodules +0 -3
- package/abcjs-midi.css +0 -166
- package/build-utils/loadPresets.js +0 -14
- package/build-utils/presets/webpack.analyze.js +0 -6
- package/build-utils/presets/webpack.optimize.js +0 -30
- package/build-utils/webpack.development.js +0 -14
- package/build-utils/webpack.production.js +0 -35
- package/deploy-docs.sh +0 -25
- package/docs/README.md +0 -33
- package/fix-versions.sh +0 -23
- package/mei.js +0 -43
- package/midi.js +0 -62
- package/src/api/abc_tunebook_midi.js +0 -116
- package/src/midi/abc_midi_controls.js +0 -701
- package/src/midi/abc_midi_flattener.js +0 -1119
- package/src/midi/abc_midi_js_preparer.js +0 -243
- package/src/midi/abc_midi_sequencer.js +0 -401
- package/src/midi/abc_midi_ui_generator.js +0 -86
- package/src/plugin/abc_plugin_midi.js +0 -220
- package/src/synth/images/loop.svg +0 -61
- package/src/synth/images/pause.svg +0 -6
- package/src/synth/images/play.svg +0 -5
- package/src/transform/abc2abc_write.js +0 -395
- package/static-wrappers/basic.js +0 -2
- package/static-wrappers/midi.js +0 -2
- package/static-wrappers/plugin-midi.js +0 -6
- package/static-wrappers/plugin.js +0 -6
- package/webpack.config.js +0 -29
|
@@ -0,0 +1,896 @@
|
|
|
1
|
+
var parseKeyVoice = require('../parse/abc_parse_key_voice');
|
|
2
|
+
var parseCommon = require('../parse/abc_common');
|
|
3
|
+
|
|
4
|
+
var TuneBuilder = function(tune) {
|
|
5
|
+
var self = this;
|
|
6
|
+
|
|
7
|
+
this.setVisualTranspose = function(visualTranspose) {
|
|
8
|
+
if (visualTranspose)
|
|
9
|
+
tune.visualTranspose = visualTranspose;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
this.resolveOverlays = function() {
|
|
13
|
+
var madeChanges = false;
|
|
14
|
+
var durationsPerLines = [];
|
|
15
|
+
for (var i = 0; i < tune.lines.length; i++) {
|
|
16
|
+
var line = tune.lines[i];
|
|
17
|
+
if (line.staff) {
|
|
18
|
+
for (var j = 0; j < line.staff.length; j++) {
|
|
19
|
+
var staff = line.staff[j];
|
|
20
|
+
var overlayVoice = [];
|
|
21
|
+
for (var k = 0; k < staff.voices.length; k++) {
|
|
22
|
+
var voice = staff.voices[k];
|
|
23
|
+
overlayVoice.push({ hasOverlay: false, voice: [], snip: []});
|
|
24
|
+
durationsPerLines[i] = 0;
|
|
25
|
+
var durationThisBar = 0;
|
|
26
|
+
var inOverlay = false;
|
|
27
|
+
var overlayDuration = 0;
|
|
28
|
+
var snipStart = -1;
|
|
29
|
+
for (var kk = 0; kk < voice.length; kk++) {
|
|
30
|
+
var event = voice[kk];
|
|
31
|
+
if (event.el_type === "overlay" && !inOverlay) {
|
|
32
|
+
madeChanges = true;
|
|
33
|
+
inOverlay = true;
|
|
34
|
+
snipStart = kk;
|
|
35
|
+
overlayVoice[k].hasOverlay = true;
|
|
36
|
+
if (overlayDuration === 0)
|
|
37
|
+
overlayDuration = durationsPerLines[i];
|
|
38
|
+
// If this isn't the first line, we also need invisible rests on the previous lines.
|
|
39
|
+
// So, if the next voice doesn't appear in a previous line, create it
|
|
40
|
+
for (var ii = 0; ii < i; ii++) {
|
|
41
|
+
if (durationsPerLines[ii] && tune.lines[ii].staff && staff.voices.length >= tune.lines[ii].staff[0].voices.length) {
|
|
42
|
+
tune.lines[ii].staff[0].voices.push([{
|
|
43
|
+
el_type: "note",
|
|
44
|
+
duration: durationsPerLines[ii],
|
|
45
|
+
rest: {type: "invisible"},
|
|
46
|
+
startChar: event.startChar,
|
|
47
|
+
endChar: event.endChar
|
|
48
|
+
}]);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
} else if (event.el_type === "bar") {
|
|
52
|
+
if (inOverlay) {
|
|
53
|
+
// delete the overlay events from this array without messing up this loop.
|
|
54
|
+
inOverlay = false;
|
|
55
|
+
overlayVoice[k].snip.push({ start: snipStart, len: kk - snipStart});
|
|
56
|
+
overlayVoice[k].voice.push(event); // Also end the overlay with the barline.
|
|
57
|
+
} else {
|
|
58
|
+
// This keeps the voices lined up: if the overlay isn't in the first measure then we need a bunch of invisible rests.
|
|
59
|
+
if (durationThisBar > 0)
|
|
60
|
+
overlayVoice[k].voice.push({ el_type: "note", duration: durationThisBar, rest: {type: "invisible"}, startChar: event.startChar, endChar: event.endChar });
|
|
61
|
+
overlayVoice[k].voice.push(event);
|
|
62
|
+
}
|
|
63
|
+
durationThisBar = 0;
|
|
64
|
+
} else if (event.el_type === "note") {
|
|
65
|
+
if (inOverlay) {
|
|
66
|
+
overlayVoice[k].voice.push(event);
|
|
67
|
+
} else {
|
|
68
|
+
durationThisBar += event.duration;
|
|
69
|
+
durationsPerLines[i] += event.duration;
|
|
70
|
+
}
|
|
71
|
+
} else if (event.el_type === "scale" || event.el_type === "stem" || event.el_type === "overlay" || event.el_type === "style" || event.el_type === "transpose") {
|
|
72
|
+
// These types of events are duplicated on the overlay layer.
|
|
73
|
+
overlayVoice[k].voice.push(event);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (overlayVoice[k].hasOverlay && overlayVoice[k].snip.length === 0) {
|
|
77
|
+
// there was no closing bar, so we didn't set the snip amount.
|
|
78
|
+
overlayVoice[k].snip.push({ start: snipStart, len: voice.length - snipStart});
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
for (k = 0; k < overlayVoice.length; k++) {
|
|
82
|
+
var ov = overlayVoice[k];
|
|
83
|
+
if (ov.hasOverlay) {
|
|
84
|
+
ov.voice.splice(0, 0, {el_type: "stem", direction: "down"})
|
|
85
|
+
staff.voices.push(ov.voice);
|
|
86
|
+
for (var kkk = ov.snip.length-1; kkk >= 0; kkk--) {
|
|
87
|
+
var snip = ov.snip[kkk];
|
|
88
|
+
staff.voices[k].splice(snip.start, snip.len);
|
|
89
|
+
staff.voices[k].splice(snip.start+1, 0, { el_type: "stem", direction: "auto" });
|
|
90
|
+
var indexOfLastBar = findLastBar(staff.voices[k], snip.start);
|
|
91
|
+
staff.voices[k].splice(indexOfLastBar, 0, { el_type: "stem", direction: "up" });
|
|
92
|
+
}
|
|
93
|
+
// remove ending marks from the overlay voice so they are not repeated
|
|
94
|
+
for (kkk = 0; kkk < staff.voices[staff.voices.length-1].length; kkk++) {
|
|
95
|
+
staff.voices[staff.voices.length-1][kkk] = parseCommon.clone(staff.voices[staff.voices.length-1][kkk]);
|
|
96
|
+
var el = staff.voices[staff.voices.length-1][kkk];
|
|
97
|
+
if (el.el_type === 'bar' && el.startEnding) {
|
|
98
|
+
delete el.startEnding;
|
|
99
|
+
}
|
|
100
|
+
if (el.el_type === 'bar' && el.endEnding)
|
|
101
|
+
delete el.endEnding;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return madeChanges;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
function findLastBar(voice, start) {
|
|
112
|
+
for (var i = start-1; i > 0 && voice[i].el_type !== "bar"; i--) {
|
|
113
|
+
|
|
114
|
+
}
|
|
115
|
+
return i;
|
|
116
|
+
}
|
|
117
|
+
function fixTitles(lines) {
|
|
118
|
+
// We might have name and subname defined. We now know what line everything is on, so we can determine which to use.
|
|
119
|
+
var firstMusicLine = true;
|
|
120
|
+
for (var i = 0; i < lines.length; i++) {
|
|
121
|
+
var line = lines[i];
|
|
122
|
+
if (line.staff) {
|
|
123
|
+
for (var j = 0; j < line.staff.length; j++) {
|
|
124
|
+
var staff = line.staff[j];
|
|
125
|
+
if (staff.title) {
|
|
126
|
+
var hasATitle = false;
|
|
127
|
+
for (var k = 0; k < staff.title.length; k++) {
|
|
128
|
+
if (staff.title[k]) {
|
|
129
|
+
staff.title[k] = (firstMusicLine) ? staff.title[k].name : staff.title[k].subname;
|
|
130
|
+
if (staff.title[k])
|
|
131
|
+
hasATitle = true;
|
|
132
|
+
else
|
|
133
|
+
staff.title[k] = '';
|
|
134
|
+
} else
|
|
135
|
+
staff.title[k] = '';
|
|
136
|
+
}
|
|
137
|
+
if (!hasATitle)
|
|
138
|
+
delete staff.title;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
firstMusicLine = false;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
this.cleanUp = function(barsperstaff, staffnonote, currSlur) {
|
|
147
|
+
this.closeLine(); // Close the last line.
|
|
148
|
+
delete tune.runningFonts;
|
|
149
|
+
|
|
150
|
+
// If the tempo was created with a string like "Allegro", then the duration of a beat needs to be set at the last moment, when it is most likely known.
|
|
151
|
+
if (tune.metaText.tempo && tune.metaText.tempo.bpm && !tune.metaText.tempo.duration)
|
|
152
|
+
tune.metaText.tempo.duration = [ tune.getBeatLength() ];
|
|
153
|
+
|
|
154
|
+
// Remove any blank lines
|
|
155
|
+
var anyDeleted = false;
|
|
156
|
+
var i, s, v;
|
|
157
|
+
for (i = 0; i < tune.lines.length; i++) {
|
|
158
|
+
if (tune.lines[i].staff !== undefined) {
|
|
159
|
+
var hasAny = false;
|
|
160
|
+
for (s = 0; s < tune.lines[i].staff.length; s++) {
|
|
161
|
+
if (tune.lines[i].staff[s] === undefined) {
|
|
162
|
+
anyDeleted = true;
|
|
163
|
+
tune.lines[i].staff[s] = null;
|
|
164
|
+
//tune.lines[i].staff[s] = { voices: []}; // TODO-PER: There was a part missing in the abc music. How should we recover?
|
|
165
|
+
} else {
|
|
166
|
+
for (v = 0; v < tune.lines[i].staff[s].voices.length; v++) {
|
|
167
|
+
if (tune.lines[i].staff[s].voices[v] === undefined)
|
|
168
|
+
tune.lines[i].staff[s].voices[v] = []; // TODO-PER: There was a part missing in the abc music. How should we recover?
|
|
169
|
+
else
|
|
170
|
+
if (this.containsNotes(tune.lines[i].staff[s].voices[v])) hasAny = true;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
if (!hasAny) {
|
|
175
|
+
tune.lines[i] = null;
|
|
176
|
+
anyDeleted = true;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
if (anyDeleted) {
|
|
181
|
+
tune.lines = parseCommon.compact(tune.lines);
|
|
182
|
+
parseCommon.each(tune.lines, function(line) {
|
|
183
|
+
if (line.staff)
|
|
184
|
+
line.staff = parseCommon.compact(line.staff);
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// if we exceeded the number of bars allowed on a line, then force a new line
|
|
189
|
+
if (barsperstaff) {
|
|
190
|
+
while (wrapMusicLines(tune.lines, barsperstaff)) {
|
|
191
|
+
// This will keep wrapping until the end of the piece.
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// If we were passed staffnonote, then we want to get rid of all staffs that contain only rests.
|
|
196
|
+
if (staffnonote) {
|
|
197
|
+
anyDeleted = false;
|
|
198
|
+
for (i = 0; i < tune.lines.length; i++) {
|
|
199
|
+
if (tune.lines[i].staff !== undefined) {
|
|
200
|
+
for (s = 0; s < tune.lines[i].staff.length; s++) {
|
|
201
|
+
var keepThis = false;
|
|
202
|
+
for (v = 0; v < tune.lines[i].staff[s].voices.length; v++) {
|
|
203
|
+
if (this.containsNotesStrict(tune.lines[i].staff[s].voices[v])) {
|
|
204
|
+
keepThis = true;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
if (!keepThis) {
|
|
208
|
+
anyDeleted = true;
|
|
209
|
+
tune.lines[i].staff[s] = null;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
if (anyDeleted) {
|
|
215
|
+
parseCommon.each(tune.lines, function(line) {
|
|
216
|
+
if (line.staff)
|
|
217
|
+
line.staff = parseCommon.compact(line.staff);
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
fixTitles(tune.lines);
|
|
223
|
+
|
|
224
|
+
// Remove the temporary working variables
|
|
225
|
+
for (i = 0; i < tune.lines.length; i++) {
|
|
226
|
+
if (tune.lines[i].staff) {
|
|
227
|
+
for (s = 0; s < tune.lines[i].staff.length; s++)
|
|
228
|
+
delete tune.lines[i].staff[s].workingClef;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// If there are overlays, create new voices for them.
|
|
233
|
+
while (this.resolveOverlays()) {
|
|
234
|
+
// keep resolving overlays as long as any are found.
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function cleanUpSlursInLine(line, staffNum, voiceNum) {
|
|
238
|
+
if (!currSlur[staffNum])
|
|
239
|
+
currSlur[staffNum] = [];
|
|
240
|
+
if (!currSlur[staffNum][voiceNum])
|
|
241
|
+
currSlur[staffNum][voiceNum] = [];
|
|
242
|
+
var x;
|
|
243
|
+
// var lyr = null; // TODO-PER: debugging.
|
|
244
|
+
|
|
245
|
+
var addEndSlur = function(obj, num, chordPos) {
|
|
246
|
+
if (currSlur[staffNum][voiceNum][chordPos] === undefined) {
|
|
247
|
+
// There isn't an exact match for note position, but we'll take any other open slur.
|
|
248
|
+
for (x = 0; x < currSlur[staffNum][voiceNum].length; x++) {
|
|
249
|
+
if (currSlur[staffNum][voiceNum][x] !== undefined) {
|
|
250
|
+
chordPos = x;
|
|
251
|
+
break;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
if (currSlur[staffNum][voiceNum][chordPos] === undefined) {
|
|
255
|
+
var offNum = chordPos*100+1;
|
|
256
|
+
parseCommon.each(obj.endSlur, function(x) { if (offNum === x) --offNum; });
|
|
257
|
+
currSlur[staffNum][voiceNum][chordPos] = [offNum];
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
var slurNum;
|
|
261
|
+
for (var i = 0; i < num; i++) {
|
|
262
|
+
slurNum = currSlur[staffNum][voiceNum][chordPos].pop();
|
|
263
|
+
obj.endSlur.push(slurNum);
|
|
264
|
+
// lyr.syllable += '<' + slurNum; // TODO-PER: debugging
|
|
265
|
+
}
|
|
266
|
+
if (currSlur[staffNum][voiceNum][chordPos].length === 0)
|
|
267
|
+
delete currSlur[staffNum][voiceNum][chordPos];
|
|
268
|
+
return slurNum;
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
var addStartSlur = function(obj, num, chordPos, usedNums) {
|
|
272
|
+
obj.startSlur = [];
|
|
273
|
+
if (currSlur[staffNum][voiceNum][chordPos] === undefined) {
|
|
274
|
+
currSlur[staffNum][voiceNum][chordPos] = [];
|
|
275
|
+
}
|
|
276
|
+
var nextNum = chordPos*100+1;
|
|
277
|
+
for (var i = 0; i < num; i++) {
|
|
278
|
+
if (usedNums) {
|
|
279
|
+
parseCommon.each(usedNums, function(x) { if (nextNum === x) ++nextNum; });
|
|
280
|
+
parseCommon.each(usedNums, function(x) { if (nextNum === x) ++nextNum; });
|
|
281
|
+
parseCommon.each(usedNums, function(x) { if (nextNum === x) ++nextNum; });
|
|
282
|
+
}
|
|
283
|
+
parseCommon.each(currSlur[staffNum][voiceNum][chordPos], function(x) { if (nextNum === x) ++nextNum; });
|
|
284
|
+
parseCommon.each(currSlur[staffNum][voiceNum][chordPos], function(x) { if (nextNum === x) ++nextNum; });
|
|
285
|
+
|
|
286
|
+
currSlur[staffNum][voiceNum][chordPos].push(nextNum);
|
|
287
|
+
obj.startSlur.push({ label: nextNum });
|
|
288
|
+
if (obj.dottedSlur) {
|
|
289
|
+
obj.startSlur[obj.startSlur.length-1].style = 'dotted';
|
|
290
|
+
delete obj.dottedSlur;
|
|
291
|
+
}
|
|
292
|
+
// lyr.syllable += ' ' + nextNum + '>'; // TODO-PER:debugging
|
|
293
|
+
nextNum++;
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
for (var i = 0; i < line.length; i++) {
|
|
298
|
+
var el = line[i];
|
|
299
|
+
// if (el.lyric === undefined) // TODO-PER: debugging
|
|
300
|
+
// el.lyric = [{ divider: '-' }]; // TODO-PER: debugging
|
|
301
|
+
// lyr = el.lyric[0]; // TODO-PER: debugging
|
|
302
|
+
// lyr.syllable = ''; // TODO-PER: debugging
|
|
303
|
+
if (el.el_type === 'note') {
|
|
304
|
+
if (el.gracenotes) {
|
|
305
|
+
for (var g = 0; g < el.gracenotes.length; g++) {
|
|
306
|
+
if (el.gracenotes[g].endSlur) {
|
|
307
|
+
var gg = el.gracenotes[g].endSlur;
|
|
308
|
+
el.gracenotes[g].endSlur = [];
|
|
309
|
+
for (var ggg = 0; ggg < gg; ggg++)
|
|
310
|
+
addEndSlur(el.gracenotes[g], 1, 20);
|
|
311
|
+
}
|
|
312
|
+
if (el.gracenotes[g].startSlur) {
|
|
313
|
+
x = el.gracenotes[g].startSlur;
|
|
314
|
+
addStartSlur(el.gracenotes[g], x, 20);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
if (el.endSlur) {
|
|
319
|
+
x = el.endSlur;
|
|
320
|
+
el.endSlur = [];
|
|
321
|
+
addEndSlur(el, x, 0);
|
|
322
|
+
}
|
|
323
|
+
if (el.startSlur) {
|
|
324
|
+
x = el.startSlur;
|
|
325
|
+
addStartSlur(el, x, 0);
|
|
326
|
+
}
|
|
327
|
+
if (el.pitches) {
|
|
328
|
+
var usedNums = [];
|
|
329
|
+
for (var p = 0; p < el.pitches.length; p++) {
|
|
330
|
+
if (el.pitches[p].endSlur) {
|
|
331
|
+
var k = el.pitches[p].endSlur;
|
|
332
|
+
el.pitches[p].endSlur = [];
|
|
333
|
+
for (var j = 0; j < k; j++) {
|
|
334
|
+
var slurNum = addEndSlur(el.pitches[p], 1, p+1);
|
|
335
|
+
usedNums.push(slurNum);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
for (p = 0; p < el.pitches.length; p++) {
|
|
340
|
+
if (el.pitches[p].startSlur) {
|
|
341
|
+
x = el.pitches[p].startSlur;
|
|
342
|
+
addStartSlur(el.pitches[p], x, p+1, usedNums);
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
// Correct for the weird gracenote case where ({g}a) should match.
|
|
346
|
+
// The end slur was already assigned to the note, and needs to be moved to the first note of the graces.
|
|
347
|
+
if (el.gracenotes && el.pitches[0].endSlur && el.pitches[0].endSlur[0] === 100 && el.pitches[0].startSlur) {
|
|
348
|
+
if (el.gracenotes[0].endSlur)
|
|
349
|
+
el.gracenotes[0].endSlur.push(el.pitches[0].startSlur[0].label);
|
|
350
|
+
else
|
|
351
|
+
el.gracenotes[0].endSlur = [el.pitches[0].startSlur[0].label];
|
|
352
|
+
if (el.pitches[0].endSlur.length === 1)
|
|
353
|
+
delete el.pitches[0].endSlur;
|
|
354
|
+
else if (el.pitches[0].endSlur[0] === 100)
|
|
355
|
+
el.pitches[0].endSlur.shift();
|
|
356
|
+
else if (el.pitches[0].endSlur[el.pitches[0].endSlur.length-1] === 100)
|
|
357
|
+
el.pitches[0].endSlur.pop();
|
|
358
|
+
if (currSlur[staffNum][voiceNum][1].length === 1)
|
|
359
|
+
delete currSlur[staffNum][voiceNum][1];
|
|
360
|
+
else
|
|
361
|
+
currSlur[staffNum][voiceNum][1].pop();
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// TODO-PER: This could be done faster as we go instead of as the last step.
|
|
369
|
+
function fixClefPlacement(el) {
|
|
370
|
+
parseKeyVoice.fixClef(el);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
function wrapMusicLines(lines, barsperstaff) {
|
|
374
|
+
for (i = 0; i < lines.length; i++) {
|
|
375
|
+
if (lines[i].staff !== undefined) {
|
|
376
|
+
for (s = 0; s < lines[i].staff.length; s++) {
|
|
377
|
+
var permanentItems = [];
|
|
378
|
+
for (v = 0; v < lines[i].staff[s].voices.length; v++) {
|
|
379
|
+
var voice = lines[i].staff[s].voices[v];
|
|
380
|
+
var barNumThisLine = 0;
|
|
381
|
+
for (var n = 0; n < voice.length; n++) {
|
|
382
|
+
if (voice[n].el_type === 'bar') {
|
|
383
|
+
barNumThisLine++;
|
|
384
|
+
if (barNumThisLine >= barsperstaff) {
|
|
385
|
+
// push everything else to the next line, if there is anything else,
|
|
386
|
+
// and there is a next line. If there isn't a next line, create one.
|
|
387
|
+
if (n < voice.length - 1) {
|
|
388
|
+
var nextLine = getNextMusicLine(lines, i);
|
|
389
|
+
if (!nextLine) {
|
|
390
|
+
var cp = JSON.parse(JSON.stringify(lines[i]));
|
|
391
|
+
lines.push(parseCommon.clone(cp));
|
|
392
|
+
nextLine = lines[lines.length - 1];
|
|
393
|
+
for (var ss = 0; ss < nextLine.staff.length; ss++) {
|
|
394
|
+
for (var vv = 0; vv < nextLine.staff[ss].voices.length; vv++)
|
|
395
|
+
nextLine.staff[ss].voices[vv] = [];
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
var startElement = n + 1;
|
|
399
|
+
var section = lines[i].staff[s].voices[v].slice(startElement);
|
|
400
|
+
lines[i].staff[s].voices[v] = lines[i].staff[s].voices[v].slice(0, startElement);
|
|
401
|
+
nextLine.staff[s].voices[v] = permanentItems.concat(section.concat(nextLine.staff[s].voices[v]));
|
|
402
|
+
return true;
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
} else if (!voice[n].duration) {
|
|
406
|
+
permanentItems.push(voice[n]);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
return false;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
function getNextMusicLine(lines, currentLine) {
|
|
417
|
+
currentLine++;
|
|
418
|
+
while (lines.length > currentLine) {
|
|
419
|
+
if (lines[currentLine].staff)
|
|
420
|
+
return lines[currentLine];
|
|
421
|
+
currentLine++;
|
|
422
|
+
}
|
|
423
|
+
return null;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
for (tune.lineNum = 0; tune.lineNum < tune.lines.length; tune.lineNum++) {
|
|
427
|
+
var staff = tune.lines[tune.lineNum].staff;
|
|
428
|
+
if (staff) {
|
|
429
|
+
for (tune.staffNum = 0; tune.staffNum < staff.length; tune.staffNum++) {
|
|
430
|
+
if (staff[tune.staffNum].clef)
|
|
431
|
+
fixClefPlacement(staff[tune.staffNum].clef);
|
|
432
|
+
for (tune.voiceNum = 0; tune.voiceNum < staff[tune.staffNum].voices.length; tune.voiceNum++) {
|
|
433
|
+
var voice = staff[tune.staffNum].voices[tune.voiceNum];
|
|
434
|
+
cleanUpSlursInLine(voice, tune.staffNum, tune.voiceNum);
|
|
435
|
+
for (var j = 0; j < voice.length; j++) {
|
|
436
|
+
if (voice[j].el_type === 'clef')
|
|
437
|
+
fixClefPlacement(voice[j]);
|
|
438
|
+
}
|
|
439
|
+
if (voice.length > 0 && voice[voice.length-1].barNumber) {
|
|
440
|
+
// Don't hang a bar number on the last bar line: it should go on the next line.
|
|
441
|
+
var nextLine = getNextMusicLine(tune.lines, tune.lineNum);
|
|
442
|
+
if (nextLine)
|
|
443
|
+
nextLine.staff[0].barNumber = voice[voice.length-1].barNumber;
|
|
444
|
+
delete voice[voice.length-1].barNumber;
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
// Remove temporary variables that the outside doesn't need to know about
|
|
452
|
+
delete tune.staffNum;
|
|
453
|
+
delete tune.voiceNum;
|
|
454
|
+
delete tune.lineNum;
|
|
455
|
+
delete tune.potentialStartBeam;
|
|
456
|
+
delete tune.potentialEndBeam;
|
|
457
|
+
delete tune.vskipPending;
|
|
458
|
+
|
|
459
|
+
return currSlur;
|
|
460
|
+
};
|
|
461
|
+
|
|
462
|
+
tune.reset();
|
|
463
|
+
|
|
464
|
+
this.getLastNote = function() {
|
|
465
|
+
if (tune.lines[tune.lineNum] && tune.lines[tune.lineNum].staff && tune.lines[tune.lineNum].staff[tune.staffNum] &&
|
|
466
|
+
tune.lines[tune.lineNum].staff[tune.staffNum].voices[tune.voiceNum]) {
|
|
467
|
+
for (var i = tune.lines[tune.lineNum].staff[tune.staffNum].voices[tune.voiceNum].length-1; i >= 0; i--) {
|
|
468
|
+
var el = tune.lines[tune.lineNum].staff[tune.staffNum].voices[tune.voiceNum][i];
|
|
469
|
+
if (el.el_type === 'note') {
|
|
470
|
+
return el;
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
return null;
|
|
475
|
+
};
|
|
476
|
+
|
|
477
|
+
this.addTieToLastNote = function(dottedTie) {
|
|
478
|
+
// TODO-PER: if this is a chord, which note?
|
|
479
|
+
var el = this.getLastNote();
|
|
480
|
+
if (el && el.pitches && el.pitches.length > 0) {
|
|
481
|
+
el.pitches[0].startTie = {};
|
|
482
|
+
if (dottedTie)
|
|
483
|
+
el.pitches[0].startTie.style = 'dotted';
|
|
484
|
+
return true;
|
|
485
|
+
}
|
|
486
|
+
return false;
|
|
487
|
+
};
|
|
488
|
+
|
|
489
|
+
this.getDuration = function(el) {
|
|
490
|
+
if (el.duration) return el.duration;
|
|
491
|
+
//if (el.pitches && el.pitches.length > 0) return el.pitches[0].duration;
|
|
492
|
+
return 0;
|
|
493
|
+
};
|
|
494
|
+
|
|
495
|
+
this.closeLine = function() {
|
|
496
|
+
if (tune.potentialStartBeam && tune.potentialEndBeam) {
|
|
497
|
+
tune.potentialStartBeam.startBeam = true;
|
|
498
|
+
tune.potentialEndBeam.endBeam = true;
|
|
499
|
+
}
|
|
500
|
+
delete tune.potentialStartBeam;
|
|
501
|
+
delete tune.potentialEndBeam;
|
|
502
|
+
};
|
|
503
|
+
|
|
504
|
+
this.appendElement = function(type, startChar, endChar, hashParams)
|
|
505
|
+
{
|
|
506
|
+
var This = tune;
|
|
507
|
+
var pushNote = function(hp) {
|
|
508
|
+
var currStaff = This.lines[This.lineNum].staff[This.staffNum];
|
|
509
|
+
if (!currStaff) {
|
|
510
|
+
// TODO-PER: This prevents a crash, but it drops the element. Need to figure out how to start a new line, or delay adding this.
|
|
511
|
+
return;
|
|
512
|
+
}
|
|
513
|
+
if (hp.pitches !== undefined) {
|
|
514
|
+
var mid = currStaff.workingClef.verticalPos;
|
|
515
|
+
parseCommon.each(hp.pitches, function(p) { p.verticalPos = p.pitch - mid; });
|
|
516
|
+
}
|
|
517
|
+
if (hp.gracenotes !== undefined) {
|
|
518
|
+
var mid2 = currStaff.workingClef.verticalPos;
|
|
519
|
+
parseCommon.each(hp.gracenotes, function(p) { p.verticalPos = p.pitch - mid2; });
|
|
520
|
+
}
|
|
521
|
+
currStaff.voices[This.voiceNum].push(hp);
|
|
522
|
+
};
|
|
523
|
+
hashParams.el_type = type;
|
|
524
|
+
if (startChar !== null)
|
|
525
|
+
hashParams.startChar = startChar;
|
|
526
|
+
if (endChar !== null)
|
|
527
|
+
hashParams.endChar = endChar;
|
|
528
|
+
var endBeamHere = function() {
|
|
529
|
+
This.potentialStartBeam.startBeam = true;
|
|
530
|
+
hashParams.endBeam = true;
|
|
531
|
+
delete This.potentialStartBeam;
|
|
532
|
+
delete This.potentialEndBeam;
|
|
533
|
+
};
|
|
534
|
+
var endBeamLast = function() {
|
|
535
|
+
if (This.potentialStartBeam !== undefined && This.potentialEndBeam !== undefined) { // Do we have a set of notes to beam?
|
|
536
|
+
This.potentialStartBeam.startBeam = true;
|
|
537
|
+
This.potentialEndBeam.endBeam = true;
|
|
538
|
+
}
|
|
539
|
+
delete This.potentialStartBeam;
|
|
540
|
+
delete This.potentialEndBeam;
|
|
541
|
+
};
|
|
542
|
+
if (type === 'note') { // && (hashParams.rest !== undefined || hashParams.end_beam === undefined)) {
|
|
543
|
+
// Now, add the startBeam and endBeam where it is needed.
|
|
544
|
+
// end_beam is already set on the places where there is a forced end_beam. We'll remove that here after using that info.
|
|
545
|
+
// this.potentialStartBeam either points to null or the start beam.
|
|
546
|
+
// this.potentialEndBeam either points to null or the start beam.
|
|
547
|
+
// If we have a beam break (note is longer than a quarter, or an end_beam is on this element), then set the beam if we have one.
|
|
548
|
+
// reset the variables for the next notes.
|
|
549
|
+
var dur = self.getDuration(hashParams);
|
|
550
|
+
if (dur >= 0.25) { // The beam ends on the note before this.
|
|
551
|
+
endBeamLast();
|
|
552
|
+
} else if (hashParams.force_end_beam_last && This.potentialStartBeam !== undefined) {
|
|
553
|
+
endBeamLast();
|
|
554
|
+
} else if (hashParams.end_beam && This.potentialStartBeam !== undefined) { // the beam is forced to end on this note, probably because of a space in the ABC
|
|
555
|
+
if (hashParams.rest === undefined)
|
|
556
|
+
endBeamHere();
|
|
557
|
+
else
|
|
558
|
+
endBeamLast();
|
|
559
|
+
} else if (hashParams.rest === undefined) { // this a short note and we aren't about to end the beam
|
|
560
|
+
if (This.potentialStartBeam === undefined) { // We aren't collecting notes for a beam, so start here.
|
|
561
|
+
if (!hashParams.end_beam) {
|
|
562
|
+
This.potentialStartBeam = hashParams;
|
|
563
|
+
delete This.potentialEndBeam;
|
|
564
|
+
}
|
|
565
|
+
} else {
|
|
566
|
+
This.potentialEndBeam = hashParams; // Continue the beaming, look for the end next note.
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
// end_beam goes on rests and notes which precede rests _except_ when a rest (or set of adjacent rests) has normal notes on both sides (no spaces)
|
|
571
|
+
// if (hashParams.rest !== undefined)
|
|
572
|
+
// {
|
|
573
|
+
// hashParams.end_beam = true;
|
|
574
|
+
// var el2 = this.getLastNote();
|
|
575
|
+
// if (el2) el2.end_beam = true;
|
|
576
|
+
// // TODO-PER: implement exception mentioned in the comment.
|
|
577
|
+
// }
|
|
578
|
+
} else { // It's not a note, so there definitely isn't beaming after it.
|
|
579
|
+
endBeamLast();
|
|
580
|
+
}
|
|
581
|
+
delete hashParams.end_beam; // We don't want this temporary variable hanging around.
|
|
582
|
+
delete hashParams.force_end_beam_last; // We don't want this temporary variable hanging around.
|
|
583
|
+
pushNote(hashParams);
|
|
584
|
+
};
|
|
585
|
+
|
|
586
|
+
this.appendStartingElement = function(type, startChar, endChar, hashParams2)
|
|
587
|
+
{
|
|
588
|
+
// If we're in the middle of beaming, then end the beam.
|
|
589
|
+
this.closeLine();
|
|
590
|
+
|
|
591
|
+
// We only ever want implied naturals the first time.
|
|
592
|
+
var impliedNaturals;
|
|
593
|
+
if (type === 'key') {
|
|
594
|
+
impliedNaturals = hashParams2.impliedNaturals;
|
|
595
|
+
delete hashParams2.impliedNaturals;
|
|
596
|
+
delete hashParams2.explicitAccidentals;
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
// Clone the object because it will be sticking around for the next line and we don't want the extra fields in it.
|
|
600
|
+
var hashParams = parseCommon.clone(hashParams2);
|
|
601
|
+
|
|
602
|
+
if (tune.lines[tune.lineNum] && tune.lines[tune.lineNum].staff) { // be sure that we are on a music type line before doing the following.
|
|
603
|
+
// If tune is the first item in tune staff, then we might have to initialize the staff, first.
|
|
604
|
+
if (tune.lines[tune.lineNum].staff.length <= tune.staffNum) {
|
|
605
|
+
tune.lines[tune.lineNum].staff[tune.staffNum] = {};
|
|
606
|
+
tune.lines[tune.lineNum].staff[tune.staffNum].clef = parseCommon.clone(tune.lines[tune.lineNum].staff[0].clef);
|
|
607
|
+
tune.lines[tune.lineNum].staff[tune.staffNum].key = parseCommon.clone(tune.lines[tune.lineNum].staff[0].key);
|
|
608
|
+
if (tune.lines[tune.lineNum].staff[0].meter)
|
|
609
|
+
tune.lines[tune.lineNum].staff[tune.staffNum].meter = parseCommon.clone(tune.lines[tune.lineNum].staff[0].meter);
|
|
610
|
+
tune.lines[tune.lineNum].staff[tune.staffNum].workingClef = parseCommon.clone(tune.lines[tune.lineNum].staff[0].workingClef);
|
|
611
|
+
tune.lines[tune.lineNum].staff[tune.staffNum].voices = [[]];
|
|
612
|
+
}
|
|
613
|
+
// If tune is a clef type, then we replace the working clef on the line. This is kept separate from
|
|
614
|
+
// the clef in case there is an inline clef field. We need to know what the current position for
|
|
615
|
+
// the note is.
|
|
616
|
+
if (type === 'clef') {
|
|
617
|
+
tune.lines[tune.lineNum].staff[tune.staffNum].workingClef = hashParams;
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
// These elements should not be added twice, so if the element exists on tune line without a note or bar before it, just replace the staff version.
|
|
621
|
+
var voice = tune.lines[tune.lineNum].staff[tune.staffNum].voices[tune.voiceNum];
|
|
622
|
+
for (var i = 0; i < voice.length; i++) {
|
|
623
|
+
if (voice[i].el_type === 'note' || voice[i].el_type === 'bar') {
|
|
624
|
+
hashParams.el_type = type;
|
|
625
|
+
hashParams.startChar = startChar;
|
|
626
|
+
hashParams.endChar = endChar;
|
|
627
|
+
if (impliedNaturals)
|
|
628
|
+
hashParams.accidentals = impliedNaturals.concat(hashParams.accidentals);
|
|
629
|
+
voice.push(hashParams);
|
|
630
|
+
return;
|
|
631
|
+
}
|
|
632
|
+
if (voice[i].el_type === type) {
|
|
633
|
+
hashParams.el_type = type;
|
|
634
|
+
hashParams.startChar = startChar;
|
|
635
|
+
hashParams.endChar = endChar;
|
|
636
|
+
if (impliedNaturals)
|
|
637
|
+
hashParams.accidentals = impliedNaturals.concat(hashParams.accidentals);
|
|
638
|
+
voice[i] = hashParams;
|
|
639
|
+
return;
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
// We didn't see either that type or a note, so replace the element to the staff.
|
|
643
|
+
tune.lines[tune.lineNum].staff[tune.staffNum][type] = hashParams2;
|
|
644
|
+
}
|
|
645
|
+
};
|
|
646
|
+
|
|
647
|
+
this.pushLine = function(hash) {
|
|
648
|
+
if (tune.vskipPending) {
|
|
649
|
+
hash.vskip = tune.vskipPending;
|
|
650
|
+
delete tune.vskipPending;
|
|
651
|
+
}
|
|
652
|
+
tune.lines.push(hash);
|
|
653
|
+
};
|
|
654
|
+
|
|
655
|
+
this.addSubtitle = function(str, info) {
|
|
656
|
+
this.pushLine({subtitle: { text: str, startChar: info.startChar, endChar: info.endChar}});
|
|
657
|
+
};
|
|
658
|
+
|
|
659
|
+
this.addSpacing = function(num) {
|
|
660
|
+
tune.vskipPending = num;
|
|
661
|
+
};
|
|
662
|
+
|
|
663
|
+
this.addNewPage = function(num) {
|
|
664
|
+
this.pushLine({newpage: num});
|
|
665
|
+
};
|
|
666
|
+
|
|
667
|
+
this.addSeparator = function(spaceAbove, spaceBelow, lineLength, info) {
|
|
668
|
+
this.pushLine({separator: {spaceAbove: Math.round(spaceAbove), spaceBelow: Math.round(spaceBelow), lineLength: Math.round(lineLength), startChar: info.startChar, endChar: info.endChar}});
|
|
669
|
+
};
|
|
670
|
+
|
|
671
|
+
this.addText = function(str, info) {
|
|
672
|
+
this.pushLine({text: { text: str, startChar: info.startChar, endChar: info.endChar}});
|
|
673
|
+
};
|
|
674
|
+
|
|
675
|
+
this.addCentered = function(str) {
|
|
676
|
+
this.pushLine({text: [{text: str, center: true }]});
|
|
677
|
+
};
|
|
678
|
+
|
|
679
|
+
this.containsNotes = function(voice) {
|
|
680
|
+
for (var i = 0; i < voice.length; i++) {
|
|
681
|
+
if (voice[i].el_type === 'note' || voice[i].el_type === 'bar')
|
|
682
|
+
return true;
|
|
683
|
+
}
|
|
684
|
+
return false;
|
|
685
|
+
};
|
|
686
|
+
|
|
687
|
+
this.containsNotesStrict = function(voice) {
|
|
688
|
+
for (var i = 0; i < voice.length; i++) {
|
|
689
|
+
if (voice[i].el_type === 'note' && (voice[i].rest === undefined || voice[i].chord !== undefined))
|
|
690
|
+
return true;
|
|
691
|
+
}
|
|
692
|
+
return false;
|
|
693
|
+
};
|
|
694
|
+
|
|
695
|
+
// anyVoiceContainsNotes: function(line) {
|
|
696
|
+
// for (var i = 0; i < line.staff.voices.length; i++) {
|
|
697
|
+
// if (this.containsNotes(line.staff.voices[i]))
|
|
698
|
+
// return true;
|
|
699
|
+
// }
|
|
700
|
+
// return false;
|
|
701
|
+
// },
|
|
702
|
+
this.changeVoiceScale = function(scale) {
|
|
703
|
+
self.appendElement('scale', null, null, { size: scale} );
|
|
704
|
+
};
|
|
705
|
+
|
|
706
|
+
this.startNewLine = function(params) {
|
|
707
|
+
// If the pointed to line doesn't exist, just create that. If the line does exist, but doesn't have any music on it, just use it.
|
|
708
|
+
// If it does exist and has music, then increment the line number. If the new element doesn't exist, create it.
|
|
709
|
+
var This = tune;
|
|
710
|
+
this.closeLine(); // Close the previous line.
|
|
711
|
+
var createVoice = function(params) {
|
|
712
|
+
var thisStaff = This.lines[This.lineNum].staff[This.staffNum];
|
|
713
|
+
thisStaff.voices[This.voiceNum] = [];
|
|
714
|
+
if (!thisStaff.title)
|
|
715
|
+
thisStaff.title = [];
|
|
716
|
+
thisStaff.title[This.voiceNum] = { name: params.name, subname: params.subname };
|
|
717
|
+
if (params.style)
|
|
718
|
+
self.appendElement('style', null, null, {head: params.style});
|
|
719
|
+
if (params.stem)
|
|
720
|
+
self.appendElement('stem', null, null, {direction: params.stem});
|
|
721
|
+
else if (This.voiceNum > 0) {
|
|
722
|
+
if (thisStaff.voices[0]!== undefined) {
|
|
723
|
+
var found = false;
|
|
724
|
+
for (var i = 0; i < thisStaff.voices[0].length; i++) {
|
|
725
|
+
if (thisStaff.voices[0].el_type === 'stem')
|
|
726
|
+
found = true;
|
|
727
|
+
}
|
|
728
|
+
if (!found) {
|
|
729
|
+
var stem = { el_type: 'stem', direction: 'up' };
|
|
730
|
+
thisStaff.voices[0].splice(0,0,stem);
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
self.appendElement('stem', null, null, {direction: 'down'});
|
|
734
|
+
}
|
|
735
|
+
if (params.scale)
|
|
736
|
+
self.appendElement('scale', null, null, { size: params.scale} );
|
|
737
|
+
};
|
|
738
|
+
var createStaff = function(params) {
|
|
739
|
+
if (params.key && params.key.impliedNaturals) {
|
|
740
|
+
params.key.accidentals = params.key.accidentals.concat(params.key.impliedNaturals);
|
|
741
|
+
delete params.key.impliedNaturals;
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
This.lines[This.lineNum].staff[This.staffNum] = {voices: [ ], clef: params.clef, key: params.key, workingClef: params.clef };
|
|
745
|
+
if (params.stafflines !== undefined) {
|
|
746
|
+
This.lines[This.lineNum].staff[This.staffNum].clef.stafflines = params.stafflines;
|
|
747
|
+
This.lines[This.lineNum].staff[This.staffNum].workingClef.stafflines = params.stafflines;
|
|
748
|
+
}
|
|
749
|
+
if (params.staffscale) {
|
|
750
|
+
This.lines[This.lineNum].staff[This.staffNum].staffscale = params.staffscale;
|
|
751
|
+
}
|
|
752
|
+
if (params.annotationfont) self.setLineFont("annotationfont", params.annotationfont);
|
|
753
|
+
if (params.gchordfont) self.setLineFont("gchordfont", params.gchordfont);
|
|
754
|
+
if (params.tripletfont) self.setLineFont("tripletfont", params.tripletfont);
|
|
755
|
+
if (params.vocalfont) self.setLineFont("vocalfont", params.vocalfont);
|
|
756
|
+
if (params.bracket) This.lines[This.lineNum].staff[This.staffNum].bracket = params.bracket;
|
|
757
|
+
if (params.brace) This.lines[This.lineNum].staff[This.staffNum].brace = params.brace;
|
|
758
|
+
if (params.connectBarLines) This.lines[This.lineNum].staff[This.staffNum].connectBarLines = params.connectBarLines;
|
|
759
|
+
if (params.barNumber) This.lines[This.lineNum].staff[This.staffNum].barNumber = params.barNumber;
|
|
760
|
+
createVoice(params);
|
|
761
|
+
// Some stuff just happens for the first voice
|
|
762
|
+
if (params.part)
|
|
763
|
+
self.appendElement('part', params.part.startChar, params.part.endChar, {title: params.part.title});
|
|
764
|
+
if (params.meter !== undefined) This.lines[This.lineNum].staff[This.staffNum].meter = params.meter;
|
|
765
|
+
if (This.vskipPending) {
|
|
766
|
+
This.lines[This.lineNum].vskip = This.vskipPending;
|
|
767
|
+
delete This.vskipPending;
|
|
768
|
+
}
|
|
769
|
+
};
|
|
770
|
+
var createLine = function(params) {
|
|
771
|
+
This.lines[This.lineNum] = {staff: []};
|
|
772
|
+
createStaff(params);
|
|
773
|
+
};
|
|
774
|
+
if (tune.lines[tune.lineNum] === undefined) createLine(params);
|
|
775
|
+
else if (tune.lines[tune.lineNum].staff === undefined) {
|
|
776
|
+
tune.lineNum++;
|
|
777
|
+
this.startNewLine(params);
|
|
778
|
+
} else if (tune.lines[tune.lineNum].staff[tune.staffNum] === undefined) createStaff(params);
|
|
779
|
+
else if (tune.lines[tune.lineNum].staff[tune.staffNum].voices[tune.voiceNum] === undefined) createVoice(params);
|
|
780
|
+
else if (!this.containsNotes(tune.lines[tune.lineNum].staff[tune.staffNum].voices[tune.voiceNum])) {
|
|
781
|
+
// We don't need a new line but we might need to update parts of it.
|
|
782
|
+
if (params.part)
|
|
783
|
+
self.appendElement('part', params.part.startChar, params.part.endChar, {title: params.part.title});
|
|
784
|
+
} else {
|
|
785
|
+
tune.lineNum++;
|
|
786
|
+
this.startNewLine(params);
|
|
787
|
+
}
|
|
788
|
+
};
|
|
789
|
+
|
|
790
|
+
this.setRunningFont = function(type, font) {
|
|
791
|
+
// This is called at tune start to set the current default fonts so we know whether to record a change.
|
|
792
|
+
tune.runningFonts[type] = font;
|
|
793
|
+
};
|
|
794
|
+
|
|
795
|
+
this.setLineFont = function(type, font) {
|
|
796
|
+
// If we haven't encountered the font type yet then we are using the default font so it doesn't
|
|
797
|
+
// need to be noted. If we have encountered it, then only record it if it is different from the last time.
|
|
798
|
+
if (tune.runningFonts[type]) {
|
|
799
|
+
var isDifferent = false;
|
|
800
|
+
var keys = Object.keys(font);
|
|
801
|
+
for (var i = 0; i < keys.length; i++) {
|
|
802
|
+
if (tune.runningFonts[type][keys[i]] !== font[keys[i]])
|
|
803
|
+
isDifferent = true;
|
|
804
|
+
}
|
|
805
|
+
if (isDifferent) {
|
|
806
|
+
tune.lines[tune.lineNum].staff[tune.staffNum][type] = font;
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
tune.runningFonts[type] = font;
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
this.setBarNumberImmediate = function(barNumber) {
|
|
813
|
+
// If tune is called right at the beginning of a line, then correct the measure number that is already written.
|
|
814
|
+
// If tune is called at the beginning of a measure, then correct the measure number that was just created.
|
|
815
|
+
// If tune is called in the middle of a measure, then subtract one from it, because it will be incremented before applied.
|
|
816
|
+
var currentVoice = this.getCurrentVoice();
|
|
817
|
+
if (currentVoice && currentVoice.length > 0) {
|
|
818
|
+
var lastElement = currentVoice[currentVoice.length-1];
|
|
819
|
+
if (lastElement.el_type === 'bar') {
|
|
820
|
+
if (lastElement.barNumber !== undefined) // the measure number might not be written for tune bar, don't override that.
|
|
821
|
+
lastElement.barNumber = barNumber;
|
|
822
|
+
} else
|
|
823
|
+
return barNumber-1;
|
|
824
|
+
}
|
|
825
|
+
return barNumber;
|
|
826
|
+
};
|
|
827
|
+
|
|
828
|
+
this.hasBeginMusic = function() {
|
|
829
|
+
// return true if there exists at least one line that contains "staff"
|
|
830
|
+
for (var i = 0; i < tune.lines.length; i++) {
|
|
831
|
+
if (tune.lines[i].staff)
|
|
832
|
+
return true;
|
|
833
|
+
}
|
|
834
|
+
return false;
|
|
835
|
+
};
|
|
836
|
+
|
|
837
|
+
this.isFirstLine = function(index) {
|
|
838
|
+
for (var i = index-1; i >= 0; i--) {
|
|
839
|
+
if (tune.lines[i].staff !== undefined) return false;
|
|
840
|
+
}
|
|
841
|
+
return true;
|
|
842
|
+
};
|
|
843
|
+
|
|
844
|
+
this.getCurrentVoice = function() {
|
|
845
|
+
var currLine = tune.lines[tune.lineNum];
|
|
846
|
+
if (!currLine)
|
|
847
|
+
return null;
|
|
848
|
+
var currStaff = currLine.staff[tune.staffNum];
|
|
849
|
+
if (!currStaff)
|
|
850
|
+
return null;
|
|
851
|
+
if (currStaff.voices[tune.voiceNum] !== undefined)
|
|
852
|
+
return currStaff.voices[tune.voiceNum];
|
|
853
|
+
else return null;
|
|
854
|
+
};
|
|
855
|
+
|
|
856
|
+
this.setCurrentVoice = function(staffNum, voiceNum) {
|
|
857
|
+
tune.staffNum = staffNum;
|
|
858
|
+
tune.voiceNum = voiceNum;
|
|
859
|
+
for (var i = 0; i < tune.lines.length; i++) {
|
|
860
|
+
if (tune.lines[i].staff) {
|
|
861
|
+
if (tune.lines[i].staff[staffNum] === undefined || tune.lines[i].staff[staffNum].voices[voiceNum] === undefined ||
|
|
862
|
+
!this.containsNotes(tune.lines[i].staff[staffNum].voices[voiceNum] )) {
|
|
863
|
+
tune.lineNum = i;
|
|
864
|
+
return;
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
tune.lineNum = i;
|
|
869
|
+
};
|
|
870
|
+
|
|
871
|
+
this.addMetaText = function(key, value, info) {
|
|
872
|
+
if (tune.metaText[key] === undefined) {
|
|
873
|
+
tune.metaText[key] = value;
|
|
874
|
+
tune.metaTextInfo[key] = info;
|
|
875
|
+
} else {
|
|
876
|
+
tune.metaText[key] += "\n" + value;
|
|
877
|
+
tune.metaTextInfo[key].endChar = info.endChar;
|
|
878
|
+
}
|
|
879
|
+
};
|
|
880
|
+
|
|
881
|
+
this.addMetaTextArray = function(key, value, info) {
|
|
882
|
+
if (tune.metaText[key] === undefined) {
|
|
883
|
+
tune.metaText[key] = [value];
|
|
884
|
+
tune.metaTextInfo[key] = info;
|
|
885
|
+
} else {
|
|
886
|
+
tune.metaText[key].push(value);
|
|
887
|
+
tune.metaTextInfo[key].endChar = info.endChar;
|
|
888
|
+
}
|
|
889
|
+
};
|
|
890
|
+
this.addMetaTextObj = function(key, value, info) {
|
|
891
|
+
tune.metaText[key] = value;
|
|
892
|
+
tune.metaTextInfo[key] = info;
|
|
893
|
+
};
|
|
894
|
+
};
|
|
895
|
+
|
|
896
|
+
module.exports = TuneBuilder;
|