@dsebastien/music-notation 0.17.0 → 0.18.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.
@@ -12,5 +12,6 @@ declare global {
12
12
  alphaTab?: AlphaTabModule;
13
13
  musicNotationTabLoaded?: boolean;
14
14
  musicNotationSoundfontPlayer?: NotePlayer;
15
+ musicNotationOptions?: unknown;
15
16
  }
16
17
  }
@@ -46,6 +46,11 @@ export interface TabOptions {
46
46
  onKeepView?: (staves: Staves) => void;
47
47
  /** The view of a block with no `staves:` line (default: the block's language). */
48
48
  defaultStaves?: Staves;
49
+ /**
50
+ * The app's volume, 0 to 1 (a number, or a function read at each Play and on `volumeChanged`,
51
+ * #85); 0.8 by default. The bar has no volume of its own (#82).
52
+ */
53
+ volume?: number | (() => number);
49
54
  }
50
55
  export type Staves = 'tab' | 'score' | 'both';
51
56
  export interface TabSource {
package/dist/tab/index.js CHANGED
@@ -1769,6 +1769,136 @@ function textTabToAlphaTex(text) {
1769
1769
  return [...header, ...ts, texOf(bars, meter)].join(`
1770
1770
  `);
1771
1771
  }
1772
+
1773
+ // src/playback/bar-parts.ts
1774
+ var ICONS = {
1775
+ play: "M4 2.5v11l9-5.5z",
1776
+ pause: "M4 3h3v10H4zM9 3h3v10H9z",
1777
+ stop: "M4 4h8v8H4z",
1778
+ all: "M1.5 2.5v11l6.5-5.5zM8 2.5v11l6.5-5.5z",
1779
+ loop: "M3 8a5 5 0 0 1 8.5-3.5L13 6V2.5M13 8a5 5 0 0 1-8.5 3.5L3 10v3.5",
1780
+ count: "M5.5 14h5L9 2.5H7zM8 11.5l4.5-7",
1781
+ click: "M3 5v6M8 5v6M13 5v6"
1782
+ };
1783
+ var STROKED = new Set(["loop", "count", "click"]);
1784
+ function icon(doc, name) {
1785
+ const root = svg(doc, "svg", {
1786
+ class: `mn-play-icon mn-play-icon-${name}${STROKED.has(name) ? " mn-play-icon-line" : ""}`,
1787
+ viewBox: "0 0 16 16",
1788
+ width: 12,
1789
+ height: 12,
1790
+ "aria-hidden": "true"
1791
+ });
1792
+ root.appendChild(svg(doc, "path", { d: ICONS[name] }));
1793
+ return root;
1794
+ }
1795
+ function playBar(doc, cls, label) {
1796
+ const bar = html(doc, "div", `mn-play mn-play-static ${cls}`);
1797
+ bar.addEventListener("mousedown", (event) => event.stopPropagation());
1798
+ bar.setAttribute("role", "group");
1799
+ bar.setAttribute("aria-label", label);
1800
+ return bar;
1801
+ }
1802
+ function iconButton(doc, cls, label, name) {
1803
+ const button = html(doc, "button", cls);
1804
+ button.type = "button";
1805
+ button.appendChild(icon(doc, name));
1806
+ button.title = label;
1807
+ button.setAttribute("aria-label", label);
1808
+ return button;
1809
+ }
1810
+ function pressedButton(doc, cls, label, name, on, set) {
1811
+ const button = iconButton(doc, cls, label, name);
1812
+ const show = () => {
1813
+ button.setAttribute("aria-pressed", String(on));
1814
+ button.classList.toggle("mn-on", on);
1815
+ };
1816
+ button.addEventListener("click", () => {
1817
+ on = !on;
1818
+ set(on);
1819
+ show();
1820
+ });
1821
+ show();
1822
+ return button;
1823
+ }
1824
+ function valueSlider(doc, label, unit, min, max, value, set, hint) {
1825
+ const field = html(doc, "label", "mn-play-field");
1826
+ const input = html(doc, "input");
1827
+ input.type = "range";
1828
+ input.min = String(min);
1829
+ input.max = String(max);
1830
+ input.setAttribute("aria-label", label);
1831
+ if (hint)
1832
+ input.setAttribute("aria-description", hint);
1833
+ const text = html(doc, "span", "mn-play-value");
1834
+ text.setAttribute("aria-hidden", "true");
1835
+ const show = (n) => {
1836
+ input.value = String(n);
1837
+ const written = `${n} ${unit}`;
1838
+ text.textContent = written;
1839
+ input.setAttribute("aria-valuetext", written);
1840
+ field.title = `${label}: ${written}${hint ? ` (${hint})` : ""}`;
1841
+ };
1842
+ input.addEventListener("input", () => {
1843
+ const n = Number(input.value);
1844
+ set(n);
1845
+ show(n);
1846
+ });
1847
+ show(value);
1848
+ field.append(input, text);
1849
+ return { field, input, show };
1850
+ }
1851
+ function choice(doc, cls, label, values, chosen, names, set) {
1852
+ const el = html(doc, "select", cls);
1853
+ el.setAttribute("aria-label", label);
1854
+ el.title = label;
1855
+ for (const value of values) {
1856
+ const option = html(doc, "option", undefined, names[value]);
1857
+ option.value = value;
1858
+ option.selected = value === chosen;
1859
+ el.appendChild(option);
1860
+ }
1861
+ el.addEventListener("change", () => set(el.value));
1862
+ return el;
1863
+ }
1864
+ var DEFAULT_VOLUME = 0.8;
1865
+ function volumeOf(volume) {
1866
+ let value = volume;
1867
+ if (typeof volume === "function") {
1868
+ try {
1869
+ value = volume();
1870
+ } catch {
1871
+ value = undefined;
1872
+ }
1873
+ }
1874
+ return typeof value === "number" && Number.isFinite(value) ? Math.min(1, Math.max(0, value)) : DEFAULT_VOLUME;
1875
+ }
1876
+
1877
+ // src/playback/live-volume.ts
1878
+ var listeners = new Set;
1879
+ var level = (value) => typeof value === "number" && Number.isFinite(value) ? Math.min(1, Math.max(0, value)) : null;
1880
+ function volumeChanged() {
1881
+ for (const listener of [...listeners]) {
1882
+ try {
1883
+ listener();
1884
+ } catch {}
1885
+ }
1886
+ }
1887
+ function followVolume(read, handles, start) {
1888
+ let last = level(start);
1889
+ const listener = () => {
1890
+ const now = level(read());
1891
+ if (now === null || now === last)
1892
+ return;
1893
+ last = now;
1894
+ for (const h of handles())
1895
+ h.setVolume?.(now);
1896
+ };
1897
+ listeners.add(listener);
1898
+ return () => {
1899
+ listeners.delete(listener);
1900
+ };
1901
+ }
1772
1902
  // src/convert/chord-sheet.ts
1773
1903
  function chordsOf(line) {
1774
1904
  const found = [];
@@ -2001,37 +2131,14 @@ var local = (doc) => {
2001
2131
  }
2002
2132
  };
2003
2133
  };
