@dsebastien/music-notation 0.21.2 → 0.22.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
@@ -2218,6 +2218,137 @@ function scoreFit(apply, fits) {
2218
2218
  };
2219
2219
  }
2220
2220
 
2221
+ // src/tab/tempo.ts
2222
+ var SPEED_MIN = 0.25;
2223
+ var SPEED_MAX = 1.5;
2224
+ var PERCENT_STEP = 5;
2225
+ var clamp = (speed) => Math.min(SPEED_MAX, Math.max(SPEED_MIN, speed));
2226
+ function tempoAt(bars, tick, fallback) {
2227
+ let tempo = fallback;
2228
+ for (const bar of bars) {
2229
+ if (bar.start > tick)
2230
+ break;
2231
+ for (const change of bar.tempoChanges)
2232
+ if (change.tick <= tick)
2233
+ tempo = change.tempo;
2234
+ }
2235
+ return tempo !== null && Number.isFinite(tempo) && tempo > 0 ? tempo : fallback;
2236
+ }
2237
+ function speedFor(bpm, tempo) {
2238
+ return clamp(bpm / tempo);
2239
+ }
2240
+ var bpmOf = (speed, tempo) => Math.round(speed * tempo);
2241
+ function stepped(speed, direction, unit, tempo) {
2242
+ if (unit === "bpm" && tempo)
2243
+ return speedFor(bpmOf(speed, tempo) + direction, tempo);
2244
+ const percent = Math.round(speed * 100);
2245
+ const onGrid = direction > 0 ? Math.floor(percent / PERCENT_STEP) * PERCENT_STEP + PERCENT_STEP : Math.ceil(percent / PERCENT_STEP) * PERCENT_STEP - PERCENT_STEP;
2246
+ return clamp(onGrid / 100);
2247
+ }
2248
+ function written(speed, unit, tempo) {
2249
+ const percent = `${Math.round(speed * 100)} %`;
2250
+ if (!tempo)
2251
+ return { value: String(Math.round(speed * 100)), unit: "%", other: "" };
2252
+ const bpm = `${bpmOf(speed, tempo)} BPM`;
2253
+ return unit === "bpm" ? { value: String(bpmOf(speed, tempo)), unit: "BPM", other: percent } : { value: String(Math.round(speed * 100)), unit: "%", other: bpm };
2254
+ }
2255
+ function typed(text, unit, tempo) {
2256
+ const n = Number(text.replace(",", ".").replace(/[^\d.]/g, ""));
2257
+ if (!text.trim() || !Number.isFinite(n) || n <= 0)
2258
+ return null;
2259
+ return unit === "bpm" && tempo ? speedFor(n, tempo) : clamp(n / 100);
2260
+ }
2261
+ function tempoControl(doc, options) {
2262
+ let speed = 1;
2263
+ let unit = options.unit ?? "bpm";
2264
+ const field = html(doc, "span", "mn-tempo");
2265
+ field.setAttribute("role", "group");
2266
+ field.setAttribute("aria-label", "Tempo");
2267
+ const button = (cls, text, label) => {
2268
+ const b = html(doc, "button", cls, text);
2269
+ b.type = "button";
2270
+ b.setAttribute("aria-label", label);
2271
+ b.title = label;
2272
+ return b;
2273
+ };
2274
+ const down = button("mn-tempo-down", "−", "Slower");
2275
+ const up = button("mn-tempo-up", "+", "Faster");
2276
+ const input = html(doc, "input", "mn-tempo-value");
2277
+ input.type = "text";
2278
+ input.inputMode = "numeric";
2279
+ input.size = 3;
2280
+ input.autocomplete = "off";
2281
+ const unitButton = button("mn-tempo-unit", "BPM", "Show the speed in percent");
2282
+ const other = button("mn-tempo-other", "", "Back to 100 %");
2283
+ let editing = false;
2284
+ input.addEventListener("input", () => editing = true);
2285
+ const shownUnit = () => options.tempo() ? unit : "percent";
2286
+ const show = () => {
2287
+ const tempo = options.tempo();
2288
+ const w = written(speed, shownUnit(), tempo);
2289
+ if (!editing)
2290
+ input.value = w.value;
2291
+ input.setAttribute("aria-label", w.unit === "BPM" ? "Tempo in BPM" : "Speed in percent");
2292
+ unitButton.textContent = w.unit;
2293
+ unitButton.hidden = !tempo;
2294
+ const toOther = shownUnit() === "bpm" ? "percent" : "BPM";
2295
+ unitButton.setAttribute("aria-label", `Show the speed in ${toOther}`);
2296
+ unitButton.title = `Show the speed in ${toOther}`;
2297
+ other.textContent = w.other ? `· ${w.other}` : "";
2298
+ other.hidden = !w.other;
2299
+ other.disabled = speed === 1;
2300
+ field.title = tempo ? `Tempo: ${bpmOf(speed, tempo)} BPM, ${Math.round(speed * 100)} % of ${Math.round(tempo)} BPM` : `Speed: ${Math.round(speed * 100)} %`;
2301
+ down.disabled = speed <= SPEED_MIN;
2302
+ up.disabled = speed >= SPEED_MAX;
2303
+ };
2304
+ const set = (next) => {
2305
+ editing = false;
2306
+ speed = clamp(next);
2307
+ options.set(speed);
2308
+ show();
2309
+ };
2310
+ down.addEventListener("click", () => set(stepped(speed, -1, shownUnit(), options.tempo())));
2311
+ up.addEventListener("click", () => set(stepped(speed, 1, shownUnit(), options.tempo())));
2312
+ other.addEventListener("click", () => set(1));
2313
+ unitButton.addEventListener("click", () => {
2314
+ unit = shownUnit() === "bpm" ? "percent" : "bpm";
2315
+ options.unitChanged?.(unit);
2316
+ show();
2317
+ });
2318
+ const commit = () => {
2319
+ const next = typed(input.value, shownUnit(), options.tempo());
2320
+ editing = false;
2321
+ if (next === null)
2322
+ show();
2323
+ else
2324
+ set(next);
2325
+ };
2326
+ input.addEventListener("change", commit);
2327
+ input.addEventListener("keydown", (event) => {
2328
+ if (event.key === "Enter") {
2329
+ event.preventDefault();
2330
+ commit();
2331
+ input.select();
2332
+ } else if (event.key === "Escape") {
2333
+ event.stopPropagation();
2334
+ editing = false;
2335
+ input.blur();
2336
+ show();
2337
+ } else if (event.key === "ArrowUp" || event.key === "ArrowDown") {
2338
+ event.preventDefault();
2339
+ set(stepped(speed, event.key === "ArrowUp" ? 1 : -1, shownUnit(), options.tempo()));
2340
+ input.select();
2341
+ }
2342
+ });
2343
+ input.addEventListener("blur", () => {
2344
+ editing = false;
2345
+ show();
2346
+ });
2347
+ field.append(down, input, unitButton, up, other);
2348
+ show();
2349
+ return { field, speed: () => speed, refresh: show };
2350
+ }
2351
+
2221
2352
  // src/tab/instruments.ts
