@dsebastien/music-notation 0.19.2 → 0.20.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.
@@ -0,0 +1,19 @@
1
+ export interface FullScreen {
2
+ /** The bar's button. */
3
+ button: HTMLButtonElement;
4
+ /** Whether the block fills the screen. */
5
+ active(): boolean;
6
+ /** Tells the control that the music plays: the score follows it again in full screen. */
7
+ playing(): void;
8
+ /** Tells the control that the score was drawn (again): out of full screen, at its own size. */
9
+ drawn(): void;
10
+ /** Leaves full screen and stops listening to the page. */
11
+ destroy(): void;
12
+ }
13
+ /**
14
+ * The full screen control of a tab block. `block` fills the screen and `surface` (the score)
15
+ * becomes its scroll area. `follow(true)` is called when the score may follow the music (in full
16
+ * screen, where only its own area scrolls), `follow(false)` when it must not: out of full screen
17
+ * (the page never moves, #89), and after a scroll by hand, until the music plays again.
18
+ */
19
+ export declare function fullScreenControl(doc: Document, block: HTMLElement, surface: HTMLElement, follow: (on: boolean) => void): FullScreen;
@@ -7,6 +7,7 @@
7
7
  */
8
8
  import type * as AT from '@coderline/alphatab';
9
9
  import type { NotePlayer } from '../playback/player.js';
10
+ import { type InstrumentSource } from './instruments.js';
10
11
  export type AlphaTabModule = typeof AT;
11
12
  /** The alphaTab release these blocks are made for (Publish loads it from jsDelivr). */
12
13
  export declare const ALPHATAB_VERSION = "1.9.0";
@@ -23,6 +24,13 @@ export interface TabAssets {
23
24
  font: string;
24
25
  /** The soundfont: its bytes or a URL. */
25
26
  soundFont: Uint8Array | string;
27
+ /**
28
+ * One soundfont per instrument (#108): the folder made by `scripts/sounds/split-soundfont.ts`
29
+ * (its URL, or a function that reads one of its files). A score, and the diagrams' player,
30
+ * then load only the instruments they play; `soundFont` is what plays when the folder can't
31
+ * be read or lacks one of them.
32
+ */
33
+ instruments?: InstrumentSource;
26
34
  }
