File size: 3,062 Bytes
4cadbaf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

const { Notation } = require("./MusicNotation.js");



//const msDelay = ms => new Promise(resolve => setTimeout(resolve, ms));
const animationDelay = () => new Promise(resolve => requestAnimationFrame(resolve));


class MidiPlayer {
	constructor (midiData, {cacheSpan = 600, onMidi, onPlayFinish, onTurnCursor} = {}) {
		this.cacheSpan = cacheSpan;
		this.onMidi = onMidi;
		this.onPlayFinish = onPlayFinish;
		this.onTurnCursor = onTurnCursor;

		let notation;
		if (midiData.notes && Number.isFinite(midiData.endTime))
			notation = midiData;
		else
			notation = Notation.parseMidi(midiData);

		this.notation = notation;
		this.events = notation.events;
		//console.log("events:", this.events);

		this.isPlaying = false;
		this.progressTime = 0;
		this.startTime = performance.now();
		this.duration = notation.endTime;
		this.cursorTurnDelta = 0;

		console.assert(notation.tempos && notation.tempos.length, "[MidiPlayer] invalid notation, tempos is empty.");
	}


	dispose () {
		this.isPlaying = false;
		this.progressTime = 0;
	}


	get progressTicks () {
		return this.notation.timeToTicks(this.progressTime);
	}


	set progressTicks (value) {
		this.progressTime = this.notation.ticksToTime(value);

		if (this.onTurnCursor)
			this.onTurnCursor(this.progressTime);
	}


	async play ({nextFrame = animationDelay} = {}) {
		if (this.progressTime >= this.duration)
			this.progressTime = 0;

		let now = performance.now();
		this.startTime = now - this.progressTime;

		this.isPlaying = true;

		let currentEventIndex = this.events.findIndex(event => event.time >= now - this.startTime);

		while (this.isPlaying) {
			for (; currentEventIndex < this.events.length; ++currentEventIndex) {
				const event = this.events[currentEventIndex];
				//console.log("play event:", currentEventIndex, event.time, this.progressTime + this.cacheSpan);
				if (!event || event.time > this.progressTime + this.cacheSpan)
					break;

				if (event.data.type === "channel" && this.startTime + event.time >= now)
					if (this.onMidi)
						this.onMidi(event.data, this.startTime + event.time);
			}

			await nextFrame();

			if (!this.isPlaying)
				break;

			if (this.cursorTurnDelta !== 0) {
				const backturn = this.cursorTurnDelta < 0;

				this.startTime -= this.cursorTurnDelta;
				this.cursorTurnDelta = 0;

				if (backturn) {
					for (; currentEventIndex > 0; --currentEventIndex) {
						const eventTime = this.events[currentEventIndex].time;
						if (this.startTime + eventTime < now)
							break;
					}
				}
			}

			now = performance.now();

			this.progressTime = now - this.startTime;

			if (this.progressTime > this.duration) {
				this.isPlaying = false;

				if (this.onPlayFinish)
					this.onPlayFinish();
			}
		}
	}


	pause () {
		this.isPlaying = false;
	}


	turnCursor (time) {
		//console.log("onTurnCursor:", time, oldTime);
		if (this.isPlaying)
			this.cursorTurnDelta += time - this.progressTime;
		else
			this.progressTime = time;

		if (this.onTurnCursor)
			this.onTurnCursor(time);
	}
};



module.exports = MidiPlayer;