@waveform-playlist/core 12.6.2 → 13.0.0-next.1
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/README.md +76 -2
- package/dist/index.d.mts +470 -43
- package/dist/index.d.ts +470 -43
- package/dist/index.js +610 -38
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +589 -37
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -2,6 +2,9 @@
|
|
|
2
2
|
var MAX_CANVAS_WIDTH = 1e3;
|
|
3
3
|
|
|
4
4
|
// src/types/clip.ts
|
|
5
|
+
function isLoadingClip(clip) {
|
|
6
|
+
return !("sampleRate" in clip) || clip.sampleRate == null;
|
|
7
|
+
}
|
|
5
8
|
function createClipFromTicks(options) {
|
|
6
9
|
const { startTick, ticksToSeconds, bpm, ppqn } = options;
|
|
7
10
|
if (startTick < 0) {
|
|
@@ -38,7 +41,8 @@ function createClipFromTicks(options) {
|
|
|
38
41
|
sourceDurationSamples: options.sourceDurationSamples,
|
|
39
42
|
midiNotes: options.midiNotes,
|
|
40
43
|
midiChannel: options.midiChannel,
|
|
41
|
-
midiProgram: options.midiProgram
|
|
44
|
+
midiProgram: options.midiProgram,
|
|
45
|
+
src: options.src
|
|
42
46
|
});
|
|
43
47
|
}
|
|
44
48
|
function createClip(options) {
|
|
@@ -55,7 +59,8 @@ function createClip(options) {
|
|
|
55
59
|
waveformData,
|
|
56
60
|
midiNotes,
|
|
57
61
|
midiChannel,
|
|
58
|
-
midiProgram
|
|
62
|
+
midiProgram,
|
|
63
|
+
src
|
|
59
64
|
} = options;
|
|
60
65
|
const sampleRate = audioBuffer?.sampleRate ?? options.sampleRate ?? waveformData?.sample_rate;
|
|
61
66
|
const sourceDurationSamples = audioBuffer?.length ?? options.sourceDurationSamples ?? (waveformData && sampleRate ? Math.ceil(waveformData.duration * sampleRate) : void 0);
|
|
@@ -92,7 +97,8 @@ function createClip(options) {
|
|
|
92
97
|
waveformData,
|
|
93
98
|
midiNotes,
|
|
94
99
|
midiChannel,
|
|
95
|
-
midiProgram
|
|
100
|
+
midiProgram,
|
|
101
|
+
...src !== void 0 ? { src } : {}
|
|
96
102
|
};
|
|
97
103
|
}
|
|
98
104
|
function createClipFromSeconds(options) {
|
|
@@ -108,7 +114,8 @@ function createClipFromSeconds(options) {
|
|
|
108
114
|
waveformData,
|
|
109
115
|
midiNotes,
|
|
110
116
|
midiChannel,
|
|
111
|
-
midiProgram
|
|
117
|
+
midiProgram,
|
|
118
|
+
src
|
|
112
119
|
} = options;
|
|
113
120
|
const sampleRate = audioBuffer?.sampleRate ?? options.sampleRate ?? waveformData?.sample_rate;
|
|
114
121
|
if (sampleRate === void 0) {
|
|
@@ -139,12 +146,14 @@ function createClipFromSeconds(options) {
|
|
|
139
146
|
waveformData,
|
|
140
147
|
midiNotes,
|
|
141
148
|
midiChannel,
|
|
142
|
-
midiProgram
|
|
149
|
+
midiProgram,
|
|
150
|
+
src
|
|
143
151
|
});
|
|
144
152
|
}
|
|
145
153
|
function createTrack(options) {
|
|
146
154
|
const {
|
|
147
155
|
name,
|
|
156
|
+
kind = "audio",
|
|
148
157
|
clips = [],
|
|
149
158
|
muted = false,
|
|
150
159
|
soloed = false,
|
|
@@ -157,6 +166,7 @@ function createTrack(options) {
|
|
|
157
166
|
} = options;
|
|
158
167
|
return {
|
|
159
168
|
id: generateId(),
|
|
169
|
+
kind,
|
|
160
170
|
name,
|
|
161
171
|
clips,
|
|
162
172
|
muted,
|
|
@@ -444,14 +454,14 @@ function computeMusicalTicks(params) {
|
|
|
444
454
|
}
|
|
445
455
|
const barIndexInSegment = Math.floor(tickOffsetInSegment / tpBar);
|
|
446
456
|
const barIndex = barOffset + barIndexInSegment;
|
|
447
|
-
let
|
|
457
|
+
let label2;
|
|
448
458
|
if (type === "major") {
|
|
449
|
-
|
|
459
|
+
label2 = `${barIndex + 1}`;
|
|
450
460
|
} else if (type === "minor" && tpBeat / ticksPerPixel >= MIN_PIXELS_PER_LABEL) {
|
|
451
461
|
const beatInBar = Math.floor(tickOffsetInSegment % tpBar / tpBeat) + 1;
|
|
452
|
-
|
|
462
|
+
label2 = `${barIndex + 1}.${beatInBar}`;
|
|
453
463
|
}
|
|
454
|
-
ticks.push({ pixel, type, barIndex, ...
|
|
464
|
+
ticks.push({ pixel, type, barIndex, ...label2 !== void 0 ? { label: label2 } : {} });
|
|
455
465
|
}
|
|
456
466
|
}
|
|
457
467
|
ticks.sort((a, b) => a.pixel - b.pixel);
|
|
@@ -719,6 +729,31 @@ function carveClipRange(clips, rangeStart, rangeEnd) {
|
|
|
719
729
|
return result;
|
|
720
730
|
}
|
|
721
731
|
|
|
732
|
+
// src/utils/meterLevels.ts
|
|
733
|
+
var METER_PEAK_DECAY = 0.98;
|
|
734
|
+
function readMeterMessage({
|
|
735
|
+
data,
|
|
736
|
+
previousSmoothedPeaks,
|
|
737
|
+
heldPeakLevels,
|
|
738
|
+
minChannels = 0
|
|
739
|
+
}) {
|
|
740
|
+
const channels = Math.floor(data.length / 2);
|
|
741
|
+
const smoothedPeaks = [];
|
|
742
|
+
const peaks = [];
|
|
743
|
+
const rms = [];
|
|
744
|
+
for (let ch = 0; ch < channels; ch++) {
|
|
745
|
+
const smoothed = Math.max(data[ch], (previousSmoothedPeaks[ch] ?? 0) * METER_PEAK_DECAY);
|
|
746
|
+
smoothedPeaks.push(smoothed);
|
|
747
|
+
peaks.push(gainToNormalized(smoothed));
|
|
748
|
+
rms.push(gainToNormalized(data[channels + ch]));
|
|
749
|
+
}
|
|
750
|
+
const mirror = channels > 0 && channels < minChannels;
|
|
751
|
+
const levels = mirror ? new Array(minChannels).fill(peaks[0]) : peaks;
|
|
752
|
+
const rmsLevels = mirror ? new Array(minChannels).fill(rms[0]) : rms;
|
|
753
|
+
const peakLevels = levels.map((level, i) => Math.max(heldPeakLevels[i] ?? 0, level));
|
|
754
|
+
return { smoothedPeaks, levels: { levels, peakLevels, rmsLevels } };
|
|
755
|
+
}
|
|
756
|
+
|
|
722
757
|
// src/clipTimeHelpers.ts
|
|
723
758
|
function clipStartTime(clip) {
|
|
724
759
|
return clip.startSample / clip.sampleRate;
|
|
@@ -742,6 +777,46 @@ function clipPixelWidth(startSample, durationSamples, samplesPerPixel) {
|
|
|
742
777
|
return Math.floor((startSample + durationSamples) / samplesPerPixel) - Math.floor(startSample / samplesPerPixel);
|
|
743
778
|
}
|
|
744
779
|
|
|
780
|
+
// src/trackKind.ts
|
|
781
|
+
function clipKind(clip) {
|
|
782
|
+
return clip.midiNotes != null ? "midi" : "audio";
|
|
783
|
+
}
|
|
784
|
+
function trackKind(track) {
|
|
785
|
+
return track.kind ?? "audio";
|
|
786
|
+
}
|
|
787
|
+
function label(named) {
|
|
788
|
+
return '"' + (named.name || named.id) + '"';
|
|
789
|
+
}
|
|
790
|
+
function assertTrackKind(track) {
|
|
791
|
+
const kind = trackKind(track);
|
|
792
|
+
if (kind !== "audio" && kind !== "midi") {
|
|
793
|
+
throw new Error(
|
|
794
|
+
"[waveform-playlist] Track " + label(track) + ' has kind "' + String(kind) + `"; a track's kind is 'audio' or 'midi'.`
|
|
795
|
+
);
|
|
796
|
+
}
|
|
797
|
+
const stray = track.clips.find((clip) => clipKind(clip) !== kind);
|
|
798
|
+
if (!stray) return;
|
|
799
|
+
const strayKind = clipKind(stray);
|
|
800
|
+
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
|
+
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
|
|
803
|
+
);
|
|
804
|
+
}
|
|
805
|
+
function splitTrackByKind(track, midiTrackId = track.id + "-midi") {
|
|
806
|
+
const audioClips = track.clips.filter((clip) => clipKind(clip) === "audio");
|
|
807
|
+
const midiClips = track.clips.filter((clip) => clipKind(clip) === "midi");
|
|
808
|
+
if (midiClips.length === 0) {
|
|
809
|
+
return [{ ...track, kind: track.clips.length === 0 ? track.kind ?? "audio" : "audio" }];
|
|
810
|
+
}
|
|
811
|
+
if (audioClips.length === 0) {
|
|
812
|
+
return [{ ...track, kind: "midi" }];
|
|
813
|
+
}
|
|
814
|
+
return [
|
|
815
|
+
{ ...track, kind: "audio", clips: audioClips },
|
|
816
|
+
{ ...track, id: midiTrackId, kind: "midi", clips: midiClips }
|
|
817
|
+
];
|
|
818
|
+
}
|
|
819
|
+
|
|
745
820
|
// src/spectrogramCanvasId.ts
|
|
746
821
|
var CANVAS_ID_RE = /^(.+)-ch(\d+)-chunk(\d+)$/;
|
|
747
822
|
function buildSpectrogramCanvasId(parts) {
|
|
@@ -854,13 +929,80 @@ function matchesKeyBinding(event, binding) {
|
|
|
854
929
|
const altMatch = binding.altKey === void 0 || event.altKey === binding.altKey;
|
|
855
930
|
return keyMatch && ctrlMatch && shiftMatch && metaMatch && altMatch;
|
|
856
931
|
}
|
|
932
|
+
var ELEMENT_NODE = 1;
|
|
933
|
+
function elementPath(event) {
|
|
934
|
+
const composed = typeof event.composedPath === "function" ? event.composedPath() : [];
|
|
935
|
+
if (composed.length > 0) {
|
|
936
|
+
return composed.filter(
|
|
937
|
+
(node) => node?.nodeType === ELEMENT_NODE
|
|
938
|
+
);
|
|
939
|
+
}
|
|
940
|
+
const path = [];
|
|
941
|
+
let current = event.target;
|
|
942
|
+
while (current && current.nodeType === ELEMENT_NODE) {
|
|
943
|
+
path.push(current);
|
|
944
|
+
current = current.parentElement;
|
|
945
|
+
}
|
|
946
|
+
return path;
|
|
947
|
+
}
|
|
948
|
+
function matchesOr(element, selector, fallback) {
|
|
949
|
+
try {
|
|
950
|
+
return element.matches(selector);
|
|
951
|
+
} catch {
|
|
952
|
+
return fallback;
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
function ownsAllKeys(element) {
|
|
956
|
+
const tag = element.tagName;
|
|
957
|
+
return tag === "INPUT" || tag === "TEXTAREA" || tag === "SELECT" || element.isContentEditable === true;
|
|
958
|
+
}
|
|
959
|
+
function activates(element, key) {
|
|
960
|
+
if (key !== " " && key !== "Enter") return false;
|
|
961
|
+
const tag = element.tagName;
|
|
962
|
+
if (tag === "BUTTON") return element.disabled !== true;
|
|
963
|
+
if (tag === "SUMMARY") return true;
|
|
964
|
+
return key === "Enter" && (tag === "A" || tag === "AREA") && element.hasAttribute("href");
|
|
965
|
+
}
|
|
966
|
+
function isRendered(element) {
|
|
967
|
+
if (element.hasAttribute("hidden")) return false;
|
|
968
|
+
if (element.getAttribute("aria-hidden") === "true") return false;
|
|
969
|
+
const check = element.checkVisibility;
|
|
970
|
+
if (typeof check !== "function") return true;
|
|
971
|
+
try {
|
|
972
|
+
return check.call(element, { visibilityProperty: true, opacityProperty: true });
|
|
973
|
+
} catch {
|
|
974
|
+
return true;
|
|
975
|
+
}
|
|
976
|
+
}
|
|
977
|
+
function isOpenModal(element) {
|
|
978
|
+
if (element.getAttribute("aria-modal") === "true") {
|
|
979
|
+
return isRendered(element);
|
|
980
|
+
}
|
|
981
|
+
return element.tagName === "DIALOG" && element.hasAttribute("open") && matchesOr(element, ":modal", true);
|
|
982
|
+
}
|
|
983
|
+
function documentHasOpenModal(doc) {
|
|
984
|
+
if (!doc) return false;
|
|
985
|
+
try {
|
|
986
|
+
return Array.from(doc.querySelectorAll('dialog[open], [aria-modal="true"]')).some(isOpenModal);
|
|
987
|
+
} catch {
|
|
988
|
+
return false;
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
function keyBelongsToFocusedControl(event) {
|
|
992
|
+
const path = elementPath(event);
|
|
993
|
+
const focused = path[0];
|
|
994
|
+
if (event.key === "Escape") {
|
|
995
|
+
const doc = focused?.ownerDocument ?? (typeof document !== "undefined" ? document : null);
|
|
996
|
+
if (path.some(isOpenModal) || documentHasOpenModal(doc)) return true;
|
|
997
|
+
}
|
|
998
|
+
if (!focused) return false;
|
|
999
|
+
if (ownsAllKeys(focused)) return true;
|
|
1000
|
+
return activates(focused, event.key);
|
|
1001
|
+
}
|
|
857
1002
|
function handleKeyboardEvent(event, shortcuts, enabled) {
|
|
858
1003
|
if (!enabled) return;
|
|
859
1004
|
if (event.repeat) return;
|
|
860
|
-
|
|
861
|
-
if (target.tagName === "INPUT" || target.tagName === "TEXTAREA" || target.isContentEditable) {
|
|
862
|
-
return;
|
|
863
|
-
}
|
|
1005
|
+
if (keyBelongsToFocusedControl(event)) return;
|
|
864
1006
|
const matchingShortcut = shortcuts.find((shortcut) => matchesKeyBinding(event, shortcut));
|
|
865
1007
|
if (matchingShortcut) {
|
|
866
1008
|
if (matchingShortcut.preventDefault !== false) {
|
|
@@ -869,24 +1011,6 @@ function handleKeyboardEvent(event, shortcuts, enabled) {
|
|
|
869
1011
|
matchingShortcut.action();
|
|
870
1012
|
}
|
|
871
1013
|
}
|
|
872
|
-
var getShortcutLabel = (shortcut) => {
|
|
873
|
-
const parts = [];
|
|
874
|
-
const isMac = typeof navigator !== "undefined" && navigator.platform.includes("Mac");
|
|
875
|
-
if (shortcut.metaKey) {
|
|
876
|
-
parts.push(isMac ? "Cmd" : "Ctrl");
|
|
877
|
-
}
|
|
878
|
-
if (shortcut.ctrlKey && !shortcut.metaKey) {
|
|
879
|
-
parts.push("Ctrl");
|
|
880
|
-
}
|
|
881
|
-
if (shortcut.altKey) {
|
|
882
|
-
parts.push(isMac ? "Option" : "Alt");
|
|
883
|
-
}
|
|
884
|
-
if (shortcut.shiftKey) {
|
|
885
|
-
parts.push("Shift");
|
|
886
|
-
}
|
|
887
|
-
parts.push(shortcut.key.toUpperCase());
|
|
888
|
-
return parts.join("+");
|
|
889
|
-
};
|
|
890
1014
|
|
|
891
1015
|
// src/probeRangeSupport.ts
|
|
892
1016
|
async function probeRangeSupport(url, fetchImpl = fetch) {
|
|
@@ -978,14 +1102,13 @@ function updateAnnotationBoundaries(params, options = {}) {
|
|
|
978
1102
|
const annotation = annotations[annotationIndex];
|
|
979
1103
|
if (isDraggingStart) {
|
|
980
1104
|
const constrainedStart = Math.min(annotation.end - minDuration, Math.max(0, newTime));
|
|
981
|
-
const delta = constrainedStart - annotation.start;
|
|
982
1105
|
updatedAnnotations[annotationIndex] = { ...annotation, start: constrainedStart };
|
|
983
1106
|
if (linkEndpoints && annotationIndex > 0) {
|
|
984
1107
|
const prevAnnotation = updatedAnnotations[annotationIndex - 1];
|
|
985
1108
|
if (Math.abs(prevAnnotation.end - annotation.start) < linkThreshold) {
|
|
986
1109
|
updatedAnnotations[annotationIndex - 1] = {
|
|
987
1110
|
...prevAnnotation,
|
|
988
|
-
end: Math.max(prevAnnotation.start + minDuration,
|
|
1111
|
+
end: Math.max(prevAnnotation.start + minDuration, constrainedStart)
|
|
989
1112
|
};
|
|
990
1113
|
} else if (constrainedStart <= prevAnnotation.end) {
|
|
991
1114
|
updatedAnnotations[annotationIndex] = {
|
|
@@ -1001,15 +1124,13 @@ function updateAnnotationBoundaries(params, options = {}) {
|
|
|
1001
1124
|
}
|
|
1002
1125
|
} else {
|
|
1003
1126
|
const constrainedEnd = Math.max(annotation.start + minDuration, Math.min(newTime, duration));
|
|
1004
|
-
const delta = constrainedEnd - annotation.end;
|
|
1005
1127
|
updatedAnnotations[annotationIndex] = { ...annotation, end: constrainedEnd };
|
|
1006
1128
|
if (linkEndpoints && annotationIndex < updatedAnnotations.length - 1) {
|
|
1007
1129
|
const nextAnnotation = updatedAnnotations[annotationIndex + 1];
|
|
1008
1130
|
if (Math.abs(nextAnnotation.start - annotation.end) < linkThreshold) {
|
|
1009
|
-
const newStart = nextAnnotation.start + delta;
|
|
1010
1131
|
updatedAnnotations[annotationIndex + 1] = {
|
|
1011
1132
|
...nextAnnotation,
|
|
1012
|
-
start: Math.min(nextAnnotation.end - minDuration,
|
|
1133
|
+
start: Math.min(nextAnnotation.end - minDuration, constrainedEnd)
|
|
1013
1134
|
};
|
|
1014
1135
|
let currentIndex = annotationIndex + 1;
|
|
1015
1136
|
while (currentIndex < updatedAnnotations.length - 1) {
|
|
@@ -1081,6 +1202,417 @@ function resolveAnnotationShortcuts(remap) {
|
|
|
1081
1202
|
return bindings.map((binding) => ({ action, binding }));
|
|
1082
1203
|
});
|
|
1083
1204
|
}
|
|
1205
|
+
|
|
1206
|
+
// src/panStage.ts
|
|
1207
|
+
var PREFIX = "[waveform-playlist] ";
|
|
1208
|
+
function panMatrix(pan, sourceChannelCount) {
|
|
1209
|
+
const p = Number.isFinite(pan) ? Math.max(-1, Math.min(1, pan)) : 0;
|
|
1210
|
+
if (sourceChannelCount < 2) {
|
|
1211
|
+
const theta = (p + 1) * Math.PI / 4;
|
|
1212
|
+
return { ll: Math.cos(theta), lr: 0, rl: 0, rr: Math.sin(theta) };
|
|
1213
|
+
}
|
|
1214
|
+
if (p <= 0) {
|
|
1215
|
+
const x2 = (p + 1) * Math.PI / 2;
|
|
1216
|
+
return { ll: 1, lr: 0, rl: Math.cos(x2), rr: Math.sin(x2) };
|
|
1217
|
+
}
|
|
1218
|
+
const x = p * Math.PI / 2;
|
|
1219
|
+
return { ll: Math.cos(x), lr: Math.sin(x), rl: 0, rr: 1 };
|
|
1220
|
+
}
|
|
1221
|
+
function createPanStage(context, pan, sourceChannelCount) {
|
|
1222
|
+
const upmix = context.createGain();
|
|
1223
|
+
upmix.channelCount = 2;
|
|
1224
|
+
upmix.channelCountMode = "explicit";
|
|
1225
|
+
upmix.channelInterpretation = "speakers";
|
|
1226
|
+
const split = context.createChannelSplitter(2);
|
|
1227
|
+
const merge = context.createChannelMerger(2);
|
|
1228
|
+
const ll = context.createGain();
|
|
1229
|
+
const lr = context.createGain();
|
|
1230
|
+
const rl = context.createGain();
|
|
1231
|
+
const rr = context.createGain();
|
|
1232
|
+
let currentPan = pan;
|
|
1233
|
+
let currentChannelCount = sourceChannelCount;
|
|
1234
|
+
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;
|
|
1240
|
+
};
|
|
1241
|
+
apply();
|
|
1242
|
+
upmix.connect(split);
|
|
1243
|
+
split.connect(ll, 0, 0);
|
|
1244
|
+
split.connect(lr, 0, 0);
|
|
1245
|
+
split.connect(rl, 1, 0);
|
|
1246
|
+
split.connect(rr, 1, 0);
|
|
1247
|
+
ll.connect(merge, 0, 0);
|
|
1248
|
+
rl.connect(merge, 0, 0);
|
|
1249
|
+
lr.connect(merge, 0, 1);
|
|
1250
|
+
rr.connect(merge, 0, 1);
|
|
1251
|
+
return {
|
|
1252
|
+
input: upmix,
|
|
1253
|
+
output: merge,
|
|
1254
|
+
setPan: (value) => {
|
|
1255
|
+
currentPan = value;
|
|
1256
|
+
apply();
|
|
1257
|
+
},
|
|
1258
|
+
setSourceChannelCount: (value) => {
|
|
1259
|
+
currentChannelCount = value;
|
|
1260
|
+
apply();
|
|
1261
|
+
},
|
|
1262
|
+
dispose: () => {
|
|
1263
|
+
for (const node of [upmix, split, ll, lr, rl, rr, merge]) {
|
|
1264
|
+
try {
|
|
1265
|
+
node.disconnect();
|
|
1266
|
+
} catch (err) {
|
|
1267
|
+
console.warn(PREFIX + "pan stage: error disconnecting node: " + String(err));
|
|
1268
|
+
}
|
|
1269
|
+
}
|
|
1270
|
+
}
|
|
1271
|
+
};
|
|
1272
|
+
}
|
|
1273
|
+
|
|
1274
|
+
// src/valueSource.ts
|
|
1275
|
+
function silentMeterLevels(channelCount) {
|
|
1276
|
+
const count = Math.max(0, Math.floor(channelCount));
|
|
1277
|
+
return {
|
|
1278
|
+
levels: new Array(count).fill(0),
|
|
1279
|
+
peakLevels: new Array(count).fill(0),
|
|
1280
|
+
rmsLevels: new Array(count).fill(0)
|
|
1281
|
+
};
|
|
1282
|
+
}
|
|
1283
|
+
function createValueSource(initial) {
|
|
1284
|
+
let current = initial;
|
|
1285
|
+
const listeners = /* @__PURE__ */ new Map();
|
|
1286
|
+
const source = {
|
|
1287
|
+
subscribe(listener) {
|
|
1288
|
+
const token = /* @__PURE__ */ Symbol("subscription");
|
|
1289
|
+
listeners.set(token, listener);
|
|
1290
|
+
return () => {
|
|
1291
|
+
listeners.delete(token);
|
|
1292
|
+
};
|
|
1293
|
+
},
|
|
1294
|
+
getSnapshot() {
|
|
1295
|
+
return current;
|
|
1296
|
+
}
|
|
1297
|
+
};
|
|
1298
|
+
const publish = (value) => {
|
|
1299
|
+
if (Object.is(value, current)) return;
|
|
1300
|
+
current = value;
|
|
1301
|
+
for (const listener of [...listeners.values()]) {
|
|
1302
|
+
try {
|
|
1303
|
+
listener(value);
|
|
1304
|
+
} catch (err) {
|
|
1305
|
+
console.warn("[waveform-playlist] A value source listener threw: " + String(err));
|
|
1306
|
+
}
|
|
1307
|
+
}
|
|
1308
|
+
};
|
|
1309
|
+
return { source, publish };
|
|
1310
|
+
}
|
|
1311
|
+
|
|
1312
|
+
// src/sourceLoader.ts
|
|
1313
|
+
function toError(reason) {
|
|
1314
|
+
return reason instanceof Error ? reason : new Error(String(reason));
|
|
1315
|
+
}
|
|
1316
|
+
function createSourceLoader(options) {
|
|
1317
|
+
const { load } = options;
|
|
1318
|
+
const entries = /* @__PURE__ */ new Map();
|
|
1319
|
+
let suspended = false;
|
|
1320
|
+
const { source, publish } = createValueSource(/* @__PURE__ */ new Map());
|
|
1321
|
+
const publishEntries = () => {
|
|
1322
|
+
const next = /* @__PURE__ */ new Map();
|
|
1323
|
+
for (const [key, entry] of entries) next.set(key, entry.state);
|
|
1324
|
+
publish(next);
|
|
1325
|
+
};
|
|
1326
|
+
const abort = (entry) => {
|
|
1327
|
+
entry.controller?.abort();
|
|
1328
|
+
};
|
|
1329
|
+
const start = (key) => {
|
|
1330
|
+
const token = /* @__PURE__ */ Symbol("load");
|
|
1331
|
+
const controller = new AbortController();
|
|
1332
|
+
const entry = { state: { status: "pending" }, controller, token };
|
|
1333
|
+
entries.set(key, entry);
|
|
1334
|
+
const settle = (state) => {
|
|
1335
|
+
const current = entries.get(key);
|
|
1336
|
+
if (!current || current.token !== token) return;
|
|
1337
|
+
entries.set(key, { state, token });
|
|
1338
|
+
publishEntries();
|
|
1339
|
+
};
|
|
1340
|
+
let promise;
|
|
1341
|
+
try {
|
|
1342
|
+
promise = Promise.resolve(load(key, controller.signal));
|
|
1343
|
+
} catch (err) {
|
|
1344
|
+
entries.set(key, { state: { status: "failed", error: toError(err) }, token });
|
|
1345
|
+
return;
|
|
1346
|
+
}
|
|
1347
|
+
promise.then(
|
|
1348
|
+
(value) => settle({ status: "loaded", value }),
|
|
1349
|
+
(err) => {
|
|
1350
|
+
if (controller.signal.aborted) return;
|
|
1351
|
+
settle({ status: "failed", error: toError(err) });
|
|
1352
|
+
}
|
|
1353
|
+
);
|
|
1354
|
+
};
|
|
1355
|
+
return {
|
|
1356
|
+
setWanted(keys) {
|
|
1357
|
+
const wanted = new Set(keys);
|
|
1358
|
+
suspended = false;
|
|
1359
|
+
let changed = false;
|
|
1360
|
+
for (const [key, entry] of [...entries]) {
|
|
1361
|
+
if (wanted.has(key)) continue;
|
|
1362
|
+
abort(entry);
|
|
1363
|
+
entries.delete(key);
|
|
1364
|
+
changed = true;
|
|
1365
|
+
}
|
|
1366
|
+
for (const key of wanted) {
|
|
1367
|
+
const entry = entries.get(key);
|
|
1368
|
+
if (entry && (entry.state.status !== "pending" || entry.controller)) continue;
|
|
1369
|
+
start(key);
|
|
1370
|
+
if (!entry || entries.get(key)?.state.status !== "pending") changed = true;
|
|
1371
|
+
}
|
|
1372
|
+
if (changed) publishEntries();
|
|
1373
|
+
},
|
|
1374
|
+
suspend() {
|
|
1375
|
+
suspended = true;
|
|
1376
|
+
for (const [key, entry] of [...entries]) {
|
|
1377
|
+
if (entry.state.status !== "pending" || !entry.controller) continue;
|
|
1378
|
+
abort(entry);
|
|
1379
|
+
entries.set(key, { state: entry.state, token: /* @__PURE__ */ Symbol("suspended") });
|
|
1380
|
+
}
|
|
1381
|
+
},
|
|
1382
|
+
provide(key, value) {
|
|
1383
|
+
const entry = entries.get(key);
|
|
1384
|
+
if (entry?.state.status === "loaded") return;
|
|
1385
|
+
if (entry) abort(entry);
|
|
1386
|
+
entries.set(key, { state: { status: "loaded", value }, token: /* @__PURE__ */ Symbol("provided") });
|
|
1387
|
+
publishEntries();
|
|
1388
|
+
},
|
|
1389
|
+
retry(key) {
|
|
1390
|
+
if (entries.get(key)?.state.status !== "failed") return;
|
|
1391
|
+
if (suspended) {
|
|
1392
|
+
entries.set(key, { state: { status: "pending" }, token: /* @__PURE__ */ Symbol("suspended") });
|
|
1393
|
+
} else {
|
|
1394
|
+
start(key);
|
|
1395
|
+
}
|
|
1396
|
+
publishEntries();
|
|
1397
|
+
},
|
|
1398
|
+
subscribe: source.subscribe,
|
|
1399
|
+
getSnapshot: source.getSnapshot
|
|
1400
|
+
};
|
|
1401
|
+
}
|
|
1402
|
+
|
|
1403
|
+
// src/audioTrack.ts
|
|
1404
|
+
function generateId2() {
|
|
1405
|
+
return `${Date.now()}-${Math.random().toString(36).slice(2, 11)}`;
|
|
1406
|
+
}
|
|
1407
|
+
function checkNonNegative(name, value) {
|
|
1408
|
+
if (value !== void 0 && !(Number.isFinite(value) && value >= 0)) {
|
|
1409
|
+
throw new Error(`audioTrack: ${name} must be a finite number of seconds, 0 or more`);
|
|
1410
|
+
}
|
|
1411
|
+
}
|
|
1412
|
+
function checkOptions(options) {
|
|
1413
|
+
checkNonNegative("start", options.start);
|
|
1414
|
+
checkNonNegative("offset", options.offset);
|
|
1415
|
+
const { duration } = options;
|
|
1416
|
+
if (duration !== void 0 && !(Number.isFinite(duration) && duration > 0)) {
|
|
1417
|
+
throw new Error("audioTrack: duration must be a positive finite number of seconds");
|
|
1418
|
+
}
|
|
1419
|
+
}
|
|
1420
|
+
function decodeName(name) {
|
|
1421
|
+
try {
|
|
1422
|
+
return decodeURIComponent(name);
|
|
1423
|
+
} catch {
|
|
1424
|
+
return name;
|
|
1425
|
+
}
|
|
1426
|
+
}
|
|
1427
|
+
function defaultName(src) {
|
|
1428
|
+
const raw = typeof src === "string" ? src : typeof File !== "undefined" && src instanceof File ? src.name : "";
|
|
1429
|
+
if (/^(data|blob):/i.test(raw)) return "Untitled";
|
|
1430
|
+
const path = raw.split(/[?#]/)[0];
|
|
1431
|
+
const file = path.slice(path.lastIndexOf("/") + 1);
|
|
1432
|
+
const dot = file.lastIndexOf(".");
|
|
1433
|
+
const base = dot > 0 ? file.slice(0, dot) : file;
|
|
1434
|
+
return decodeName(base) || "Untitled";
|
|
1435
|
+
}
|
|
1436
|
+
function fitToAudio(name, offset, duration, sampleRate, sourceSamples) {
|
|
1437
|
+
const offsetSamples = Math.round(offset * sampleRate);
|
|
1438
|
+
if (offsetSamples >= sourceSamples) {
|
|
1439
|
+
throw new Error("audioTrack: offset is past the end of the audio");
|
|
1440
|
+
}
|
|
1441
|
+
const remaining = sourceSamples - offsetSamples;
|
|
1442
|
+
if (duration === void 0) return remaining / sampleRate;
|
|
1443
|
+
const wanted = Math.round(duration * sampleRate);
|
|
1444
|
+
if (offsetSamples + wanted > sourceSamples + 1) {
|
|
1445
|
+
console.warn(
|
|
1446
|
+
`[waveform-playlist] audioTrack: offset + duration of "${name}" runs past the end of the audio; the clip is cut to the end.`
|
|
1447
|
+
);
|
|
1448
|
+
}
|
|
1449
|
+
return Math.min(wanted, remaining) / sampleRate;
|
|
1450
|
+
}
|
|
1451
|
+
function buildLoadingClip(id, src, name, options) {
|
|
1452
|
+
const given = {
|
|
1453
|
+
start: options.start,
|
|
1454
|
+
duration: options.duration,
|
|
1455
|
+
offset: options.offset,
|
|
1456
|
+
gain: options.gain,
|
|
1457
|
+
fadeIn: options.fadeIn,
|
|
1458
|
+
fadeOut: options.fadeOut,
|
|
1459
|
+
color: options.color,
|
|
1460
|
+
waveformData: options.waveformData
|
|
1461
|
+
};
|
|
1462
|
+
const present = Object.entries(given).filter(([, value]) => value !== void 0);
|
|
1463
|
+
return { id, src, name, ...Object.fromEntries(present) };
|
|
1464
|
+
}
|
|
1465
|
+
function buildClip(options, name) {
|
|
1466
|
+
const { audioBuffer, waveformData, start = 0, offset = 0, duration } = options;
|
|
1467
|
+
const src = options.src || void 0;
|
|
1468
|
+
const id = options.clipId || generateId2();
|
|
1469
|
+
if (src !== void 0 && !audioBuffer) return buildLoadingClip(id, src, name, options);
|
|
1470
|
+
if (!audioBuffer && !waveformData) {
|
|
1471
|
+
throw new Error("audioTrack: give src, audioBuffer or waveformData");
|
|
1472
|
+
}
|
|
1473
|
+
const sampleRate = audioBuffer?.sampleRate ?? waveformData?.sample_rate ?? 0;
|
|
1474
|
+
const sourceSamples = audioBuffer?.length ?? Math.ceil((waveformData?.duration ?? 0) * sampleRate);
|
|
1475
|
+
const clipSeconds = fitToAudio(name, offset, duration, sampleRate, sourceSamples);
|
|
1476
|
+
const clip = createClipFromSeconds({
|
|
1477
|
+
audioBuffer,
|
|
1478
|
+
waveformData,
|
|
1479
|
+
startTime: start,
|
|
1480
|
+
offset,
|
|
1481
|
+
duration: clipSeconds,
|
|
1482
|
+
name,
|
|
1483
|
+
gain: options.gain,
|
|
1484
|
+
fadeIn: options.fadeIn,
|
|
1485
|
+
fadeOut: options.fadeOut,
|
|
1486
|
+
color: options.color,
|
|
1487
|
+
src
|
|
1488
|
+
});
|
|
1489
|
+
return { ...clip, id };
|
|
1490
|
+
}
|
|
1491
|
+
function audioTrack(options) {
|
|
1492
|
+
checkOptions(options);
|
|
1493
|
+
const name = options.name ?? defaultName(options.src);
|
|
1494
|
+
const clip = buildClip(options, name);
|
|
1495
|
+
const base = createTrack({
|
|
1496
|
+
name,
|
|
1497
|
+
muted: options.muted,
|
|
1498
|
+
soloed: options.soloed,
|
|
1499
|
+
volume: options.volume,
|
|
1500
|
+
pan: options.pan,
|
|
1501
|
+
spectrogramConfig: options.spectrogramConfig,
|
|
1502
|
+
spectrogramColorMap: options.spectrogramColorMap,
|
|
1503
|
+
color: options.color
|
|
1504
|
+
});
|
|
1505
|
+
const track = {
|
|
1506
|
+
...base,
|
|
1507
|
+
id: options.id || base.id,
|
|
1508
|
+
clips: [clip],
|
|
1509
|
+
renderMode: options.renderMode
|
|
1510
|
+
};
|
|
1511
|
+
return track;
|
|
1512
|
+
}
|
|
1513
|
+
|
|
1514
|
+
// src/effects/registry.ts
|
|
1515
|
+
var REGISTRY_KEY = /* @__PURE__ */ Symbol.for("@waveform-playlist/core/effect-registry/v1");
|
|
1516
|
+
function registryState() {
|
|
1517
|
+
const holder = globalThis;
|
|
1518
|
+
const existing = holder[REGISTRY_KEY];
|
|
1519
|
+
if (existing) return existing;
|
|
1520
|
+
const created = { definitions: /* @__PURE__ */ new Map(), prepared: /* @__PURE__ */ new WeakMap() };
|
|
1521
|
+
holder[REGISTRY_KEY] = created;
|
|
1522
|
+
return created;
|
|
1523
|
+
}
|
|
1524
|
+
var registry = registryState().definitions;
|
|
1525
|
+
var prepared = registryState().prepared;
|
|
1526
|
+
function registerEffect(type, definition) {
|
|
1527
|
+
if (registry.has(type)) {
|
|
1528
|
+
console.warn(
|
|
1529
|
+
'[waveform-playlist] registerEffect: overwriting existing effect type "' + type + '"'
|
|
1530
|
+
);
|
|
1531
|
+
}
|
|
1532
|
+
registry.set(type, definition);
|
|
1533
|
+
}
|
|
1534
|
+
function unregisterEffect(type) {
|
|
1535
|
+
return registry.delete(type);
|
|
1536
|
+
}
|
|
1537
|
+
function getEffectDefinitions() {
|
|
1538
|
+
return new Map(registry);
|
|
1539
|
+
}
|
|
1540
|
+
function prepareEffect(type, audioContext) {
|
|
1541
|
+
const definition = registry.get(type);
|
|
1542
|
+
if (!definition?.prepare) {
|
|
1543
|
+
return Promise.resolve();
|
|
1544
|
+
}
|
|
1545
|
+
const forContext = prepared.get(audioContext) ?? /* @__PURE__ */ new Map();
|
|
1546
|
+
prepared.set(audioContext, forContext);
|
|
1547
|
+
const existing = forContext.get(type);
|
|
1548
|
+
if (existing && existing.definition === definition) {
|
|
1549
|
+
return existing.promise;
|
|
1550
|
+
}
|
|
1551
|
+
const prepare = definition.prepare;
|
|
1552
|
+
const record = {
|
|
1553
|
+
definition,
|
|
1554
|
+
settled: false,
|
|
1555
|
+
// The async wrapper turns a synchronous throw into a rejection.
|
|
1556
|
+
promise: (async () => prepare(audioContext))().then(
|
|
1557
|
+
() => {
|
|
1558
|
+
record.settled = true;
|
|
1559
|
+
},
|
|
1560
|
+
(err) => {
|
|
1561
|
+
if (forContext.get(type) === record) {
|
|
1562
|
+
forContext.delete(type);
|
|
1563
|
+
}
|
|
1564
|
+
throw err;
|
|
1565
|
+
}
|
|
1566
|
+
)
|
|
1567
|
+
};
|
|
1568
|
+
forContext.set(type, record);
|
|
1569
|
+
return record.promise;
|
|
1570
|
+
}
|
|
1571
|
+
function isPrepared(type, definition, audioContext) {
|
|
1572
|
+
const record = prepared.get(audioContext)?.get(type);
|
|
1573
|
+
return record !== void 0 && record.definition === definition && record.settled;
|
|
1574
|
+
}
|
|
1575
|
+
function needsPrepare(type, audioContext) {
|
|
1576
|
+
const definition = registry.get(type);
|
|
1577
|
+
return definition?.prepare !== void 0 && !isPrepared(type, definition, audioContext);
|
|
1578
|
+
}
|
|
1579
|
+
async function ensureEffectPrepared(type, audioContext) {
|
|
1580
|
+
while (needsPrepare(type, audioContext)) {
|
|
1581
|
+
await prepareEffect(type, audioContext);
|
|
1582
|
+
}
|
|
1583
|
+
}
|
|
1584
|
+
function createEffectInstance(type, audioContext, params = {}) {
|
|
1585
|
+
const definition = registry.get(type);
|
|
1586
|
+
if (!definition) {
|
|
1587
|
+
throw new Error(
|
|
1588
|
+
'[waveform-playlist] createEffectInstance: unknown effect type "' + type + '". Available types: ' + [...registry.keys()].join(", ")
|
|
1589
|
+
);
|
|
1590
|
+
}
|
|
1591
|
+
const merged = { ...definition.defaults, ...params };
|
|
1592
|
+
let instance;
|
|
1593
|
+
try {
|
|
1594
|
+
instance = definition.create(audioContext, merged);
|
|
1595
|
+
} catch (err) {
|
|
1596
|
+
if (!definition.prepare || isPrepared(type, definition, audioContext)) {
|
|
1597
|
+
throw err;
|
|
1598
|
+
}
|
|
1599
|
+
const hinted = new Error(
|
|
1600
|
+
'[waveform-playlist] effect "' + type + `" has a prepare() step that has not completed on this audio context \u2014 await prepareEffect('` + type + "', audioContext) before creating it (on <daw-editor>: await editor.prepareEffect('" + type + "') before addEffect). Original error: " + (err instanceof Error ? err.message : String(err))
|
|
1601
|
+
);
|
|
1602
|
+
hinted.cause = err;
|
|
1603
|
+
throw hinted;
|
|
1604
|
+
}
|
|
1605
|
+
try {
|
|
1606
|
+
instance.applyParams(merged);
|
|
1607
|
+
} catch (err) {
|
|
1608
|
+
try {
|
|
1609
|
+
instance.dispose?.();
|
|
1610
|
+
} catch {
|
|
1611
|
+
}
|
|
1612
|
+
throw err;
|
|
1613
|
+
}
|
|
1614
|
+
return { instance, params: merged, wetParam: definition.wetParam };
|
|
1615
|
+
}
|
|
1084
1616
|
export {
|
|
1085
1617
|
ANNOTATION_LINK_THRESHOLD_TICKS,
|
|
1086
1618
|
DEFAULT_ANNOTATION_SHORTCUTS,
|
|
@@ -1088,6 +1620,7 @@ export {
|
|
|
1088
1620
|
InteractionState,
|
|
1089
1621
|
LINK_THRESHOLD,
|
|
1090
1622
|
MAX_CANVAS_WIDTH,
|
|
1623
|
+
METER_PEAK_DECAY,
|
|
1091
1624
|
MIN_ANNOTATION_DURATION,
|
|
1092
1625
|
MIN_PIXELS_PER_UNIT,
|
|
1093
1626
|
PPQN,
|
|
@@ -1097,12 +1630,15 @@ export {
|
|
|
1097
1630
|
appendToAudioBuffer,
|
|
1098
1631
|
applyFadeIn,
|
|
1099
1632
|
applyFadeOut,
|
|
1633
|
+
assertTrackKind,
|
|
1100
1634
|
audibleLatencySamples,
|
|
1635
|
+
audioTrack,
|
|
1101
1636
|
buildSpectrogramCanvasId,
|
|
1102
1637
|
calculateDuration,
|
|
1103
1638
|
carveClipRange,
|
|
1104
1639
|
clipDurationTime,
|
|
1105
1640
|
clipEndTime,
|
|
1641
|
+
clipKind,
|
|
1106
1642
|
clipOffsetTime,
|
|
1107
1643
|
clipPixelWidth,
|
|
1108
1644
|
clipStartTime,
|
|
@@ -1113,10 +1649,15 @@ export {
|
|
|
1113
1649
|
createClip,
|
|
1114
1650
|
createClipFromSeconds,
|
|
1115
1651
|
createClipFromTicks,
|
|
1652
|
+
createEffectInstance,
|
|
1653
|
+
createPanStage,
|
|
1654
|
+
createSourceLoader,
|
|
1116
1655
|
createTimeline,
|
|
1117
1656
|
createTrack,
|
|
1657
|
+
createValueSource,
|
|
1118
1658
|
dBToNormalized,
|
|
1119
1659
|
detectMeterChanges,
|
|
1660
|
+
ensureEffectPrepared,
|
|
1120
1661
|
enumerateMicrophones,
|
|
1121
1662
|
exponentialCurve,
|
|
1122
1663
|
findGaps,
|
|
@@ -1126,16 +1667,23 @@ export {
|
|
|
1126
1667
|
generatePeaks,
|
|
1127
1668
|
getClipsAtSample,
|
|
1128
1669
|
getClipsInRange,
|
|
1129
|
-
|
|
1670
|
+
getEffectDefinitions,
|
|
1130
1671
|
handleKeyboardEvent,
|
|
1672
|
+
isLoadingClip,
|
|
1673
|
+
keyBelongsToFocusedControl,
|
|
1131
1674
|
linearCurve,
|
|
1132
1675
|
logarithmicCurve,
|
|
1133
1676
|
matchesKeyBinding,
|
|
1677
|
+
needsPrepare,
|
|
1134
1678
|
normalizedToDb,
|
|
1679
|
+
panMatrix,
|
|
1135
1680
|
parseSpectrogramCanvasId,
|
|
1136
1681
|
pixelsToSamples,
|
|
1137
1682
|
pixelsToSeconds,
|
|
1683
|
+
prepareEffect,
|
|
1138
1684
|
probeRangeSupport,
|
|
1685
|
+
readMeterMessage,
|
|
1686
|
+
registerEffect,
|
|
1139
1687
|
resolveAnnotationShortcuts,
|
|
1140
1688
|
resolveRecordingOffsetSamples,
|
|
1141
1689
|
sCurveCurve,
|
|
@@ -1144,15 +1692,19 @@ export {
|
|
|
1144
1692
|
samplesToTicks,
|
|
1145
1693
|
secondsToPixels,
|
|
1146
1694
|
secondsToSamples,
|
|
1695
|
+
silentMeterLevels,
|
|
1147
1696
|
snapTickToGrid,
|
|
1148
1697
|
snapToGrid,
|
|
1149
1698
|
snapToTicks,
|
|
1150
1699
|
sortClipsByTime,
|
|
1700
|
+
splitTrackByKind,
|
|
1151
1701
|
ticksPerBar,
|
|
1152
1702
|
ticksPerBeat,
|
|
1153
1703
|
ticksToBarBeat,
|
|
1154
1704
|
ticksToSamples,
|
|
1155
1705
|
trackChannelCount,
|
|
1706
|
+
trackKind,
|
|
1707
|
+
unregisterEffect,
|
|
1156
1708
|
updateAnnotationBoundaries,
|
|
1157
1709
|
watchMicrophoneDevices
|
|
1158
1710
|
};
|