@waveform-playlist/recording 14.0.0-next.16 → 14.0.0-next.18
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 +3 -3
- package/dist/index.d.mts +8 -24
- package/dist/index.d.ts +8 -24
- package/dist/index.js +130 -49
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +130 -49
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.mjs
CHANGED
|
@@ -12,6 +12,9 @@ function emptyPeaks(bits) {
|
|
|
12
12
|
return bits === 8 ? new Int8Array(0) : new Int16Array(0);
|
|
13
13
|
}
|
|
14
14
|
function useRecording(stream, options = {}) {
|
|
15
|
+
return useRecordingWithTakeEnded(stream, options);
|
|
16
|
+
}
|
|
17
|
+
function useRecordingWithTakeEnded(stream, options = {}, onTakeEnded) {
|
|
15
18
|
const { channelCount = 1, samplesPerPixel = 1024, bits = 16 } = options;
|
|
16
19
|
const [isRecording, setIsRecording] = useState(false);
|
|
17
20
|
const [isPaused, setIsPaused] = useState(false);
|
|
@@ -38,7 +41,10 @@ function useRecording(stream, options = {}) {
|
|
|
38
41
|
const finishStopRecordingRef = useRef(null);
|
|
39
42
|
const isStartingRef = useRef(false);
|
|
40
43
|
const isUnmountedRef = useRef(false);
|
|
44
|
+
const mountEpochRef = useRef(0);
|
|
41
45
|
const durationRef = useRef(0);
|
|
46
|
+
const onTakeEndedRef = useRef(onTakeEnded);
|
|
47
|
+
onTakeEndedRef.current = onTakeEnded;
|
|
42
48
|
const peaksRef = useRef(progress.getSnapshot().peaks);
|
|
43
49
|
const publishProgress = useCallback(() => {
|
|
44
50
|
const current = progress.getSnapshot();
|
|
@@ -76,7 +82,16 @@ function useRecording(stream, options = {}) {
|
|
|
76
82
|
setError(new Error("No microphone stream available"));
|
|
77
83
|
return false;
|
|
78
84
|
}
|
|
85
|
+
if (stream.getAudioTracks()[0]?.readyState === "ended") {
|
|
86
|
+
setError(
|
|
87
|
+
new Error(
|
|
88
|
+
"The microphone stream has ended (the device was disconnected or its permission revoked). Request microphone access again."
|
|
89
|
+
)
|
|
90
|
+
);
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
79
93
|
isStartingRef.current = true;
|
|
94
|
+
const mountEpoch = mountEpochRef.current;
|
|
80
95
|
try {
|
|
81
96
|
setError(null);
|
|
82
97
|
const context = getGlobalContext();
|
|
@@ -84,7 +99,7 @@ function useRecording(stream, options = {}) {
|
|
|
84
99
|
await context.resume();
|
|
85
100
|
}
|
|
86
101
|
await loadWorklet();
|
|
87
|
-
if (isUnmountedRef.current) return false;
|
|
102
|
+
if (isUnmountedRef.current || mountEpoch !== mountEpochRef.current) return false;
|
|
88
103
|
const source = context.createMediaStreamSource(stream);
|
|
89
104
|
mediaStreamSourceRef.current = source;
|
|
90
105
|
const detectedChannelCount = stream.getAudioTracks()[0]?.getSettings().channelCount;
|
|
@@ -149,8 +164,9 @@ function useRecording(stream, options = {}) {
|
|
|
149
164
|
if (audioTrack) {
|
|
150
165
|
const onEnded = () => {
|
|
151
166
|
console.warn("[waveform-playlist] Audio track ended (mic unplugged or revoked)");
|
|
152
|
-
stopRecordingRef.current?.();
|
|
167
|
+
const stopped = stopRecordingRef.current?.();
|
|
153
168
|
setError(new Error("Microphone disconnected during recording"));
|
|
169
|
+
stopped?.then((buffer) => onTakeEndedRef.current?.(buffer));
|
|
154
170
|
};
|
|
155
171
|
onTrackEndedRef.current = onEnded;
|
|
156
172
|
audioTrack.addEventListener("ended", onEnded);
|
|
@@ -190,6 +206,18 @@ function useRecording(stream, options = {}) {
|
|
|
190
206
|
stopPromiseRef.current = stopPromise;
|
|
191
207
|
return stopPromise;
|
|
192
208
|
}, []);
|
|
209
|
+
const buildTakeBuffer = useCallback(() => {
|
|
210
|
+
const context = getGlobalContext();
|
|
211
|
+
const rawContext = context.rawContext;
|
|
212
|
+
const numChannels = recordedChunksRef.current.length || channelCount;
|
|
213
|
+
const channelData = recordedChunksRef.current.map((chunks) => concatenateAudioData(chunks));
|
|
214
|
+
const totalSamples = channelData[0]?.length ?? 0;
|
|
215
|
+
if (totalSamples === 0) {
|
|
216
|
+
console.warn("[waveform-playlist] Recording stopped with 0 samples captured \u2014 discarding");
|
|
217
|
+
return null;
|
|
218
|
+
}
|
|
219
|
+
return createAudioBuffer(rawContext, channelData, rawContext.sampleRate, numChannels);
|
|
220
|
+
}, [channelCount]);
|
|
193
221
|
const finishStopRecording = useCallback(async () => {
|
|
194
222
|
try {
|
|
195
223
|
if (animationFrameRef.current !== null) {
|
|
@@ -237,16 +265,8 @@ function useRecording(stream, options = {}) {
|
|
|
237
265
|
workletNodeRef.current = null;
|
|
238
266
|
mediaStreamSourceRef.current = null;
|
|
239
267
|
}
|
|
240
|
-
const
|
|
241
|
-
|
|
242
|
-
const numChannels = recordedChunksRef.current.length || channelCount;
|
|
243
|
-
const channelData = recordedChunksRef.current.map((chunks) => concatenateAudioData(chunks));
|
|
244
|
-
const totalSamples = channelData[0]?.length ?? 0;
|
|
245
|
-
if (totalSamples === 0) {
|
|
246
|
-
console.warn("[waveform-playlist] Recording stopped with 0 samples captured \u2014 discarding");
|
|
247
|
-
return null;
|
|
248
|
-
}
|
|
249
|
-
const buffer = createAudioBuffer(rawContext, channelData, rawContext.sampleRate, numChannels);
|
|
268
|
+
const buffer = buildTakeBuffer();
|
|
269
|
+
if (!buffer) return null;
|
|
250
270
|
setAudioBuffer(buffer);
|
|
251
271
|
durationRef.current = buffer.duration;
|
|
252
272
|
publishProgress();
|
|
@@ -262,9 +282,11 @@ function useRecording(stream, options = {}) {
|
|
|
262
282
|
setIsRecording(false);
|
|
263
283
|
setIsPaused(false);
|
|
264
284
|
}
|
|
265
|
-
}, [
|
|
285
|
+
}, [buildTakeBuffer, publishProgress]);
|
|
266
286
|
stopRecordingRef.current = stopRecording;
|
|
267
287
|
finishStopRecordingRef.current = finishStopRecording;
|
|
288
|
+
const buildTakeBufferRef = useRef(buildTakeBuffer);
|
|
289
|
+
buildTakeBufferRef.current = buildTakeBuffer;
|
|
268
290
|
const pauseRecording = useCallback(() => {
|
|
269
291
|
if (isRecordingRef.current && !isPausedRef.current && !isStoppingRef.current) {
|
|
270
292
|
workletNodeRef.current?.port.postMessage({ command: "pause" });
|
|
@@ -288,6 +310,7 @@ function useRecording(stream, options = {}) {
|
|
|
288
310
|
}, [startDurationLoop]);
|
|
289
311
|
useEffect(() => {
|
|
290
312
|
isUnmountedRef.current = false;
|
|
313
|
+
mountEpochRef.current += 1;
|
|
291
314
|
return () => {
|
|
292
315
|
isUnmountedRef.current = true;
|
|
293
316
|
if (audioTrackRef.current && onTrackEndedRef.current) {
|
|
@@ -311,6 +334,28 @@ function useRecording(stream, options = {}) {
|
|
|
311
334
|
}
|
|
312
335
|
if (animationFrameRef.current !== null) {
|
|
313
336
|
cancelAnimationFrame(animationFrameRef.current);
|
|
337
|
+
animationFrameRef.current = null;
|
|
338
|
+
}
|
|
339
|
+
if (!isRecordingRef.current || isStoppingRef.current) return;
|
|
340
|
+
isRecordingRef.current = false;
|
|
341
|
+
isPausedRef.current = false;
|
|
342
|
+
workletNodeRef.current = null;
|
|
343
|
+
mediaStreamSourceRef.current = null;
|
|
344
|
+
audioTrackRef.current = null;
|
|
345
|
+
onTrackEndedRef.current = null;
|
|
346
|
+
let buffer = null;
|
|
347
|
+
try {
|
|
348
|
+
buffer = buildTakeBufferRef.current();
|
|
349
|
+
} catch (err) {
|
|
350
|
+
console.warn("[waveform-playlist] Failed to keep the interrupted take:", String(err));
|
|
351
|
+
}
|
|
352
|
+
setIsRecording(false);
|
|
353
|
+
setIsPaused(false);
|
|
354
|
+
if (buffer) setAudioBuffer(buffer);
|
|
355
|
+
try {
|
|
356
|
+
onTakeEndedRef.current?.(buffer);
|
|
357
|
+
} catch (err) {
|
|
358
|
+
console.warn("[waveform-playlist] Failed to hand over the interrupted take:", String(err));
|
|
314
359
|
}
|
|
315
360
|
};
|
|
316
361
|
}, []);
|
|
@@ -344,7 +389,7 @@ function useMicrophoneAccess() {
|
|
|
344
389
|
const { devices: audioInputs } = await enumerateMicrophones();
|
|
345
390
|
setDevices(audioInputs);
|
|
346
391
|
} catch (err) {
|
|
347
|
-
console.
|
|
392
|
+
console.warn("[waveform-playlist] Failed to enumerate devices:", String(err));
|
|
348
393
|
setError(err instanceof Error ? err : new Error("Failed to enumerate devices"));
|
|
349
394
|
}
|
|
350
395
|
}, []);
|
|
@@ -375,7 +420,13 @@ function useMicrophoneAccess() {
|
|
|
375
420
|
audio,
|
|
376
421
|
video: false
|
|
377
422
|
};
|
|
378
|
-
const
|
|
423
|
+
const mediaDevices = typeof navigator !== "undefined" ? navigator.mediaDevices : void 0;
|
|
424
|
+
if (!mediaDevices || typeof mediaDevices.getUserMedia !== "function") {
|
|
425
|
+
throw new Error(
|
|
426
|
+
"Microphone access is not available: navigator.mediaDevices is undefined. It needs a secure context (https or localhost)."
|
|
427
|
+
);
|
|
428
|
+
}
|
|
429
|
+
const newStream = await mediaDevices.getUserMedia(constraints);
|
|
379
430
|
if (generation !== requestGenerationRef.current || isUnmountedRef.current) {
|
|
380
431
|
newStream.getTracks().forEach((track) => track.stop());
|
|
381
432
|
return;
|
|
@@ -383,9 +434,21 @@ function useMicrophoneAccess() {
|
|
|
383
434
|
streamRef.current = newStream;
|
|
384
435
|
setStream(newStream);
|
|
385
436
|
setHasPermission(true);
|
|
437
|
+
const onEnded = () => {
|
|
438
|
+
if (streamRef.current !== newStream) return;
|
|
439
|
+
streamRef.current = null;
|
|
440
|
+
setStream(null);
|
|
441
|
+
setHasPermission(false);
|
|
442
|
+
setError(
|
|
443
|
+
new Error(
|
|
444
|
+
"The microphone was disconnected or its permission was revoked. Request microphone access again."
|
|
445
|
+
)
|
|
446
|
+
);
|
|
447
|
+
};
|
|
448
|
+
newStream.getTracks().forEach((track) => track.addEventListener("ended", onEnded, { once: true }));
|
|
386
449
|
await enumerateDevices();
|
|
387
450
|
} catch (err) {
|
|
388
|
-
console.
|
|
451
|
+
console.warn("[waveform-playlist] Failed to access microphone:", String(err));
|
|
389
452
|
if (generation === requestGenerationRef.current) {
|
|
390
453
|
setError(err instanceof Error ? err : new Error("Failed to access microphone"));
|
|
391
454
|
setHasPermission(false);
|
|
@@ -410,13 +473,16 @@ function useMicrophoneAccess() {
|
|
|
410
473
|
useEffect2(() => {
|
|
411
474
|
isUnmountedRef.current = false;
|
|
412
475
|
enumerateDevices();
|
|
413
|
-
navigator
|
|
476
|
+
const mediaDevices = typeof navigator !== "undefined" ? navigator.mediaDevices : void 0;
|
|
477
|
+
mediaDevices?.addEventListener("devicechange", enumerateDevices);
|
|
414
478
|
return () => {
|
|
415
|
-
|
|
479
|
+
mediaDevices?.removeEventListener("devicechange", enumerateDevices);
|
|
416
480
|
isUnmountedRef.current = true;
|
|
417
481
|
if (streamRef.current) {
|
|
418
482
|
streamRef.current.getTracks().forEach((track) => track.stop());
|
|
419
483
|
streamRef.current = null;
|
|
484
|
+
setStream(null);
|
|
485
|
+
setHasPermission(false);
|
|
420
486
|
}
|
|
421
487
|
};
|
|
422
488
|
}, [enumerateDevices]);
|
|
@@ -619,7 +685,7 @@ import {
|
|
|
619
685
|
} from "@waveform-playlist/playout";
|
|
620
686
|
function useIntegratedRecording(arrangement, selectedTrackId, options = {}) {
|
|
621
687
|
const { currentTime = 0, audioConstraints, latencyOffset, ...recordingOptions } = options;
|
|
622
|
-
const
|
|
688
|
+
const audioResumedRef = useRef4(false);
|
|
623
689
|
const [selectedDevice, setSelectedDevice] = useState4(null);
|
|
624
690
|
const [hookError, setHookError] = useState4(null);
|
|
625
691
|
const recordingStartTimeRef = useRef4(0);
|
|
@@ -630,6 +696,10 @@ function useIntegratedRecording(arrangement, selectedTrackId, options = {}) {
|
|
|
630
696
|
selectedTrackIdRef.current = selectedTrackId;
|
|
631
697
|
const currentTimeRef = useRef4(currentTime);
|
|
632
698
|
currentTimeRef.current = currentTime;
|
|
699
|
+
const audioConstraintsRef = useRef4(audioConstraints);
|
|
700
|
+
audioConstraintsRef.current = audioConstraints;
|
|
701
|
+
const finishTakeRef = useRef4(() => {
|
|
702
|
+
});
|
|
633
703
|
const { stream, devices, hasPermission, requestAccess, error: micError } = useMicrophoneAccess();
|
|
634
704
|
const {
|
|
635
705
|
meter,
|
|
@@ -647,7 +717,16 @@ function useIntegratedRecording(arrangement, selectedTrackId, options = {}) {
|
|
|
647
717
|
pauseRecording,
|
|
648
718
|
resumeRecording,
|
|
649
719
|
error: recError
|
|
650
|
-
} =
|
|
720
|
+
} = useRecordingWithTakeEnded(
|
|
721
|
+
stream,
|
|
722
|
+
recordingOptions,
|
|
723
|
+
(buffer) => (
|
|
724
|
+
// The take ended without our stopRecording(): keep it (#822).
|
|
725
|
+
finishTakeRef.current(buffer)
|
|
726
|
+
)
|
|
727
|
+
);
|
|
728
|
+
const startRecRef = useRef4(startRec);
|
|
729
|
+
startRecRef.current = startRec;
|
|
651
730
|
const startRecording = useCallback4(async () => {
|
|
652
731
|
if (isRecordingRef.current || isStartingRef.current) return false;
|
|
653
732
|
if (!selectedTrackIdRef.current) {
|
|
@@ -664,14 +743,14 @@ function useIntegratedRecording(arrangement, selectedTrackId, options = {}) {
|
|
|
664
743
|
isStartingRef.current = true;
|
|
665
744
|
try {
|
|
666
745
|
setHookError(null);
|
|
667
|
-
if (!
|
|
746
|
+
if (!audioResumedRef.current) {
|
|
668
747
|
await resumeGlobalAudioContext();
|
|
669
|
-
|
|
748
|
+
audioResumedRef.current = true;
|
|
670
749
|
}
|
|
671
750
|
recordingStartTimeRef.current = currentTimeRef.current;
|
|
672
751
|
recordingTrackIdRef.current = selectedTrackIdRef.current;
|
|
673
752
|
setRecordingTrackId(selectedTrackIdRef.current);
|
|
674
|
-
const started = await
|
|
753
|
+
const started = await startRecRef.current();
|
|
675
754
|
if (!started) setRecordingTrackId(null);
|
|
676
755
|
return started;
|
|
677
756
|
} catch (err) {
|
|
@@ -681,19 +760,14 @@ function useIntegratedRecording(arrangement, selectedTrackId, options = {}) {
|
|
|
681
760
|
} finally {
|
|
682
761
|
isStartingRef.current = false;
|
|
683
762
|
}
|
|
684
|
-
}, [arrangement
|
|
685
|
-
const
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
setHookError(err instanceof Error ? err : new Error(String(err)));
|
|
691
|
-
return;
|
|
692
|
-
} finally {
|
|
763
|
+
}, [arrangement]);
|
|
764
|
+
const finishTake = useCallback4(
|
|
765
|
+
(buffer) => {
|
|
766
|
+
if (!buffer) return;
|
|
767
|
+
const trackId = recordingTrackIdRef.current;
|
|
768
|
+
recordingTrackIdRef.current = null;
|
|
693
769
|
setRecordingTrackId(null);
|
|
694
|
-
|
|
695
|
-
const trackId = recordingTrackIdRef.current;
|
|
696
|
-
if (buffer && trackId) {
|
|
770
|
+
if (!trackId) return;
|
|
697
771
|
if (!arrangement.getTracks().some((t) => t.id === trackId)) {
|
|
698
772
|
const err = new Error(
|
|
699
773
|
`Recording completed but track "${trackId}" no longer exists. The recorded audio could not be saved.`
|
|
@@ -739,19 +813,25 @@ function useIntegratedRecording(arrangement, selectedTrackId, options = {}) {
|
|
|
739
813
|
console.warn(`[waveform-playlist] ${notSaved.message}`);
|
|
740
814
|
setHookError(notSaved);
|
|
741
815
|
}
|
|
816
|
+
},
|
|
817
|
+
[arrangement, latencyOffset]
|
|
818
|
+
);
|
|
819
|
+
finishTakeRef.current = finishTake;
|
|
820
|
+
const stopRecording = useCallback4(async () => {
|
|
821
|
+
let buffer = null;
|
|
822
|
+
try {
|
|
823
|
+
buffer = await stopRec();
|
|
824
|
+
} catch (err) {
|
|
825
|
+
setHookError(err instanceof Error ? err : new Error(String(err)));
|
|
742
826
|
}
|
|
743
|
-
|
|
827
|
+
finishTakeRef.current(buffer);
|
|
828
|
+
}, [stopRec]);
|
|
744
829
|
useEffect4(() => {
|
|
745
830
|
if (!hasPermission || devices.length === 0) return;
|
|
746
|
-
if (selectedDevice === null) {
|
|
831
|
+
if (selectedDevice === null || !devices.some((d) => d.deviceId === selectedDevice)) {
|
|
747
832
|
setSelectedDevice(devices[0].deviceId);
|
|
748
|
-
} else if (!devices.some((d) => d.deviceId === selectedDevice)) {
|
|
749
|
-
const fallbackId = devices[0].deviceId;
|
|
750
|
-
setSelectedDevice(fallbackId);
|
|
751
|
-
resetPeak();
|
|
752
|
-
requestAccess(fallbackId, audioConstraints);
|
|
753
833
|
}
|
|
754
|
-
}, [hasPermission, devices, selectedDevice
|
|
834
|
+
}, [hasPermission, devices, selectedDevice]);
|
|
755
835
|
const isRecordingRef = useRef4(isRecording);
|
|
756
836
|
isRecordingRef.current = isRecording;
|
|
757
837
|
const requestMicAccess = useCallback4(async () => {
|
|
@@ -761,13 +841,14 @@ function useIntegratedRecording(arrangement, selectedTrackId, options = {}) {
|
|
|
761
841
|
}
|
|
762
842
|
try {
|
|
763
843
|
setHookError(null);
|
|
764
|
-
|
|
844
|
+
setSelectedDevice(null);
|
|
845
|
+
await requestAccess(void 0, audioConstraintsRef.current);
|
|
765
846
|
await resumeGlobalAudioContext();
|
|
766
|
-
|
|
847
|
+
audioResumedRef.current = true;
|
|
767
848
|
} catch (err) {
|
|
768
849
|
setHookError(err instanceof Error ? err : new Error(String(err)));
|
|
769
850
|
}
|
|
770
|
-
}, [requestAccess
|
|
851
|
+
}, [requestAccess]);
|
|
771
852
|
const changeDevice = useCallback4(
|
|
772
853
|
async (deviceId) => {
|
|
773
854
|
if (isRecordingRef.current) {
|
|
@@ -778,14 +859,14 @@ function useIntegratedRecording(arrangement, selectedTrackId, options = {}) {
|
|
|
778
859
|
setHookError(null);
|
|
779
860
|
setSelectedDevice(deviceId);
|
|
780
861
|
resetPeak();
|
|
781
|
-
await requestAccess(deviceId,
|
|
862
|
+
await requestAccess(deviceId, audioConstraintsRef.current);
|
|
782
863
|
await resumeGlobalAudioContext();
|
|
783
|
-
|
|
864
|
+
audioResumedRef.current = true;
|
|
784
865
|
} catch (err) {
|
|
785
866
|
setHookError(err instanceof Error ? err : new Error(String(err)));
|
|
786
867
|
}
|
|
787
868
|
},
|
|
788
|
-
[requestAccess,
|
|
869
|
+
[requestAccess, resetPeak]
|
|
789
870
|
);
|
|
790
871
|
return {
|
|
791
872
|
// Recording state
|