2004
- var ICONS = {
2005
- play: "M4 2.5v11l9-5.5z",
2006
- pause: "M4 3h3v10H4zM9 3h3v10H9z",
2007
- stop: "M4 4h8v8H4z",
2008
- loop: "M3 8a5 5 0 0 1 8.5-3.5L13 6V2.5M13 8a5 5 0 0 1-8.5 3.5L3 10v3.5",
2009
- metronome: "M6 2h4l3 12H3zM8 11l3-7",
2010
- countIn: "M3 13h10M5 13V9M8 13V6M11 13V3",
2011
- volume: "M2 6h3l4-3v10l-4-3H2zM11 5.5a3.5 3.5 0 0 1 0 5"
2012
- };
2013
- function icon(doc, name) {
2014
- const filled = name === "play" || name === "pause" || name === "stop";
2015
- const root = svg(doc, "svg", {
2016
- class: `mn-play-icon${filled ? "" : " mn-play-icon-loop"}`,
2017
- viewBox: "0 0 16 16",
2018
- width: 12,
2019
- height: 12,
2020
- "aria-hidden": "true"
2021
- });
2022
- root.appendChild(svg(doc, "path", { d: ICONS[name] }));
2023
- return root;
2024
- }
2025
2134
  var time = (ms) => {
2026
2135
  const s = Math.max(0, Math.round(ms / 1000));
2027
2136
  return `${Math.floor(s / 60)}:${String(s % 60).padStart(2, "0")}`;
2028
2137
  };
