@waveform-playlist/core 13.0.0-next.1 → 13.0.0-next.10

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/index.js CHANGED
@@ -22,6 +22,7 @@ var index_exports = {};
22
22
  __export(index_exports, {
23
23
  ANNOTATION_LINK_THRESHOLD_TICKS: () => ANNOTATION_LINK_THRESHOLD_TICKS,
24
24
  DEFAULT_ANNOTATION_SHORTCUTS: () => DEFAULT_ANNOTATION_SHORTCUTS,
25
+ DEFAULT_PAN_LAW: () => DEFAULT_PAN_LAW,
25
26
  DEFAULT_SPECTROGRAM_COLOR_MAP: () => DEFAULT_SPECTROGRAM_COLOR_MAP,
26
27
  InteractionState: () => InteractionState,
27
28
  LINK_THRESHOLD: () => LINK_THRESHOLD,
@@ -29,18 +30,21 @@ __export(index_exports, {
29
30
  METER_PEAK_DECAY: () => METER_PEAK_DECAY,
30
31
  MIN_ANNOTATION_DURATION: () => MIN_ANNOTATION_DURATION,
31
32
  MIN_PIXELS_PER_UNIT: () => MIN_PIXELS_PER_UNIT,
33
+ PAN_LAW_RAMP_SECONDS: () => PAN_LAW_RAMP_SECONDS,
32
34
  PPQN: () => PPQN,
33
35
  SPECTROGRAM_DEFAULTS: () => SPECTROGRAM_DEFAULTS,
36
+ TRACK_VIEW_FIELDS: () => TRACK_VIEW_FIELDS,
34
37
  annotationMinDurationTicks: () => annotationMinDurationTicks,
35
38
  appendPeaks: () => appendPeaks,
36
39
  appendToAudioBuffer: () => appendToAudioBuffer,
40
+ applyAnnotationBounds: () => applyAnnotationBounds,
37
41
  applyFadeIn: () => applyFadeIn,
38
42
  applyFadeOut: () => applyFadeOut,
39
43
  assertTrackKind: () => assertTrackKind,
40
44
  audibleLatencySamples: () => audibleLatencySamples,
45
+ audioPanLaw: () => audioPanLaw,
41
46
  audioTrack: () => audioTrack,
42
47
  buildSpectrogramCanvasId: () => buildSpectrogramCanvasId,
43
- calculateDuration: () => calculateDuration,
44
48
  carveClipRange: () => carveClipRange,
45
49
  clipDurationTime: () => clipDurationTime,
46
50
  clipEndTime: () => clipEndTime,
@@ -51,11 +55,13 @@ __export(index_exports, {
51
55
  clipsOverlap: () => clipsOverlap,
52
56
  computeMusicalTicks: () => computeMusicalTicks,
53
57
  concatenateAudioData: () => concatenateAudioData,
58
+ createAnnotationStore: () => createAnnotationStore,
54
59
  createAudioBuffer: () => createAudioBuffer,
55
60
  createClip: () => createClip,
56
61
  createClipFromSeconds: () => createClipFromSeconds,
57
62
  createClipFromTicks: () => createClipFromTicks,
58
63
  createEffectInstance: () => createEffectInstance,
64
+ createMixerGesture: () => createMixerGesture,
59
65
  createPanStage: () => createPanStage,
60
66
  createSourceLoader: () => createSourceLoader,
61
67
  createTimeline: () => createTimeline,
@@ -75,6 +81,7 @@ __export(index_exports, {
75
81
  getClipsInRange: () => getClipsInRange,
76
82
  getEffectDefinitions: () => getEffectDefinitions,
77
83
  handleKeyboardEvent: () => handleKeyboardEvent,
84
+ isDecidedPanLaw: () => isDecidedPanLaw,
78
85
  isLoadingClip: () => isLoadingClip,
79
86
  keyBelongsToFocusedControl: () => keyBelongsToFocusedControl,
80
87
  linearCurve: () => linearCurve,
@@ -110,6 +117,7 @@ __export(index_exports, {
110
117
  ticksToSamples: () => ticksToSamples,
111
118
  trackChannelCount: () => trackChannelCount,
112
119
  trackKind: () => trackKind,
120
+ trackPanLaw: () => trackPanLaw,
113
121
  unregisterEffect: () => unregisterEffect,
114
122
  updateAnnotationBoundaries: () => updateAnnotationBoundaries,
115
123
  watchMicrophoneDevices: () => watchMicrophoneDevices
@@ -120,6 +128,14 @@ module.exports = __toCommonJS(index_exports);
120
128
  var MAX_CANVAS_WIDTH = 1e3;
121
129
 
122
130
  // src/types/clip.ts
131
+ var TRACK_VIEW_FIELDS = [
132
+ "name",
133
+ "color",
134
+ "height",
135
+ "renderMode",
136
+ "spectrogramConfig",
137
+ "spectrogramColorMap"
138
+ ];
123
139
  function isLoadingClip(clip) {
124
140
  return !("sampleRate" in clip) || clip.sampleRate == null;
125
141
  }
@@ -777,9 +793,6 @@ function appendToAudioBuffer(audioContext, existingBuffer, newSamples, sampleRat
777
793
  const combined = concatenateAudioData([existingData, newSamples]);
778
794
  return createAudioBuffer(audioContext, [combined], sampleRate);
779
795
  }
780
- function calculateDuration(sampleCount, sampleRate) {
781
- return sampleCount / sampleRate;
782
- }
783
796
 
784
797
  // src/utils/latency.ts
785
798
  function audibleLatencySamples(outputLatency, lookAhead, sampleRate) {
@@ -917,7 +930,7 @@ function assertTrackKind(track) {
917
930
  const strayKind = clipKind(stray);
918
931
  const fix = track.kind === void 0 ? "The track has no kind, which is read as 'audio': set kind: 'midi' on a track of MIDI clips." : "A track holds one kind of clip: put the clip on a track of its own kind (splitTrackByKind() splits a track that has both).";
919
932
  throw new Error(
920
- "[waveform-playlist] Track " + label(track) + " is an " + kind + " track but its clip " + label(stray) + " is " + (strayKind === "audio" ? "an audio" : "a midi") + " clip. " + fix
933
+ "[waveform-playlist] Track " + label(track) + (kind === "audio" ? " is an audio" : " is a midi") + " track but its clip " + label(stray) + " is " + (strayKind === "audio" ? "an audio" : "a midi") + " clip. " + fix
921
934
  );
922
935
  }
923
936
  function splitTrackByKind(track, midiTrackId = track.id + "-midi") {
@@ -1117,14 +1130,17 @@ function keyBelongsToFocusedControl(event) {
1117
1130
  if (ownsAllKeys(focused)) return true;
1118
1131
  return activates(focused, event.key);
1119
1132
  }
1133
+ var preventedByShortcut = /* @__PURE__ */ new WeakSet();
1120
1134
  function handleKeyboardEvent(event, shortcuts, enabled) {
1121
1135
  if (!enabled) return;
1122
1136
  if (event.repeat) return;
1137
+ if (event.key === "Escape" && event.defaultPrevented && !preventedByShortcut.has(event)) return;
1123
1138
  if (keyBelongsToFocusedControl(event)) return;
1124
1139
  const matchingShortcut = shortcuts.find((shortcut) => matchesKeyBinding(event, shortcut));
1125
1140
  if (matchingShortcut) {
1126
1141
  if (matchingShortcut.preventDefault !== false) {
1127
1142
  event.preventDefault();
1143
+ preventedByShortcut.add(event);
1128
1144
  }
1129
1145
  matchingShortcut.action();
1130
1146
  }
@@ -1321,11 +1337,104 @@ function resolveAnnotationShortcuts(remap) {
1321
1337
  });
1322
1338
  }
1323
1339
 
1340
+ // src/annotations/devFreeze.ts
1341
+ function isDevelopmentBuild() {
1342
+ try {
1343
+ return process.env.NODE_ENV !== "production";
1344
+ } catch {
1345
+ return false;
1346
+ }
1347
+ }
1348
+ function freezeAnnotations(list) {
1349
+ if (!isDevelopmentBuild()) return list;
1350
+ for (const annotation of list) {
1351
+ if (Array.isArray(annotation.lines)) Object.freeze(annotation.lines);
1352
+ Object.freeze(annotation);
1353
+ }
1354
+ return Object.freeze(list);
1355
+ }
1356
+
1357
+ // src/annotations/store.ts
1358
+ function invalid(message) {
1359
+ return new TypeError("[waveform-playlist] Invalid annotations: " + message);
1360
+ }
1361
+ function validateAnnotations(list) {
1362
+ if (!Array.isArray(list)) throw invalid("expected an array.");
1363
+ const seen = /* @__PURE__ */ new Set();
1364
+ for (const annotation of list) {
1365
+ const { id, start, end } = annotation ?? {};
1366
+ if (!Number.isFinite(start) || !Number.isFinite(end)) {
1367
+ throw invalid(
1368
+ `annotation "${String(id)}" needs numeric start and end (got ${typeof start} and ${typeof end}). Parse text formats first, e.g. parseAeneas() from @waveform-playlist/annotations.`
1369
+ );
1370
+ }
1371
+ if (seen.has(id)) throw invalid(`two annotations have the id "${id}".`);
1372
+ seen.add(id);
1373
+ }
1374
+ }
1375
+ function createAnnotationStore(initial = [], options = {}) {
1376
+ validateAnnotations(initial);
1377
+ let annotations = freezeAnnotations(initial);
1378
+ const listeners = /* @__PURE__ */ new Set();
1379
+ const { beforeChange } = options;
1380
+ const commit = (proposed) => {
1381
+ if (proposed === annotations) return;
1382
+ validateAnnotations(proposed);
1383
+ let next = proposed;
1384
+ if (beforeChange) {
1385
+ next = beforeChange(proposed, annotations);
1386
+ if (next === annotations) return;
1387
+ if (next !== proposed) validateAnnotations(next);
1388
+ }
1389
+ annotations = freezeAnnotations(next);
1390
+ for (const entry of [...listeners]) {
1391
+ if (!listeners.has(entry)) continue;
1392
+ try {
1393
+ entry.listener();
1394
+ } catch (error) {
1395
+ console.warn("[waveform-playlist] Error in an annotation store subscriber:", error);
1396
+ }
1397
+ }
1398
+ };
1399
+ return {
1400
+ getAnnotations: () => annotations,
1401
+ subscribe(listener) {
1402
+ const entry = { listener };
1403
+ listeners.add(entry);
1404
+ return () => {
1405
+ listeners.delete(entry);
1406
+ };
1407
+ },
1408
+ set(next) {
1409
+ commit(next);
1410
+ },
1411
+ update(fn) {
1412
+ commit(fn(annotations));
1413
+ }
1414
+ };
1415
+ }
1416
+
1417
+ // src/annotations/bounds.ts
1418
+ function applyAnnotationBounds(current, changes) {
1419
+ if (changes.length === 0) return current;
1420
+ const byId = new Map(changes.map((change) => [change.id, change]));
1421
+ let changed = false;
1422
+ const next = current.map((annotation) => {
1423
+ const change = byId.get(annotation.id);
1424
+ if (!change || change.start === annotation.start && change.end === annotation.end) {
1425
+ return annotation;
1426
+ }
1427
+ changed = true;
1428
+ return { ...annotation, start: change.start, end: change.end };
1429
+ });
1430
+ return changed ? next : current;
1431
+ }
1432
+
1324
1433
  // src/panStage.ts
1325
1434
  var PREFIX = "[waveform-playlist] ";
1326
- function panMatrix(pan, sourceChannelCount) {
1435
+ function panMatrix(pan, panLaw) {
1327
1436
  const p = Number.isFinite(pan) ? Math.max(-1, Math.min(1, pan)) : 0;
1328
- if (sourceChannelCount < 2) {
1437
+ if (panLaw === "mono") {
1329
1438
  const theta = (p + 1) * Math.PI / 4;
1330
1439
  return { ll: Math.cos(theta), lr: 0, rl: 0, rr: Math.sin(theta) };
1331
1440
  }
@@ -1336,7 +1445,8 @@ function panMatrix(pan, sourceChannelCount) {
1336
1445
  const x = p * Math.PI / 2;
1337
1446
  return { ll: Math.cos(x), lr: Math.sin(x), rl: 0, rr: 1 };
1338
1447
  }
1339
- function createPanStage(context, pan, sourceChannelCount) {
1448
+ var PAN_LAW_RAMP_SECONDS = 0.01;
1449
+ function createPanStage(context, pan, panLaw) {
1340
1450
  const upmix = context.createGain();
1341
1451
  upmix.channelCount = 2;
1342
1452
  upmix.channelCountMode = "explicit";
@@ -1348,13 +1458,32 @@ function createPanStage(context, pan, sourceChannelCount) {
1348
1458
  const rl = context.createGain();
1349
1459
  const rr = context.createGain();
1350
1460
  let currentPan = pan;
1351
- let currentChannelCount = sourceChannelCount;
1461
+ let currentLaw = panLaw;
1462
+ let rampScheduled = false;
1463
+ const targets = () => {
1464
+ const matrix = panMatrix(currentPan, currentLaw);
1465
+ return [
1466
+ [ll, matrix.ll],
1467
+ [lr, matrix.lr],
1468
+ [rl, matrix.rl],
1469
+ [rr, matrix.rr]
1470
+ ];
1471
+ };
1352
1472
  const apply = () => {
1353
- const matrix = panMatrix(currentPan, currentChannelCount);
1354
- ll.gain.value = matrix.ll;
1355
- lr.gain.value = matrix.lr;
1356
- rl.gain.value = matrix.rl;
1357
- rr.gain.value = matrix.rr;
1473
+ for (const [node, value] of targets()) {
1474
+ if (rampScheduled) node.gain.cancelScheduledValues(context.currentTime);
1475
+ node.gain.value = value;
1476
+ }
1477
+ rampScheduled = false;
1478
+ };
1479
+ const ramp = () => {
1480
+ const now = context.currentTime;
1481
+ for (const [node, value] of targets()) {
1482
+ node.gain.cancelScheduledValues(now);
1483
+ node.gain.setValueAtTime(node.gain.value, now);
1484
+ node.gain.linearRampToValueAtTime(value, now + PAN_LAW_RAMP_SECONDS);
1485
+ }
1486
+ rampScheduled = true;
1358
1487
  };
1359
1488
  apply();
1360
1489
  upmix.connect(split);
@@ -1373,9 +1502,10 @@ function createPanStage(context, pan, sourceChannelCount) {
1373
1502
  currentPan = value;
1374
1503
  apply();
1375
1504
  },
1376
- setSourceChannelCount: (value) => {
1377
- currentChannelCount = value;
1378
- apply();
1505
+ setPanLaw: (value) => {
1506
+ if (value === currentLaw) return;
1507
+ currentLaw = value;
1508
+ ramp();
1379
1509
  },
1380
1510
  dispose: () => {
1381
1511
  for (const node of [upmix, split, ll, lr, rl, rr, merge]) {
@@ -1389,6 +1519,26 @@ function createPanStage(context, pan, sourceChannelCount) {
1389
1519
  };
1390
1520
  }
1391
1521
 
1522
+ // src/panLaw.ts
1523
+ var DEFAULT_PAN_LAW = "auto";
1524
+ function isDecidedPanLaw(value) {
1525
+ return value === "mono" || value === "stereo";
1526
+ }
1527
+ function audioPanLaw(track) {
1528
+ let law = null;
1529
+ for (const clip of track.clips) {
1530
+ const channels = clip.audioBuffer?.numberOfChannels;
1531
+ if (channels === void 0) continue;
1532
+ if (channels >= 2) return "stereo";
1533
+ law = "mono";
1534
+ }
1535
+ return law;
1536
+ }
1537
+ function trackPanLaw(track) {
1538
+ if (isDecidedPanLaw(track.panLaw)) return track.panLaw;
1539
+ return audioPanLaw(track) ?? "mono";
1540
+ }
1541
+
1392
1542
  // src/valueSource.ts
1393
1543
  function silentMeterLevels(channelCount) {
1394
1544
  const count = Math.max(0, Math.floor(channelCount));
@@ -1624,11 +1774,72 @@ function audioTrack(options) {
1624
1774
  ...base,
1625
1775
  id: options.id || base.id,
1626
1776
  clips: [clip],
1627
- renderMode: options.renderMode
1777
+ renderMode: options.renderMode,
1778
+ ...options.panLaw !== void 0 && { panLaw: options.panLaw }
1628
1779
  };
1629
1780
  return track;
1630
1781
  }
1631
1782
 
1783
+ // src/mixerGesture.ts
1784
+ var RANGE_KEYS = /* @__PURE__ */ new Set([
1785
+ "ArrowUp",
1786
+ "ArrowDown",
1787
+ "ArrowLeft",
1788
+ "ArrowRight",
1789
+ "PageUp",
1790
+ "PageDown",
1791
+ "Home",
1792
+ "End"
1793
+ ]);
1794
+ var ID_PREFIX = Math.random().toString(36).slice(2, 10);
1795
+ var idCount = 0;
1796
+ function nextId() {
1797
+ idCount += 1;
1798
+ return `mg-${ID_PREFIX}-${idCount}`;
1799
+ }
1800
+ function createMixerGesture() {
1801
+ let id = nextId();
1802
+ const end = () => {
1803
+ id = nextId();
1804
+ };
1805
+ const attach = (input) => {
1806
+ let heldKey = null;
1807
+ const onKeyDown = (event) => {
1808
+ const { key } = event;
1809
+ if (RANGE_KEYS.has(key)) heldKey = key;
1810
+ };
1811
+ const onKeyUp = (event) => {
1812
+ if (event.key !== heldKey) return;
1813
+ heldKey = null;
1814
+ end();
1815
+ };
1816
+ const onChange = () => {
1817
+ if (heldKey === null) end();
1818
+ };
1819
+ const onBlur = () => {
1820
+ heldKey = null;
1821
+ end();
1822
+ };
1823
+ input.addEventListener("keydown", onKeyDown);
1824
+ input.addEventListener("keyup", onKeyUp);
1825
+ input.addEventListener("change", onChange);
1826
+ input.addEventListener("blur", onBlur);
1827
+ return () => {
1828
+ input.removeEventListener("keydown", onKeyDown);
1829
+ input.removeEventListener("keyup", onKeyUp);
1830
+ input.removeEventListener("change", onChange);
1831
+ input.removeEventListener("blur", onBlur);
1832
+ };
1833
+ };
1834
+ return {
1835
+ get id() {
1836
+ return id;
1837
+ },
1838
+ end,
1839
+ attach
1840
+ };
1841
+ }
1842
+
1632
1843
  // src/effects/registry.ts
1633
1844
  var REGISTRY_KEY = /* @__PURE__ */ Symbol.for("@waveform-playlist/core/effect-registry/v1");
1634
1845
  function registryState() {
@@ -1735,6 +1946,7 @@ function createEffectInstance(type, audioContext, params = {}) {
1735
1946
  0 && (module.exports = {
1736
1947
  ANNOTATION_LINK_THRESHOLD_TICKS,
1737
1948
  DEFAULT_ANNOTATION_SHORTCUTS,
1949
+ DEFAULT_PAN_LAW,
1738
1950
  DEFAULT_SPECTROGRAM_COLOR_MAP,
1739
1951
  InteractionState,
1740
1952
  LINK_THRESHOLD,
@@ -1742,18 +1954,21 @@ function createEffectInstance(type, audioContext, params = {}) {
1742
1954
  METER_PEAK_DECAY,
1743
1955
  MIN_ANNOTATION_DURATION,
1744
1956
  MIN_PIXELS_PER_UNIT,
1957
+ PAN_LAW_RAMP_SECONDS,
1745
1958
  PPQN,
1746
1959
  SPECTROGRAM_DEFAULTS,
1960
+ TRACK_VIEW_FIELDS,
1747
1961
  annotationMinDurationTicks,
1748
1962
  appendPeaks,
1749
1963
  appendToAudioBuffer,
1964
+ applyAnnotationBounds,
1750
1965
  applyFadeIn,
1751
1966
  applyFadeOut,
1752
1967
  assertTrackKind,
1753
1968
  audibleLatencySamples,
1969
+ audioPanLaw,
1754
1970
  audioTrack,
1755
1971
  buildSpectrogramCanvasId,
1756
- calculateDuration,
1757
1972
  carveClipRange,
1758
1973
  clipDurationTime,
1759
1974
  clipEndTime,
@@ -1764,11 +1979,13 @@ function createEffectInstance(type, audioContext, params = {}) {
1764
1979
  clipsOverlap,
1765
1980
  computeMusicalTicks,
1766
1981
  concatenateAudioData,
1982
+ createAnnotationStore,
1767
1983
  createAudioBuffer,
1768
1984
  createClip,
1769
1985
  createClipFromSeconds,
1770
1986
  createClipFromTicks,
1771
1987
  createEffectInstance,
1988
+ createMixerGesture,
1772
1989
  createPanStage,
1773
1990
  createSourceLoader,
1774
1991
  createTimeline,
@@ -1788,6 +2005,7 @@ function createEffectInstance(type, audioContext, params = {}) {
1788
2005
  getClipsInRange,
1789
2006
  getEffectDefinitions,
1790
2007
  handleKeyboardEvent,
2008
+ isDecidedPanLaw,
1791
2009
  isLoadingClip,
1792
2010
  keyBelongsToFocusedControl,
1793
2011
  linearCurve,
@@ -1823,6 +2041,7 @@ function createEffectInstance(type, audioContext, params = {}) {
1823
2041
  ticksToSamples,
1824
2042
  trackChannelCount,
1825
2043
  trackKind,
2044
+ trackPanLaw,
1826
2045
  unregisterEffect,
1827
2046
  updateAnnotationBoundaries,
1828
2047
  watchMicrophoneDevices