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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

const MidiSequence = require("./MidiSequence.js");



const PedalControllerTypes = {
	64: "Sustain",
	65: "Portamento",
	66: "Sostenuto",
	67: "Soft",
};



class Notation {
	static parseMidi (data, {fixOverlap = true} = {}) {
		const channelStatus = [];
		const pedalStatus = {};
		const pedals = {};
		const channels = [];
		const bars = [];
		let time = 0;
		let millisecondsPerBeat = 600000 / 120;
		let beats = 0;
		let numerator = 4;
		let barIndex = 0;
		const keyRange = {};
		let rawTicks = 0;
		let ticks = 0;
		let correspondences;
		const tempos = [];

		const ticksPerBeat = data.header.ticksPerBeat;

		let rawEvents = MidiSequence.midiToSequence(data);

		if (fixOverlap)
			rawEvents = MidiSequence.trimSequence(MidiSequence.fixOverlapNotes(rawEvents));

		const events = rawEvents.map(d => ({
			data: d[0].event,
			track: d[0].track,
			deltaTime: d[1],
			deltaTicks: d[0].ticksToEvent,
		}));

		let index = 0;

		const ticksNormal = 1;

		for (const ev of events) {
			rawTicks += ev.deltaTicks;
			ticks = Math.round(rawTicks * ticksNormal);

			if (ev.deltaTicks > 0) {
				// append bars
				const deltaBeats = ev.deltaTicks / ticksPerBeat;
				for (let b = Math.ceil(beats); b < beats + deltaBeats; ++b) {
					const t = time + (b - beats) * millisecondsPerBeat;
					bars.push({time: t, index: barIndex % numerator});

					++barIndex;
				}

				beats += deltaBeats;
			}

			time += ev.deltaTime;

			//const ticksTime = beats * millisecondsPerBeat;
			//console.log("time:", time, ticksTime, ticksTime - time);

			ev.time = time;
			ev.ticks = ticks;

			const event = ev.data;
			switch (event.type) {
			case "channel":
				//channelStatus[event.channel] = channelStatus[event.channel] || [];

				switch (event.subtype) {
				case "noteOn":
					{
						const pitch = event.noteNumber;
						//channelStatus[event.channel][pitch] = {
						channelStatus.push({
							channel: event.channel,
							pitch,
							startTick: ticks,
							start: time,
							velocity: event.velocity,
							beats: beats,
							track: ev.track,
						});

						keyRange.low = Math.min(keyRange.low || pitch, pitch);

						ev.index = index;
						++index;
					}

					break;
				case "noteOff":
					{
						const pitch = event.noteNumber;

						channels[event.channel] = channels[event.channel] || [];

						const statusIndex = channelStatus.findIndex(status => status.channel == event.channel && status.pitch == pitch);
						if (statusIndex >= 0) {
							const status = channelStatus.splice(statusIndex, 1)[0];

							channels[event.channel].push({
								channel: event.channel,
								startTick: status.startTick,
								endTick: ticks,
								pitch,
								start: status.start,
								duration: time - status.start,
								velocity: status.velocity,
								beats: status.beats,
								track: status.track,
								finger: status.finger,
							});
						}
						else
							console.debug("unexpected noteOff: ", time, event);

						keyRange.high = Math.max(keyRange.high || pitch, pitch);
					}

					break;
				case "controller":
					switch (event.controllerType) {
					// pedal controllers
					case 64:
					case 65:
					case 66:
					case 67:
						const pedalType = PedalControllerTypes[event.controllerType];

						pedalStatus[event.channel] = pedalStatus[event.channel] || {};
						pedals[event.channel] = pedals[event.channel] || [];

						const status = pedalStatus[event.channel][pedalType];

						if (status)
							pedals[event.channel].push({type: pedalType, start: status.start, duration: time - status.start, value: status.value});
						pedalStatus[event.channel][pedalType] = {start: time, value: event.value};

						break;
					}

					break;
				}

				break;
			case "meta":
				switch (event.subtype) {
				case "setTempo":
					millisecondsPerBeat = event.microsecondsPerBeat / 1000;
					//beats = Math.round(beats);
					//console.assert(Number.isFinite(time), "invalid time:", time);
					tempos.push({tempo: event.microsecondsPerBeat, tick: ticks, time});

					break;
				case "timeSignature":
					numerator = event.numerator;
					barIndex = 0;

					break;
				case "text":
					if (!correspondences && /^find-corres:/.test(event.text)) {
						const captures = event.text.match(/:([\d\,-]+)/);
						const str = captures && captures[1] || "";
						correspondences = str.split(",").map(s => Number(s));
					}
					else if (/fingering\(.*\)/.test(event.text)) {
						const [_, fingers] = event.text.match(/\((.+)\)/);
						const finger = Number(fingers);
						if (!Number.isNaN(finger)) {
							const status = channelStatus[channelStatus.length - 1];
							if (status)
								status.finger = finger;

							const event = events.find(e => e.index == index - 1);
							if (event)
								event.data.finger = finger;
						}
					}

					break;
				case "copyrightNotice":
					console.log("MIDI copyright:", event.text);

					break;
				}

				break;
			}
		}

		channelStatus.forEach(status => {
			console.debug("unclosed noteOn event at", status.startTick, status);

			channels[status.channel].push({
				startTick: status.startTick,
				endTick: ticks,
				pitch: status.pitch,
				start: status.start,
				duration: time - status.start,
				velocity: status.velocity,
				beats: status.beats,
				track: status.track,
				finger: status.finger,
			});
		});

		return new Notation({
			channels,
			keyRange,
			pedals,
			bars,
			endTime: time,
			endTick: ticks,
			correspondences,
			events,
			tempos,
			ticksPerBeat,
			meta: {},
		});
	}


