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

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.mjs CHANGED
@@ -2,6 +2,14 @@
2
2
  var MAX_CANVAS_WIDTH = 1e3;
3
3
 
4
4
  // src/types/clip.ts
5
+ var TRACK_VIEW_FIELDS = [
6
+ "name",
7
+ "color",
8
+ "height",
9
+ "renderMode",
10
+ "spectrogramConfig",
11
+ "spectrogramColorMap"
12
+ ];
5
13
  function isLoadingClip(clip) {
6
14
  return !("sampleRate" in clip) || clip.sampleRate == null;
7
15
  }
@@ -659,9 +667,6 @@ function appendToAudioBuffer(audioContext, existingBuffer, newSamples, sampleRat
659
667
  const combined = concatenateAudioData([existingData, newSamples]);
660
668
  return createAudioBuffer(audioContext, [combined], sampleRate);
661
669
  }
662
- function calculateDuration(sampleCount, sampleRate) {
663
- return sampleCount / sampleRate;
664
- }
665
670
 
666
671
  // src/utils/latency.ts
667
672
  function audibleLatencySamples(outputLatency, lookAhead, sampleRate) {
@@ -799,7 +804,7 @@ function assertTrackKind(track) {
799
804
  const strayKind = clipKind(stray);
800
805
  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).";
801
806
  throw new Error(
802
- "[waveform-playlist] Track " + label(track) + " is an " + kind + " track but its clip " + label(stray) + " is " + (strayKind === "audio" ? "an audio" : "a midi") + " clip. " + fix
807
+ "[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
803
808
  );
804
809
  }
805
810
  function splitTrackByKind(track, midiTrackId = track.id + "-midi") {
@@ -999,14 +1004,17 @@ function keyBelongsToFocusedControl(event) {
999
1004
  if (ownsAllKeys(focused)) return true;
1000
1005
  return activates(focused, event.key);
1001
1006
  }
1007
+ var preventedByShortcut = /* @__PURE__ */ new WeakSet();
1002
1008
  function handleKeyboardEvent(event, shortcuts, enabled) {
1003
1009
  if (!enabled) return;
1004
1010
  if (event.repeat) return;
1011
+ if (event.key === "Escape" && event.defaultPrevented && !preventedByShortcut.has(event)) return;
1005
1012
  if (keyBelongsToFocusedControl(event)) return;
1006
1013
  const matchingShortcut = shortcuts.find((shortcut) => matchesKeyBinding(event, shortcut));
1007
1014
  if (matchingShortcut) {
1008
1015
  if (matchingShortcut.preventDefault !== false) {
1009
1016
  event.preventDefault();
1017
+ preventedByShortcut.add(event);
1010
1018
  }
1011
1019
  matchingShortcut.action();
1012
1020
  }
@@ -1203,11 +1211,104 @@ function resolveAnnotationShortcuts(remap) {
1203
1211
  });
1204
1212
  }
1205
1213
 
