Repository navigation
Expand file tree
/
Copy pathscreen.js
More file actions
1065 lines (969 loc) · 50.8 KB
/
Copy pathscreen.js
File metadata and controls
1065 lines (969 loc) · 50.8 KB
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
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Tab View visualization plugin — renders arrangements as
// scrolling tablature via alphaTab (https://alphatab.net/).
//
// Wave C (slopsmith#36): per-instance refactor. Earlier Wave B
// landed setRenderer support with an explicit single-instance
// module-state assumption (one alphaTab API, one container, one
// cursor highlight, one set of fetch sentinels). Wave C lifts that:
// every piece of per-render state moves into createFactory closures
// so N tabview instances coexist under splitscreen panels.
//
// Module-scope retained for genuine singletons:
// - alphaTab CDN script load (one <script> tag per tab)
// - _tvFilename — captured from window.playSong + arrangement:changed,
// applies to the single global player so all instances share it
//
// Tabview has no MIDI input and no focus-driven behavior — every
// panel renders independently from its own bundle.currentTime, and
// the splitscreen helper is consulted only for the mount target via
// panelChromeFor(). Absence of window.slopsmithSplitscreen OR
// isActive()===false means "main-player, mount into #player."
//
// alphaTab multi-instance: alphaTab loads its font + soundfont as
// CDN-cached static resources, so N AlphaTabApi instances on the
// same page share the underlying assets without coordination. Each
// instance owns its own AlphaTabApi + its own scoreLoaded /
// renderFinished / error subscriptions.
(function () {
'use strict';
// ═══════════════════════════════════════════════════════════════════════
// Module-level state (singletons)
// ═══════════════════════════════════════════════════════════════════════
// Captured from playSong wrap + arrangement:changed. All tabview
// instances see the same filename because slopsmith plays one song
// per tab — splitscreen panels render different arrangements OF THE
// SAME song, not different songs. Per-instance arrangement index
// arrives via bundle.songInfo.arrangement_index.
let _tvFilename = null;
// Monotonic id for per-instance DOM tagging (containers, alphaTab
// mount divs, highlight overlays, error banners — every node a
// tabview instance creates is suffixed with this so N instances
// don't collide on getElementById.
let _nextInstanceId = 0;
// ═══════════════════════════════════════════════════════════════════════
// alphaTab CDN loader (memoized — one load per page)
// ═══════════════════════════════════════════════════════════════════════
// Pin alphaTab to a specific release so new jsDelivr cache invalidations
// or upstream breaking changes can't land silently in production. Bump
// this when the alphaTab CDN publishes a version tested against the
// cursor-sync / tab-highlight behavior below.
const ALPHATAB_VERSION = '1.8.2';
const ALPHATAB_CDN_BASE = 'https://cdn.jsdelivr.net/npm/@coderline/alphatab@' + ALPHATAB_VERSION + '/dist';
// Matches rs2gp.py:TICKS_PER_BEAT. The GP5 builder places the first measure
// at tick TICKS_PER_BEAT (one beat in), hence the baseline offset added in
// _tvTimeToTick and subtracted back out in _tvSyncCursor before looking the
// beat up against alphaTab's 0-based absoluteDisplayStart (slopsmith#336).
const TICKS_PER_BEAT = 960;
let _alphaTabLoadPromise = null;
function _tvLoadScript() {
if (window.alphaTab) return Promise.resolve();
if (_alphaTabLoadPromise) return _alphaTabLoadPromise;
_alphaTabLoadPromise = new Promise((resolve, reject) => {
const s = document.createElement('script');
s.src = ALPHATAB_CDN_BASE + '/alphaTab.min.js';
s.onload = resolve;
s.onerror = () => {
_alphaTabLoadPromise = null; // allow retry on next init
reject(new Error('Failed to load alphaTab'));
};
document.head.appendChild(s);
});
return _alphaTabLoadPromise;
}
// ═══════════════════════════════════════════════════════════════════════
// Filename tracking (module-level — one global player)
// ═══════════════════════════════════════════════════════════════════════
//
// slopsmith core doesn't expose the current song's filename via a
// getter (song_info carries metadata, not the WS URL). Capture it
// ourselves by wrapping window.playSong once at module load and
// subscribing to arrangement:changed. init() consumes the cached
// _tvFilename when bundle.songInfo.filename isn't populated.
(function () {
// Idempotency: if screen.js is re-evaluated (loader cache miss, hot reload,
// older core builds without the load-side guard), don't re-wrap playSong
// and don't re-subscribe to arrangement:changed — re-wrap grows the
// wrapper chain, and a duplicate listener would update _tvFilename twice
// per event.
//
// Two independent install steps with their own guards: the first eval
// may run before window.playSong / window.slopsmith are populated (load
// order, hot reload), so a single combined flag would lock out a later
// retry from the second eval. Mark the wrapper itself for playSong (per
// notedetect/stepmode convention) and a window flag for the listener.
const origPlay = typeof window.playSong === 'function' ? window.playSong : null;
if (origPlay && !origPlay._tabviewWrapped) {
const wrapper = async function (filename, arrangement) {
_tvFilename = filename;
return origPlay.call(this, filename, arrangement);
};
wrapper._tabviewWrapped = true;
window.playSong = wrapper;
}
if (
window.slopsmith &&
typeof window.slopsmith.on === 'function' &&
!window.__slopsmithTabviewArrangementSubscribed
) {
window.slopsmith.on('arrangement:changed', (e) => {
// detail = { index, filename }
if (e && e.detail && e.detail.filename) _tvFilename = e.detail.filename;
});
window.__slopsmithTabviewArrangementSubscribed = true;
}
})();
// ═══════════════════════════════════════════════════════════════════════
// Splitscreen helper wrapper
// ═══════════════════════════════════════════════════════════════════════
//
// Tabview only needs panelChromeFor() — there's no MIDI routing or
// focus-driven behavior. Validate ONLY that surface so a partial
// helper that lacks the focus-related methods (which tabview doesn't
// consume) still routes through the splitscreen mount target.
function _ssActive() {
const ss = window.slopsmithSplitscreen;
if (!ss || typeof ss.isActive !== 'function' || !ss.isActive()) return false;
return typeof ss.panelChromeFor === 'function';
}
function _ssPanelChrome(highwayCanvas) {
const ss = window.slopsmithSplitscreen;
if (!_ssActive()) return null;
return ss.panelChromeFor(highwayCanvas);
}
// Resolve the DOM mount target for tabview's container / error banner.
// Splitscreen-active: ONLY the panel chrome is acceptable; if
// panelChromeFor returns null mid-creation or during a screen
// transition, return null so callers treat the mount as unavailable
// (the container won't be cached, and a later draw() / resize() /
// banner attempt retries cleanly once the panel chrome resolves).
// Falling through to #player here would (a) cache _tvContainer
// against the main player surface for the rest of the instance's
// lifetime, rendering this panel's tabs over the wrong area, and
// (b) confuse _tvSizeContainer's splitscreen vs main-player branch
// since _ssActive() would still be true on subsequent calls.
function _resolveMount(highwayCanvas) {
if (_ssActive()) {
return _ssPanelChrome(highwayCanvas);
}
return document.getElementById('player');
}
// ═══════════════════════════════════════════════════════════════════════
// Cursor sync helpers (stateless — beats come from the bundle)
// ═══════════════════════════════════════════════════════════════════════
function _tvTimeToTick(seconds, beats) {
if (!beats || beats.length < 2) return TICKS_PER_BEAT;
if (seconds < beats[0].time) return TICKS_PER_BEAT;
// Largest idx in [0, beats.length-2] with beats[idx].time <= seconds.
// Binary search (beats are time-sorted) — identical result to a linear
// scan, but O(log n) per frame instead of O(n) (CodeRabbit nitpick: this
// is the dominant per-frame sync cost). beats[0].time <= seconds holds
// here (guarded above), so idx is always >= 0.
let idx = 0, lo = 0, hi = beats.length - 2;
while (lo <= hi) {
const mid = (lo + hi) >> 1;
if (beats[mid].time <= seconds) { idx = mid; lo = mid + 1; }
else { hi = mid - 1; }
}
let frac = 0;
if (idx < beats.length - 1) {
const bStart = beats[idx].time;
const bEnd = beats[idx + 1].time;
if (bEnd > bStart) {
frac = Math.min(1, Math.max(0, (seconds - bStart) / (bEnd - bStart)));
}
}
return TICKS_PER_BEAT + Math.round((idx + frac) * TICKS_PER_BEAT);
}
// ═══════════════════════════════════════════════════════════════════════
// Factory — slopsmith#36 setRenderer contract (multi-instance)
// ═══════════════════════════════════════════════════════════════════════
function createFactory() {
const _instanceId = ++_nextInstanceId;
// Lifecycle
let _isReady = false;
// alphaTab + DOM state (per-instance)
let _tvApi = null;
let _tvContainer = null;
let _tvAtMount = null; // inner <div> alphaTab renders into
let _tvHighlight = null; // cursor highlight overlay element
let _tvErrorBanner = null; // current error banner element (if any)
let _tvErrorBannerTimeout = null;
let _tvReady = false;
// Highway canvas swap state
let _tvHighwayCanvas = null;
let _tvPrevVisibility = '';
// Mount position restore — when _tvCreateContainer() promotes a static
// mount to position:relative it saves the original inline style here so
// _tvRemoveContainer() can put it back on teardown.
let _tvPrevMountPosition = null;
// Observes #player-controls so the overlay re-insets when the controls
// bar wraps to a second row on narrow viewports (slopsmith#336). The
// window-resize listener only fires for viewport changes; this catches
// content reflow within an unchanged viewport. Main-player mode only —
// splitscreen panel chrome owns its own bottom-bar layout.
let _tvControlsObserver = null;
// Fetch / load tracking
let _tvCurrentFile = null; // filename the currently-loaded GP5 was fetched for
let _tvCurrentArr = null; // arrangement_index the current GP5 was fetched for
let _tvLoadingFile = null; // filename a currently-in-flight fetch is targeting
let _tvLoadingArr = null; // arrangement_index that fetch is targeting
let _tvFailedFile = null; // last (filename, arr_index) pair whose fetch failed —
let _tvFailedArr = null; // used by draw() to avoid a per-frame retry storm
// Cursor sync
let _tvLastTick = -1;
// Self-driven cursor rAF handle (slopsmith#734 follow-up). In
// single-player the marker is advanced from our OWN requestAnimationFrame
// loop, not the host draw() pump — see _tvCursorLoop for why.
let _tvCursorRAF = null;
// Marker positioning (slopsmith#734). We render our OWN playback
// marker from alphaTab's layout geometry instead of relying on
// alphaTab's internal player cursor (.at-cursor-bar). That cursor
// only appears once alphaTab's *player* reaches the "ready" state,
// which requires the soundfont to download from the CDN — fragile
// in the desktop shell (the cursor silently vanished in 0.2.9-beta.2).
// We don't use alphaTab's synth at all (slopsmith drives audio), so
// the player is disabled and the marker is driven by boundsLookup,
// which is available from layout alone (core.includeNoteBounds).
//
// _tvAtBeats: flat [{ beat, start }] for every rhythmic beat in the
// loaded score, sorted by absoluteDisplayStart (960-ppq MIDI ticks,
// bar 0 beat 0 == tick 0). Rebuilt on each scoreLoaded.
// _tvLastBeat: the alphaTab Beat the marker currently sits on — kept
// so a resize (which re-lays-out and rebuilds boundsLookup) can
// re-place the marker without waiting for the next time tick.
let _tvAtBeats = [];
let _tvLastBeat = null;
// Latest beats snapshot — bundle.beats is the source of truth
// under Wave C (the bare `highway` global used in Wave B was the
// main-player's highway, not ours under splitscreen).
let _tvLatestBeats = null;
// Monotonic init counter. Each init() bumps it; fetch / alphaTab
// callbacks capture the token and bail if a newer init has started
// since. Guards against a rapid arrangement switch where a pending
// fetch would otherwise install stale GP5 bytes over the new one.
let _tvInitToken = 0;
// ── Listener ref (per-instance so destroy() detach matches) ──
const _onWinResize = () => _tvSizeContainer();
// Tell the core highway whether its canvas is currently covered by the
// tab view. We hide the host canvas with visibility:hidden (so alphaTab
// can still measure its width), but visibility:hidden doesn't trip the
// highway's offsetParent-based rAF gate — so without this the underlying
// renderer (e.g. the 3D Highway WebGL overlay) keeps rendering full-tilt
// behind the opaque tab view. setVisible(false) trips the gate (pausing
// the host draw) and fires highway:visibility so overlay renderers hide
// their sibling DOM; setVisible(null) restores DOM-based detection when
// we hand the highway back. Guarded for older cores without the API.
// (slopsmith#654)
function _tvSetHighwayVisible(v) {
// Splitscreen: window.highway is the *main-player* highway, not a
// per-panel instance, and panels expose no per-canvas setVisible
// (only panelChromeFor). So only *force-hide* (false) in single-
// player mode — where _tvHighwayCanvas IS window.highway's canvas;
// forcing it from a panel would pause the wrong renderer and panels
// would race the shared gate. Clearing the override (null) is always
// safe and idempotent, and MUST run even if splitscreen became
// active after a single-player hide, so a prior force-hide can't
// strand the global highway paused. (slopsmith#654)
if (v === false && _ssActive()) return;
try {
const hw = window.highway;
if (hw && typeof hw.setVisible === 'function') hw.setVisible(v);
} catch (_) { /* best-effort: visibility hint only */ }
}
// ── Container setup ─────────────────────────────────────────────
function _tvCreateContainer() {
if (_tvContainer) return _tvContainer;
const mount = _resolveMount(_tvHighwayCanvas);
if (!mount) return null;
// The overlay is positioned with left:0/right:0 to inherit width
// from the mount; that requires the mount to be a positioned
// ancestor. Existing splitscreen/main-player mounts are; this
// is an idempotent guard so a future host with a static mount
// doesn't silently collapse our overlay to 0 width. The original
// inline position value is saved to _tvPrevMountPosition so
// _tvRemoveContainer() can restore it on teardown.
if (getComputedStyle(mount).position === 'static') {
_tvPrevMountPosition = mount.style.position; // save inline value (often '')
mount.style.position = 'relative';
}
const c = document.createElement('div');
c.id = 'tabview-container-' + _instanceId;
c.className = 'tabview-container';
c.dataset.tabviewInstance = String(_instanceId);
// visibility:hidden (not display:none) so alphaTab can measure
// the container's width during init. With display:none the
// element is out of layout and clientWidth is 0, which makes
// alphaTab skip the render entirely (warning: "AlphaTab skipped
// rendering because of width=0"). renderFinished swaps
// visibility to '' once the first paint lands, preserving the
// flash-free handoff this layer was originally designed for.
c.style.cssText = [
'visibility:hidden',
'position:absolute',
'top:0',
'left:0',
'right:0',
'overflow-y:auto',
'background:#fff',
'z-index:5',
].join(';');
const inner = document.createElement('div');
inner.id = 'tabview-at-' + _instanceId;
inner.className = 'tabview-at';
c.appendChild(inner);
// Playback marker overlay (slopsmith#734). A Songsterr-style
// vertical band: a translucent fill spanning the current beat's
// width + staff height, with a bright left border reading as the
// playhead at the beat's leading edge. Positioned from boundsLookup
// geometry in _tvUpdateMarker — NOT from alphaTab's internal cursor.
const hl = document.createElement('div');
hl.id = 'tabview-marker-' + _instanceId;
hl.className = 'tabview-marker';
hl.style.cssText = [
'position:absolute',
'left:0',
'top:0',
'width:0',
'height:0',
'background:rgba(34,211,238,0.16)',
'border-left:2px solid rgba(34,211,238,0.95)',
'box-shadow:0 0 8px rgba(34,211,238,0.55)',
'pointer-events:none',
'z-index:999',
'display:none',
].join(';');
c.appendChild(hl);
mount.appendChild(c);
_tvContainer = c;
_tvAtMount = inner;
_tvHighlight = hl;
// Re-inset on content reflow of #player-controls (e.g. flex-wrap
// promotes the controls to a second row at narrow widths).
if (!_ssActive() && typeof ResizeObserver !== 'undefined') {
const controls = document.getElementById('player-controls');
if (controls) {
_tvControlsObserver = new ResizeObserver(() => _tvSizeContainer());
_tvControlsObserver.observe(controls);
}
}
return c;
}
function _tvSizeContainer() {
if (!_tvContainer) return;
const mount = _resolveMount(_tvHighwayCanvas);
if (!mount) return;
// Splitscreen: fill the panel chrome top-to-bottom (the panel bar
// layers on top via z-index). Main-player: clear #player-hud at
// the top and #player-controls at the bottom (slopsmith#336 —
// the previous code reserved the wrong edge, hiding the last
// tab row behind the controls bar). Measure dynamically so the
// controls' flex-wrap to a second row at narrow widths still
// leaves the last row visible. Fallbacks match the historical
// 60px top assumption + a single-row controls bar.
let topInset = 0;
let bottomInset = 0;
if (!_ssActive()) {
const hud = document.getElementById('player-hud');
const controls = document.getElementById('player-controls');
topInset = (hud && hud.offsetHeight) || 60;
bottomInset = (controls && controls.offsetHeight) || 48;
}
_tvContainer.style.top = topInset + 'px';
_tvContainer.style.height = Math.max(0, mount.clientHeight - topInset - bottomInset) + 'px';
// After a resize alphaTab re-lays-out and rebuilds boundsLookup,
// so the marker's geometry changes even at the same tick. Re-place
// it from the last known beat; _tvSyncCursor skips redundant
// same-tick updates, so resize has to drive this itself.
_tvUpdateMarker();
}
function _tvRemoveContainer() {
if (_tvControlsObserver) {
try { _tvControlsObserver.disconnect(); } catch (_) {}
_tvControlsObserver = null;
}
if (_tvContainer) {
// Restore mount's position style if we changed it in _tvCreateContainer().
if (_tvPrevMountPosition !== null) {
const mount = _tvContainer.parentElement;
if (mount) mount.style.position = _tvPrevMountPosition;
_tvPrevMountPosition = null;
}
_tvContainer.remove();
_tvContainer = null;
_tvAtMount = null;
_tvHighlight = null;
}
}
// ── Error banner ────────────────────────────────────────────────
//
// When the GP5 fetch or alphaTab render fails, we hide the tabview
// container so the 2D highway stays visible. That alone leaves the
// failure silent to anyone who can't open devtools. A small,
// auto-dismissing banner anchored to this instance's mount surfaces
// the error without covering the highway — living OUTSIDE the
// tabview container so it coexists with the fallback renderer
// instead of occluding it.
function _tvShowErrorBanner(message) {
_tvRemoveErrorBanner();
const mount = _resolveMount(_tvHighwayCanvas);
if (!mount) return;
const banner = document.createElement('div');
banner.id = 'tabview-error-banner-' + _instanceId;
banner.className = 'tabview-error-banner';
banner.dataset.tabviewInstance = String(_instanceId);
banner.setAttribute('role', 'alert');
banner.style.cssText = [
'position:absolute',
'top:10px',
'left:50%',
'transform:translateX(-50%)',
'background:rgba(220,80,80,0.94)',
'color:#fff',
'padding:8px 16px',
'border-radius:8px',
'z-index:30',
'font-size:12px',
'font-family:system-ui,sans-serif',
'max-width:80%',
'box-shadow:0 2px 8px rgba(0,0,0,0.3)',
'pointer-events:none',
].join(';');
banner.textContent = 'Tab View: ' + (message || 'failed to load');
mount.appendChild(banner);
_tvErrorBanner = banner;
_tvErrorBannerTimeout = setTimeout(_tvRemoveErrorBanner, 6000);
}
function _tvRemoveErrorBanner() {
if (_tvErrorBanner) {
_tvErrorBanner.remove();
_tvErrorBanner = null;
}
if (_tvErrorBannerTimeout) {
clearTimeout(_tvErrorBannerTimeout);
_tvErrorBannerTimeout = null;
}
}
// ── alphaTab init ───────────────────────────────────────────────
async function _tvInitAlphaTab(arrayBuffer, myToken) {
const c = _tvCreateContainer();
if (!c) return;
// Destroy previous API before re-init so scoreLoaded / error
// handlers from the old lifetime don't fire into stale DOM.
if (_tvApi) {
try { _tvApi.destroy(); } catch (_) {}
_tvApi = null;
}
_tvReady = false;
_tvAtBeats = [];
_tvLastBeat = null;
if (_tvAtMount) _tvAtMount.innerHTML = '';
_tvApi = new alphaTab.AlphaTabApi(_tvAtMount, {
core: {
fontDirectory: ALPHATAB_CDN_BASE + '/font/',
// Build the bounds lookup during layout so we can map a
// beat → rendered pixel geometry for our own marker
// (slopsmith#734). Without this api.boundsLookup is null.
includeNoteBounds: true,
},
display: {
layoutMode: alphaTab.LayoutMode.Page,
scale: 0.9,
},
player: {
// No alphaTab synth: slopsmith owns audio. Disabling the
// player drops the soundfont CDN download entirely and,
// crucially, removes the player-ready dependency that the
// old .at-cursor-bar marker relied on (slopsmith#734).
enablePlayer: false,
},
});
// On load, flatten the score into a tick-sorted beat timeline so
// _tvSyncCursor can resolve the current playback tick → Beat →
// boundsLookup geometry. Single track (rs2gp emits one).
_tvApi.scoreLoaded.on(function (score) {
if (_tvInitToken !== myToken) return;
_tvAtBeats = _tvBuildBeatTimeline(score);
_tvLastBeat = null;
});
_tvApi.renderFinished.on(function () {
if (_tvInitToken !== myToken) return;
_tvReady = true;
// Start the self-driven cursor loop here (not in init()): _tvReady
// is only true once the score has rendered, so starting earlier
// just idle-spins for the whole async GP5 fetch. Idempotent, so
// the resize-driven re-fires of renderFinished are harmless.
_tvStartCursorLoop();
// Swap visibility only once alphaTab has actually produced
// output. _tvApi.load() kicks off rendering synchronously
// but the first frame lands several rAFs later; if we hid
// the highway in _tvFetchAndInit right after load() returned
// (the previous behaviour) the player flashed blank for
// the duration of the render, or stayed blank forever if
// renderFinished never fired. Doing it here guarantees a
// painted-to-painted handoff and lets the error path below
// fall back to the still-visible 2D highway.
if (_tvContainer) _tvContainer.style.visibility = '';
if (_tvHighwayCanvas) _tvHighwayCanvas.style.visibility = 'hidden';
_tvSetHighwayVisible(false);
_tvFailedFile = null;
_tvFailedArr = null;
// A successful render supersedes any prior error banner.
_tvRemoveErrorBanner();
// renderFinished fires after EVERY (re)layout, including a
// resize-driven re-render. boundsLookup is freshly valid at
// this point, so re-place the marker from the last known beat:
// a width-change resize transiently nulls boundsLookup, and
// _tvSizeContainer's _tvUpdateMarker() call mid-relayout hides
// the marker — without this it would stay hidden while paused
// (or until _tvSyncCursor's tick advances >30) (slopsmith#734).
// No-op on the first render (_tvLastBeat is null → marker hidden).
_tvUpdateMarker();
});
_tvApi.error.on(function (e) {
if (_tvInitToken !== myToken) return;
console.error('[TabView] alphaTab error:', e);
// Render or parse error after GP5 fetch succeeded: tabview
// can't display anything for this target. Mark it failed so
// draw()'s change-detection doesn't re-fetch on every rAF,
// hide our (possibly empty) overlay, and restore highway
// visibility so the player isn't stranded blank. Use
// _tvCurrentFile/Arr if set (post-fetch) else fall back to
// the in-flight _tvLoadingFile/Arr so we always remember
// what went wrong.
const failedFile = _tvCurrentFile || _tvLoadingFile;
const failedArr = _tvCurrentArr != null ? _tvCurrentArr : _tvLoadingArr;
_tvReady = false;
_tvCurrentFile = null;
_tvCurrentArr = null;
if (failedFile != null) {
_tvFailedFile = failedFile;
_tvFailedArr = failedArr;
}
if (_tvContainer) _tvContainer.style.visibility = 'hidden';
if (_tvHighwayCanvas) _tvHighwayCanvas.style.visibility = _tvPrevVisibility || '';
_tvSetHighwayVisible(null);
const msg = (e && e.message) ? e.message : (typeof e === 'string' ? e : 'render failed');
_tvShowErrorBanner(msg);
});
_tvApi.load(new Uint8Array(arrayBuffer));
}
async function _tvFetchAndInit(filename, arrIdx, myToken) {
if (!filename) {
console.warn('[TabView] no filename known yet; skipping fetch');
return;
}
// Mount-availability guard. In splitscreen the panel chrome
// can be null transiently (panel mid-creation, screen
// transitions) — _resolveMount returns null in that case.
// Bail BEFORE setting _tvLoading* / hitting the network so
// draw()'s change-detect doesn't treat us as in-flight, and
// so we don't spam the GP5 endpoint with fetches that would
// immediately discard their results because _tvCreateContainer
// returns null too. The next draw() retries cleanly once the
// panel chrome resolves; load state stays "needs fetch" via
// _tvCurrentFile/_tvCurrentArr remaining unset.
if (!_resolveMount(_tvHighwayCanvas)) {
return;
}
_tvLoadingFile = filename;
_tvLoadingArr = arrIdx;
try {
await _tvLoadScript();
if (_tvInitToken !== myToken) return;
// Decode first — filename may already be URI-encoded from
// the data-play attribute — then re-encode for the request
// path. decodeURIComponent throws URIError on stray % or
// bare `%xx` where xx isn't valid hex; fall back to the raw
// filename so a rare encoding edge case doesn't land in the
// (_tvFailedFile, _tvFailedArr) cache and permanently block
// retries for that song / arrangement.
let decoded = filename;
try {
decoded = decodeURIComponent(filename);
} catch (e) {
console.warn('[TabView] decodeURIComponent failed; using raw filename:', filename, e);
}
const url = '/api/plugins/tabview/gp5/' + encodeURIComponent(decoded) +
'?arrangement=' + arrIdx;
const resp = await fetch(url);
if (_tvInitToken !== myToken) return;
if (!resp.ok) throw new Error(await resp.text());
const data = await resp.arrayBuffer();
if (_tvInitToken !== myToken) return;
// _tvCreateContainer returns null when the mount target
// isn't in the DOM (player screen closed, unusual timing
// during screen transitions). Without this guard the next
// line's _tvContainer.style.visibility = '' would throw on
// null and the failure path below would cache this as a
// permanent failure for the song, even though the real
// issue is transient DOM state.
const container = _tvCreateContainer();
if (!container) {
console.warn('[TabView] mount container missing; leaving highway visible');
if (_tvHighwayCanvas) _tvHighwayCanvas.style.visibility = _tvPrevVisibility || '';
_tvSetHighwayVisible(null);
return;
}
_tvSizeContainer();
await _tvInitAlphaTab(data, myToken);
if (_tvInitToken !== myToken) return;
_tvCurrentFile = filename;
_tvCurrentArr = arrIdx;
// DO NOT show the container or hide the highway here:
// _tvApi.load() inside _tvInitAlphaTab kicks off rendering
// but resolves before the first frame is painted, so doing
// the visibility swap at this point would flash the player
// blank during the render setup (or forever if render never
// completes). The renderFinished handler inside
// _tvInitAlphaTab takes over: on success it swaps in the
// overlay, on error it keeps the highway visible.
} catch (e) {
if (_tvInitToken !== myToken) return;
console.error('[TabView] GP5 fetch/init failed:', e);
_tvFailedFile = filename;
_tvFailedArr = arrIdx;
// Hide any stale tab overlay (either a prior successful load
// that's being reloaded into a failing song, or the freshly
// created empty container from an initial failed load) so
// the highway fallback actually becomes visible.
if (_tvContainer) _tvContainer.style.visibility = 'hidden';
if (_tvHighwayCanvas) _tvHighwayCanvas.style.visibility = _tvPrevVisibility || '';
_tvSetHighwayVisible(null);
const msg = (e && e.message) ? e.message : String(e);
console.warn('[TabView] ' + msg);
_tvShowErrorBanner(msg);
} finally {
// Only clear the loading-target if this fetch is still the
// latest in-flight one — a newer token bump already cleared /
// re-set these fields for a subsequent fetch.
if (_tvInitToken === myToken) {
_tvLoadingFile = null;
_tvLoadingArr = null;
}
}
}
// ── Beat timeline (tick → Beat) ─────────────────────────────────
// Flatten the loaded score into a tick-sorted [{ beat, start }] list.
// `start` is absoluteDisplayStart in 960-ppq MIDI ticks (bar 0 beat 0
// == tick 0). One track only (rs2gp emits a single guitar/bass track);
// voice 0 carries the notes. Returns [] on any unexpected shape so the
// marker simply stays hidden rather than throwing on the rAF path.
function _tvBuildBeatTimeline(score) {
const out = [];
try {
const track = score && score.tracks && score.tracks[0];
const staff = track && track.staves && track.staves[0];
const bars = staff && staff.bars;
if (!bars) return out;
for (let i = 0; i < bars.length; i++) {
const voices = bars[i].voices || [];
const voice = voices[0];
const beats = voice && voice.beats;
if (!beats) continue;
for (let j = 0; j < beats.length; j++) {
const b = beats[j];
const start = (typeof b.absoluteDisplayStart === 'number')
? b.absoluteDisplayStart
: b.absolutePlaybackStart;
if (typeof start === 'number') out.push({ beat: b, start: start });
}
}
} catch (_) { /* malformed score → empty timeline → marker hidden */ }
out.sort(function (a, b) { return a.start - b.start; });
return out;
}
// Greatest beat whose start <= tick (the beat currently sounding).
// Binary search — runs once per advanced tick, per instance.
function _tvFindBeatAtTick(tick) {
const arr = _tvAtBeats;
if (!arr || arr.length === 0) return null;
if (tick < arr[0].start) return arr[0].beat;
let lo = 0, hi = arr.length - 1, ans = 0;
while (lo <= hi) {
const mid = (lo + hi) >> 1;
if (arr[mid].start <= tick) { ans = mid; lo = mid + 1; }
else { hi = mid - 1; }
}
return arr[ans].beat;
}
// ── Cursor sync ─────────────────────────────────────────────────
function _tvSyncCursor(currentTime) {
if (!_tvApi || !_tvReady) return;
const tick = _tvTimeToTick(currentTime, _tvLatestBeats);
// Skip the (relatively expensive) beat lookup + marker reposition
// when the tick hasn't advanced — at 60fps × N splitscreen
// instances that's meaningful cost for state that doesn't change
// between frames. Resize-driven movement still re-places the
// marker via _onWinResize → _tvSizeContainer → _tvUpdateMarker.
if (Math.abs(tick - _tvLastTick) <= 30) return;
_tvLastTick = tick;
// _tvTimeToTick adds a one-beat baseline because rs2gp starts the
// first measure at tick TICKS_PER_BEAT; alphaTab's
// absoluteDisplayStart is 0-based, so subtract it back out before
// looking the beat up. Floor at 0 for the very first beat.
const lookupTick = Math.max(0, tick - TICKS_PER_BEAT);
_tvLastBeat = _tvFindBeatAtTick(lookupTick);
_tvUpdateMarker();
}
// ── Self-driven cursor loop (slopsmith#734 follow-up) ────────────
//
// Single-player Tab View hides the highway via setVisible(false) so the
// occluded underlying renderer stops burning GPU behind the opaque tab
// (slopsmith#654). But that same flag gates the host's per-frame draw
// pump (`highway.js`: `if (!_lastVisible) return` *before* it calls the
// active renderer's draw(bundle)) — and we ARE the active renderer. So
// our draw(bundle) stopped being called the instant the first render
// finished, and the boundsLookup marker silently froze / never appeared.
//
// Fix: advance the marker from our own requestAnimationFrame loop,
// reading the clock + beats straight off window.highway, so the cursor
// no longer depends on whether the host pumps draw().
//
// Time source is getTime() (chartTime — the AUDIO-aligned clock), NOT
// bundle.currentTime. bundle.currentTime is the *render* clock
// (chartTime + avOffset); on stem songs avOffset is non-zero (e.g.
// −215 ms), which dragged the marker onto the previous note. getTime()
// is exactly the audio position, so the marker sits on the note you hear.
//
// Splitscreen still rides the per-panel draw(bundle) path: setVisible
// (false) is skipped there (so draw() keeps flowing), and window.highway
// is the *main* player's instance — wrong clock/beats for a panel — so
// the loop bows out when _ssActive().
function _tvCursorLoop() {
_tvCursorRAF = window.requestAnimationFrame(_tvCursorLoop);
if (!_isReady || !_tvReady) return;
if (_ssActive()) return;
const hw = window.highway;
if (!hw || typeof hw.getTime !== 'function') return;
if (typeof hw.getBeats === 'function') {
const b = hw.getBeats();
if (b) _tvLatestBeats = b;
}
// getTime() can return NaN in transient states (pre-anchor boot,
// mid-seek flush — see highway.js). Feeding NaN to _tvSyncCursor
// resolves to lookupTick 0 and snaps the marker back to beat 0, so
// skip the frame instead.
const t = hw.getTime();
if (t == null || !isFinite(t)) return;
_tvSyncCursor(t);
}
function _tvStartCursorLoop() {
if (_tvCursorRAF != null) return;
_tvCursorRAF = window.requestAnimationFrame(_tvCursorLoop);
}
function _tvStopCursorLoop() {
if (_tvCursorRAF != null) {
window.cancelAnimationFrame(_tvCursorRAF);
_tvCursorRAF = null;
}
}
// ── Playback marker (boundsLookup-driven, slopsmith#734) ─────────
function _tvUpdateMarker() {
if (!_tvHighlight || !_tvContainer || !_tvAtMount) return;
if (!_tvLastBeat) { _tvHighlight.style.display = 'none'; return; }
// boundsLookup is rebuilt on every (re)layout; it can be briefly
// null between a resize and the next renderFinished. Bail quietly.
const bl = _tvApi && _tvApi.boundsLookup;
const bb = bl ? bl.findBeat(_tvLastBeat) : null;
const vb = bb && bb.visualBounds;
if (!vb) { _tvHighlight.style.display = 'none'; return; }
// visualBounds are in the rendered surface's coordinate space,
// whose origin is _tvAtMount's content box. Both the marker and
// _tvAtMount are absolutely positioned children of the scrolling
// _tvContainer, so they share its scrolled space — add _tvAtMount's
// offset, no scroll math needed. The translucent band spans the
// beat's width; the bright left border (static CSS) reads as the
// playhead at the beat's leading edge.
const baseX = _tvAtMount.offsetLeft;
const baseY = _tvAtMount.offsetTop;
const left = Math.round(baseX + vb.x);
const top = Math.round(baseY + vb.y);
const width = Math.max(2, Math.round(vb.w));
const height = Math.max(8, Math.round(vb.h));
_tvHighlight.style.left = left + 'px';
_tvHighlight.style.top = top + 'px';
_tvHighlight.style.width = width + 'px';
_tvHighlight.style.height = height + 'px';
_tvHighlight.style.display = '';
// Auto-advance: scroll to keep the marker comfortably in view.
const viewW = _tvContainer.clientWidth;
const viewH = _tvContainer.clientHeight;
const paddingX = Math.min(180, viewW * 0.3);
const paddingY = Math.min(100, viewH * 0.25);
const relX = left - _tvContainer.scrollLeft;
const relY = top - _tvContainer.scrollTop;
let needScroll = false;
let targetX = _tvContainer.scrollLeft;
let targetY = _tvContainer.scrollTop;
if (relX < paddingX || relX > viewW - paddingX) {
targetX = left - viewW / 2;
needScroll = true;
}
if (relY < paddingY || relY > viewH - paddingY) {
targetY = top - viewH / 2;
needScroll = true;
}
if (needScroll) {
_tvContainer.scrollTo({
left: Math.max(0, targetX),
top: Math.max(0, targetY),
behavior: 'auto',
});
}
}
// ── Teardown ────────────────────────────────────────────────────
function _teardown(restoreCanvas) {
_tvStopCursorLoop();
_tvReady = false;
_tvLastTick = -1;
_tvCurrentFile = null;
_tvCurrentArr = null;
_tvLoadingFile = null;
_tvLoadingArr = null;
_tvFailedFile = null;
_tvFailedArr = null;
_tvLatestBeats = null;
_tvAtBeats = [];
_tvLastBeat = null;
if (_tvApi) {
try { _tvApi.destroy(); } catch (_) {}
_tvApi = null;
}
_tvRemoveContainer();
_tvRemoveErrorBanner();
if (restoreCanvas && _tvHighwayCanvas) {
_tvHighwayCanvas.style.visibility = _tvPrevVisibility;
_tvSetHighwayVisible(null);
_tvHighwayCanvas = null;
_tvPrevVisibility = '';
}
}
// ── Factory return: setRenderer contract ────────────────────────
return {
init(canvas, bundle) {
// Always run teardown at init start, even when there's
// no visible container/API to tear down. A previous
// activation that failed BEFORE alphaTab initialised
// (e.g. CDN load error, fetch error pre-container) would
// otherwise leak _tvFailedFile / _tvFailedArr into this
// lifetime — the new fetch would hit the previouslyFailed
// guard in draw() and silently skip, so re-picking Tab
// View would appear to do nothing.
//
// restoreCanvas=true (not false) is critical here: a
// prior successful render hid the highway canvas via
// renderFinished, and skipping the restore would leave
// the canvas at visibility:hidden when the new init
// captures _tvPrevVisibility below — so a subsequent
// failed fetch / destroy would "restore" the canvas to
// hidden and strand the player blank. The
// _tvHighwayCanvas reference is also nulled by the
// restore branch, freeing the new init() to install
// the freshly-passed canvas without aliasing.
_teardown(/* restoreCanvas */ true);
window.removeEventListener('resize', _onWinResize);
const myToken = ++_tvInitToken;
_tvHighwayCanvas = canvas;
_tvPrevVisibility = canvas ? canvas.style.visibility : '';
// DON'T hide the 2D highway yet — if GP5 fetch, CDN load,
// or alphaTab init fails (missing filename, server down,
// network error), we want the default visible as a
// fallback so the player isn't stranded blank. The hide
// happens inside renderFinished on success, and a failed
// fetch restores _tvPrevVisibility explicitly.
_tvLastTick = -1;
window.addEventListener('resize', _onWinResize);
const songInfo = (bundle && bundle.songInfo) || {};
const filename = (typeof songInfo.filename === 'string' && songInfo.filename)
|| _tvFilename;
const arrIdx = Number.isInteger(songInfo.arrangement_index)
? songInfo.arrangement_index : 0;
_tvFetchAndInit(filename, arrIdx, myToken);
_isReady = true;
// The self-driven cursor loop (the marker can't rely on the host
// draw() pump once the highway is hidden — slopsmith#654 gate) is
// started from renderFinished, once _tvReady is true, so it
// doesn't idle-spin through the async GP5 fetch.
},
draw(bundle) {
if (!_isReady || !bundle) return;
// Cache beats per frame so cursor sync uses the
// filter-aware beats from THIS instance's bundle, not
// the main-player's `highway` global (which under
// splitscreen belongs to the hidden default highway and
// wouldn't reflect this panel's arrangement).
_tvLatestBeats = bundle.beats || null;
// Detect arrangement / song change: re-fetch GP5 when the
// active (filename, arrangement_index) differs from the
// one the currently-displayed score was loaded for. Guard
// against per-frame retry loops — while a fetch is in
// flight for the same target, skip. draw() runs every rAF
// and a typical fetch takes well over one frame; without
// this check we'd spam the endpoint and keep bumping the
// init token, invalidating each request before it lands.
//
// Prefer bundle.songInfo.filename when present and fall
// back to the _tvFilename cache from our playSong wrap.
// slopsmith core doesn't expose filename in song_info
// today, but routing through bundle first means we pick