	constructor (fields) {
		Object.assign(this, fields);

		// channels to notes
		this.notes = [];
		for (const channel of this.channels) {
			if (channel) {
				for (const note of channel)
					this.notes.push(note);
			}
		}
		this.notes.sort(function (n1, n2) {
			return n1.start - n2.start;
		});

		for (const i in this.notes)
			this.notes[i].index = Number(i);


		// duration
		this.duration = this.notes.length > 0 ? (this.endTime - this.notes[0].start) : 0,

		//this.endSoftIndex = this.notes.length ? this.notes[this.notes.length - 1].softIndex : 0;


		// pitch map
		this.pitchMap = [];
		for (const c in this.channels) {
			for (const n in this.channels[c]) {
				const pitch = this.channels[c][n].pitch;
				this.pitchMap[pitch] = this.pitchMap[pitch] || [];

				this.pitchMap[pitch].push(this.channels[c][n]);
			}
		}

		this.pitchMap.forEach(notes => notes.sort((n1, n2) => n1.start - n2.start));


		/*// setup measure notes index
		if (this.measures) {
			const measure_list = [];

			let last_measure = null;
			const measure_entries = Object.entries(this.measures).sort((e1, e2) => Number(e1[0]) - Number(e2[0]));
			for (const [t, measure] of measure_entries) {
				//console.log("measure time:", Number(t));
				measure.startTick = Number(t);
				measure.notes = [];

				if (last_measure)
					last_measure.endTick = measure.startTick;

				const m = measure.measure;
				measure_list[m] = measure_list[m] || [];
				measure_list[m].push(measure);

				last_measure = measure;
			}
			if (last_measure)
				last_measure.endTick = this.notes[this.notes.length - 1].endTick;
			for (const i in this.notes) {
				const note = this.notes[i];
				for (const t in this.measures) {
					const measure = this.measures[t];
					if (note.startTick >= measure.startTick && note.startTick < measure.endTick || note.endTick > measure.startTick && note.endTick <= measure.endTick)
						measure.notes.push(note);
				}
			}

			this.measure_list = measure_list;
		}*/


		// prepare beats info
		if (this.meta.beatInfos) {
			for (let i = 0; i < this.meta.beatInfos.length; ++i) {
				const info = this.meta.beatInfos[i];
				if (i > 0) {
					const lastInfo = this.meta.beatInfos[i - 1];
					info.beatIndex = lastInfo.beatIndex + Math.ceil((info.tick - lastInfo.tick) / this.ticksPerBeat);
				}
				else
					info.beatIndex = 0;
			}
		}


		// compute tempos tick -> time
		{
			let time = 0;
			let ticks = 0;
			let tempo = 500000;
			for (const entry of this.tempos) {
				const deltaTicks = entry.tick - ticks;
				time += (tempo / 1000) * deltaTicks / this.ticksPerBeat;

				ticks = entry.tick;
				tempo = entry.tempo;

				entry.time = time;
			}
		}
	}