27
35
  export interface TabOptions {
28
36
  alphaTab: AlphaTabModule;
@@ -51,6 +59,12 @@ export interface TabOptions {
51
59
  * #85); 0.8 by default. The bar has no volume of its own (#82).
52
60
  */
53
61
  volume?: number | (() => number);
62
+ /**
63
+ * The Full screen button at the end of the player bar (default true; #113): the block fills
64
+ * the screen, the score is drawn again at that width and scrolls in an area of its own, where
65
+ * it follows the music. `false` for an app that has its own view.
66
+ */
67
+ fullScreen?: boolean;
54
68
  }
55
69
  export type Staves = 'tab' | 'score' | 'both';
56
70
  export interface TabSource {
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,234 @@ function followVolume(read, handles, start) {
1937
1939
  listeners.delete(listener);
1938
1940
  };
1939
1941
  }
1942
+
1943
+ // src/tab/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) {
1957
+ const win = doc.defaultView;
1958
+ const button = iconButton(doc, "mn-tab-full", "Full screen", "full");
1959
+ let on = false;
1960
+ let browser = false;
1961
+ let following = false;
1962
+ let before = null;
1963
+ let place = null;
1964
+ let held = false;
1965
+ let ownMinHeight = "";
1966
+ const setFollow = (yes) => {
1967
+ if (following === yes)
1968
+ return;
1969
+ following = yes;
1970
+ follow(yes);
1971
+ };
1972
+ const show = () => {
1973
+ block.classList.toggle("mn-fullscreen", on);
1974
+ button.classList.toggle("mn-on", on);
1975
+ button.setAttribute("aria-pressed", String(on));
1976
+ button.title = on ? "Exit full screen" : "Full screen";
1977
+ button.replaceChildren(icon(doc, on ? "exit" : "full"));
1978
+ };
1979
+ const enter = () => {
1980
+ if (on)
1981
+ return;
1982
+ on = true;
1983
+ before = {
1984
+ x: win?.scrollX ?? 0,
1985
+ y: win?.scrollY ?? 0,
1986
+ background: block.style.backgroundColor
1987
+ };
1988
+ if (!held)
1989
+ ownMinHeight = block.style.minHeight;
1990
+ held = false;
1991
+ place = doc.createElement("div");
1992
+ place.className = "mn-fullscreen-place";
1993
+ place.style.height = `${block.offsetHeight}px`;
1994
+ const margins = win?.getComputedStyle(block);
1995
+ if (margins) {
1996
+ place.style.marginTop = margins.marginTop;
1997
+ place.style.marginBottom = margins.marginBottom;
1998
+ }
1999
+ block.before(place);
2000
+ const background = backgroundBehind(block);
2001
+ if (background)
2002
+ block.style.backgroundColor = background;
2003
+ show();
2004
+ surface.tabIndex = 0;
2005
+ surface.focus({ preventScroll: true });
2006
+ setFollow(true);
2007
+ try {
2008
+ block.requestFullscreen?.().catch(() => {});
2009
+ } catch {}
2010
+ };
2011
+ const leave = (focus = true) => {
2012
+ if (!on)
2013
+ return;
2014
+ on = false;
2015
+ setFollow(false);
2016
+ show();
2017
+ block.style.backgroundColor = before?.background ?? "";
2018
+ if (place) {
2019
+ block.style.minHeight = place.style.height;
2020
+ held = true;
2021
+ place.remove();
2022
+ place = null;
2023
+ }
2024
+ surface.removeAttribute("tabindex");
2025
+ surface.scrollTop = 0;
2026
+ if (browser && doc.fullscreenElement === block)
2027
+ doc.exitFullscreen?.().catch(() => {});
2028
+ browser = false;
2029
+ if (before && win)
2030
+ win.scrollTo(before.x, before.y);
2031
+ before = null;
2032
+ if (focus)
2033
+ button.focus({ preventScroll: true });
2034
+ };
2035
+ button.addEventListener("click", () => on ? leave() : enter());
2036
+ block.addEventListener("keydown", (event) => {
2037
+ if (!on || event.key !== "Escape")
2038
+ return;
2039
+ event.stopPropagation();
2040
+ event.preventDefault();
2041
+ leave();
2042
+ });
2043
+ const byHand = () => {
2044
+ if (on)
2045
+ setFollow(false);
2046
+ };
2047
+ surface.addEventListener("wheel", byHand, { passive: true });
2048
+ surface.addEventListener("touchmove", byHand, { passive: true });
2049
+ surface.addEventListener("keydown", (event) => {
2050
+ if (SCROLL_KEYS.has(event.key))
2051
+ byHand();
2052
+ });
2053
+ const changed = () => {
2054
+ if (doc.fullscreenElement === block)
2055
+ browser = true;
2056
+ else if (browser)
2057
+ leave();
2058
+ };
2059
+ doc.addEventListener("fullscreenchange", changed);
2060
+ show();
2061
+ return {
2062
+ button,
2063
+ active: () => on,
2064
+ playing: () => {
2065
+ if (on)
2066
+ setFollow(true);
2067
+ },
2068
+ drawn: () => {
2069
+ if (on || !held)
2070
+ return;
2071
+ held = false;
2072
+ block.style.minHeight = ownMinHeight;
2073
+ },
2074
+ destroy: () => {
2075
+ leave(false);
2076
+ doc.removeEventListener("fullscreenchange", changed);
2077
+ }
2078
+ };
2079
+ }
2080
+
2081
+ // src/tab/instruments.ts
2082
+ function pageInstruments(options) {
2083
+ if (!options || typeof options !== "object" || !("play" in options))
2084
+ return;
2085
+ const play = options.play;
2086
+ if (!play || typeof play !== "object" || !("instruments" in play))
2087
+ return;
2088
+ const folder = play.instruments;
2089
+ return typeof folder === "string" && folder.trim() ? folder.trim() : undefined;
2090
+ }
2091
+ function soundsOfScore(score) {
2092
+ const programs = new Set;
2093
+ let drums = false;
2094
+ for (const track of score.tracks) {
2095
+ if (track.isPercussion)
2096
+ drums = true;
2097
+ else
2098
+ programs.add(track.playbackInfo.program);
2099
+ }
2100
+ return { programs: [...programs].sort((a, b) => a - b), drums };
2101
+ }
2102
+ function filesOf(index, sounds) {
2103
+ const files = [];
2104
+ for (const program of sounds.programs) {
2105
+ const entry = index.programs[String(program)];
2106
+ if (!entry?.file)
2107
+ return null;
2108
+ files.push(entry.file);
2109
+ }
2110
+ if (sounds.drums) {
2111
+ if (!index.drums?.file)
2112
+ return null;
2113
+ files.push(index.drums.file);
2114
+ } else if (index.click?.file && sounds.click !== false)
2115
+ files.push(index.click.file);
2116
+ return files.length ? files : null;
2117
+ }
2118
+ var soundCaches = new WeakMap;
2119
+ var isIndex = (value) => typeof value === "object" && value !== null && ("programs" in value) && typeof value.programs === "object" && value.programs !== null;
2120
+ async function instrumentSounds(source, sounds, win) {
2121
+ let perWindow = soundCaches.get(win);
2122
+ if (!perWindow)
2123
+ soundCaches.set(win, perWindow = new Map);
2124
+ let cache = perWindow.get(source);
2125
+ if (!cache)
2126
+ perWindow.set(source, cache = { index: null, files: new Map });
2127
+ const read = async (file) => {
2128
+ try {
2129
+ if (typeof source === "function")
2130
+ return await source(file);
2131
+ const response = await win.fetch(`${source.replace(/\/+$/, "")}/${file}`);
2132
+ return response.ok ? new Uint8Array(await response.arrayBuffer()) : null;
2133
+ } catch {
2134
+ return null;
2135
+ }
2136
+ };
2137
+ const fileOf = (file) => {
2138
+ let bytes = cache.files.get(file);
2139
+ if (!bytes) {
2140
+ bytes = read(file);
2141
+ cache.files.set(file, bytes);
2142
+ bytes.then((b) => {
2143
+ if (!b)
2144
+ cache.files.delete(file);
2145
+ });
2146
+ }
2147
+ return bytes;
2148
+ };
2149
+ cache.index ??= read("index.json").then((bytes) => {
2150
+ if (!bytes)
2151
+ return null;
2152
+ try {
2153
+ const value = JSON.parse(new TextDecoder().decode(bytes));
2154
+ return isIndex(value) ? value : null;
2155
+ } catch {
2156
+ return null;
2157
+ }
2158
+ });
2159
+ const index = await cache.index;
2160
+ if (!index) {
2161
+ cache.index = null;
2162
+ return null;
2163
+ }
2164
+ const files = filesOf(index, sounds);
2165
+ if (!files)
2166
+ return null;
2167
+ const all = await Promise.all(files.map(fileOf));
2168
+ return all.every((b) => b !== null) ? all : null;
2169
+ }
1940
2170
  // src/convert/chord-sheet.ts