1214
+ // src/annotations/devFreeze.ts
1215
+ function isDevelopmentBuild() {
1216
+ try {
1217
+ return process.env.NODE_ENV !== "production";
1218
+ } catch {
1219
+ return false;
1220
+ }
1221
+ }
1222
+ function freezeAnnotations(list) {
1223
+ if (!isDevelopmentBuild()) return list;
1224
+ for (const annotation of list) {
1225
+ if (Array.isArray(annotation.lines)) Object.freeze(annotation.lines);
1226
+ Object.freeze(annotation);
1227
+ }
1228
+ return Object.freeze(list);
1229
+ }
1230
+
1231
+ // src/annotations/store.ts
1232
+ function invalid(message) {
1233
+ return new TypeError("[waveform-playlist] Invalid annotations: " + message);
1234
+ }
1235
+ function validateAnnotations(list) {
1236
+ if (!Array.isArray(list)) throw invalid("expected an array.");
1237
+ const seen = /* @__PURE__ */ new Set();
1238
+ for (const annotation of list) {
1239
+ const { id, start, end } = annotation ?? {};
1240
+ if (!Number.isFinite(start) || !Number.isFinite(end)) {
1241
+ throw invalid(
1242
+ `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.`
1243
+ );
1244
+ }
1245
+ if (seen.has(id)) throw invalid(`two annotations have the id "${id}".`);
1246
+ seen.add(id);
1247
+ }
1248
+ }
1249
+ function createAnnotationStore(initial = [], options = {}) {
1250
+ validateAnnotations(initial);
1251
+ let annotations = freezeAnnotations(initial);
1252
+ const listeners = /* @__PURE__ */ new Set();
1253
+ const { beforeChange } = options;
1254
+ const commit = (proposed) => {
1255
+ if (proposed === annotations) return;
1256
+ validateAnnotations(proposed);
1257
+ let next = proposed;
1258
+ if (beforeChange) {
1259
+ next = beforeChange(proposed, annotations);
1260
+ if (next === annotations) return;
1261
+ if (next !== proposed) validateAnnotations(next);
1262
+ }
1263
+ annotations = freezeAnnotations(next);
1264
+ for (const entry of [...listeners]) {
1265
+ if (!listeners.has(entry)) continue;
1266
+ try {
1267
+ entry.listener();
1268
+ } catch (error) {
1269
+ console.warn("[waveform-playlist] Error in an annotation store subscriber:", error);
1270
+ }
1271
+ }
1272
+ };
1273
+ return {
1274
+ getAnnotations: () => annotations,
1275
+ subscribe(listener) {
1276
+ const entry = { listener };
1277
+ listeners.add(entry);
1278
+ return () => {
1279
+ listeners.delete(entry);
1280
+ };
1281
+ },
1282
+ set(next) {
1283
+ commit(next);
1284
+ },
1285
+ update(fn) {
1286
+ commit(fn(annotations));
1287
+ }
1288
+ };
1289
+ }
1290
+
1291
+ // src/annotations/bounds.ts
1292
+ function applyAnnotationBounds(current, changes) {
1293
+ if (changes.length === 0) return current;
1294
+ const byId = new Map(changes.map((change) => [change.id, change]));
1295
+ let changed = false;
1296
+ const next = current.map((annotation) => {
1297
+ const change = byId.get(annotation.id);
1298
+ if (!change || change.start === annotation.start && change.end === annotation.end) {
1299
+ return annotation;
1300
+ }
1301
+ changed = true;
1302
+ return { ...annotation, start: change.start, end: change.end };
1303
+ });
1304
+ return changed ? next : current;
1305
+ }
1306
+
1206
1307
  // src/panStage.ts
1207
1308
  var PREFIX = "[waveform-playlist] ";
