File size: 26,516 Bytes
af6912c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
//    abc_midi_sequencer.js: Turn parsed abc into a linear series of events.

var sequence;
var parseCommon = require("../parse/abc_common");

(function() {
	"use strict";

	var measureLength = 1; // This should be set by the meter, but just in case that is missing, we'll take a guess.
	// The abc is provided to us line by line. It might have repeats in it. We want to re arrange the elements to
	// be an array of voices with all the repeats embedded, and no lines. Then it is trivial to go through the events
	// one at a time and turn it into midi.

	var PERCUSSION_PROGRAM = 128;

	sequence = function(abctune, options) {
		// Global options
		options = options || {};
		var qpm;
		var program = options.program || 0;	// The program if there isn't a program specified.
		var transpose = options.midiTranspose || 0;
		// If the tune has a visual transpose then that needs to be subtracted out because we are getting the visual object.
		if (abctune.visualTranspose)
			transpose -= abctune.visualTranspose;
		var channel = options.channel || 0;
		var channelExplicitlySet = false;
		var drumPattern = options.drum || "";
		var drumBars = options.drumBars || 1;
		var drumIntro = options.drumIntro || 0;
		var drumOn = drumPattern !== "";
		var drumOffAfterIntro = !!options.drumOff
		var style = []; // The note head style for each voice.
		var rhythmHeadThisBar = false; // Rhythm notation was detected.
		var crescendoSize = 50; // how much to increase or decrease volume when crescendo/diminuendo is encountered.

		// All of the above overrides need to be integers
		program = parseInt(program, 10);
		transpose = parseInt(transpose, 10);
		channel = parseInt(channel, 10);
		if (channel === 10)
			program = PERCUSSION_PROGRAM;
		drumPattern = drumPattern.split(" ");
		drumBars = parseInt(drumBars, 10);
		drumIntro = parseInt(drumIntro, 10);

		var bagpipes = abctune.formatting.bagpipes; // If it is bagpipes, then the gracenotes are played on top of the main note.
		if (bagpipes)
			program = 71;

		// %%MIDI fermatafixed
		// %%MIDI fermataproportional
		// %%MIDI deltaloudness n
		// %%MIDI gracedivider b
		// %%MIDI ratio n m
		// %%MIDI beat a b c n
		// %%MIDI grace a/b
		// %%MIDI trim x/y

		// %MIDI gchordon
		// %MIDI gchordoff
		// %%MIDI bassprog 45
		// %%MIDI chordprog 24
		// %%MIDI chordname name n1 n2 n3 n4 n5 n6

		//%%MIDI beat ⟨int1⟩ ⟨int2⟩ ⟨int3⟩ ⟨int4⟩: controls the volumes of the notes in a measure. The first note in a bar has volume ⟨int1⟩; other ‘strong’ notes have volume ⟨int2⟩ and all the rest have volume ⟨int3⟩. These values must be in the range 0–127. The parameter ⟨int4⟩ determines which notes are ‘strong’. If the time signature is x/y, then each note is given a position number k = 0, 1, 2. . . x-1 within each bar. If k is a multiple of ⟨int4⟩, then the note is ‘strong’.

		var startingMidi = [];
		if (abctune.formatting.midi) {
			//console.log("MIDI Formatting:", abctune.formatting.midi);
			var globals = abctune.formatting.midi;
			if (globals.program && globals.program.length > 0) {
				program = globals.program[0];
				if (globals.program.length > 1) {
					program = globals.program[1];
					channel = globals.program[0];
				}
				channelExplicitlySet = true;
			}
			if (globals.transpose)
				transpose = globals.transpose[0];
			if (globals.channel) {
				channel = globals.channel[0];
				channelExplicitlySet = true;
			}
			if (globals.drum)
				drumPattern = globals.drum;
			if (globals.drumbars)
				drumBars = globals.drumbars[0];
			if (globals.drumon)
				drumOn = true;
			if (channel === 10)
				program = PERCUSSION_PROGRAM;
			if (globals.beat)
				startingMidi.push({ el_type: 'beat', beats: globals.beat })
			if (globals.nobeataccents)
				startingMidi.push({ el_type: 'beataccents', value: false });

		}

		// Specified options in abc string.

		// If the tempo was passed in, use that.
		// If the tempo is specified, use that.
		// If there is a default, use that.
		// Otherwise, use the default.
		if (options.qpm)
			qpm = parseInt(options.qpm, 10);
		else if (abctune.metaText.tempo)
			qpm = interpretTempo(abctune.metaText.tempo, abctune.getBeatLength());
		else if (options.defaultQpm)
			qpm = options.defaultQpm;
		else
			qpm = 180; 	// The tempo if there isn't a tempo specified.

		var startVoice = [];
		if (bagpipes)
			startVoice.push({ el_type: 'bagpipes' });
		startVoice.push({ el_type: 'instrument', program: program });
		if (channel)
			startVoice.push({ el_type: 'channel', channel: channel });
		if (transpose)
			startVoice.push({ el_type: 'transpose', transpose: transpose });
		startVoice.push({ el_type: 'tempo', qpm: qpm });
		for (var ss = 0; ss < startingMidi.length;ss++)
			startVoice.push(startingMidi[ss]);

		// the relevant part of the input structure is:
		// abctune
		//		array lines
		//			array staff
		//				object key
		//				object meter
		//				array voices
		//					array abcelem

		// visit each voice completely in turn
		var voices = [];
		var inCrescendo = [];
		var inDiminuendo = [];
		var durationCounter = [0];
		var tempoChanges = {};
		tempoChanges["0"] = { el_type: 'tempo', qpm: qpm, timing: 0 };
		var currentVolume;
		var startRepeatPlaceholder = []; // There is a place holder for each voice.
		var skipEndingPlaceholder = []; // This is the place where the first ending starts.
		var startingDrumSet = false;
		var lines = abctune.lines; //abctune.deline(); TODO-PER: can switch to this, then simplify the loops below.
		for (var i = 0; i < lines.length; i++) {
			// For each group of staff lines in the tune.
			var line = lines[i];
			if (line.staff) {
				var staves = line.staff;
				var voiceNumber = 0;
				for (var j = 0; j < staves.length; j++) {
					var staff = staves[j];
					if (staff.clef && staff.clef.type === "TAB")
						continue;

					// For each staff line
					for (var k = 0; k < staff.voices.length; k++) {
						// For each voice in a staff line
						var voice = staff.voices[k];
						if (!voices[voiceNumber]) {
							voices[voiceNumber] = [].concat(JSON.parse(JSON.stringify(startVoice)));
							var voiceName = getTrackTitle(line.staff, voiceNumber);
							if (voiceName)
								voices[voiceNumber].unshift({el_type: "name", trackName: voiceName});
						}
						// Negate any transposition for the percussion staff.
						if (transpose && staff.clef.type === "perc")
							voices[voiceNumber].push({ el_type: 'transpose', transpose: 0 });

						if (staff.clef && staff.clef.type === 'perc' && !channelExplicitlySet) {
							for (var cl = 0; cl < voices[voiceNumber].length; cl++) {
								if (voices[voiceNumber][cl].el_type === 'instrument')
									voices[voiceNumber][cl].program = PERCUSSION_PROGRAM;
							}
						} else if (staff.key) {
							addKey(voices[voiceNumber], staff.key);
						}
						if (staff.meter) {
							addMeter(voices[voiceNumber], staff.meter);
						}
						if (!startingDrumSet && drumOn) { // drum information is only needed once, so use the first line and track 0.
							voices[voiceNumber].push({el_type: 'drum', params: {pattern: drumPattern, bars: drumBars, on: drumOn, intro: drumIntro}});
							startingDrumSet = true;
						}
						if (staff.clef && staff.clef.type !== "perc" && staff.clef.transpose) {
							staff.clef.el_type = 'clef';
							voices[voiceNumber].push({ el_type: 'transpose', transpose: staff.clef.transpose });
						}
						if (staff.clef && staff.clef.type) {
							if (staff.clef.type.indexOf("-8") >= 0)
								voices[voiceNumber].push({ el_type: 'transpose', transpose: -12 });
							else if (staff.clef.type.indexOf("+8") >= 0)
								voices[voiceNumber].push({ el_type: 'transpose', transpose: 12 });
						}

						if (abctune.formatting.midi && abctune.formatting.midi.drumoff) {
							// If there is a drum off command right at the beginning it is put in the metaText instead of the stream,
							// so we will just insert it here.
							voices[voiceNumber].push({ el_type: 'bar' });
							voices[voiceNumber].push({el_type: 'drum', params: {pattern: "", on: false }});
						}
						var noteEventsInBar = 0;
						var tripletMultiplier = 0;
						var tripletDurationTotal = 0; // try to mitigate the js rounding problems.
						var tripletDurationCount = 0;
						currentVolume = [105, 95, 85, 1];

						for (var v = 0; v < voice.length; v++) {
							// For each element in a voice
							var elem = voice[v];
							switch (elem.el_type) {
								case "note":
									if (inCrescendo[k]) {
										currentVolume[0] += inCrescendo[k];
										currentVolume[1] += inCrescendo[k];
										currentVolume[2] += inCrescendo[k];
										voices[voiceNumber].push({ el_type: 'beat', beats: currentVolume.slice(0) });
									}

									if (inDiminuendo[k]) {
										currentVolume[0] += inDiminuendo[k];
										currentVolume[1] += inDiminuendo[k];
										currentVolume[2] += inDiminuendo[k];
										voices[voiceNumber].push({ el_type: 'beat', beats: currentVolume.slice(0) });
									}
									setDynamics(elem);

									// regular items are just pushed.
									if (!elem.rest || elem.rest.type !== 'spacer') {
										var noteElem = { elem: elem, el_type: "note", timing: durationCounter[voiceNumber] }; // Make a copy so that modifications aren't kept except for adding the midiPitches
										if (elem.style)
											noteElem.style = elem.style;
										else if (style[voiceNumber])
											noteElem.style = style[voiceNumber];
										noteElem.duration = (elem.duration === 0) ? 0.25 : elem.duration;
										if (elem.startTriplet) {
											tripletMultiplier = elem.tripletMultiplier;
											tripletDurationTotal = elem.startTriplet * tripletMultiplier * elem.duration;
											if (elem.startTriplet !== elem.tripletR) { // most commonly (3:2:2
												if (v + elem.tripletR <= voice.length) {
													var durationTotal = 0;
													for (var w = v; w < v + elem.tripletR; w++) {
														durationTotal += voice[w].duration;
													}
													tripletDurationTotal = tripletMultiplier * durationTotal;
												}
											}
											noteElem.duration = noteElem.duration * tripletMultiplier;
											noteElem.duration = Math.round(noteElem.duration*1000000)/1000000;
											tripletDurationCount = noteElem.duration;
										} else if (tripletMultiplier) {
											if (elem.endTriplet) {
												tripletMultiplier = 0;
												noteElem.duration = Math.round((tripletDurationTotal - tripletDurationCount)*1000000)/1000000;
											} else {
												noteElem.duration = noteElem.duration * tripletMultiplier;
												noteElem.duration = Math.round(noteElem.duration*1000000)/1000000;
												tripletDurationCount += noteElem.duration;
											}
										}
										if (elem.rest) noteElem.rest = elem.rest;
										if (elem.decoration) noteElem.decoration = elem.decoration.slice(0);
										if (elem.pitches) noteElem.pitches = parseCommon.cloneArray(elem.pitches);
										if (elem.gracenotes) noteElem.gracenotes = parseCommon.cloneArray(elem.gracenotes);
										if (elem.chord) noteElem.chord = parseCommon.cloneArray(elem.chord);

										voices[voiceNumber].push(noteElem);
										if (elem.style === "rhythm") {
											rhythmHeadThisBar = true;
											chordVoiceOffThisBar(voices)
										}
										noteEventsInBar++;
										durationCounter[voiceNumber] += noteElem.duration;
									}
									break;
								case "key":
								case "keySignature":
									addKey(voices[voiceNumber], elem);
									break;
								case "meter":
									addMeter(voices[voiceNumber], elem);
									break;
								case "clef": // need to keep this to catch the "transpose" element.
									if (elem.transpose)
										voices[voiceNumber].push({ el_type: 'transpose', transpose: elem.transpose });
									if (elem.type) {
										if (elem.type.indexOf("-8") >= 0)
											voices[voiceNumber].push({ el_type: 'transpose', transpose: -12 });
										else if (elem.type.indexOf("+8") >= 0)
											voices[voiceNumber].push({ el_type: 'transpose', transpose: 12 });
									}
									break;
								case "tempo":
									qpm = interpretTempo(elem, abctune.getBeatLength());
									voices[voiceNumber].push({ el_type: 'tempo', qpm: qpm, timing: durationCounter[voiceNumber] });
									tempoChanges[''+durationCounter[voiceNumber]] = { el_type: 'tempo', qpm: qpm, timing: durationCounter[voiceNumber] };
									break;
								case "bar":
									if (noteEventsInBar > 0) // don't add two bars in a row.
										voices[voiceNumber].push({ el_type: 'bar' }); // We need the bar marking to reset the accidentals.
									setDynamics(elem);
									noteEventsInBar = 0;
									// figure out repeats and endings --
									// The important part is where there is a start repeat, and end repeat, or a first ending.
									var endRepeat = (elem.type === "bar_right_repeat" || elem.type === "bar_dbl_repeat");
									var startEnding = (elem.startEnding === '1');
									var startRepeat = (elem.type === "bar_left_repeat" || elem.type === "bar_dbl_repeat" || elem.type === "bar_right_repeat");
									if (endRepeat) {
										var s = startRepeatPlaceholder[voiceNumber];
										if (!s) s = 0; // If there wasn't a left repeat, then we repeat from the beginning.
										var e = skipEndingPlaceholder[voiceNumber];
										if (!e) e = voices[voiceNumber].length; // If there wasn't a first ending marker, then we copy everything.
										// duplicate each of the elements - this has to be a deep copy.
										for (var z = s; z < e; z++) {
											var item = Object.assign({},voices[voiceNumber][z]);
											if (item.pitches)
												item.pitches = parseCommon.cloneArray(item.pitches);
											voices[voiceNumber].push(item);
										}
										// reset these in case there is a second repeat later on.
										skipEndingPlaceholder[voiceNumber] = undefined;
										startRepeatPlaceholder[voiceNumber] = undefined;
									}
									if (startEnding)
										skipEndingPlaceholder[voiceNumber] = voices[voiceNumber].length;
									if (startRepeat)
										startRepeatPlaceholder[voiceNumber] = voices[voiceNumber].length;
									rhythmHeadThisBar = false;
									break;
								case 'style':
									style[voiceNumber] = elem.head;
									break;
								case 'timeSignature':
									voices[voiceNumber].push(interpretMeter(elem));
									break;
								case 'part':
									// TODO-PER: If there is a part section in the header, then this should probably affect the repeats.
									break;
								case 'stem':
								case 'scale':
								case 'break':
								case 'font':
									// These elements don't affect sound
									break;
								case 'midi':
									//console.log("MIDI inline", elem); // TODO-PER: for debugging. Remove this.
									var drumChange = false;
									switch (elem.cmd) {
										case "drumon": drumOn = true; drumChange = true; break;
										case "drumoff": drumOn = false; drumChange = true; break;
										case "drum": drumPattern = elem.params; drumChange = true; break;
										case "drumbars": drumBars = elem.params[0]; drumChange = true; break;
										case "drummap":
											// This is handled before getting here so it can be ignored.
											break;
										case "channel":
											// There's not much needed for the channel except to look out for the percussion channel
											if (elem.params[0] === 10)
												voices[voiceNumber].push({ el_type: 'instrument', program: PERCUSSION_PROGRAM });
											break;
										case "program":
											addIfDifferent(voices[voiceNumber], { el_type: 'instrument', program: elem.params[0] });
											channelExplicitlySet = true;
											break;
										case "transpose":
											voices[voiceNumber].push({ el_type: 'transpose', transpose: elem.params[0] });
											break;
										case "gchordoff":
											voices[voiceNumber].push({ el_type: 'gchordOn', tacet: true });
											break;
										case "gchordon":
											voices[voiceNumber].push({ el_type: 'gchordOn', tacet: false });
											break;
										case "beat":
											voices[voiceNumber].push({ el_type: 'beat', beats: elem.params });
											break;
										case "nobeataccents":
											voices[voiceNumber].push({ el_type: 'beataccents', value: false });
											break;
										case "beataccents":
											voices[voiceNumber].push({ el_type: 'beataccents', value: true });
											break;
										case "vol":
										case "volinc":
											voices[voiceNumber].push({ el_type: elem.cmd, volume: elem.params[0] });
											break;
										case "swing":
										case "gchord":
										case "bassvol":
										case "chordvol":
											voices[voiceNumber].push({ el_type: elem.cmd, param: elem.params[0] });
											break;

										case "bassprog": // MAE 22 May 2024
										case "chordprog": // MAE 22 May 2024
					                      voices[voiceNumber].push({
					                        el_type: elem.cmd,
					                        value: elem.params[0],
					                        octaveShift: elem.params[1]
					                      });
					                      break;

					                    // MAE 23 Jun 2024
					                    case "gchordbars":
					                        voices[voiceNumber].push({
					                          el_type: elem.cmd,
					                          param: elem.params[0]
					                        });
					                    break;
										default:
											console.log("MIDI seq: midi cmd not handled: ", elem.cmd, elem);
									}
									if (drumChange) {
										voices[0].push({el_type: 'drum', params: { pattern: drumPattern, bars: drumBars, intro: drumIntro, on: drumOn}});
										startingDrumSet = true;
									}
									break;
								default:
									console.log("MIDI: element type " + elem.el_type + " not handled.");
							}
						}
						voiceNumber++;
						if (!durationCounter[voiceNumber])
							durationCounter[voiceNumber] = 0;
					}
				}

				function setDynamics(elem) {
					var volumes = {
						'pppp': [15, 10, 5, 1],
						'ppp': [30, 20, 10, 1],
						'pp': [45, 35, 20, 1],
						'p': [60, 50, 35, 1],
						'mp': [75, 65, 50, 1],
						'mf': [90, 80, 65, 1],
						'f': [105, 95, 80, 1],
						'ff': [120, 110, 95, 1],
						'fff': [127, 125, 110, 1],
						'ffff': [127, 125, 110, 1]
					};

					var dynamicType;
					if (elem.decoration) {
						if (elem.decoration.indexOf('pppp') >= 0)
							dynamicType = 'pppp';
						else if (elem.decoration.indexOf('ppp') >= 0)
							dynamicType = 'ppp';
						else if (elem.decoration.indexOf('pp') >= 0)
							dynamicType = 'pp';
						else if (elem.decoration.indexOf('p') >= 0)
							dynamicType = 'p';
						else if (elem.decoration.indexOf('mp') >= 0)
							dynamicType = 'mp';
						else if (elem.decoration.indexOf('mf') >= 0)
							dynamicType = 'mf';
						else if (elem.decoration.indexOf('f') >= 0)
							dynamicType = 'f';
						else if (elem.decoration.indexOf('ff') >= 0)
							dynamicType = 'ff';
						else if (elem.decoration.indexOf('fff') >= 0)
							dynamicType = 'fff';
						else if (elem.decoration.indexOf('ffff') >= 0)
							dynamicType = 'ffff';

						if (dynamicType) {
							currentVolume = volumes[dynamicType].slice(0);
							voices[voiceNumber].push({ el_type: 'beat', beats: currentVolume.slice(0) });
							inCrescendo[k] = false;
							inDiminuendo[k] = false;
						}

						if (elem.decoration.indexOf("crescendo(") >= 0) {
							var n = numNotesToDecoration(voice, v, "crescendo)");
							var top = Math.min(127, currentVolume[0] + crescendoSize);
							var endDec = endingVolume(voice, v+n+1, Object.keys(volumes));
							if (endDec)
								top = volumes[endDec][0];
							if (n > 0)
								inCrescendo[k] = Math.floor((top - currentVolume[0]) / n);
							else
								inCrescendo[k] = false;
							inDiminuendo[k] = false;
						} else if (elem.decoration.indexOf("crescendo)") >= 0) {
							inCrescendo[k] = false;
						} else if (elem.decoration.indexOf("diminuendo(") >= 0) {
							var n2 = numNotesToDecoration(voice, v, "diminuendo)");
							var bottom = Math.max(15, currentVolume[0] - crescendoSize);
							var endDec2 = endingVolume(voice, v+n2+1, Object.keys(volumes));
							if (endDec2)
								bottom = volumes[endDec2][0];
							inCrescendo[k] = false;
							if (n2 > 0)
								inDiminuendo[k] = Math.floor((bottom - currentVolume[0]) / n2);
							else
								inDiminuendo[k] = false;
						} else if (elem.decoration.indexOf("diminuendo)") >= 0) {
							inDiminuendo[k] = false;
						}
					}
				}
			}
		}
		// If there are tempo changes, make sure they are in all the voices. This must be done post process because all the elements in all the voices need to be created first.
		insertTempoChanges(voices, tempoChanges);

		if (drumIntro) {
			var pickups = abctune.getPickupLength();
			// add some measures of rests to the start of each track.
			for (var vv = 0; vv < voices.length; vv++) {
				var insertPoint = 0;
				while (voices[vv][insertPoint].el_type !== "note" && voices[vv].length > insertPoint)
					insertPoint++;
				if (voices[vv].length > insertPoint) {
					for (var w = 0; w < drumIntro; w++) {
						// If it is the last measure of intro, subtract the pickups.
						if (pickups === 0 || w < drumIntro-1) {
							voices[vv].splice(insertPoint, 0, 
								{el_type: "note", rest: {type: "rest"}, duration: measureLength},
								{ el_type: "bar" }
							);
							insertPoint += 2
						} else {
							voices[vv].splice(insertPoint++, 0, {el_type: "note", rest: {type: "rest"}, duration: measureLength-pickups});
						}
					}
					if (drumOffAfterIntro) {
						drumOn = false
						voices[vv].splice(insertPoint++, 0, {el_type: 'drum', params: { pattern: drumPattern, bars: drumBars, intro: drumIntro, on: drumOn}});
						drumOffAfterIntro = false
					}
				}
			}
		}
		if (voices.length > 0 && voices[0].length > 0) {
			voices[0][0].pickupLength = abctune.getPickupLength();
		}
		return voices;
	};

	function numNotesToDecoration(voice, start, decoration) {
		var counter = 0;
		for (var i = start+1; i < voice.length; i++) {
			if (voice[i].el_type === "note")
				counter++;
			if (voice[i].decoration && voice[i].decoration.indexOf(decoration) >= 0)
				return counter;
		}
		return counter;
	}
	function endingVolume(voice, start, volumeDecorations) {
		var end = Math.min(voice.length, start + 3); // If we have a volume within a couple notes of the end then assume that is the destination.
		for (var i = start; i < end; i++) {
			if (voice[i].el_type === "note") {
				if (voice[i].decoration) {
					for (var j = 0; j < voice[i].decoration.length; j++) {
						if (volumeDecorations.indexOf(voice[i].decoration[j]) >= 0)
							return voice[i].decoration[j];
					}
				}
			}
		}
		return null;
	}

	function insertTempoChanges(voices, tempoChanges) {
		if (!tempoChanges || tempoChanges.length === 0)
			return;
		var changePositions = Object.keys(tempoChanges);
		for (var i = 0; i < voices.length; i++) {
			var voice = voices[i];
			var lastTempo = tempoChanges['0'] ? tempoChanges['0'].qpm : 0; // Don't insert redundant changes. This happens normally when repeating from the beginning, but could happen anywhere that there is a tempo marking that is the same as the last one.
			for (var j = 0; j < voice.length; j++) {
				var el = voice[j];
				if (el.el_type === "tempo")
					lastTempo = el.qpm;
				if (changePositions.indexOf(''+el.timing) >= 0 && lastTempo !== tempoChanges[''+el.timing].qpm) {
					lastTempo = tempoChanges[''+el.timing].qpm;
					if (el.el_type === "tempo") {
						el.qpm = tempoChanges[''+el.timing].qpm;
						j++; // when there is a tempo element the next element has the same timing and we don't want it to match the second time.
					} else {
						//console.log("tempo position", i, j, el);
						voices[i].splice(j, 0, {el_type: "tempo", qpm: tempoChanges[''+el.timing].qpm, timing: el.timing});
						j +=2; // skip the element we just inserted.
					}
				}
			}
		}
	}

	function chordVoiceOffThisBar(voices) {
		for (var i = 0; i < voices.length; i++) {
			var voice = voices[i];
			var j = voice.length-1;
			while (j >= 0 && voice[j].el_type !== 'bar') {
				voice[j].noChordVoice = true;
				j--;
			}
		}
	}

	function getTrackTitle(staff, voiceNumber) {
		if (!staff || staff.length <= voiceNumber || !staff[voiceNumber].title)
			return undefined;
		return staff[voiceNumber].title.join(" ");
	}

	function interpretTempo(element, beatLength) {
		var duration = 1/4;
		if (element.duration) {
			duration = element.duration[0];
		}
		var bpm = 60;
		if (element.bpm) {
			bpm = element.bpm;
		}
		// The tempo is defined with a beat length of "duration". If that isn't the natural beat length then there is a translation.
		return duration * bpm / beatLength;
	}

	function interpretMeter(element) {
		var meter;
		switch (element.type) {
			case "common_time":
				meter = { el_type: 'meter', num: 4, den: 4 };
				break;
			case "cut_time":
				meter = { el_type: 'meter', num: 2, den: 2 };
				break;
			case "specified":
				// TODO-PER: only taking the first meter, so the complex meters are not handled.
				meter = { el_type: 'meter', num: element.value[0].num, den: element.value[0].den };
				break;
			default:
				// This should never happen.
				meter = { el_type: 'meter' };
		}
		measureLength = meter.num/meter.den;
		return meter;
	}

	function removeNaturals(accidentals) {
		var acc = [];
		for (var i = 0; i < accidentals.length; i++) {
			if (accidentals[i].acc !== "natural")
				acc.push(accidentals[i])
		}
		return acc;
	}
	function addKey(arr, key) {
		var newKey;
		if (key.root === 'HP')
			newKey = {el_type: 'key', accidentals: [{acc: 'natural', note: 'g'}, {acc: 'sharp', note: 'f'}, {acc: 'sharp', note: 'c'}]};
		else
			newKey = {el_type: 'key', accidentals: removeNaturals(key.accidentals) };
		addIfDifferent(arr, newKey);
	}
	function addMeter(arr, meter) {
		var newMeter = interpretMeter(meter);
		addIfDifferent(arr, newMeter);
	}
	function addIfDifferent(arr, item) {
		for (var i = arr.length-1; i >= 0; i--) {
			if (arr[i].el_type === item.el_type) {
				if (JSON.stringify(arr[i]) !== JSON.stringify(item))
					arr.push(item);
				return;
			}
		}
		arr.push(item);
	}

})();

module.exports = sequence;