@dsebastien/music-notation 0.19.2 → 0.21.0

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/dist/tab/index.js CHANGED
@@ -1801,9 +1801,11 @@ var ICONS = {
1801
1801
  loop: "M3 8a5 5 0 0 1 8.5-3.5L13 6V2.5M13 8a5 5 0 0 1-8.5 3.5L3 10v3.5",
1802
1802
  count: "M5.5 14h5L9 2.5H7zM8 11.5l4.5-7",
1803
1803
  click: "M3 5v6M8 5v6M13 5v6",
1804
- copy: "M2.5 3.5h11v9h-11zM2.5 11l3.5-3.5 3 3 2-2 2.5 2.5M10.5 6.5h.01"
1804
+ copy: "M2.5 3.5h11v9h-11zM2.5 11l3.5-3.5 3 3 2-2 2.5 2.5M10.5 6.5h.01",
1805
+ full: "M2.5 6V2.5H6M10 2.5h3.5V6M13.5 10v3.5H10M6 13.5H2.5V10",
1806
+ exit: "M6 2.5V6H2.5M13.5 6H10V2.5M10 13.5V10h3.5M2.5 10H6v3.5"
1805
1807
  };
1806
- var STROKED = new Set(["loop", "count", "click", "copy"]);
1808
+ var STROKED = new Set(["loop", "count", "click", "copy", "full", "exit"]);
1807
1809
  function icon(doc, name) {
1808
1810
  const root = svg(doc, "svg", {
1809
1811
  class: `mn-play-icon mn-play-icon-${name}${STROKED.has(name) ? " mn-play-icon-line" : ""}`,
@@ -1937,6 +1939,374 @@ function followVolume(read, handles, start) {
1937
1939
  listeners.delete(listener);
1938
1940
  };
1939
1941
  }
1942
+
1943
+ // src/playback/full-screen.ts
1944
+ var SCROLL_KEYS = new Set(["PageUp", "PageDown", "Home", "End", "ArrowUp", "ArrowDown"]);
1945
+ function backgroundBehind(el) {
1946
+ const win = el.ownerDocument.defaultView;
1947
+ if (!win)
1948
+ return "";
1949
+ for (let node = el.parentElement;node; node = node.parentElement) {
1950
+ const color = win.getComputedStyle(node).backgroundColor;
1951
+ if (color && color !== "transparent" && !/^rgba\(.*,\s*0\)$/.test(color))
1952
+ return color;
1953
+ }
1954
+ return "";
1955
+ }
1956
+ function fullScreenControl(doc, block, surface, follow = () => {}, options = {}) {
1957
+ const redraws = options.redraws !== false;
1958
+ const win = doc.defaultView;
1959
+ const button = iconButton(doc, "mn-tab-full", "Full screen", "full");
1960
+ button.classList.add("mn-play-full");
1961
+ let on = false;
1962
+ let browser = false;
1963
+ let following = false;
1964
+ let before = null;
1965
+ let place = null;
1966
+ let held = false;
1967
+ let ownMinHeight = "";
1968
+ const setFollow = (yes) => {
1969
+ if (following === yes)
1970
+ return;
1971
+ following = yes;
1972
+ follow(yes);
1973
+ };
1974
+ const show = () => {
1975
+ block.classList.toggle("mn-fullscreen", on);
1976
+ button.classList.toggle("mn-on", on);
1977
+ button.setAttribute("aria-pressed", String(on));
1978
+ button.title = on ? "Exit full screen" : "Full screen";
1979
+ button.replaceChildren(icon(doc, on ? "exit" : "full"));
1980
+ };
1981
+ const enter = () => {
1982
+ if (on)
1983
+ return;
1984
+ on = true;
1985
+ before = {
1986
+ x: win?.scrollX ?? 0,
1987
+ y: win?.scrollY ?? 0,
1988
+ background: block.style.backgroundColor
1989
+ };
1990
+ if (!held)
1991
+ ownMinHeight = block.style.minHeight;
1992
+ held = false;
1993
+ place = doc.createElement("div");
1994
+ place.className = "mn-fullscreen-place";
1995
+ place.style.height = `${block.offsetHeight}px`;
1996
+ const margins = win?.getComputedStyle(block);
1997
+ if (margins) {
1998
+ place.style.marginTop = margins.marginTop;
1999
+ place.style.marginBottom = margins.marginBottom;
2000
+ }
2001
+ block.before(place);
2002
+ const background = backgroundBehind(block);
2003
+ if (background)
2004
+ block.style.backgroundColor = background;
2005
+ show();
2006
+ surface.tabIndex = 0;
2007
+ surface.focus({ preventScroll: true });
2008
+ setFollow(true);
2009
+ options.changed?.(true);
2010
+ try {
2011
+ block.requestFullscreen?.().catch(() => {});
2012
+ } catch {}
2013
+ };
2014
+ const leave = (focus = true) => {
2015
+ if (!on)
2016
+ return;
2017
+ on = false;
2018
+ setFollow(false);
2019
+ show();
2020
+ block.style.backgroundColor = before?.background ?? "";
2021
+ if (place) {
2022
+ if (redraws) {
2023
+ block.style.minHeight = place.style.height;
2024
+ held = true;
2025
+ }
2026
+ place.remove();
2027
+ place = null;
2028
+ }
2029
+ surface.removeAttribute("tabindex");
2030
+ surface.scrollTop = 0;
2031
+ if (browser && doc.fullscreenElement === block)
2032
+ doc.exitFullscreen?.().catch(() => {});
2033
+ browser = false;
2034
+ if (before && win)
2035
+ win.scrollTo(before.x, before.y);
2036
+ before = null;
2037
+ options.changed?.(false);
2038
+ if (focus)
2039
+ button.focus({ preventScroll: true });
2040
+ };
2041
+ button.addEventListener("click", () => on ? leave() : enter());
2042
+ block.addEventListener("keydown", (event) => {
2043
+ if (!on || event.key !== "Escape")
2044
+ return;
2045
+ event.stopPropagation();
2046
+ event.preventDefault();
2047
+ leave();
2048
+ });
2049
+ const byHand = () => {
2050
+ if (on)
2051
+ setFollow(false);
2052
+ };
2053
+ surface.addEventListener("wheel", byHand, { passive: true });
2054
+ surface.addEventListener("touchmove", byHand, { passive: true });
2055
+ surface.addEventListener("keydown", (event) => {
2056
+ if (SCROLL_KEYS.has(event.key))
2057
+ byHand();
2058
+ });
2059
+ const space = (event) => {
2060
+ if (!on || event.key !== " " || event.ctrlKey || event.metaKey || event.altKey)
2061
+ return;
2062
+ if (!block.isConnected) {
2063
+ leave(false);
2064
+ return;
2065
+ }
2066
+ const target = event.target;
2067
+ if (target?.closest("input:not([type=range]), textarea, select, [contenteditable=true]"))
2068
+ return;
2069
+ event.preventDefault();
2070
+ event.stopPropagation();
2071
+ if (event.type !== "keydown" || event.repeat)
2072
+ return;
2073
+ block.querySelector(".mn-play .mn-play-toggle")?.click();
2074
+ };
2075
+ doc.addEventListener("keydown", space, true);
2076
+ doc.addEventListener("keyup", space, true);
2077
+ const changed = () => {
2078
+ if (doc.fullscreenElement === block)
2079
+ browser = true;
2080
+ else if (browser)
2081
+ leave();
2082
+ };
2083
+ doc.addEventListener("fullscreenchange", changed);
2084
+ show();
2085
+ return {
2086
+ button,
2087
+ active: () => on,
2088
+ playing: () => {
2089
+ if (on)
2090
+ setFollow(true);
2091
+ },
2092
+ drawn: () => {
2093
+ if (on || !held)
2094
+ return;
2095
+ held = false;
2096
+ block.style.minHeight = ownMinHeight;
2097
+ },
2098
+ destroy: () => {
2099
+ leave(false);
2100
+ doc.removeEventListener("fullscreenchange", changed);
2101
+ doc.removeEventListener("keydown", space, true);
2102
+ doc.removeEventListener("keyup", space, true);
2103
+ }
2104
+ };
2105
+ }
2106
+ var ZOOMS = [4, 3.5, 3, 2.5, 2.25, 2, 1.75, 1.5, 1.25, 1];
2107
+ var TALL_ZOOM = 1.5;
2108
+ function chooseZoom(fits) {
2109
+ const wide = [];
2110
+ for (const zoom of ZOOMS) {
2111
+ const fit = fits(zoom);
2112
+ if (fit.width && fit.height)
2113
+ return zoom;
2114
+ if (fit.width)
2115
+ wide.push(zoom);
2116
+ }
2117
+ return wide.find((zoom) => zoom <= TALL_ZOOM) ?? 1;
2118
+ }
2119
+ function blockFullScreen(doc, block) {
2120
+ const win = doc.defaultView;
2121
+ let frame = 0;
2122
+ const fit = () => {
2123
+ frame = 0;
2124
+ if (!block.classList.contains("mn-fullscreen"))
2125
+ return;
2126
+ const zoom = chooseZoom((z) => {
2127
+ block.style.setProperty("--mn-zoom", String(z));
2128
+ return {
2129
+ width: block.scrollWidth <= block.clientWidth + 1,
2130
+ height: block.scrollHeight <= block.clientHeight + 1
2131
+ };
2132
+ });
2133
+ block.style.setProperty("--mn-zoom", String(zoom));
2134
+ for (const bar of nested())
2135
+ bar.style.setProperty("zoom", String(1 / zoom));
2136
+ };
2137
+ const nested = () => [
2138
+ ...block.querySelectorAll(":scope > :not(.mn-play) .mn-play")
2139
+ ];
2140
+ const later = () => {
2141
+ if (!win || frame)
2142
+ return;
2143
+ frame = win.requestAnimationFrame(fit);
2144
+ };
2145
+ const control = fullScreenControl(doc, block, block, undefined, {
2146
+ redraws: false,
2147
+ changed: (on) => {
2148
+ block.classList.toggle("mn-fullscreen-fit", on);
2149
+ if (on) {
2150
+ fit();
2151
+ later();
2152
+ win?.addEventListener("resize", later);
2153
+ } else {
2154
+ win?.removeEventListener("resize", later);
2155
+ block.style.removeProperty("--mn-zoom");
2156
+ for (const bar of nested())
2157
+ bar.style.removeProperty("zoom");
2158
+ }
2159
+ }
2160
+ });
2161
+ return {
2162
+ ...control,
2163
+ destroy: () => {
2164
+ control.destroy();
2165
+ win?.removeEventListener("resize", later);
2166
+ if (frame)
2167
+ win?.cancelAnimationFrame(frame);
2168
+ }
2169
+ };
2170
+ }
2171
+ function addFullScreen(doc, bar, block, on = true) {
2172
+ if (!on || !block || block.querySelector(".mn-play-full"))
2173
+ return () => {};
2174
+ const full = blockFullScreen(doc, block);
2175
+ bar.appendChild(full.button);
2176
+ return () => full.destroy();
2177
+ }
2178
+
2179
+ // src/tab/score-fit.ts
2180
+ var SCORE_SCALES = [1, 1.25, 1.5, 1.75, 2, 2.5, 3];
2181
+ function scoreFit(apply, fits) {
2182
+ let step = null;
2183
+ let back = false;
2184
+ const go = (to) => {
2185
+ step = to;
2186
+ apply(SCORE_SCALES[to], true);
2187
+ };
2188
+ return {
2189
+ enter() {
2190
+ back = false;
2191
+ go(0);
2192
+ },
2193
+ leave() {
2194
+ step = null;
2195
+ back = false;
2196
+ apply(1, false);
2197
+ },
2198
+ drawn() {
2199
+ if (step === null)
2200
+ return;
2201
+ if (back) {
2202
+ step = null;
2203
+ return;
2204
+ }
2205
+ if (fits()) {
2206
+ if (step + 1 < SCORE_SCALES.length)
2207
+ go(step + 1);
2208
+ else
2209
+ step = null;
2210
+ return;
2211
+ }
2212
+ if (step > 0) {
2213
+ back = true;
2214
+ go(step - 1);
2215
+ } else
2216
+ step = null;
2217
+ }
2218
+ };
2219
+ }
2220
+
2221
+ // src/tab/instruments.ts
2222
+ function pageInstruments(options) {
2223
+ if (!options || typeof options !== "object" || !("play" in options))
2224
+ return;
2225
+ const play = options.play;
2226
+ if (!play || typeof play !== "object" || !("instruments" in play))
2227
+ return;
2228
+ const folder = play.instruments;
2229
+ return typeof folder === "string" && folder.trim() ? folder.trim() : undefined;
2230
+ }
2231
+ function soundsOfScore(score) {
2232
+ const programs = new Set;
2233
+ let drums = false;
2234
+ for (const track of score.tracks) {
2235
+ if (track.isPercussion)
2236
+ drums = true;
2237
+ else
2238
+ programs.add(track.playbackInfo.program);
2239
+ }
2240
+ return { programs: [...programs].sort((a, b) => a - b), drums };
2241
+ }
2242
+ function filesOf(index, sounds) {
2243
+ const files = [];
2244
+ for (const program of sounds.programs) {
2245
+ const entry = index.programs[String(program)];
2246
+ if (!entry?.file)
2247
+ return null;
2248
+ files.push(entry.file);
2249
+ }
2250
+ if (sounds.drums) {
2251
+ if (!index.drums?.file)
2252
+ return null;
2253
+ files.push(index.drums.file);
2254
+ } else if (index.click?.file && sounds.click !== false)
2255
+ files.push(index.click.file);
2256
+ return files.length ? files : null;
2257
+ }
2258
+ var soundCaches = new WeakMap;
2259
+ var isIndex = (value) => typeof value === "object" && value !== null && ("programs" in value) && typeof value.programs === "object" && value.programs !== null;
2260
+ async function instrumentSounds(source, sounds, win) {
2261
+ let perWindow = soundCaches.get(win);
2262
+ if (!perWindow)
2263
+ soundCaches.set(win, perWindow = new Map);
2264
+ let cache = perWindow.get(source);
2265
+ if (!cache)
2266
+ perWindow.set(source, cache = { index: null, files: new Map });
2267
+ const read = async (file) => {
2268
+ try {
2269
+ if (typeof source === "function")
2270
+ return await source(file);
2271
+ const response = await win.fetch(`${source.replace(/\/+$/, "")}/${file}`);
2272
+ return response.ok ? new Uint8Array(await response.arrayBuffer()) : null;
2273
+ } catch {
2274
+ return null;
2275
+ }
2276
+ };
2277
+ const fileOf = (file) => {
2278
+ let bytes = cache.files.get(file);
2279
+ if (!bytes) {
2280
+ bytes = read(file);
2281
+ cache.files.set(file, bytes);
2282
+ bytes.then((b) => {
2283
+ if (!b)
2284
+ cache.files.delete(file);
2285
+ });
2286
+ }
2287
+ return bytes;
2288
+ };
2289
+ cache.index ??= read("index.json").then((bytes) => {
2290
+ if (!bytes)
2291
+ return null;
2292
+ try {
2293
+ const value = JSON.parse(new TextDecoder().decode(bytes));
2294
+ return isIndex(value) ? value : null;
2295
+ } catch {
2296
+ return null;
2297
+ }
2298
+ });
2299
+ const index = await cache.index;
2300
+ if (!index) {
2301
+ cache.index = null;
2302
+ return null;
2303
+ }
2304
+ const files = filesOf(index, sounds);
2305
+ if (!files)
2306
+ return null;
2307
+ const all = await Promise.all(files.map(fileOf));
2308
+ return all.every((b) => b !== null) ? all : null;
2309
+ }
1940
2310
  // src/convert/chord-sheet.ts
1941
2311
  function chordsOf(line) {
1942
2312
  const found = [];
@@ -2009,6 +2379,38 @@ var alphaTabCdn = (version = ALPHATAB_VERSION) => {
2009
2379
  soundFont: `${base}/soundfont/sonivox.sf3`
2010
2380
  };
2011
2381
  };
2382
+ async function loadSounds(api, doc, assets, sounds, add = false) {
2383
+ const win = doc.defaultView;
2384
+ const made = local(doc);
2385
+ let files = null;
2386
+ if (assets.instruments && win)
2387
+ files = await instrumentSounds(assets.instruments, sounds, win);
2388
+ const kind = files ? "instruments" : "whole";
2389
+ if (!files) {
2390
+ if (typeof assets.soundFont === "string") {
2391
+ try {
2392
+ const response = await win?.fetch(assets.soundFont);
2393
+ files = response?.ok ? [new Uint8Array(await response.arrayBuffer())] : null;
2394
+ } catch {
2395
+ files = null;
2396
+ }
2397
+ if (!files)
2398
+ throw new Error(`Couldn't load the soundfont (${assets.soundFont})`);
2399
+ } else
2400
+ files = [assets.soundFont];
2401
+ }
2402
+ const all = files;
2403
+ let taken = 0;
2404
+ await new Promise((resolve) => {
2405
+ api.soundFontLoaded.on(() => {
2406
+ if (++taken >= all.length)
2407
+ resolve();
2408
+ });
2409
+ setTimeout(resolve, 8000);
2410
+ all.forEach((bytes, i) => api.loadSoundFont(made.bytes(bytes), add || i > 0));
2411
+ });
2412
+ return kind;
2413
+ }
2012
2414
  var SETTING = /^\s*(staves|file|tracks?)\s*:\s*(.*)$/i;
2013
2415
  function parseTabSource(language, source) {
2014
2416
  const lines = source.split(/\r?\n/);
@@ -2177,19 +2579,42 @@ function playerBar(doc, api, alphaTab, assets, volume) {
2177
2579
  const bar = playBar(doc, "mn-tab-bar", "Playback");
2178
2580
  const state = { loop: false, metronome: false, countIn: false, speed: 1 };
2179
2581
  const level = () => volumeOf(volume);
2582
+ let cant = () => {};
2180
2583
  let started = null;
2181
2584
  let cantStart = null;
2182
2585
  const start = () => {
2183
2586
  started ??= new Promise((resolve) => {
2184
- api.playerReady.on(() => resolve(true));
2185
2587
  cantStart = () => resolve(false);
2186
2588
  api.settings.player.playerMode = alphaTab.PlayerMode.EnabledSynthesizer;
2187
2589
  api.settings.player.enableCursor = true;
2188
- if (typeof assets.soundFont === "string")
2189
- api.settings.player.soundFont = assets.soundFont;
2590
+ if (!assets.instruments) {
2591
+ api.playerReady.on(() => resolve(true));
2592
+ if (typeof assets.soundFont === "string")
2593
+ api.settings.player.soundFont = assets.soundFont;
2594
+ api.updateSettings();
2595
+ if (assets.soundFont instanceof Uint8Array)
2596
+ api.loadSoundFont(local(doc).bytes(assets.soundFont), false);
2597
+ return;
2598
+ }
2599
+ let ready = false;
2600
+ let loaded = false;
2601
+ const settle = () => {
2602
+ if (ready && loaded)
2603
+ resolve(true);
2604
+ };
2605
+ api.playerReady.on(() => {
2606
+ ready = true;
2607
+ settle();
2608
+ });
2190
2609
  api.updateSettings();
2191
- if (assets.soundFont instanceof Uint8Array)
2192
- api.loadSoundFont(local(doc).bytes(assets.soundFont), false);
2610
+ const sounds = api.score ? soundsOfScore(api.score) : { programs: [], drums: false };
2611
+ loadSounds(api, doc, assets, sounds).then(() => {
2612
+ loaded = true;
2613
+ settle();
2614
+ }, (e) => {
2615
+ resolve(false);
2616
+ cant(e instanceof Error ? e.message : String(e));
2617
+ });
2193
2618
  }).then((ok) => {
2194
2619
  if (ok) {
2195
2620
  api.isLooping = state.loop;
@@ -2213,6 +2638,11 @@ function playerBar(doc, api, alphaTab, assets, volume) {
2213
2638
  api.playPause();
2214
2639
  });
2215
2640
  });
2641
+ cant = (message) => {
2642
+ play.disabled = true;
2643
+ play.title = `Can't play: ${message}`;
2644
+ play.setAttribute("aria-label", play.title);
2645
+ };
2216
2646
  const stop = iconButton(doc, "mn-tab-stop", "Stop", "stop");
2217
2647
  stop.addEventListener("click", () => live(() => api.stop()));
2218
2648
  const toggles = () => live(() => {
@@ -2254,9 +2684,7 @@ function playerBar(doc, api, alphaTab, assets, volume) {
2254
2684
  destroy: unfollow,
2255
2685
  failed: (message) => {
2256
2686
  cantStart?.();
2257
- play.disabled = true;
2258
- play.title = `Can't play: ${message}`;
2259
- play.setAttribute("aria-label", play.title);
2687
+ cant(message);
2260
2688
  }
2261
2689
  };
2262
2690
  }
@@ -2373,6 +2801,7 @@ function renderTab(language, source, host, options) {
2373
2801
  };
2374
2802
  const current = new at.AlphaTabApi(surface, settings);
2375
2803
  api = current;
2804
+ current.settings.player.scrollElement = surface;
2376
2805
  if (gone) {
2377
2806
  current.destroy();
2378
2807
  return;
@@ -2406,6 +2835,49 @@ function renderTab(language, source, host, options) {
2406
2835
  chooseStaves(staves, score);
2407
2836
  current.render();
2408
2837
  }, options.onKeepView));
2838
+ if (options.fullScreen !== false) {
2839
+ let page = null;
2840
+ const fit = scoreFit((scale, stretch) => {
2841
+ const display = current.settings.display;
2842
+ if (gone || !display)
2843
+ return;
2844
+ page ??= {
2845
+ scale: display.scale,
2846
+ fill: display.lastSystemFillThreshold
2847
+ };
2848
+ display.scale = stretch ? scale : page.scale;
2849
+ display.lastSystemFillThreshold = stretch ? 0 : page.fill;
2850
+ current.updateSettings();
2851
+ current.render();
2852
+ }, () => surface.scrollHeight <= surface.clientHeight + 1);
2853
+ const full = fullScreenControl(doc, el, surface, (on) => {
2854
+ current.settings.player.scrollMode = on ? at.ScrollMode.OffScreen : at.ScrollMode.Off;
2855
+ current.updateSettings();
2856
+ }, { changed: (on) => on ? fit.enter() : fit.leave() });
2857
+ current.postRenderFinished.on(() => {
2858
+ const area = surface.querySelector(".at-surface");
2859
+ if (area && full.active()) {
2860
+ const bottom = Math.max(0, ...[...area.children].map((part) => {
2861
+ const p = part;
2862
+ return p.offsetTop + p.offsetHeight;
2863
+ }));
2864
+ if (bottom > area.offsetHeight)
2865
+ area.style.height = `${bottom}px`;
2866
+ }
2867
+ fit.drawn();
2868
+ });
2869
+ current.playerStateChanged.on((e) => {
2870
+ if (Number(e.state) === Number(at.synth.PlayerState.Playing))
2871
+ full.playing();
2872
+ });
2873
+ current.renderFinished.on(() => full.drawn());
2874
+ bar.bar.appendChild(full.button);
2875
+ const off = unbar;
2876
+ unbar = () => {
2877
+ full.destroy();
2878
+ off();
2879
+ };
2880
+ }
2409
2881
  el.addEventListener("pointerdown", (event) => {
2410
2882
  if (event.pointerType !== "touch" || bar?.bar.contains(event.target))
2411
2883
  return;
@@ -2519,7 +2991,11 @@ function createSoundfontPlayer(alphaTab, assets, doc, fallback) {
2519
2991
  let api = null;
2520
2992
  let host = null;
2521
2993
  let ready = null;
2522
- const start = () => {
2994
+ const have = new Set;
2995
+ const reading = new Map;
2996
+ let whole = !assets.instruments;
2997
+ const programsOf = (sound) => sound === "guitar" ? [PROGRAMS.guitar, MUTED.program] : [PROGRAMS[sound]];
2998
+ const start = (first = "guitar") => {
2523
2999
  if (ready)
2524
3000
  return ready;
2525
3001
  host = html(doc, "div", "mn-tab-hidden");
@@ -2536,7 +3012,7 @@ function createSoundfontPlayer(alphaTab, assets, doc, fallback) {
2536
3012
  playerMode: alphaTab.PlayerMode.EnabledSynthesizer,
2537
3013
  enableCursor: false,
2538
3014
  scrollMode: alphaTab.ScrollMode.Off,
2539
- ...typeof assets.soundFont === "string" ? { soundFont: assets.soundFont } : {}
3015
+ ...typeof assets.soundFont === "string" && !assets.instruments ? { soundFont: assets.soundFont } : {}
2540
3016
  }
2541
3017
  });
2542
3018
  const current = api;
@@ -2547,25 +3023,57 @@ function createSoundfontPlayer(alphaTab, assets, doc, fallback) {
2547
3023
  if (soundFont && player)
2548
3024
  resolve(true);
2549
3025
  };
2550
- current.soundFontLoaded.on(() => {
3026
+ const loaded = () => {
2551
3027
  soundFont = true;
2552
3028
  settle();
2553
3029
  setTimeout(() => {
2554
3030
  player = true;
2555
3031
  settle();
2556
3032
  }, 4000);
2557
- });
3033
+ };
2558
3034
  current.playerReady?.on(() => {
2559
3035
  player = true;
2560
3036
  settle();
2561
3037
  });
2562
3038
  current.error.on(() => resolve(false));
3039
+ if (assets.instruments)
3040
+ loadSounds(current, doc, assets, {
3041
+ programs: programsOf(first),
3042
+ drums: false
3043
+ }).then((kind) => {
3044
+ if (kind === "whole")
3045
+ whole = true;
3046
+ else
3047
+ have.add(first);
3048
+ loaded();
3049
+ }, () => resolve(false));
3050
+ else
3051
+ current.soundFontLoaded.on(loaded);
2563
3052
  });
2564
- if (assets.soundFont instanceof Uint8Array)
3053
+ if (assets.soundFont instanceof Uint8Array && !assets.instruments)
2565
3054
  current.loadSoundFont(made.bytes(assets.soundFont), false);
2566
3055
  current.tex(`\\track "Guitar" \\instrument ${PROGRAMS.guitar} r \\track "Piano" \\instrument ${PROGRAMS.piano} r \\track "Muted" \\instrument ${MUTED.program} r`);
2567
3056
  return ready;
2568
3057
  };
3058
+ const sounding = async (sound) => {
3059
+ if (!await start(sound))
3060
+ return false;
3061
+ if (whole || have.has(sound) || !api)
3062
+ return true;
3063
+ const current = api;
3064
+ let more = reading.get(sound);
3065
+ if (!more) {
3066
+ more = loadSounds(current, doc, assets, { programs: programsOf(sound), drums: false, click: false }, true).then((kind) => {
3067
+ if (kind === "whole")
3068
+ whole = true;
3069
+ else
3070
+ have.add(sound);
3071
+ return true;
3072
+ }, () => false);
3073
+ reading.set(sound, more);
3074
+ }
3075
+ return more;
3076
+ };
2569
3077
  const inner = soundfontPlayer(alphaTab, () => api);
2570
3078
  return {
2571
3079
  reportsStart: true,
@@ -2573,7 +3081,7 @@ function createSoundfontPlayer(alphaTab, assets, doc, fallback) {
2573
3081
  let handle = null;
2574
3082
  let stopped = false;
2575
3083
  let volume = options.volume;
2576
- start().then((ok) => {
3084
+ sounding(options.sound).then((ok) => {
2577
3085
  if (stopped)
2578
3086
  return;
2579
3087
  try {
@@ -2606,6 +3114,9 @@ function createSoundfontPlayer(alphaTab, assets, doc, fallback) {
2606
3114
  host?.remove();
2607
3115
  api = null;
2608
3116
  ready = null;
3117
+ have.clear();
3118
+ reading.clear();
3119
+ whole = !assets.instruments;
2609
3120
  }
2610
3121
  };
2611
3122
  }
@@ -2640,5 +3151,5 @@ export {
2640
3151
  textTabToAlphaTex
2641
3152
  };
2642
3153
 
2643
- //# debugId=89F5004D5F1EAB8664756E2164756E21
3154
+ //# debugId=74AA16B9663E346964756E2164756E21
2644
3155
  //# sourceMappingURL=index.js.map