1941
2171
  function chordsOf(line) {
1942
2172
  const found = [];
@@ -2009,6 +2239,38 @@ var alphaTabCdn = (version = ALPHATAB_VERSION) => {
2009
2239
  soundFont: `${base}/soundfont/sonivox.sf3`
2010
2240
  };
2011
2241
  };
2242
+ async function loadSounds(api, doc, assets, sounds, add = false) {
2243
+ const win = doc.defaultView;
2244
+ const made = local(doc);
2245
+ let files = null;
2246
+ if (assets.instruments && win)
2247
+ files = await instrumentSounds(assets.instruments, sounds, win);
2248
+ const kind = files ? "instruments" : "whole";
2249
+ if (!files) {
2250
+ if (typeof assets.soundFont === "string") {
2251
+ try {
2252
+ const response = await win?.fetch(assets.soundFont);
2253
+ files = response?.ok ? [new Uint8Array(await response.arrayBuffer())] : null;
2254
+ } catch {
2255
+ files = null;
2256
+ }
2257
+ if (!files)
2258
+ throw new Error(`Couldn't load the soundfont (${assets.soundFont})`);
2259
+ } else
2260
+ files = [assets.soundFont];
2261
+ }
2262
+ const all = files;
2263
+ let taken = 0;
2264
+ await new Promise((resolve) => {
2265
+ api.soundFontLoaded.on(() => {
2266
+ if (++taken >= all.length)
2267
+ resolve();
2268
+ });
2269
+ setTimeout(resolve, 8000);
2270
+ all.forEach((bytes, i) => api.loadSoundFont(made.bytes(bytes), add || i > 0));
2271
+ });
2272
+ return kind;
2273
+ }
2012
2274
  var SETTING = /^\s*(staves|file|tracks?)\s*:\s*(.*)$/i;