1208
- function panMatrix(pan, sourceChannelCount) {
1309
+ function panMatrix(pan, panLaw) {
1209
1310
  const p = Number.isFinite(pan) ? Math.max(-1, Math.min(1, pan)) : 0;
1210
- if (sourceChannelCount < 2) {
1311
+ if (panLaw === "mono") {
1211
1312
  const theta = (p + 1) * Math.PI / 4;
1212
1313
  return { ll: Math.cos(theta), lr: 0, rl: 0, rr: Math.sin(theta) };
1213
1314
  }
@@ -1218,7 +1319,8 @@ function panMatrix(pan, sourceChannelCount) {
1218
1319
  const x = p * Math.PI / 2;
1219
1320
  return { ll: Math.cos(x), lr: Math.sin(x), rl: 0, rr: 1 };
1220
1321
  }
1221
- function createPanStage(context, pan, sourceChannelCount) {
1322
+ var PAN_LAW_RAMP_SECONDS = 0.01;
1323
+ function createPanStage(context, pan, panLaw) {
1222
1324
  const upmix = context.createGain();
1223
1325
  upmix.channelCount = 2;
1224
1326
  upmix.channelCountMode = "explicit";
@@ -1230,13 +1332,32 @@ function createPanStage(context, pan, sourceChannelCount) {
1230
1332
  const rl = context.createGain();
1231
1333
  const rr = context.createGain();
1232
1334
  let currentPan = pan;
1233
- let currentChannelCount = sourceChannelCount;
1335
+ let currentLaw = panLaw;
1336
+ let rampScheduled = false;
1337
+ const targets = () => {
1338
+ const matrix = panMatrix(currentPan, currentLaw);
1339
+ return [
1340
+ [ll, matrix.ll],
1341
+ [lr, matrix.lr],
1342
+ [rl, matrix.rl],
1343
+ [rr, matrix.rr]
1344
+ ];
1345
+ };
1234
1346
  const apply = () => {
1235
- const matrix = panMatrix(currentPan, currentChannelCount);
1236
- ll.gain.value = matrix.ll;
1237
- lr.gain.value = matrix.lr;
1238
- rl.gain.value = matrix.rl;
1239
- rr.gain.value = matrix.rr;
1347
+ for (const [node, value] of targets()) {
1348
+ if (rampScheduled) node.gain.cancelScheduledValues(context.currentTime);
1349
+ node.gain.value = value;
1350
+ }
1351
+ rampScheduled = false;
1352
+ };
1353
+ const ramp = () => {
1354
+ const now = context.currentTime;
1355
+ for (const [node, value] of targets()) {
1356
+ node.gain.cancelScheduledValues(now);
1357
+ node.gain.setValueAtTime(node.gain.value, now);
1358
+ node.gain.linearRampToValueAtTime(value, now + PAN_LAW_RAMP_SECONDS);
1359
+ }
1360
+ rampScheduled = true;
1240
1361
  };
1241
1362
  apply();
1242
1363
  upmix.connect(split);
@@ -1255,9 +1376,10 @@ function createPanStage(context, pan, sourceChannelCount) {
1255
1376
  currentPan = value;
1256
1377
  apply();
1257
1378
  },
1258
- setSourceChannelCount: (value) => {
1259
- currentChannelCount = value;
1260
- apply();
1379
+ setPanLaw: (value) => {
1380
+ if (value === currentLaw) return;
1381
+ currentLaw = value;
1382
+ ramp();
1261
1383
  },
1262
1384
  dispose: () => {
1263
1385
  for (const node of [upmix, split, ll, lr, rl, rr, merge]) {
@@ -1271,6 +1393,26 @@ function createPanStage(context, pan, sourceChannelCount) {
1271
1393
  };
1272
1394
  }
1273
1395
 
1396
+ // src/panLaw.ts
1397
+ var DEFAULT_PAN_LAW = "auto";
1398
+ function isDecidedPanLaw(value) {
1399
+ return value === "mono" || value === "stereo";
1400
+ }
1401
+ function audioPanLaw(track) {
1402
+ let law = null;
1403
+ for (const clip of track.clips) {
1404
+ const channels = clip.audioBuffer?.numberOfChannels;
1405
+ if (channels === void 0) continue;
1406
+ if (channels >= 2) return "stereo";
1407
+ law = "mono";
1408
+ }
1409
+ return law;
1410
+ }
1411
+ function trackPanLaw(track) {
1412
+ if (isDecidedPanLaw(track.panLaw)) return track.panLaw;
1413
+ return audioPanLaw(track) ?? "mono";
1414
+ }
1415
+
1274
1416
  // src/valueSource.ts
1275
1417
  function silentMeterLevels(channelCount) {
1276
1418
  const count = Math.max(0, Math.floor(channelCount));
@@ -1506,11 +1648,72 @@ function audioTrack(options) {
1506
1648
  ...base,
1507
1649
  id: options.id || base.id,
1508
1650
  clips: [clip],
1509
- renderMode: options.renderMode
1651
+ renderMode: options.renderMode,
1652
+ ...options.panLaw !== void 0 && { panLaw: options.panLaw }
1510
1653
  };
1511
1654
  return track;
1512
1655
  }
1513
1656
 
1657
+ // src/mixerGesture.ts
1658
+ var RANGE_KEYS = /* @__PURE__ */ new Set([
1659
+ "ArrowUp",
1660
+ "ArrowDown",
1661
+ "ArrowLeft",
1662
+ "ArrowRight",
1663
+ "PageUp",
1664
+ "PageDown",
1665
+ "Home",
1666
+ "End"
1667
+ ]);
1668
+ var ID_PREFIX = Math.random().toString(36).slice(2, 10);
1669
+ var idCount = 0;
1670
+ function nextId() {
1671
+ idCount += 1;
1672
+ return `mg-${ID_PREFIX}-${idCount}`;
1673
+ }
1674
+ function createMixerGesture() {
1675
+ let id = nextId();
1676
+ const end = () => {
1677
+ id = nextId();
1678
+ };
1679
+ const attach = (input) => {
1680
+ let heldKey = null;
1681
+ const onKeyDown = (event) => {
1682
+ const { key } = event;
1683
+ if (RANGE_KEYS.has(key)) heldKey = key;
1684
+ };
1685
+ const onKeyUp = (event) => {
1686
+ if (event.key !== heldKey) return;
1687
+ heldKey = null;
1688
+ end();
1689
+ };
1690
+ const onChange = () => {
1691
+ if (heldKey === null) end();
1692
+ };
1693
+ const onBlur = () => {
1694
+ heldKey = null;
1695
+ end();
1696
+ };
1697
+ input.addEventListener("keydown", onKeyDown);
1698
+ input.addEventListener("keyup", onKeyUp);
1699
+ input.addEventListener("change", onChange);
1700
+ input.addEventListener("blur", onBlur);
1701
+ return () => {
1702
+ input.removeEventListener("keydown", onKeyDown);
1703
+ input.removeEventListener("keyup", onKeyUp);
1704
+ input.removeEventListener("change", onChange);
1705
+ input.removeEventListener("blur", onBlur);
1706
+ };
1707
+ };
1708
+ return {
1709
+ get id() {
1710
+ return id;
1711
+ },
1712
+ end,
1713
+ attach
1714
+ };
1715
+ }
1716
+
1514
1717
  // src/effects/registry.ts
1515
1718
  var REGISTRY_KEY = /* @__PURE__ */ Symbol.for("@waveform-playlist/core/effect-registry/v1");
1516
1719
  function registryState() {
@@ -1616,6 +1819,7 @@ function createEffectInstance(type, audioContext, params = {}) {
1616
1819
  export {
1617
1820
  ANNOTATION_LINK_THRESHOLD_TICKS,
1618
1821
  DEFAULT_ANNOTATION_SHORTCUTS,
1822
+ DEFAULT_PAN_LAW,
1619
1823
  DEFAULT_SPECTROGRAM_COLOR_MAP,
1620
1824
  InteractionState,
1621
1825
  LINK_THRESHOLD,
@@ -1623,18 +1827,21 @@ export {
1623
1827
  METER_PEAK_DECAY,
1624
1828
  MIN_ANNOTATION_DURATION,
1625
1829
  MIN_PIXELS_PER_UNIT,
1830
+ PAN_LAW_RAMP_SECONDS,
1626
1831
  PPQN,
1627
1832
  SPECTROGRAM_DEFAULTS,
1833
+ TRACK_VIEW_FIELDS,
1628
1834
  annotationMinDurationTicks,
1629
1835
  appendPeaks,
1630
1836
  appendToAudioBuffer,
1837
+ applyAnnotationBounds,
1631
1838
  applyFadeIn,
1632
1839
  applyFadeOut,
1633
1840
  assertTrackKind,
1634
1841
  audibleLatencySamples,
1842
+ audioPanLaw,
1635
1843
  audioTrack,
1636
1844
  buildSpectrogramCanvasId,
1637
- calculateDuration,
1638
1845
  carveClipRange,
1639
1846
  clipDurationTime,
1640
1847
  clipEndTime,
@@ -1645,11 +1852,13 @@ export {
1645
1852
  clipsOverlap,
1646
1853
  computeMusicalTicks,
1647
1854
  concatenateAudioData,
1855
+ createAnnotationStore,
1648
1856
  createAudioBuffer,
1649
1857
  createClip,
1650
1858
  createClipFromSeconds,
1651
1859
  createClipFromTicks,
1652
1860
  createEffectInstance,
1861
+ createMixerGesture,
1653
1862
  createPanStage,
1654
1863
  createSourceLoader,
1655
1864
  createTimeline,
@@ -1669,6 +1878,7 @@ export {
1669
1878
  getClipsInRange,
1670
1879
  getEffectDefinitions,
1671
1880
  handleKeyboardEvent,
1881
+ isDecidedPanLaw,
1672
1882
  isLoadingClip,
1673
1883
  keyBelongsToFocusedControl,
1674
1884
  linearCurve,
@@ -1704,6 +1914,7 @@ export {
1704
1914
  ticksToSamples,
1705
1915
  trackChannelCount,
1706
1916
  trackKind,
1917
+ trackPanLaw,
1707
1918
  unregisterEffect,
1708
1919
  updateAnnotationBoundaries,
1709
1920
  watchMicrophoneDevices