2029
- function playerBar(doc, api, alphaTab, assets) {
2030
- const bar = html(doc, "div", "mn-play mn-tab-bar");
2031
- bar.addEventListener("mousedown", (event) => event.stopPropagation());
2032
- bar.setAttribute("role", "group");
2033
- bar.setAttribute("aria-label", "Playback");
2034
- const state = { loop: false, metronome: false, countIn: false, speed: 1, volume: 1 };
2138
+ function playerBar(doc, api, alphaTab, assets, volume) {
2139
+ const bar = playBar(doc, "mn-tab-bar", "Playback");
2140
+ const state = { loop: false, metronome: false, countIn: false, speed: 1 };
2141
+ const level = () => volumeOf(volume);
2035
2142
  let started = null;
2036
2143
  let cantStart = null;
2037
2144
  const start = () => {
@@ -2051,78 +2158,49 @@ function playerBar(doc, api, alphaTab, assets) {
2051
2158
  api.metronomeVolume = state.metronome ? 1 : 0;
2052
2159
  api.countInVolume = state.countIn ? 1 : 0;
2053
2160
  api.playbackSpeed = state.speed;
2054
- api.masterVolume = state.volume;
2161
+ api.masterVolume = level();
2055
2162
  }
2056
2163
  return ok;
2057
2164
  });
2058
2165
  return started;
2059
2166
  };
2060
- const button = (name, label, run) => {
2061
- const b = html(doc, "button");
2062
- b.type = "button";
2063
- b.title = label;
2064
- b.setAttribute("aria-label", label);
2065
- b.appendChild(icon(doc, name));
2066
- b.addEventListener("click", run);
2067
- bar.appendChild(b);
2068
- return b;
2069
- };
2070
2167
  const live = (apply) => {
2071
2168
  if (api.player)
2072
2169
  apply();
2073
2170
  };
