File size: 26,960 Bytes
3ff20ad
1fc8ebd
a88ecdb
 
5556415
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a88ecdb
5556415
 
 
5887d9d
5556415
 
 
 
 
 
 
 
 
 
5887d9d
5556415
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b36d9d3
 
 
 
 
 
 
 
 
4f6e9c5
b36d9d3
 
4f6e9c5
5556415
 
 
 
472db58
5556415
 
 
 
 
472db58
5556415
 
 
472db58
5556415
 
 
472db58
5556415
 
 
b36d9d3
 
 
 
3ff20ad
 
 
 
b36d9d3
 
 
 
 
 
 
472db58
 
5887d9d
472db58
5556415
 
 
 
3ff20ad
5556415
058721d
 
 
 
 
5556415
058721d
3ff20ad
5556415
 
3ff20ad
5556415
058721d
 
5556415
b36d9d3
 
 
 
 
 
 
 
 
058721d
5556415
b36d9d3
 
 
 
 
 
5556415
 
b36d9d3
 
 
3ff20ad
5556415
 
 
 
 
b36d9d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5556415
b36d9d3
 
 
 
 
5556415
 
 
 
 
 
 
 
 
 
 
b36d9d3
 
 
 
 
 
 
 
 
 
 
5556415
 
 
 
 
 
 
 
 
 
 
 
 
 
244fae7
5556415
 
 
b36d9d3
 
 
 
 
058721d
 
b36d9d3
 
058721d
 
b36d9d3
 
 
 
 
3ff20ad
5556415
 
 
 
 
 
 
244fae7
5556415
b36d9d3
7fa6f3c
3ff20ad
b36d9d3
 
 
 
 
 
 
 
 
 
 
 
 
 
95f67b0
b36d9d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7fa6f3c
b36d9d3
 
 
85ca102
b36d9d3
 
 
 
3ff20ad
 
 
 
 
b36d9d3
3ff20ad
 
 
b36d9d3
3ff20ad
058721d
b36d9d3
a88ecdb
3ff20ad
 
 
 
 
5556415
b36d9d3
3ff20ad
 
 
058721d
b36d9d3
244fae7
3ff20ad
 
244fae7
b36d9d3
3ff20ad
 
 
5556415
472db58
5556415
 
 
472db58
5556415
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
472db58
5556415
 
 
 
 
 
d0e92a6
5556415
d0e92a6
5556415
 
 
 
 
 
b36d9d3
 
 
 
 
95f67b0
b36d9d3
 
 
 
 
 
 
 
 
 
 
 
 
5556415
 
 
 
 
 
 
 
 
3ff20ad
 
 
 
 
 
 
5556415
 
 
 
 
 
3ff20ad
 
 
 
 
 
 
c54433e
5556415
 
 
 
 
 
c54433e
5556415
 
 
 
 
 
 
 
3ff20ad
5556415
 
b36d9d3
 
 
 
 
 
 
 
5556415
 
 
 
b36d9d3
 
 
 
 
 
 
 
5556415
 
 
21d96bc
5556415
 
b36d9d3
 
 
 
 
 
 
 
3ff20ad
5556415
 
b36d9d3
 
 
 
 
 
 
 
5556415
 
 
 
 
 
 
3ff20ad
 
5556415
3ff20ad
5556415
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b36d9d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5556415
 
b36d9d3
5556415
3ff20ad
5556415
 
 
 
 
 
fb9ab3a
5556415
 
 
 
 
 
 
 
 
 
a88ecdb
5556415
 
 
 
 
3ff20ad
 
 
 
5556415
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
472db58
5556415
a88ecdb
5556415
 
 
 
 
3ff20ad
 
5556415
5c50842
 
5556415
472db58
5556415
 
472db58
5556415
 
7d25a49
a88ecdb
5556415
 
 
b36d9d3
5556415
 
472db58
5556415
 
 
472db58
5556415
472db58
5556415
 
 
 
 
 
fb9ab3a
3ff20ad
b36d9d3
3ff20ad
 
 
 
 
 
 
 
5556415
 
1fc8ebd
5556415
7d25a49
 