	findChordBySoftindex (softIndex, radius = 0.8) {
		return this.notes.filter(note => Math.abs(note.softIndex - softIndex) < radius);
	}


	averageTempo (tickRange) {
		tickRange = tickRange || {from: 0, to: this.endtick};

		console.assert(this.tempos, "no tempos.");
		console.assert(tickRange.to > tickRange.from, "range is invalid:", tickRange);

		const span = index => {
			const from = Math.max(tickRange.from, this.tempos[index].tick);
			const to = (index < this.tempos.length - 1) ? Math.min(this.tempos[index + 1].tick, tickRange.to) : tickRange.to;

			return Math.max(0, to - from);
		};

		const tempo_sum = this.tempos.reduce((sum, tempo, index) => sum + tempo.tempo * span(index), 0);

		const average = tempo_sum / (tickRange.to - tickRange.from);

		// convert microseconds per beat to beats per minute
		return 60e+6 / average;
	}


	ticksToTime (tick) {
		console.assert(Number.isFinite(tick), "invalid tick value:", tick);
		console.assert(this.tempos && this.tempos.length, "no tempos.");

		const next_tempo_index = this.tempos.findIndex(tempo => tempo.tick > tick);
		const tempo_index = next_tempo_index < 0 ? this.tempos.length - 1 : Math.max(next_tempo_index - 1, 0);

		const tempo = this.tempos[tempo_index];

		return tempo.time + (tick - tempo.tick) * tempo.tempo * 1e-3 / this.ticksPerBeat;
	}


	timeToTicks (time) {
		console.assert(Number.isFinite(time), "invalid time value:", time);
		console.assert(this.tempos && this.tempos.length, "no tempos.");

		const next_tempo_index = this.tempos.findIndex(tempo => tempo.time > time);
		const tempo_index = next_tempo_index < 0 ? this.tempos.length - 1 : Math.max(next_tempo_index - 1, 0);

		const tempo = this.tempos[tempo_index];

		return tempo.tick + (time - tempo.time) * this.ticksPerBeat / (tempo.tempo * 1e-3);
	}


	tickRangeToTimeRange (tickRange) {
		console.assert(tickRange.to >= tickRange.from, "invalid tick range:", tickRange);

		return {
			from: this.ticksToTime(tickRange.from),
			to: this.ticksToTime(tickRange.to),
		};
	}


	/*getMeasureRange (measureRange) {
		console.assert(Number.isInteger(measureRange.start) && Number.isInteger(measureRange.end), "invalid measure range:", measureRange);
		console.assert(this.measure_list && this.measure_list[measureRange.start] && this.measure_list[measureRange.end], "no measure data for specific index:", this.measure_list, measureRange);

		const startMeasure = this.measure_list[measureRange.start][0];
		let endMeasure = null;
		for (const measure of this.measure_list[measureRange.end]) {
			if (measure.endTick > startMeasure.startTick) {
				endMeasure = measure;
				break;
			}
		}

		// there no path between start measure and end measure.
		if (!endMeasure)
			return null;

		const tickRange = {from: startMeasure.startTick, to: endMeasure.endTick, duration: endMeasure.endTick - startMeasure.startTick};
		const timeRange = this.tickRangeToTimeRange(tickRange);
		timeRange.duration = timeRange.to - timeRange.from;

		return {
			tickRange,
			timeRange,
		};
	}*/


	scaleTempo ({factor, headTempo}) {
		console.assert(this.tempos && this.tempos.length, "[Notation.scaleTempo] tempos is empty.");

		if (headTempo)
			factor = headTempo / this.tempos[0].tempo;

		console.assert(Number.isFinite(factor) && factor > 0, "[Notation.scaleTempo] invalid factor:", factor);

		this.tempos.forEach(tempo => {
			tempo.tempo *= factor;
			tempo.time *= factor;
		});
		this.events.forEach(event => {
			event.deltaTime *= factor;
			event.time *= factor;
		});
		this.notes.forEach(note => {
			note.start *= factor;
			note.duration *= factor;
		});

		this.endTime *= factor;
	}
};



module.exports = {
	Notation,
};