2074
- const toggle = (name, label, key) => {
2075
- const b = button(name, label, () => {
2076
- state[key] = !state[key];
2077
- live(() => {
2078
- api.isLooping = state.loop;
2079
- api.metronomeVolume = state.metronome ? 1 : 0;
2080
- api.countInVolume = state.countIn ? 1 : 0;
2081
- });
2082
- b.classList.toggle("mn-on", state[key]);
2083
- b.setAttribute("aria-pressed", String(state[key]));
2084
- });
2085
- b.setAttribute("aria-pressed", "false");
2086
- };
2087
- const play = button("play", "Play", () => {
2171
+ const play = iconButton(doc, "mn-play-toggle", "Play", "play");
2172
+ play.addEventListener("click", () => {
2088
2173
  start().then((ok) => {
2089
2174
  if (ok)
2090
2175
  api.playPause();
2091
2176
  });
2092
2177
  });
2093
- button("stop", "Stop", () => live(() => api.stop()));
2094
- toggle("loop", "Loop", "loop");
2095
- toggle("metronome", "Metronome", "metronome");
2096
- toggle("countIn", "Count-in", "countIn");
2097
- const slider = (label, mark, min, max, value, set, unit = "") => {
2098
- const field = html(doc, "label", "mn-play-field");
2099
- const tag = html(doc, "span", "mn-play-label");
2100
- tag.setAttribute("aria-hidden", "true");
2101
- tag.append(mark);
2102
- const input = html(doc, "input");
2103
- input.type = "range";
2104
- input.min = String(min);
2105
- input.max = String(max);
2106
- input.value = String(value);
2107
- input.setAttribute("aria-label", label);
2108
- field.title = `${label}: ${value}${unit}`;
2109
- input.addEventListener("input", () => {
2110
- set(Number(input.value));
2111
- field.title = `${label}: ${input.value}${unit}`;
2112
- });
2113
- field.append(tag, input);
2114
- bar.appendChild(field);
2115
- };
2116
- slider("Speed", "%", 25, 150, 100, (n) => {
2178
+ const stop = iconButton(doc, "mn-tab-stop", "Stop", "stop");
2179
+ stop.addEventListener("click", () => live(() => api.stop()));
2180
+ const toggles = () => live(() => {
2181
+ api.isLooping = state.loop;
2182
+ api.metronomeVolume = state.metronome ? 1 : 0;
2183
+ api.countInVolume = state.countIn ? 1 : 0;
2184
+ });
2185
+ const loop = pressedButton(doc, "mn-tab-loop", "Loop", "loop", false, (on) => {
2186
+ state.loop = on;
2187
+ toggles();
2188
+ });
2189
+ const metronome = pressedButton(doc, "mn-tab-click", "Metronome", "click", false, (on) => {
2190
+ state.metronome = on;
2191
+ toggles();
2192
+ });
2193
+ const countIn = pressedButton(doc, "mn-tab-count", "Count-in", "count", false, (on) => {
2194
+ state.countIn = on;
2195
+ toggles();
2196
+ });
2197
+ const speed = valueSlider(doc, "Speed", "%", 25, 150, 100, (n) => {
2117
2198
  state.speed = n / 100;
2118
2199
  live(() => api.playbackSpeed = state.speed);
2119
- }, "%");
2120
- slider("Volume", icon(doc, "volume"), 0, 100, 100, (n) => {
2121
- state.volume = n / 100;
2122
- live(() => api.masterVolume = state.volume);
2123
2200
  });
2124
2201
  const position = html(doc, "span", "mn-tab-time", "");
2125
- bar.appendChild(position);
2202
+ bar.append(play, stop, loop, metronome, countIn, speed.field, position);
2203
+ const unfollow = followVolume(level, () => [{ stop() {}, setVolume: (v) => live(() => api.masterVolume = v) }], level());
2126
2204
  api.playerStateChanged.on((e) => {
2127
2205
  const playing = Number(e.state) === Number(alphaTab.synth.PlayerState.Playing);
2128
2206
  play.replaceChildren(icon(doc, playing ? "pause" : "play"));
@@ -2135,6 +2213,7 @@ function playerBar(doc, api, alphaTab, assets) {
2135
2213
  return {
2136
2214
  bar,
2137
2215
  speed: () => state.speed,
2216
+ destroy: unfollow,
2138
2217
  failed: (message) => {
2139
2218
  cantStart?.();
2140
2219
  play.disabled = true;
@@ -2198,6 +2277,7 @@ function renderTab(language, source, host, options) {
2198
2277
  host.appendChild(el);
2199
2278
  let api = null;
2200
2279
  let speedOf = () => 1;
2280
+ let unbar = () => {};
2201
2281
  let gone = false;
2202
2282
  let settled = false;
2203
2283
  let settle = () => {};
@@ -2272,7 +2352,8 @@ function renderTab(language, source, host, options) {
2272
2352
  if (settled || gone)
2273
2353
  return;
2274
2354
  if (options.player !== false) {
2275
- bar = playerBar(doc, current, at, assets);
2355
+ bar = playerBar(doc, current, at, assets, options.volume);
2356
+ unbar = bar.destroy;
2276
2357
  speedOf = bar.speed;
2277
2358
  el.appendChild(bar.bar);
2278
2359
  if (options.views !== false && tablature)
@@ -2333,6 +2414,7 @@ function renderTab(language, source, host, options) {
2333
2414
  speed: () => speedOf(),
2334
2415
  destroy() {
2335
2416
  gone = true;
2417
+ unbar();
2336
2418
  try {
2337
2419
  api?.destroy();
2338
2420
  } catch {}
@@ -2517,5 +2599,5 @@ export {
2517
2599
  textTabToAlphaTex
2518
2600
  };
2519
2601
 
2520
- //# debugId=BC271816D895D2D864756E2164756E21
2602
+ //# debugId=10B85D48D2B2B5E264756E2164756E21
2521
2603
  //# sourceMappingURL=index.js.map