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

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
@@ -1,6 +1,30 @@
1
1
  // src/constants.ts
2
2
  var MAX_CANVAS_WIDTH = 1e3;
3
3
 
4
+ // src/utils/conversions.ts
5
+ function samplesToSeconds(samples, sampleRate) {
6
+ return samples / sampleRate;
7
+ }
8
+ function secondsToSamples(seconds, sampleRate) {
9
+ return Math.ceil(seconds * sampleRate);
10
+ }
11
+ function samplesToPixels(samples, samplesPerPixel) {
12
+ return Math.floor(samples / samplesPerPixel);
13
+ }
14
+ function pixelsToSamples(pixels, samplesPerPixel) {
15
+ return Math.floor(pixels * samplesPerPixel);
16
+ }
17
+ function pixelsToSeconds(pixels, samplesPerPixel, sampleRate) {
18
+ return pixels * samplesPerPixel / sampleRate;
19
+ }
20
+ function secondsToPixels(seconds, samplesPerPixel, sampleRate) {
21
+ return Math.ceil(seconds * sampleRate / samplesPerPixel);
22
+ }
23
+ function durationSamplesFromSeconds(startSeconds, durationSeconds, sampleRate) {
24
+ const length = Math.round((startSeconds + durationSeconds) * sampleRate) - Math.round(startSeconds * sampleRate);
25
+ return length < 1 && Math.round(durationSeconds * sampleRate) >= 1 ? 1 : length;
26
+ }
27
+
4
28
  // src/types/clip.ts
5
29
  var TRACK_VIEW_FIELDS = [
6
30
  "name",
@@ -138,12 +162,20 @@ function createClipFromSeconds(options) {
138
162
  );
139
163
  }
140
164
  const duration = options.duration ?? sourceDuration;
