@marmooo/midi-player 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -6,6 +6,7 @@
6
6
  ## Demo
7
7
 
8
8
  - [Basic usage](https://marmooo.github.io/midi-player/)
9
+ - [Humidy](https://marmooo.github.io/humidy/) - GM2 MIDI mixer app
9
10
 
10
11
  ## Usage
11
12
 
@@ -12,6 +12,11 @@ export class MIDIPlayer {
12
12
  pauseNode: any;
13
13
  resumeNode: any;
14
14
  stopNode: any;
15
+ isPlaying: boolean;
16
+ isPausing: boolean;
17
+ isPaused: boolean;
18
+ isStopping: boolean;
19
+ isSeeking: boolean;
15
20
  root: any;
16
21
  defaultLayout(): void;
17
22
  row(): any;
@@ -20,6 +25,7 @@ export class MIDIPlayer {
20
25
  formRange(title: any, className: any, value: any, display: any): any;
21
26
  text(text: any, className: any): any;
22
27
  startTimer(): void;
28
+ stopTimer(): void;
23
29
  loadMIDI(file: any): Promise<void>;
24
30
  setSoundFontDir(dir: any): void;
25
31
  getSoundFontPaths(): string[];
@@ -27,7 +33,7 @@ export class MIDIPlayer {
27
33
  handleStop(): Promise<void>;
28
34
  stop(): any;
29
35
  handlePlay(): Promise<void>;
30
- handlePause(): void;
36
+ handlePause(): Promise<void>;
31
37
  handleResume(): Promise<void>;
32
38
  playPauseResume(): any;
33
39
  volumeText(): any;
@@ -1 +1 @@
1
- {"version":3,"file":"midi-player.d.ts","sourceRoot":"","sources":["../src/midi-player.js"],"names":[],"mappings":"AAAA;IAcE,uBAGC;IAhBD,qBAAoE;IACpE,UAAK;IACL,WAAM;IACN,oBAAgB;IAChB,yBAAuB;IACvB,kBAAa;IACb,mBAAc;IACd,iBAAY;IACZ,cAAS;IACT,eAAU;IACV,gBAAW;IACX,cAAS;IAIP,UAAiD;IAGnD,sBAMC;IAED,WAOC;IAED,iCASC;IAED,qEASC;IAED,qEAOC;IAED,qCAKC;IAED,mBAYC;IAED,mCAKC;IAED,gCAEC;IAED,8BAaC;IAED,uBAaC;IAED,4BAQC;IAED,YAKC;IAED,4BAUC;IAED,oBAOC;IAED,8BAYC;IAED,uBA2BC;IAED,kBAEC;IAED,cAwCC;IAED,eAgBC;IAED,gBAIC;IAED,iBAIC;IAED,yBAWC;CACF"}
1
+ {"version":3,"file":"midi-player.d.ts","sourceRoot":"","sources":["../src/midi-player.js"],"names":[],"mappings":"AAAA;IAmBE,uBAGC;IArBD,qBAAoE;IACpE,UAAK;IACL,WAAM;IACN,oBAAgB;IAChB,yBAAuB;IACvB,kBAAa;IACb,mBAAc;IACd,iBAAY;IACZ,cAAS;IACT,eAAU;IACV,gBAAW;IACX,cAAS;IACT,mBAAkB;IAClB,mBAAkB;IAClB,kBAAiB;IACjB,oBAAmB;IACnB,mBAAkB;IAIhB,UAAiD;IAGnD,sBAMC;IAED,WAOC;IAED,iCASC;IAED,qEASC;IAED,qEAOC;IAED,qCAKC;IAED,mBAeC;IAED,kBAKC;IAED,mCAKC;IAED,gCAEC;IAED,8BAaC;IAED,uBAYC;IAED,4BAUC;IAED,YAKC;IAED,4BAYC;IAED,6BAUC;IAED,8BAcC;IAED,uBA2BC;IAED,kBAEC;IAED,cAwCC;IAED,eAkBC;IAED,gBAIC;IAED,iBAIC;IAED,yBAWC;CACF"}
@@ -72,6 +72,36 @@ export class MIDIPlayer {
72
72
  writable: true,
73
73
  value: void 0
74
74
  });
75
+ Object.defineProperty(this, "isPlaying", {
76
+ enumerable: true,
77
+ configurable: true,
78
+ writable: true,
79
+ value: false
80
+ });
81
+ Object.defineProperty(this, "isPausing", {
82
+ enumerable: true,
83
+ configurable: true,
84
+ writable: true,
85
+ value: false
86
+ });
87
+ Object.defineProperty(this, "isPaused", {
88
+ enumerable: true,
89
+ configurable: true,
90
+ writable: true,
91
+ value: false
92
+ });
93
+ Object.defineProperty(this, "isStopping", {
94
+ enumerable: true,
95
+ configurable: true,
96
+ writable: true,
97
+ value: false
98
+ });
99
+ Object.defineProperty(this, "isSeeking", {
100
+ enumerable: true,
101
+ configurable: true,
102
+ writable: true,
103
+ value: false
104
+ });
75
105
  this.midy = midy;
76
106
  this.root = document.createElement("midi-player");
77
107
  }
@@ -126,16 +156,25 @@ export class MIDIPlayer {
126
156
  }
127
157
  startTimer() {
128
158
  const endTime = this.midy.totalTime;
129
- this.timer = setInterval(() => {
159
+ this.stopTimer();
160
+ const update = () => {
130
161
  const now = this.midy.currentTime();
131
- const seconds = Math.ceil(now);
132
162
  if (this.currTimeNode) {
163
+ const seconds = Math.ceil(now);
133
164
  this.currTimeNode.textContent = this.formatTime(seconds);
134
165
  }
135
166
  if (this.seekBarNode) {
136
167
  this.seekBarNode.value = now / endTime;
137
168
  }
138
- }, this.currTimeInterval);
169
+ this.timer = requestAnimationFrame(update);
170
+ };
171
+ this.timer = requestAnimationFrame(update);
172
+ }
173
+ stopTimer() {
174
+ if (this.timer) {
175
+ cancelAnimationFrame(this.timer);
176
+ this.timer = null;
177
+ }
139
178
  }
140
179
  async loadMIDI(file) {
141
180
  await this.midy.loadMIDI(file);
@@ -162,14 +201,13 @@ export class MIDIPlayer {
162
201
  return paths;
163
202
  }
164
203
  async start() {
204
+ this.isPlaying = true;
165
205
  const midy = this.midy;
166
- if (midy.soundFonts.length === 0) {
167
- const paths = this.getSoundFontPaths();
168
- await midy.loadSoundFont(paths);
169
- }
206
+ await midy.loadSoundFont(this.getSoundFontPaths());
170
207
  this.startTimer();
171
208
  await midy.start();
172
- clearInterval(this.timer);
209
+ this.stopTimer();
210
+ this.isPlaying = false;
173
211
  if (!midy.isPaused && this.currTimeNode) {
174
212
  this.currTimeNode.textContent = "0:00";
175
213
  this.seekBarNode.value = 0;
@@ -179,11 +217,13 @@ export class MIDIPlayer {
179
217
  const midy = this.midy;
180
218
  if (!midy.isPlaying)
181
219
  return;
182
- clearInterval(this.timer);
220
+ this.isStopping = true;
183
221
  this.playNode.style.display = "initial";
184
222
  this.pauseNode.style.display = "none";
185
223
  this.resumeNode.style.display = "none";
224
+ this.stopTimer();
186
225
  await midy.stop();
226
+ this.isPlaying = false;
187
227
  }
188
228
  stop() {
189
229
  const stop = this.button("start", "midi-player-start", "stop", "initial");
@@ -195,32 +235,39 @@ export class MIDIPlayer {
195
235
  const { midy, playNode, pauseNode } = this;
196
236
  if (midy.isPlaying || midy.isPaused)
197
237
  return;
238
+ this.isPlaying = true;
198
239
  playNode.style.display = "none";
199
240
  pauseNode.style.display = "initial";
200
241
  await this.start();
242
+ this.isPlaying = false;
201
243
  if (!midy.isPaused) {
202
244
  pauseNode.style.display = "none";
203
245
  playNode.style.display = "initial";
204
246
  }
205
247
  }
206
- handlePause() {
248
+ async handlePause() {
207
249
  const midy = this.midy;
208
250
  if (!midy.isPlaying || midy.isPaused)
209
251
  return;
252
+ this.isPausing = true;
210
253
  this.pauseNode.style.display = "none";
211
254
  this.resumeNode.style.display = "initial";
212
- clearInterval(this.timer);
213
- midy.pause();
255
+ this.stopTimer();
256
+ await midy.pause();
257
+ this.isPausing = false;
258
+ this.isPaused = true;
214
259
  }
215
260
  async handleResume() {
216
261
  const { midy, playNode, pauseNode, resumeNode } = this;
217
262
  if (!midy.isPaused)
218
263
  return;
264
+ this.isPlaying = true;
219
265
  pauseNode.style.display = "initial";
220
266
  resumeNode.style.display = "none";
221
267
  this.startTimer();
222
268
  await midy.resume();
223
- clearInterval(this.timer);
269
+ this.stopTimer();
270
+ this.isPlaying = false;
224
271
  if (!midy.isPaused) {
225
272
  pauseNode.style.display = "none";
226
273
  playNode.style.display = "initial";
@@ -281,11 +328,13 @@ export class MIDIPlayer {
281
328
  seekBar() {
282
329
  const seekBar = this.formRange("playback position", "midi-player-seekBar", 0, "initial");
283
330
  seekBar.oninput = (event) => {
331
+ this.isSeeking = true;
284
332
  const time = event.target.value * this.midy.totalTime;
285
333
  this.midy.seekTo(time);
286
334
  if (this.currTimeNode) {
287
335
  this.currTimeNode.textContent = this.formatTime(time);
288
336
  }
337
+ this.isSeeking = false;
289
338
  };
290
339
  this.seekBarNode = seekBar;
291
340
  return seekBar;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@marmooo/midi-player",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "description": "<midi-player> HTML elements powered by Midy.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -12,6 +12,11 @@ export class MIDIPlayer {
12
12
  pauseNode: any;
13
13
  resumeNode: any;
14
14
  stopNode: any;
15
+ isPlaying: boolean;
16
+ isPausing: boolean;
17
+ isPaused: boolean;
18
+ isStopping: boolean;
19
+ isSeeking: boolean;
15
20
  root: any;
16
21
  defaultLayout(): void;
17
22
  row(): any;
@@ -20,6 +25,7 @@ export class MIDIPlayer {
20
25
  formRange(title: any, className: any, value: any, display: any): any;
21
26
  text(text: any, className: any): any;
22
27
  startTimer(): void;
28
+ stopTimer(): void;
23
29
  loadMIDI(file: any): Promise<void>;
24
30
  setSoundFontDir(dir: any): void;
25
31
  getSoundFontPaths(): string[];
@@ -27,7 +33,7 @@ export class MIDIPlayer {
27
33
  handleStop(): Promise<void>;
28
34
  stop(): any;
29
35
  handlePlay(): Promise<void>;
30
- handlePause(): void;
36
+ handlePause(): Promise<void>;
31
37
  handleResume(): Promise<void>;
32
38
  playPauseResume(): any;
33
39
  volumeText(): any;
@@ -1 +1 @@
1
- {"version":3,"file":"midi-player.d.ts","sourceRoot":"","sources":["../src/midi-player.js"],"names":[],"mappings":"AAAA;IAcE,uBAGC;IAhBD,qBAAoE;IACpE,UAAK;IACL,WAAM;IACN,oBAAgB;IAChB,yBAAuB;IACvB,kBAAa;IACb,mBAAc;IACd,iBAAY;IACZ,cAAS;IACT,eAAU;IACV,gBAAW;IACX,cAAS;IAIP,UAAiD;IAGnD,sBAMC;IAED,WAOC;IAED,iCASC;IAED,qEASC;IAED,qEAOC;IAED,qCAKC;IAED,mBAYC;IAED,mCAKC;IAED,gCAEC;IAED,8BAaC;IAED,uBAaC;IAED,4BAQC;IAED,YAKC;IAED,4BAUC;IAED,oBAOC;IAED,8BAYC;IAED,uBA2BC;IAED,kBAEC;IAED,cAwCC;IAED,eAgBC;IAED,gBAIC;IAED,iBAIC;IAED,yBAWC;CACF"}
1
+ {"version":3,"file":"midi-player.d.ts","sourceRoot":"","sources":["../src/midi-player.js"],"names":[],"mappings":"AAAA;IAmBE,uBAGC;IArBD,qBAAoE;IACpE,UAAK;IACL,WAAM;IACN,oBAAgB;IAChB,yBAAuB;IACvB,kBAAa;IACb,mBAAc;IACd,iBAAY;IACZ,cAAS;IACT,eAAU;IACV,gBAAW;IACX,cAAS;IACT,mBAAkB;IAClB,mBAAkB;IAClB,kBAAiB;IACjB,oBAAmB;IACnB,mBAAkB;IAIhB,UAAiD;IAGnD,sBAMC;IAED,WAOC;IAED,iCASC;IAED,qEASC;IAED,qEAOC;IAED,qCAKC;IAED,mBAeC;IAED,kBAKC;IAED,mCAKC;IAED,gCAEC;IAED,8BAaC;IAED,uBAYC;IAED,4BAUC;IAED,YAKC;IAED,4BAYC;IAED,6BAUC;IAED,8BAcC;IAED,uBA2BC;IAED,kBAEC;IAED,cAwCC;IAED,eAkBC;IAED,gBAIC;IAED,iBAIC;IAED,yBAWC;CACF"}
@@ -75,6 +75,36 @@ class MIDIPlayer {
75
75
  writable: true,
76
76
  value: void 0
77
77
  });
78
+ Object.defineProperty(this, "isPlaying", {
79
+ enumerable: true,
80
+ configurable: true,
81
+ writable: true,
82
+ value: false
83
+ });
84
+ Object.defineProperty(this, "isPausing", {
85
+ enumerable: true,
86
+ configurable: true,
87
+ writable: true,
88
+ value: false
89
+ });
90
+ Object.defineProperty(this, "isPaused", {
91
+ enumerable: true,
92
+ configurable: true,
93
+ writable: true,
94
+ value: false
95
+ });
96
+ Object.defineProperty(this, "isStopping", {
97
+ enumerable: true,
98
+ configurable: true,
99
+ writable: true,
100
+ value: false
101
+ });
102
+ Object.defineProperty(this, "isSeeking", {
103
+ enumerable: true,
104
+ configurable: true,
105
+ writable: true,
106
+ value: false
107
+ });
78
108
  this.midy = midy;
79
109
  this.root = document.createElement("midi-player");
80
110
  }
@@ -129,16 +159,25 @@ class MIDIPlayer {
129
159
  }
130
160
  startTimer() {
131
161
  const endTime = this.midy.totalTime;
132
- this.timer = setInterval(() => {
162
+ this.stopTimer();
163
+ const update = () => {
133
164
  const now = this.midy.currentTime();
134
- const seconds = Math.ceil(now);
135
165
  if (this.currTimeNode) {
166
+ const seconds = Math.ceil(now);
136
167
  this.currTimeNode.textContent = this.formatTime(seconds);
137
168
  }
138
169
  if (this.seekBarNode) {
139
170
  this.seekBarNode.value = now / endTime;
140
171
  }
141
- }, this.currTimeInterval);
172
+ this.timer = requestAnimationFrame(update);
173
+ };
174
+ this.timer = requestAnimationFrame(update);
175
+ }
176
+ stopTimer() {
177
+ if (this.timer) {
178
+ cancelAnimationFrame(this.timer);
179
+ this.timer = null;
180
+ }
142
181
  }
143
182
  async loadMIDI(file) {
144
183
  await this.midy.loadMIDI(file);
@@ -165,14 +204,13 @@ class MIDIPlayer {
165
204
  return paths;
166
205
  }
167
206
  async start() {
207
+ this.isPlaying = true;
168
208
  const midy = this.midy;
169
- if (midy.soundFonts.length === 0) {
170
- const paths = this.getSoundFontPaths();
171
- await midy.loadSoundFont(paths);
172
- }
209
+ await midy.loadSoundFont(this.getSoundFontPaths());
173
210
  this.startTimer();
174
211
  await midy.start();
175
- clearInterval(this.timer);
212
+ this.stopTimer();
213
+ this.isPlaying = false;
176
214
  if (!midy.isPaused && this.currTimeNode) {
177
215
  this.currTimeNode.textContent = "0:00";
178
216
  this.seekBarNode.value = 0;
@@ -182,11 +220,13 @@ class MIDIPlayer {
182
220
  const midy = this.midy;
183
221
  if (!midy.isPlaying)
184
222
  return;
185
- clearInterval(this.timer);
223
+ this.isStopping = true;
186
224
  this.playNode.style.display = "initial";
187
225
  this.pauseNode.style.display = "none";
188
226
  this.resumeNode.style.display = "none";
227
+ this.stopTimer();
189
228
  await midy.stop();
229
+ this.isPlaying = false;
190
230
  }
191
231
  stop() {
192
232
  const stop = this.button("start", "midi-player-start", "stop", "initial");
@@ -198,32 +238,39 @@ class MIDIPlayer {
198
238
  const { midy, playNode, pauseNode } = this;
199
239
  if (midy.isPlaying || midy.isPaused)
200
240
  return;
241
+ this.isPlaying = true;
201
242
  playNode.style.display = "none";
202
243
  pauseNode.style.display = "initial";
203
244
  await this.start();
245
+ this.isPlaying = false;
204
246
  if (!midy.isPaused) {
205
247
  pauseNode.style.display = "none";
206
248
  playNode.style.display = "initial";
207
249
  }
208
250
  }
209
- handlePause() {
251
+ async handlePause() {
210
252
  const midy = this.midy;
211
253
  if (!midy.isPlaying || midy.isPaused)
212
254
  return;
255
+ this.isPausing = true;
213
256
  this.pauseNode.style.display = "none";
214
257
  this.resumeNode.style.display = "initial";
215
- clearInterval(this.timer);
216
- midy.pause();
258
+ this.stopTimer();
259
+ await midy.pause();
260
+ this.isPausing = false;
261
+ this.isPaused = true;
217
262
  }
218
263
  async handleResume() {
219
264
  const { midy, playNode, pauseNode, resumeNode } = this;
220
265
  if (!midy.isPaused)
221
266
  return;
267
+ this.isPlaying = true;
222
268
  pauseNode.style.display = "initial";
223
269
  resumeNode.style.display = "none";
224
270
  this.startTimer();
225
271
  await midy.resume();
226
- clearInterval(this.timer);
272
+ this.stopTimer();
273
+ this.isPlaying = false;
227
274
  if (!midy.isPaused) {
228
275
  pauseNode.style.display = "none";
229
276
  playNode.style.display = "initial";
@@ -284,11 +331,13 @@ class MIDIPlayer {
284
331
  seekBar() {
285
332
  const seekBar = this.formRange("playback position", "midi-player-seekBar", 0, "initial");
286
333
  seekBar.oninput = (event) => {
334
+ this.isSeeking = true;
287
335
  const time = event.target.value * this.midy.totalTime;
288
336
  this.midy.seekTo(time);
289
337
  if (this.currTimeNode) {
290
338
  this.currTimeNode.textContent = this.formatTime(time);
291
339
  }
340
+ this.isSeeking = false;
292
341
  };
293
342
  this.seekBarNode = seekBar;
294
343
  return seekBar;