2222
2353
  function pageInstruments(options) {
2223
2354
  if (!options || typeof options !== "object" || !("play" in options))
@@ -2575,7 +2706,7 @@ var time = (ms) => {
2575
2706
  const s = Math.max(0, Math.round(ms / 1000));
2576
2707
  return `${Math.floor(s / 60)}:${String(s % 60).padStart(2, "0")}`;
2577
2708
  };
2578
- function playerBar(doc, api, alphaTab, assets, volume) {
2709
+ function playerBar(doc, api, alphaTab, assets, volume, tempoOptions = {}) {
2579
2710
  const bar = playBar(doc, "mn-tab-bar", "Playback");
2580
2711
  const state = { loop: false, metronome: false, countIn: false, speed: 1 };
2581
2712
  const level = () => volumeOf(volume);
@@ -2662,9 +2793,15 @@ function playerBar(doc, api, alphaTab, assets, volume) {
2662
2793
  state.countIn = on;
2663
2794
  toggles();
2664
2795
  });
2665
- const speed = valueSlider(doc, "Speed", "%", 25, 150, 100, (n) => {
2666
- state.speed = n / 100;
2667
- live(() => api.playbackSpeed = state.speed);
2796
+ let tempoNow = api.score?.tempo ?? null;
2797
+ const speed = tempoControl(doc, {
2798
+ ...tempoOptions.tempoUnit ? { unit: tempoOptions.tempoUnit } : {},
2799
+ tempo: () => tempoNow,
2800
+ set: (n) => {
2801
+ state.speed = n;
2802
+ live(() => api.playbackSpeed = state.speed);
2803
+ },
2804
+ ...tempoOptions.onTempoUnit ? { unitChanged: tempoOptions.onTempoUnit } : {}
2668
2805
  });
2669
2806
  const position = html(doc, "span", "mn-tab-time", "");
2670
2807
  bar.append(play, stop, loop, metronome, countIn, speed.field, position);
@@ -2677,6 +2814,11 @@ function playerBar(doc, api, alphaTab, assets, volume) {
2677
2814
  });
2678
2815
  api.playerPositionChanged.on((e) => {
2679
2816
  position.textContent = `${time(e.currentTime)} / ${time(e.endTime)}`;
2817
+ const tempo = tempoAt(api.tickCache?.masterBars ?? [], e.currentTick, api.score?.tempo ?? null);
2818
+ if (tempo !== tempoNow) {
2819
+ tempoNow = tempo;
2820
+ speed.refresh();
2821
+ }
2680
2822
  });
2681
2823
  return {
2682
2824
  bar,
@@ -2820,7 +2962,7 @@ function renderTab(language, source, host, options) {
2820
2962
  if (settled || gone)
2821
2963
  return;
2822
2964
  if (options.player !== false) {
2823
- bar = playerBar(doc, current, at, assets, options.volume);
2965
+ bar = playerBar(doc, current, at, assets, options.volume, options);
2824
2966
  unbar = bar.destroy;
2825
2967
  speedOf = bar.speed;
2826
2968
  el.appendChild(bar.bar);
@@ -3151,5 +3293,5 @@ export {
3151
3293
  textTabToAlphaTex
3152
3294
  };
3153
3295
 
3154
- //# debugId=74AA16B9663E346964756E2164756E21
3296
+ //# debugId=E1D3DD446F8C393264756E2164756E21
3155
3297
  //# sourceMappingURL=index.js.map