abcjs 6.4.0 → 6.4.2

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