165
+ const offsetSamples = Math.round(offset * sampleRate);
166
+ const sourceSamples = audioBuffer?.length ?? Math.ceil(sourceDuration * sampleRate);
167
+ const alone = Math.round(duration * sampleRate);
168
+ const rest = sourceSamples - offsetSamples;
169
+ const durationSamples = Math.min(
170
+ durationSamplesFromSeconds(startTime, duration, sampleRate),
171
+ Math.max(rest, alone)
172
+ );
141
173
  return createClip({
142
174
  audioBuffer,
143
175
  startSample: Math.round(startTime * sampleRate),
144
176
  startTick: options.startTick,
145
- durationSamples: Math.round(duration * sampleRate),
146
- offsetSamples: Math.round(offset * sampleRate),
177
+ durationSamples,
178
+ offsetSamples,
147
179
  sampleRate,
148
180
  sourceDurationSamples: Math.ceil(sourceDuration * sampleRate),
149
181
  gain,
@@ -255,26 +287,6 @@ var InteractionState = /* @__PURE__ */ ((InteractionState2) => {
255
287
  return InteractionState2;
256
288
  })(InteractionState || {});
257
289
 
258
- // src/utils/conversions.ts
259
- function samplesToSeconds(samples, sampleRate) {
260
- return samples / sampleRate;
261
- }
262
- function secondsToSamples(seconds, sampleRate) {
263
- return Math.ceil(seconds * sampleRate);
264
- }
265
- function samplesToPixels(samples, samplesPerPixel) {
266
- return Math.floor(samples / samplesPerPixel);
267
- }
268
- function pixelsToSamples(pixels, samplesPerPixel) {
269
- return Math.floor(pixels * samplesPerPixel);
270
- }
271
- function pixelsToSeconds(pixels, samplesPerPixel, sampleRate) {
272
- return pixels * samplesPerPixel / sampleRate;
273
- }
274
- function secondsToPixels(seconds, samplesPerPixel, sampleRate) {
275
- return Math.ceil(seconds * sampleRate / samplesPerPixel);
276
- }
277
-
278
290
  // src/utils/beatsAndBars.ts
279
291
  var PPQN = 192;
280
292
  function ticksPerBeat(timeSignature, ppqn = PPQN) {
@@ -696,6 +708,34 @@ var SPECTROGRAM_DEFAULTS = {
696
708
  };
697
709
  var DEFAULT_SPECTROGRAM_COLOR_MAP = "viridis";
698
710
 
711
+ // src/loadingClipSpan.ts
712
+ function isTime(value) {
713
+ return value === void 0 || Number.isFinite(value) && value >= 0;
714
+ }
715
+ function peaksSeconds(data) {
716
+ if (!data) return void 0;
717
+ const ok = Number.isFinite(data.sample_rate) && data.sample_rate > 0 && Number.isFinite(data.duration) && data.duration >= 0;
718
+ return ok ? data.duration : void 0;
719
+ }
720
+ function loadingClipSpan(clip, sampleRate) {
721
+ if (!(Number.isFinite(sampleRate) && sampleRate > 0)) return null;
722
+ if (!isTime(clip.start)) return null;
723
+ const start = clip.start ?? 0;
724
+ const startSample = Math.round(start * sampleRate);
725
+ if (!Number.isSafeInteger(startSample)) return null;
726
+ const unknown = { startSample, endSample: null };
727
+ const duration = clip.duration ?? void 0;
728
+ if (!isTime(clip.offset)) return unknown;
729
+ if (duration !== void 0 && !(Number.isFinite(duration) && duration > 0)) return unknown;
730
+ const fileSeconds = peaksSeconds(clip.waveformData);
731
+ const rest = fileSeconds === void 0 ? void 0 : fileSeconds - (clip.offset ?? 0);
732
+ const seconds = duration === void 0 ? rest : rest === void 0 ? duration : Math.min(duration, rest);
733
+ if (seconds === void 0) return unknown;
734
+ const endSample = startSample + durationSamplesFromSeconds(start, seconds, sampleRate);
735
+ if (!Number.isSafeInteger(endSample) || endSample - startSample < 1) return unknown;
736
+ return { startSample, endSample };
737
+ }
738
+
699
739
  // src/utils/carveClipRange.ts
700
740
  function carveClipRange(clips, rangeStart, rangeEnd) {
701
741
  if (!(rangeEnd > rangeStart)) {
@@ -733,6 +773,60 @@ function carveClipRange(clips, rangeStart, rangeEnd) {
733
773
  }
734
774
  return result;
735
775
  }
776
+ function carveLoadingClip(clip, rangeStart, rangeEnd, sampleRate) {
777
+ const span = loadingClipSpan(clip, sampleRate);
778
+ if (!span) return [clip];
779
+ const { startSample, endSample } = span;
780
+ if (startSample >= rangeEnd || endSample !== null && endSample <= rangeStart) return [clip];
781
+ if (endSample === null && startSample < rangeStart) return [clip];
782
+ const start = clip.start ?? 0;
783
+ const rangeStartSeconds = rangeStart / sampleRate;
784
+ const rangeEndSeconds = rangeEnd / sampleRate;
785
+ const result = [];
786
+ const keepHead = startSample < rangeStart;
787
+ const keepTail = endSample === null || endSample > rangeEnd;
788
+ if (keepHead) result.push({ ...clip, duration: rangeStartSeconds - start });
789
+ if (keepTail) {
790
+ const carved = rangeEndSeconds - start;
791
+ const duration = clip.duration ?? void 0;
792
+ const tail = {
793
+ ...clip,
794
+ id: keepHead ? `${clip.id}-carve-${rangeEnd}` : clip.id,
795
+ start: rangeEndSeconds,
796
+ offset: (clip.offset ?? 0) + carved
797
+ };
798
+ if (duration !== void 0) tail.duration = duration - carved;
799
+ result.push(tail);
800
+ }
801
+ return result;
802
+ }
803
+ function startsUnknownInRange(clip, rangeStart, rangeEnd, sampleRate) {
804
+ if (!isLoadingClip(clip)) return false;
805
+ const span = loadingClipSpan(clip, sampleRate);
806
+ return span !== null && span.endSample === null && span.startSample >= rangeStart && span.startSample < rangeEnd;
807
+ }
808
+ function startSampleOf(clip, sampleRate) {
809
+ if (!isLoadingClip(clip)) return clip.startSample;
810
+ return loadingClipSpan(clip, sampleRate)?.startSample ?? null;
811
+ }
812
+ function carveTrackClipRange(clips, rangeStart, rangeEnd, sampleRate) {
813
+ if (!(rangeEnd > rangeStart)) return [...clips];
814
+ const carved = clips.map((clip) => {
815
+ if (startsUnknownInRange(clip, rangeStart, rangeEnd, sampleRate)) return [clip];
816
+ return isLoadingClip(clip) ? carveLoadingClip(clip, rangeStart, rangeEnd, sampleRate) : carveClipRange([clip], rangeStart, rangeEnd);
817
+ });
818
+ const starts = [...clips, ...carved.flat()].map((clip) => ({ clip, start: startSampleOf(clip, sampleRate) })).filter(
819
+ (entry) => entry.start !== null
820
+ );
821
+ return carved.flatMap((pieces, i) => {
822
+ const clip = clips[i];
823
+ if (!startsUnknownInRange(clip, rangeStart, rangeEnd, sampleRate)) return pieces;
824
+ const own = startSampleOf(clip, sampleRate);
825
+ const next = starts.filter((entry) => entry.clip !== clip && entry.start > own).reduce((min, entry) => Math.min(min, entry.start), Number.POSITIVE_INFINITY);
826
+ if (next <= rangeEnd) return [];
827
+ return carveLoadingClip(clip, rangeStart, rangeEnd, sampleRate);
828
+ });
829
+ }
736
830
 
737
831
  // src/utils/meterLevels.ts
738
832
  var METER_PEAK_DECAY = 0.98;
@@ -1103,81 +1197,43 @@ function annotationMinDurationTicks(ppqn) {
1103
1197
  return Math.max(1, Math.round(ppqn / 32));
1104
1198
  }
1105
1199
  function updateAnnotationBoundaries(params, options = {}) {
1106
- const { annotationIndex, newTime, isDraggingStart, annotations, duration, linkEndpoints } = params;
1107
- const linkThreshold = options.linkThreshold ?? LINK_THRESHOLD;
1108
- const minDuration = options.minDuration ?? MIN_ANNOTATION_DURATION;
1109
- const updatedAnnotations = [...annotations];
1200
+ const units = {
1201
+ linkThreshold: options.linkThreshold ?? LINK_THRESHOLD,
1202
+ minDuration: options.minDuration ?? MIN_ANNOTATION_DURATION
1203
+ };
1204
+ return params.isDraggingStart ? moveStart(params, units) : moveEnd(params, units);
1205
+ }
1206
+ function moveStart({ annotationIndex, newTime, annotations, linkEndpoints }, { linkThreshold, minDuration }) {
1207
+ const updated = [...annotations];
1110
1208
  const annotation = annotations[annotationIndex];
1111
- if (isDraggingStart) {
1112
- const constrainedStart = Math.min(annotation.end - minDuration, Math.max(0, newTime));
1113
- updatedAnnotations[annotationIndex] = { ...annotation, start: constrainedStart };
1114
- if (linkEndpoints && annotationIndex > 0) {
1115
- const prevAnnotation = updatedAnnotations[annotationIndex - 1];
1116
- if (Math.abs(prevAnnotation.end - annotation.start) < linkThreshold) {
1117
- updatedAnnotations[annotationIndex - 1] = {
1118
- ...prevAnnotation,
1119
- end: Math.max(prevAnnotation.start + minDuration, constrainedStart)
1120
- };
1121
- } else if (constrainedStart <= prevAnnotation.end) {
1122
- updatedAnnotations[annotationIndex] = {
1123
- ...updatedAnnotations[annotationIndex],
1124
- start: prevAnnotation.end
1125
- };
1126
- }
1127
- } else if (!linkEndpoints && annotationIndex > 0 && constrainedStart < updatedAnnotations[annotationIndex - 1].end) {
1128
- updatedAnnotations[annotationIndex - 1] = {
1129
- ...updatedAnnotations[annotationIndex - 1],
1130
- end: constrainedStart
1131
- };
1132
- }
1209
+ const prev = annotationIndex > 0 ? annotations[annotationIndex - 1] : void 0;
1210
+ const start = Math.max(0, Math.min(annotation.end - minDuration, newTime));
1211
+ if (!prev) {
1212
+ updated[annotationIndex] = { ...annotation, start };
1213
+ } else if (linkEndpoints && Math.abs(prev.end - annotation.start) < linkThreshold) {
1214
+ const shared = Math.max(start, Math.min(prev.start + minDuration, prev.end));
1215
+ updated[annotationIndex - 1] = { ...prev, end: shared };
1216
+ updated[annotationIndex] = { ...annotation, start: shared };
1133
1217
  } else {
1134
- const constrainedEnd = Math.max(annotation.start + minDuration, Math.min(newTime, duration));
1135
- updatedAnnotations[annotationIndex] = { ...annotation, end: constrainedEnd };
1136
- if (linkEndpoints && annotationIndex < updatedAnnotations.length - 1) {
1137
- const nextAnnotation = updatedAnnotations[annotationIndex + 1];
1138
- if (Math.abs(nextAnnotation.start - annotation.end) < linkThreshold) {
1139
- updatedAnnotations[annotationIndex + 1] = {
1140
- ...nextAnnotation,
1141
- start: Math.min(nextAnnotation.end - minDuration, constrainedEnd)
1142
- };
1143
- let currentIndex = annotationIndex + 1;
1144
- while (currentIndex < updatedAnnotations.length - 1) {
1145
- const current = updatedAnnotations[currentIndex];
1146
- const next = updatedAnnotations[currentIndex + 1];
1147
- if (Math.abs(next.start - current.end) < linkThreshold) {
1148
- const nextDelta = current.end - annotations[currentIndex].end;
1149
- updatedAnnotations[currentIndex + 1] = {
1150
- ...next,
1151
- start: Math.min(next.end - minDuration, next.start + nextDelta)
1152
- };
1153
- currentIndex++;
1154
- } else {
1155
- break;
1156
- }
1157
- }
1158
- } else if (constrainedEnd >= nextAnnotation.start) {
1159
- updatedAnnotations[annotationIndex] = {
1160
- ...updatedAnnotations[annotationIndex],
1161
- end: nextAnnotation.start
1162
- };
1163
- }
1164
- } else if (!linkEndpoints && annotationIndex < updatedAnnotations.length - 1 && constrainedEnd > updatedAnnotations[annotationIndex + 1].start) {
1165
- const nextAnnotation = updatedAnnotations[annotationIndex + 1];
1166
- updatedAnnotations[annotationIndex + 1] = { ...nextAnnotation, start: constrainedEnd };
1167
- let currentIndex = annotationIndex + 1;
1168
- while (currentIndex < updatedAnnotations.length - 1) {
1169
- const current = updatedAnnotations[currentIndex];
1170
- const next = updatedAnnotations[currentIndex + 1];
1171
- if (current.end > next.start) {
1172
- updatedAnnotations[currentIndex + 1] = { ...next, start: current.end };
1173
- currentIndex++;
1174
- } else {
1175
- break;
1176
- }
1177
- }
1178
- }
1218
+ updated[annotationIndex] = { ...annotation, start: Math.max(start, prev.end) };
1219
+ }
1220
+ return updated;
1221
+ }
1222
+ function moveEnd({ annotationIndex, newTime, annotations, duration, linkEndpoints }, { linkThreshold, minDuration }) {
1223
+ const updated = [...annotations];
1224
+ const annotation = annotations[annotationIndex];
1225
+ const next = annotations[annotationIndex + 1];
1226
+ const end = Math.max(annotation.start + minDuration, Math.min(newTime, duration));
1227
+ if (!next) {
1228
+ updated[annotationIndex] = { ...annotation, end };
1229
+ } else if (linkEndpoints && Math.abs(next.start - annotation.end) < linkThreshold) {
1230
+ const shared = Math.min(end, Math.max(next.end - minDuration, next.start));
1231
+ updated[annotationIndex] = { ...annotation, end: shared };
1232
+ updated[annotationIndex + 1] = { ...next, start: shared };
1233
+ } else {
1234
+ updated[annotationIndex] = { ...annotation, end: Math.min(end, next.start) };
1179
1235
  }
1180
- return updatedAnnotations;
1236
+ return updated;
1181
1237
  }
1182
1238
 
1183
1239
  // src/annotations/shortcuts.ts
@@ -1588,7 +1644,7 @@ function fitToAudio(name, offset, duration, sampleRate, sourceSamples) {
1588
1644
  `[waveform-playlist] audioTrack: offset + duration of "${name}" runs past the end of the audio; the clip is cut to the end.`
1589
1645
  );
1590
1646
  }
1591
- return Math.min(wanted, remaining) / sampleRate;
1647
+ return wanted > remaining ? remaining / sampleRate : duration;
1592
1648
  }
1593
1649
  function buildLoadingClip(id, src, name, options) {
1594
1650
  const given = {
@@ -1843,6 +1899,7 @@ export {
1843
1899
  audioTrack,
1844
1900
  buildSpectrogramCanvasId,
1845
1901
  carveClipRange,
1902
+ carveTrackClipRange,
1846
1903
  clipDurationTime,
1847
1904
  clipEndTime,
1848
1905
  clipKind,
@@ -1866,6 +1923,7 @@ export {
1866
1923
  createValueSource,
1867
1924
  dBToNormalized,
1868
1925
  detectMeterChanges,
1926
+ durationSamplesFromSeconds,
1869
1927
  ensureEffectPrepared,
1870
1928
  enumerateMicrophones,
1871
1929
  exponentialCurve,
@@ -1882,6 +1940,7 @@ export {
1882
1940
  isLoadingClip,
1883
1941
  keyBelongsToFocusedControl,
1884
1942
  linearCurve,
1943
+ loadingClipSpan,
1885
1944
  logarithmicCurve,
1886
1945
  matchesKeyBinding,
1887
1946
  needsPrepare,