2013
2275
  function parseTabSource(language, source) {
2014
2276
  const lines = source.split(/\r?\n/);
@@ -2177,19 +2439,42 @@ function playerBar(doc, api, alphaTab, assets, volume) {
2177
2439
  const bar = playBar(doc, "mn-tab-bar", "Playback");
2178
2440
  const state = { loop: false, metronome: false, countIn: false, speed: 1 };
2179
2441
  const level = () => volumeOf(volume);
2442
+ let cant = () => {};
2180
2443
  let started = null;
2181
2444
  let cantStart = null;
2182
2445
  const start = () => {
2183
2446
  started ??= new Promise((resolve) => {
2184
- api.playerReady.on(() => resolve(true));
2185
2447
  cantStart = () => resolve(false);
2186
2448
  api.settings.player.playerMode = alphaTab.PlayerMode.EnabledSynthesizer;
2187
2449
  api.settings.player.enableCursor = true;
2188
- if (typeof assets.soundFont === "string")
2189
- api.settings.player.soundFont = assets.soundFont;
2450
+ if (!assets.instruments) {
2451
+ api.playerReady.on(() => resolve(true));
2452
+ if (typeof assets.soundFont === "string")
2453
+ api.settings.player.soundFont = assets.soundFont;
2454
+ api.updateSettings();
2455
+ if (assets.soundFont instanceof Uint8Array)
2456
+ api.loadSoundFont(local(doc).bytes(assets.soundFont), false);
2457
+ return;
2458
+ }
2459
+ let ready = false;
2460
+ let loaded = false;
2461
+ const settle = () => {
2462
+ if (ready && loaded)
2463
+ resolve(true);
2464
+ };
2465
+ api.playerReady.on(() => {
2466
+ ready = true;
2467
+ settle();
2468
+ });
2190
2469
  api.updateSettings();
2191
- if (assets.soundFont instanceof Uint8Array)
2192
- api.loadSoundFont(local(doc).bytes(assets.soundFont), false);
2470
+ const sounds = api.score ? soundsOfScore(api.score) : { programs: [], drums: false };
2471
+ loadSounds(api, doc, assets, sounds).then(() => {
2472
+ loaded = true;
2473
+ settle();
2474
+ }, (e) => {
2475
+ resolve(false);
2476
+ cant(e instanceof Error ? e.message : String(e));
2477
+ });
2193
2478
  }).then((ok) => {
2194
2479
  if (ok) {
2195
2480
  api.isLooping = state.loop;
@@ -2213,6 +2498,11 @@ function playerBar(doc, api, alphaTab, assets, volume) {
2213
2498
  api.playPause();
2214
2499
  });
2215
2500
  });
2501
+ cant = (message) => {
2502
+ play.disabled = true;
2503
+ play.title = `Can't play: ${message}`;
2504
+ play.setAttribute("aria-label", play.title);
2505
+ };
2216
2506
  const stop = iconButton(doc, "mn-tab-stop", "Stop", "stop");
2217
2507
  stop.addEventListener("click", () => live(() => api.stop()));
2218
2508
  const toggles = () => live(() => {
@@ -2254,9 +2544,7 @@ function playerBar(doc, api, alphaTab, assets, volume) {
2254
2544
  destroy: unfollow,
2255
2545
  failed: (message) => {
2256
2546
  cantStart?.();
2257
- play.disabled = true;
2258
- play.title = `Can't play: ${message}`;
2259
- play.setAttribute("aria-label", play.title);
2547
+ cant(message);
2260
2548
  }
2261
2549
  };
2262
2550
  }
@@ -2373,6 +2661,7 @@ function renderTab(language, source, host, options) {
2373
2661
  };
2374
2662
  const current = new at.AlphaTabApi(surface, settings);
2375
2663
  api = current;
2664
+ current.settings.player.scrollElement = surface;
2376
2665
  if (gone) {
2377
2666
  current.destroy();
2378
2667
  return;
@@ -2406,6 +2695,23 @@ function renderTab(language, source, host, options) {
2406
2695
  chooseStaves(staves, score);
2407
2696
  current.render();
2408
2697
  }, options.onKeepView));
2698
+ if (options.fullScreen !== false) {
2699
+ const full = fullScreenControl(doc, el, surface, (on) => {
2700
+ current.settings.player.scrollMode = on ? at.ScrollMode.OffScreen : at.ScrollMode.Off;
2701
+ current.updateSettings();
2702
+ });
2703
+ current.playerStateChanged.on((e) => {
2704
+ if (Number(e.state) === Number(at.synth.PlayerState.Playing))
2705
+ full.playing();
2706
+ });
2707
+ current.renderFinished.on(() => full.drawn());
2708
+ bar.bar.appendChild(full.button);
2709
+ const off = unbar;
2710
+ unbar = () => {
2711
+ full.destroy();
2712
+ off();
2713
+ };
2714
+ }
2409
2715
  el.addEventListener("pointerdown", (event) => {
2410
2716
  if (event.pointerType !== "touch" || bar?.bar.contains(event.target))
2411
2717
  return;
@@ -2519,7 +2825,11 @@ function createSoundfontPlayer(alphaTab, assets, doc, fallback) {
2519
2825
  let api = null;
2520
2826
  let host = null;
2521
2827
  let ready = null;
2522
- const start = () => {
2828
+ const have = new Set;
2829
+ const reading = new Map;
2830
+ let whole = !assets.instruments;
2831
+ const programsOf = (sound) => sound === "guitar" ? [PROGRAMS.guitar, MUTED.program] : [PROGRAMS[sound]];
2832
+ const start = (first = "guitar") => {
2523
2833
  if (ready)
2524
2834
  return ready;
2525
2835
  host = html(doc, "div", "mn-tab-hidden");
@@ -2536,7 +2846,7 @@ function createSoundfontPlayer(alphaTab, assets, doc, fallback) {
2536
2846
  playerMode: alphaTab.PlayerMode.EnabledSynthesizer,
2537
2847
  enableCursor: false,
2538
2848
  scrollMode: alphaTab.ScrollMode.Off,
2539
- ...typeof assets.soundFont === "string" ? { soundFont: assets.soundFont } : {}
2849
+ ...typeof assets.soundFont === "string" && !assets.instruments ? { soundFont: assets.soundFont } : {}
2540
2850
  }
2541
2851
  });
2542
2852
  const current = api;
@@ -2547,25 +2857,57 @@ function createSoundfontPlayer(alphaTab, assets, doc, fallback) {
2547
2857
  if (soundFont && player)
2548
2858
  resolve(true);
2549
2859
  };
2550
- current.soundFontLoaded.on(() => {
2860
+ const loaded = () => {
2551
2861
  soundFont = true;
2552
2862
  settle();
2553
2863
  setTimeout(() => {
2554
2864
  player = true;
2555
2865
  settle();
2556
2866
  }, 4000);
2557
- });
2867
+ };
2558
2868
  current.playerReady?.on(() => {
2559
2869
  player = true;
2560
2870
  settle();
2561
2871
  });
2562
2872
  current.error.on(() => resolve(false));
2873
+ if (assets.instruments)
2874
+ loadSounds(current, doc, assets, {
2875
+ programs: programsOf(first),
2876
+ drums: false
2877
+ }).then((kind) => {
2878
+ if (kind === "whole")
2879
+ whole = true;
2880
+ else
2881
+ have.add(first);
2882
+ loaded();
2883
+ }, () => resolve(false));
2884
+ else
2885
+ current.soundFontLoaded.on(loaded);
2563
2886
  });
2564
- if (assets.soundFont instanceof Uint8Array)
2887
+ if (assets.soundFont instanceof Uint8Array && !assets.instruments)
2565
2888
  current.loadSoundFont(made.bytes(assets.soundFont), false);
2566
2889
  current.tex(`\\track "Guitar" \\instrument ${PROGRAMS.guitar} r \\track "Piano" \\instrument ${PROGRAMS.piano} r \\track "Muted" \\instrument ${MUTED.program} r`);
2567
2890
  return ready;
2568
2891
  };
2892
+ const sounding = async (sound) => {
2893
+ if (!await start(sound))
2894
+ return false;
2895
+ if (whole || have.has(sound) || !api)
2896
+ return true;
2897
+ const current = api;
2898
+ let more = reading.get(sound);
2899
+ if (!more) {
2900
+ more = loadSounds(current, doc, assets, { programs: programsOf(sound), drums: false, click: false }, true).then((kind) => {
2901
+ if (kind === "whole")
2902
+ whole = true;
2903
+ else
2904
+ have.add(sound);
2905
+ return true;
2906
+ }, () => false);
2907
+ reading.set(sound, more);
2908
+ }
2909
+ return more;
2910
+ };
2569
2911
  const inner = soundfontPlayer(alphaTab, () => api);
2570
2912
  return {
2571
2913
  reportsStart: true,
@@ -2573,7 +2915,7 @@ function createSoundfontPlayer(alphaTab, assets, doc, fallback) {
2573
2915
  let handle = null;
2574
2916
  let stopped = false;
2575
2917
  let volume = options.volume;
2576
- start().then((ok) => {
2918
+ sounding(options.sound).then((ok) => {
2577
2919
  if (stopped)
2578
2920
  return;
2579
2921
  try {
@@ -2606,6 +2948,9 @@ function createSoundfontPlayer(alphaTab, assets, doc, fallback) {
2606
2948
  host?.remove();
2607
2949
  api = null;
2608
2950
  ready = null;
2951
+ have.clear();
2952
+ reading.clear();
2953
+ whole = !assets.instruments;
2609
2954
  }
2610
2955
  };
2611
2956
  }
@@ -2640,5 +2985,5 @@ export {
2640
2985
  textTabToAlphaTex
2641
2986
  };
2642
2987
 
2643
- //# debugId=89F5004D5F1EAB8664756E2164756E21
2988
+ //# debugId=F24D6355DCB2CF9764756E2164756E21
2644
2989
  //# sourceMappingURL=index.js.map