5556415
 
 
a88ecdb
7d25a49
5556415
7d25a49
 
5556415
 
 
 
 
 
7d25a49
 
5556415
 
 
 
 
a88ecdb
5556415
 
 
 
 
 
 
472db58
5556415
 
 
 
 
 
 
472db58
5556415
f16ac40
5556415
 
 
d0e92a6
 
5556415
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3ff20ad
5556415
472db58
3ff20ad
472db58
 
 
 
 
 
 
a88ecdb
5556415
472db58
3ff20ad
 
 
 
 
 
5556415
 
a88ecdb
5556415
 
 
 
 
1fc8ebd
5556415
 
 
3ff20ad
 
 
 
5556415
 
 
 
 
3ff20ad
 
 
 
 
 
5556415
1fc8ebd
5556415
 
 
1fc8ebd
5556415
 
 
 
 
 
 
 
 
 
 
b36d9d3
 
 
 
 
058721d
b36d9d3
 
 
 
 
3ff20ad
 
5556415
 
 
 
 
 
 
 
 
 
 
 
a88ecdb
5556415
a88ecdb
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
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
// midipane.js - MIDI ピアノプレイヤー(スクロール対応版)
// 依存: @tonejs/midi のみ(Tone.js は不要)

(function() {
  'use strict';

  window.MIDIPane = {
    create: createMIDIPane,
    dispose: disposeMIDIPane,
    syncAll: syncAllMIDIPanes,
    setPlaybackRate: setAllPlaybackRate,
    playAll: playAllMIDIPanes,
    pauseAll: pauseAllMIDIPanes,
    stopAll: stopAllMIDIPanes,
    seekAll: seekAllMIDIPanes
  };

  const instances = new Map();

  function createMIDIPane(container, filePath, headerElement, offset = 0) {
    if (instances.has(container)) {
      disposeMIDIPane(container);
    }
    if (!headerElement) {
      const pane = container.closest('.pane');
      if (pane) headerElement = pane.querySelector('.pane-header');
    }
    const instance = new MIDIPlayer(container, filePath, headerElement, offset);
    instances.set(container, instance);
    return instance;
  }

  function disposeMIDIPane(container) {
    if (instances.has(container)) {
      const inst = instances.get(container);
      inst.destroy();
      instances.delete(container);
    }
  }

  function syncAllMIDIPanes(time) {
    instances.forEach(inst => {
      if (inst && typeof inst.seekTo === 'function') {
        inst.seekTo(time || 0);
      }
    });
  }

  function setAllPlaybackRate(rate) {
    instances.forEach(inst => {
      if (inst && typeof inst.setSpeed === 'function') {
        inst.setSpeed(rate);
      }
    });
  }

  function playAllMIDIPanes() {
    instances.forEach(inst => {
      if (inst && typeof inst.play === 'function') inst.play();
    });
  }

  function pauseAllMIDIPanes() {
    instances.forEach(inst => {
      if (inst && typeof inst.pause === 'function') inst.pause();
    });
  }

  function stopAllMIDIPanes() {
    instances.forEach(inst => {
      if (inst && typeof inst.stop === 'function') inst.stop();
    });
  }

  function seekAllMIDIPanes(time) {
    instances.forEach(inst => {
      if (inst && typeof inst.seekTo === 'function') inst.seekTo(time);
    });
  }

  function convertTrackName(trackName, filePath, visibleIndex) {
    const fileName = filePath.split('/').pop().replace(/\.[^/.]+$/, '');
    const cleaned = (trackName || '').replace(/[\r\n]+/g, ' ').trim();

    if (cleaned === '') {
      return `${fileName} / Track${visibleIndex + 1}`;
    }

    if (cleaned === 'Piano') return 'ピアノ';

    return cleaned;
  }

  const DEFAULT_COLORS = [
    '#FF595E', '#FFCA3A', '#8AC926', '#1982C4', '#6A4C93',
    '#F564A9', '#4CC9F0', '#FF9F1C', '#00F5D4', '#E0B1CB'
  ];

  class MIDIPlayer {
    constructor(container, filePath, headerElement, offset = 0) {
      this.container = container;
      this.filePath = filePath;
      this.headerElement = headerElement;
      this.offset = offset;
      this.speed = 1.0;
      this.isPlaying = false;
      this.totalDuration = 0;
      this.originalDuration = 0;
      this.trackEntries = [];
      this.keyMap = {};
      this.animationId = null;
      this.playheadSeconds = 0;
      this.lastFrameTime = 0;
      this.settingsPanelVisible = false;
      this.lastMasterTime = 0;
      this.keyboardHeightRatio = 0.18;
      this.isResizingKeyboard = false;
      this.scrollY = 0;
      this.isDragging = false;
      this.dragStartY = 0;
      this.dragStartScrollY = 0;
      this.isMouseDown = false;

      // ★ イベントハンドラをバインド(後で解除できるように)
      this._onResizeBarMouseDown = this._onResizeBarMouseDown.bind(this);
      this._onMouseDown = this._onMouseDown.bind(this);
      this._onMouseMove = this._onMouseMove.bind(this);
      this._onMouseUp = this._onMouseUp.bind(this);
      this._onWheel = this._onWheel.bind(this);

      this.buildUI();
      this.loadMIDI(filePath);
    }

    buildUI() {
      const container = this.container;
      container.innerHTML = '';
      container.className = 'midi-container';
      container.style.cssText = 'display:flex;flex-direction:column;width:100%;height:100%;background:#0a0a0a;position:relative;';

      // ★ ノート表示用キャンバスコンテナ(スクロール可能領域)
      const canvasOuter = document.createElement('div');
      canvasOuter.style.cssText = 'flex:1;position:relative;min-height:0;overflow:hidden;cursor:grab;';
      canvasOuter.id = 'midi-canvas-outer';

      const canvasContainer = document.createElement('div');
      canvasContainer.style.cssText = 'position:relative;width:100%;height:100%;';
      canvasContainer.id = 'midi-canvas-container';

      this.canvas = document.createElement('canvas');
      this.canvas.style.cssText = 'display:block;width:100%;height:100%;';
      canvasContainer.appendChild(this.canvas);
      canvasOuter.appendChild(canvasContainer);
      container.appendChild(canvasOuter);

      this.keyboardResizeBar = document.createElement('div');
      this.keyboardResizeBar.style.cssText = `
        height:6px;
        cursor:ns-resize;
        background:#444;
        flex-shrink:0;
      `;
      container.appendChild(this.keyboardResizeBar);

      // ★ 鍵盤は固定(canvasOuterの外)
      this.keyboardDiv = document.createElement('div');
      this.keyboardDiv.style.cssText = `
        position:relative;
        background:#000;
        overflow:hidden;
        flex-shrink:0;
      `;
      container.appendChild(this.keyboardDiv);

      // ★ リサイズバーのイベント(一度だけ登録)
      this.keyboardResizeBar.addEventListener('mousedown', this._onResizeBarMouseDown);

      // 設定パネル
      const paneElement = container.closest('.pane');
      if (paneElement) {
        const paneContainer = paneElement.parentElement;
        this.settingsPanel = document.createElement('div');
        this.settingsPanel.style.cssText = `
          position: absolute;
          top: ${paneElement.offsetTop + 32}px;
          right: 10px;
          background: #112240;
          border: 1px solid #64ffda;
          border-radius: 8px;
          padding: 12px;
          z-index: 100;
          min-width: 280px;
          max-width: 400px;
          max-height: 300px;
          overflow-y: auto;
          display: none;
          box-shadow: 0 4px 20px rgba(0,0,0,0.8);
          pointer-events: auto;
        `;
        this.settingsPanel.innerHTML = `
          <div style="color:#e6f1ff;font-size:13px;font-weight:bold;margin-bottom:8px;border-bottom:1px solid rgba(100,255,218,0.2);padding-bottom:4px;">
            🎵 トラック設定
          </div>
          <div id="midi-track-settings" style="display:flex;flex-direction:column;gap:6px;"></div>
        `;
        paneContainer.style.position = 'relative';
        paneContainer.appendChild(this.settingsPanel);
      }

      if (this.headerElement) {
        const existingBtn = this.headerElement.querySelector('.midi-settings-btn');
        if (!existingBtn) {
          const settingsBtn = document.createElement('button');
          settingsBtn.className = 'midi-settings-btn';
          settingsBtn.textContent = '⚙';
          settingsBtn.style.cssText = `
            background: rgba(100,255,218,0.1);
            border: 1px solid rgba(100,255,218,0.3);
            color: #e6f1ff;
            border-radius: 3px;
            padding: 2px 6px;
            font-size: 14px;
            cursor: pointer;
            line-height: 1.4;
            margin-left: auto;
            flex-shrink: 0;
          `;
          settingsBtn.title = 'トラック設定';
          settingsBtn.addEventListener('click', (e) => {
            e.stopPropagation();
            this.toggleSettings();
          });
          const zoomBtn = this.headerElement.querySelector('.pane-zoom');
          if (zoomBtn) {
            this.headerElement.insertBefore(settingsBtn, zoomBtn);
          } else {
            this.headerElement.appendChild(settingsBtn);
          }
          this.settingsBtn = settingsBtn;
        } else {
          this.settingsBtn = existingBtn;
        }
      }

      this.createKeyboard();

      this._resizeObserver = new ResizeObserver(() => {
        this.updateKeyboardHeight();
        this.resizeCanvas();
      });
      this._resizeObserver.observe(canvasOuter);

      // ★ マウスイベント(canvasOuter に対して設定)- バインド済みメソッドを使用
      this.canvasOuter = canvasOuter;
      this.canvasContainer = canvasContainer;

      canvasOuter.addEventListener('mousedown', this._onMouseDown);
      canvasOuter.addEventListener('mousemove', this._onMouseMove);
      canvasOuter.addEventListener('mouseup', this._onMouseUp);
      canvasOuter.addEventListener('mouseleave', this._onMouseUp);
      canvasOuter.addEventListener('wheel', this._onWheel, { passive: false });

      this.drawLoop();

      document.addEventListener('click', (e) => {
        if (this.settingsPanel && this.settingsPanelVisible) {
          if (!this.settingsPanel.contains(e.target) && e.target !== this.settingsBtn) {
            this.hideSettings();
          }
        }
      });

      this.updateKeyboardHeight();
    }

    // ★ リサイズバーのマウスダウン処理(クラスメソッドとして独立)
    _onResizeBarMouseDown(e) {
      e.preventDefault();
      e.stopPropagation();
      if (this.isResizingKeyboard) return;
      this.isResizingKeyboard = true;

      const startY = e.clientY;
      const startRatio = this.keyboardHeightRatio;
      const paneHeight = this.container.clientHeight;

      // ★ 名前付き関数で mousemove / mouseup を定義
      const onMouseMove = (ev) => {
        const delta = startY - ev.clientY;
        let newRatio = startRatio + delta / paneHeight;
        newRatio = Math.max(0.08, Math.min(0.5, newRatio));
        this.keyboardHeightRatio = newRatio;
        this.updateKeyboardHeight();
        this.resizeCanvas();
      };

      const onMouseUp = () => {
        document.removeEventListener('mousemove', onMouseMove);
        document.removeEventListener('mouseup', onMouseUp);
        this.isResizingKeyboard = false;
      };

      document.addEventListener('mousemove', onMouseMove);
      document.addEventListener('mouseup', onMouseUp);
    }

    updateKeyboardHeight() {
      const paneHeight = this.container.clientHeight;
      const keyboardHeight = Math.max(40, paneHeight * this.keyboardHeightRatio);
      this.keyboardDiv.style.height = keyboardHeight + 'px';
      void this.keyboardDiv.offsetHeight;
    }

    // ★ マウスイベントハンドラ(クラスメソッドとして定義)
    _onMouseDown(e) {
      if (e.button !== 0) return;
      this.isDragging = true;
      this.isMouseDown = true;
      this.dragStartY = e.clientY;
      this.dragStartScrollY = this.scrollY;
      this.canvasOuter.style.cursor = 'grabbing';
      e.preventDefault();
    }

    _onMouseMove(e) {
      if (!this.isDragging) {
        if (this.canvasOuter) {
          this.canvasOuter.style.cursor = 'grab';
        }
        return;
      }
      const deltaY = (e.clientY - this.dragStartY) * 1.0;
      this.scrollY = this.dragStartScrollY + deltaY;
    }

    _onMouseUp(e) {
      if (this.isDragging) {
        this.isDragging = false;
        this.isMouseDown = false;
        if (this.canvasOuter) {
          this.canvasOuter.style.cursor = 'grab';
        }
      }
    }

    _onWheel(e) {
      if (e.ctrlKey || e.metaKey) return;
      e.preventDefault();
      this.scrollY += e.deltaY * 0.8;
    }

    setOffset(newOffset) {
      this.offset = newOffset;
    }

    toggleSettings() {
      if (this.settingsPanelVisible) this.hideSettings();
      else this.showSettings();
    }

    showSettings() {
      if (!this.settingsPanel) return;
      this.settingsPanel.style.display = 'block';
      this.settingsPanelVisible = true;
      this.renderTrackSettings();
      this.updateSettingsPosition();
    }

    hideSettings() {
      if (!this.settingsPanel) return;
      this.settingsPanel.style.display = 'none';
      this.settingsPanelVisible = false;
    }

    updateSettingsPosition() {
      const paneElement = this.container.closest('.pane');
      if (paneElement && this.settingsPanel) {
        const paneRect = paneElement.getBoundingClientRect();
        const containerRect = paneElement.parentElement.getBoundingClientRect();
        const top = paneRect.top - containerRect.top + 32;
        const right = containerRect.right - paneRect.right + 10;
        this.settingsPanel.style.top = top + 'px';
        this.settingsPanel.style.right = right + 'px';
      }
    }

    createKeyboard() {
      const keyboardDiv = this.keyboardDiv;
      keyboardDiv.innerHTML = '';
      this.keyMap = {};

      const minNote = 21;
      const maxNote = 95;
      let whiteIndex = 0;
      const numWhiteKeys = 44;

      for (let i = minNote; i <= maxNote; i++) {
        const isBlack = [1, 3, 6, 8, 10].includes(i % 12);
        const el = document.createElement('div');
        el.className = 'key ' + (isBlack ? 'black' : 'white');
        el.style.cssText = `
          position:absolute;
          box-sizing:border-box;
          border-radius:3px;
          ${isBlack ? `
            background:#111;
            box-shadow:inset 0 0 0 1px #000, inset 0 -4px 0 1px #000;
            height:55%;
            z-index:3;
          ` : `
            background:#f5f5f5;
            background-image:linear-gradient(to bottom, transparent, #fff, transparent);
            border:1px solid rgba(0,0,0,0.1);
            border-right-width:0;
            border-bottom-width:2px;
            height:100%;
            z-index:1;
            margin-left:-1px;
          `}
        `;
        const origColor = isBlack ? '#111' : '#f5f5f5';
        el.dataset.originalColor = origColor;

        let leftPct, widthPct;
        if (!isBlack) {
          leftPct = (whiteIndex / numWhiteKeys) * 100;
          widthPct = (1 / numWhiteKeys) * 100;
          el.style.left = leftPct + '%';
          el.style.width = widthPct + '%';
          this.keyMap[i] = {
            el,
            left: leftPct,
            width: widthPct,
            black: false,
            active: false
          };
          whiteIndex++;
        } else {
          leftPct = (whiteIndex / numWhiteKeys) * 100;
          widthPct = ((2 / 3) / numWhiteKeys) * 100;
          el.style.left = (leftPct - widthPct / 2) + '%';
          el.style.width = widthPct + '%';
          this.keyMap[i] = {
            el,
            left: leftPct - widthPct / 2,
            width: widthPct,
            black: true,
            active: false
          };
        }
        keyboardDiv.appendChild(el);
      }

      const lastWhite = keyboardDiv.querySelector('.key.white:last-child');
      if (lastWhite) lastWhite.style.borderRightWidth = '1px';
    }

    renderTrackSettings() {
      const container = this.settingsPanel?.querySelector('#midi-track-settings');
      if (!container) return;
      container.innerHTML = '';
      if (this.trackEntries.length === 0) {
        container.innerHTML = '<div style="color:#888;font-size:12px;text-align:center;">トラックがありません</div>';
        return;
      }
      this.trackEntries.forEach((entry, index) => {
        const row = document.createElement('div');
        row.style.cssText = `
          display:flex;
          align-items:center;
          gap:8px;
          padding:4px 6px;
          background:rgba(255,255,255,0.03);
          border-radius:4px;
          min-height:28px;
        `;
        const colorInput = document.createElement('input');
        colorInput.type = 'color';
        colorInput.value = entry.color;
        colorInput.style.cssText = `
          width:24px;
          height:24px;
          border:none;
          padding:0;
          cursor:pointer;
          background:none;
          flex-shrink:0;
        `;
        colorInput.addEventListener('input', () => {
          entry.color = colorInput.value;
        });

        const nameSpan = document.createElement('span');
        nameSpan.style.cssText = `
          flex:1;
          font-size:12px;
          color:#e6f1ff;
          white-space:nowrap;
          overflow:hidden;
          text-overflow:ellipsis;
          min-width:0;
        `;
        nameSpan.textContent = entry.trackName || `Track${index + 1}`;
        const checkLabel = document.createElement('label');
        checkLabel.style.cssText = `
          display:flex;
          align-items:center;
          gap:4px;
          font-size:12px;
          color:#ccd6f6;
          flex-shrink:0;
          cursor:pointer;
        `;
        const checkbox = document.createElement('input');
        checkbox.type = 'checkbox';
        checkbox.checked = entry.visible;
        checkbox.style.cssText = 'cursor:pointer;';
        checkbox.addEventListener('change', () => {
          entry.visible = checkbox.checked;
        });
        const checkText = document.createElement('span');
        checkText.textContent = '表示';
        checkLabel.appendChild(checkbox);
        checkLabel.appendChild(checkText);
        row.appendChild(colorInput);
        row.appendChild(nameSpan);
        row.appendChild(checkLabel);
        container.appendChild(row);
      });
    }

    async loadMIDI(filePath) {
      try {
        const response = await fetch(filePath);
        if (!response.ok) throw new Error('MIDI file not found: ' + filePath);
        const arrayBuffer = await response.arrayBuffer();
        this.disposePlayback();
        const parsedMidi = new Midi(arrayBuffer);
        this.parsedMidi = parsedMidi;
        this.trackEntries = [];
        let maxTime = 0;
        parsedMidi.tracks.forEach(t => {
          t.notes.forEach(n => {
            const end = n.time + (n.duration || 0);
            if (end > maxTime) maxTime = end;
          });
        });
        this.originalDuration = maxTime;
        this.totalDuration = maxTime;
        let globalTrackIndex = 0;
        let visibleTrackIndex = 0;

        parsedMidi.tracks.forEach((track, idx) => {
          if (track.notes.length === 0) return;

          this.normalizeTrack(track);

          const color = DEFAULT_COLORS[globalTrackIndex % DEFAULT_COLORS.length];
          globalTrackIndex++;

          const notes = track.notes.map(n => ({
            time: n.time,
            name: n.name,
            duration: Math.max(n.duration || 0, 0.02),
            velocity: n.velocity || 0.8,
            midi: n.midi
          }));

          const entry = {
            fileName: filePath.split('/').pop(),
            trackName: convertTrackName(track.name, filePath, visibleTrackIndex),
            color,
            track,
            notes,
            visible: (convertTrackName(track.name, filePath, visibleTrackIndex) === 'ピアノ')
          };

          visibleTrackIndex++;
          this.trackEntries.push(entry);
        });
        this.clearKeys();
        this.playheadSeconds = 0;
        this.scrollY = 0;
        this.isPlaying = false;
        this.resizeCanvas();
        this.renderTrackSettings();
      } catch (err) {
        console.error('MIDI load error:', err);
        this.container.innerHTML = `<div style="color:#ff6b6b;padding:20px;text-align:center;">MIDI読み込みエラー: ${err.message}</div>`;
      }
    }

    normalizeTrack(track) {
      const sortedNotes = [...track.notes].sort((a, b) => a.time - b.time);
      const noteMap = new Map();
      sortedNotes.forEach(note => {
        const key = `${note.midi}_${Math.floor(note.time * 1000) / 1000}`;
        if (noteMap.has(key)) {
          const existing = noteMap.get(key);
          if (note.duration > existing.duration) noteMap.set(key, note);
        } else {
          noteMap.set(key, note);
        }
      });
      const MAX_DURATION = 10;
      const limitedNotes = [];
      noteMap.forEach(note => {
        if (note.duration > MAX_DURATION) {
          limitedNotes.push({
            ...note,
            duration: MAX_DURATION
          });
        } else {
          limitedNotes.push(note);
        }
      });
      limitedNotes.sort((a, b) => a.time - b.time);
      track.notes = limitedNotes;
    }

    clearKeys() {
      Object.values(this.keyMap).forEach(k => {
        k.active = false;
        const orig = k.el.dataset.originalColor || (k.black ? '#111' : '#f5f5f5');
        k.el.style.backgroundColor = orig;
        k.el.classList.remove('active');
      });
    }

    setSpeed(rate) {
      this.speed = rate;
    }

    play() {
      if (!this.parsedMidi || this.trackEntries.length === 0) return;
      if (this.isPlaying) return;
      this.isPlaying = true;
      this.lastFrameTime = performance.now();
      this.scrollY = 0;

      if (this.offset > 0 && this.playheadSeconds === 0) {
        const remainingDelay = Math.max(0, this.offset - (this.lastMasterTime || 0)) * 1000;
        this.playStartDelayUntil = performance.now() + remainingDelay;
      } else {
        this.playStartDelayUntil = 0;
      }
    }

    pause() {
      this.isPlaying = false;
    }

    stop() {
      this.isPlaying = false;
      this.playheadSeconds = 0;
      this.scrollY = 0;
      this.clearKeys();
    }

    getCurrentTime() {
      return this.playheadSeconds + this.offset;
    }

    seekTo(masterTime) {
      this.lastMasterTime = masterTime;
      let internalTime = masterTime - this.offset;
      if (internalTime < 0) internalTime = 0;
      const clamped = Math.min(internalTime, this.originalDuration);
      this.playheadSeconds = clamped;
      this.clearKeys();
    }

    resizeCanvas() {
      const rect = this.canvasOuter.getBoundingClientRect();
      const w = rect.width || 300;
      const h = rect.height || 200;
      this.canvas.width = Math.max(100, w * 2);
      this.canvas.height = Math.max(100, h * 2);
      this.canvas.style.width = w + 'px';
      this.canvas.style.height = h + 'px';
    }

    drawLoop() {
      this.animationId = requestAnimationFrame(() => this.drawLoop());

      if (this.isPlaying) {
        const now = performance.now();
        if (this.playStartDelayUntil && now < this.playStartDelayUntil) {
          this.lastFrameTime = now;
          this.draw();
          return;
        }
        if (this.playStartDelayUntil && now >= this.playStartDelayUntil) {
          this.playStartDelayUntil = 0;
        }
        if (this.lastFrameTime) {
          const delta = (now - this.lastFrameTime) / 1000;
          this.playheadSeconds += delta * this.speed;
          if (this.totalDuration > 0 && this.playheadSeconds >= this.totalDuration) {
            this.playheadSeconds = this.totalDuration;
            this.isPlaying = false;
          }
        }
        this.lastFrameTime = now;
      } else {
        this.lastFrameTime = performance.now();
      }
      this.draw();
    }

    draw() {
      const canvas = this.canvas;
      const ctx = canvas.getContext('2d');
      const W = canvas.width;
      const H = canvas.height;

      ctx.clearRect(0, 0, W, H);

      if (this.trackEntries.length === 0) {
        ctx.fillStyle = '#555';
        ctx.font = '20px sans-serif';
        ctx.textAlign = 'center';
        ctx.fillText('MIDI読み込み中...', W / 2, H / 2);
        return;
      }

      const now = this.playheadSeconds;
      const SPEED = 250;

      ctx.strokeStyle = 'rgba(255,255,255,0.03)';
      ctx.lineWidth = 1;
      for (let i = 0; i < 44; i++) {
        const x = (i / 44) * W;
        ctx.beginPath();
        ctx.moveTo(x, 0);
        ctx.lineTo(x, H);
        ctx.stroke();
      }

      this.trackEntries.forEach(entry => {
        if (!entry.visible) return;
        const notes = entry.notes || [];
        const color = entry.color;

        notes.forEach(note => {
          const midi = note.midi;
          const keyData = this.keyMap[midi];
          if (!keyData) return;

          const duration = Math.max(note.duration || 0, 0.02);
          const yBottom = H - (note.time - now) * SPEED + this.scrollY;
          const yTop = yBottom - (duration * SPEED);

          if (yBottom <= -10 || yTop >= H + 10) return;

          if (now >= note.time && now < note.time + duration) {
            const kd = this.keyMap[midi];
            if (kd) {
              kd.active = true;
              kd.el.style.backgroundColor = color;
              kd.el.classList.add('active');
            }
          }

          const x = (keyData.left / 100) * W;
          const w = (keyData.width / 100) * W;
          ctx.fillStyle = color;
          ctx.globalAlpha = 0.85;
          ctx.fillRect(x + 1, yTop, Math.max(w - 2, 1), Math.max(yBottom - yTop, 2));
          ctx.globalAlpha = 1.0;
        });
      });

      Object.values(this.keyMap).forEach(k => {
        if (!k.active) {
          const orig = k.el.dataset.originalColor || (k.black ? '#111' : '#f5f5f5');
          k.el.style.backgroundColor = orig;
          k.el.classList.remove('active');
        }
        k.active = false;
      });

      ctx.strokeStyle = 'rgba(100,255,218,0.4)';
      ctx.lineWidth = 2;
      ctx.setLineDash([6, 8]);
      const lineY = H / 2;
      ctx.beginPath();
      ctx.moveTo(0, lineY);
      ctx.lineTo(W, lineY);
      ctx.stroke();
      ctx.setLineDash([]);

      ctx.fillStyle = 'rgba(255,255,255,0.15)';
      ctx.font = '12px monospace';
      ctx.textAlign = 'left';
      const displayTime = (now + this.offset).toFixed(1);
      ctx.fillText(`⏱ ${displayTime}s`, 10, 20);
    }

    disposePlayback() {
      this.clearKeys();
    }

    destroy() {
      if (this.animationId) {
        cancelAnimationFrame(this.animationId);
        this.animationId = null;
      }
      if (this._resizeObserver) {
        this._resizeObserver.disconnect();
        this._resizeObserver = null;
      }
      this.disposePlayback();

      // ★ イベントリスナーを解除
      if (this.keyboardResizeBar) {
        this.keyboardResizeBar.removeEventListener('mousedown', this._onResizeBarMouseDown);
      }

      if (this.canvasOuter) {
        this.canvasOuter.removeEventListener('mousedown', this._onMouseDown);
        this.canvasOuter.removeEventListener('mousemove', this._onMouseMove);
        this.canvasOuter.removeEventListener('mouseup', this._onMouseUp);
        this.canvasOuter.removeEventListener('mouseleave', this._onMouseUp);
        this.canvasOuter.removeEventListener('wheel', this._onWheel);
      }

      if (this.settingsPanel && this.settingsPanel.parentNode) {
        this.settingsPanel.parentNode.removeChild(this.settingsPanel);
      }
      if (this.settingsBtn && this.settingsBtn.parentNode) {
        this.settingsBtn.parentNode.removeChild(this.settingsBtn);
      }
      if (this.container) {
        this.container.innerHTML = '';
      }
      if (instances.has(this.container)) {
        instances.delete(this.container);
      }
    }
  }
})();