@waveform-playlist/recording 14.0.0-next.2 → 14.0.0-next.20
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 +10 -9
- package/dist/index.d.mts +42 -26
- package/dist/index.d.ts +42 -26
- package/dist/index.js +174 -76
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +175 -77
- package/dist/index.mjs.map +1 -1
- package/package.json +9 -8
package/dist/index.js
CHANGED
|
@@ -37,6 +37,9 @@ function emptyPeaks(bits) {
|
|
|
37
37
|
return bits === 8 ? new Int8Array(0) : new Int16Array(0);
|
|
38
38
|
}
|
|
39
39
|
function useRecording(stream, options = {}) {
|
|
40
|
+
return useRecordingWithTakeEnded(stream, options);
|
|
41
|
+
}
|
|
42
|
+
function useRecordingWithTakeEnded(stream, options = {}, onTakeEnded) {
|
|
40
43
|
const { channelCount = 1, samplesPerPixel = 1024, bits = 16 } = options;
|
|
41
44
|
const [isRecording, setIsRecording] = (0, import_react.useState)(false);
|
|
42
45
|
const [isPaused, setIsPaused] = (0, import_react.useState)(false);
|
|
@@ -61,9 +64,12 @@ function useRecording(stream, options = {}) {
|
|
|
61
64
|
const isStoppingRef = (0, import_react.useRef)(false);
|
|
62
65
|
const stopPromiseRef = (0, import_react.useRef)(null);
|
|
63
66
|
const finishStopRecordingRef = (0, import_react.useRef)(null);
|
|
64
|
-
const
|
|
67
|
+
const startingEpochRef = (0, import_react.useRef)(null);
|
|
65
68
|
const isUnmountedRef = (0, import_react.useRef)(false);
|
|
69
|
+
const mountEpochRef = (0, import_react.useRef)(0);
|
|
66
70
|
const durationRef = (0, import_react.useRef)(0);
|
|
71
|
+
const onTakeEndedRef = (0, import_react.useRef)(onTakeEnded);
|
|
72
|
+
onTakeEndedRef.current = onTakeEnded;
|
|
67
73
|
const peaksRef = (0, import_react.useRef)(progress.getSnapshot().peaks);
|
|
68
74
|
const publishProgress = (0, import_react.useCallback)(() => {
|
|
69
75
|
const current = progress.getSnapshot();
|
|
@@ -96,12 +102,22 @@ function useRecording(stream, options = {}) {
|
|
|
96
102
|
}
|
|
97
103
|
}, []);
|
|
98
104
|
const startRecording = (0, import_react.useCallback)(async () => {
|
|
99
|
-
|
|
105
|
+
const refuseEndedStream = (message) => {
|
|
106
|
+
if (stream?.getAudioTracks()[0]?.readyState !== "ended") return false;
|
|
107
|
+
setError(new Error(message));
|
|
108
|
+
return true;
|
|
109
|
+
};
|
|
110
|
+
if (isRecordingRef.current || startingEpochRef.current === mountEpochRef.current) return false;
|
|
100
111
|
if (!stream) {
|
|
101
112
|
setError(new Error("No microphone stream available"));
|
|
102
113
|
return false;
|
|
103
114
|
}
|
|
104
|
-
|
|
115
|
+
if (refuseEndedStream(
|
|
116
|
+
"The microphone stream has ended (the device was disconnected or its permission revoked). Request microphone access again."
|
|
117
|
+
))
|
|
118
|
+
return false;
|
|
119
|
+
const mountEpoch = mountEpochRef.current;
|
|
120
|
+
startingEpochRef.current = mountEpoch;
|
|
105
121
|
try {
|
|
106
122
|
setError(null);
|
|
107
123
|
const context = (0, import_playout.getGlobalContext)();
|
|
@@ -109,7 +125,11 @@ function useRecording(stream, options = {}) {
|
|
|
109
125
|
await context.resume();
|
|
110
126
|
}
|
|
111
127
|
await loadWorklet();
|
|
112
|
-
if (isUnmountedRef.current) return false;
|
|
128
|
+
if (isUnmountedRef.current || mountEpoch !== mountEpochRef.current) return false;
|
|
129
|
+
if (refuseEndedStream(
|
|
130
|
+
"The microphone changed or was disconnected while recording was starting. Start recording again."
|
|
131
|
+
))
|
|
132
|
+
return false;
|
|
113
133
|
const source = context.createMediaStreamSource(stream);
|
|
114
134
|
mediaStreamSourceRef.current = source;
|
|
115
135
|
const detectedChannelCount = stream.getAudioTracks()[0]?.getSettings().channelCount;
|
|
@@ -174,8 +194,9 @@ function useRecording(stream, options = {}) {
|
|
|
174
194
|
if (audioTrack) {
|
|
175
195
|
const onEnded = () => {
|
|
176
196
|
console.warn("[waveform-playlist] Audio track ended (mic unplugged or revoked)");
|
|
177
|
-
stopRecordingRef.current?.();
|
|
197
|
+
const stopped = stopRecordingRef.current?.();
|
|
178
198
|
setError(new Error("Microphone disconnected during recording"));
|
|
199
|
+
stopped?.then((buffer) => onTakeEndedRef.current?.(buffer));
|
|
179
200
|
};
|
|
180
201
|
onTrackEndedRef.current = onEnded;
|
|
181
202
|
audioTrack.addEventListener("ended", onEnded);
|
|
@@ -192,7 +213,7 @@ function useRecording(stream, options = {}) {
|
|
|
192
213
|
setError(err instanceof Error ? err : new Error("Failed to start recording"));
|
|
193
214
|
return false;
|
|
194
215
|
} finally {
|
|
195
|
-
|
|
216
|
+
if (startingEpochRef.current === mountEpoch) startingEpochRef.current = null;
|
|
196
217
|
}
|
|
197
218
|
}, [
|
|
198
219
|
stream,
|
|
@@ -215,6 +236,18 @@ function useRecording(stream, options = {}) {
|
|
|
215
236
|
stopPromiseRef.current = stopPromise;
|
|
216
237
|
return stopPromise;
|
|
217
238
|
}, []);
|
|
239
|
+
const buildTakeBuffer = (0, import_react.useCallback)(() => {
|
|
240
|
+
const context = (0, import_playout.getGlobalContext)();
|
|
241
|
+
const rawContext = context.rawContext;
|
|
242
|
+
const numChannels = recordedChunksRef.current.length || channelCount;
|
|
243
|
+
const channelData = recordedChunksRef.current.map((chunks) => (0, import_core.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
|
+
return (0, import_core.createAudioBuffer)(rawContext, channelData, rawContext.sampleRate, numChannels);
|
|
250
|
+
}, [channelCount]);
|
|
218
251
|
const finishStopRecording = (0, import_react.useCallback)(async () => {
|
|
219
252
|
try {
|
|
220
253
|
if (animationFrameRef.current !== null) {
|
|
@@ -262,16 +295,8 @@ function useRecording(stream, options = {}) {
|
|
|
262
295
|
workletNodeRef.current = null;
|
|
263
296
|
mediaStreamSourceRef.current = null;
|
|
264
297
|
}
|
|
265
|
-
const
|
|
266
|
-
|
|
267
|
-
const numChannels = recordedChunksRef.current.length || channelCount;
|
|
268
|
-
const channelData = recordedChunksRef.current.map((chunks) => (0, import_core.concatenateAudioData)(chunks));
|
|
269
|
-
const totalSamples = channelData[0]?.length ?? 0;
|
|
270
|
-
if (totalSamples === 0) {
|
|
271
|
-
console.warn("[waveform-playlist] Recording stopped with 0 samples captured \u2014 discarding");
|
|
272
|
-
return null;
|
|
273
|
-
}
|
|
274
|
-
const buffer = (0, import_core.createAudioBuffer)(rawContext, channelData, rawContext.sampleRate, numChannels);
|
|
298
|
+
const buffer = buildTakeBuffer();
|
|
299
|
+
if (!buffer) return null;
|
|
275
300
|
setAudioBuffer(buffer);
|
|
276
301
|
durationRef.current = buffer.duration;
|
|
277
302
|
publishProgress();
|
|
@@ -287,9 +312,11 @@ function useRecording(stream, options = {}) {
|
|
|
287
312
|
setIsRecording(false);
|
|
288
313
|
setIsPaused(false);
|
|
289
314
|
}
|
|
290
|
-
}, [
|
|
315
|
+
}, [buildTakeBuffer, publishProgress]);
|
|
291
316
|
stopRecordingRef.current = stopRecording;
|
|
292
317
|
finishStopRecordingRef.current = finishStopRecording;
|
|
318
|
+
const buildTakeBufferRef = (0, import_react.useRef)(buildTakeBuffer);
|
|
319
|
+
buildTakeBufferRef.current = buildTakeBuffer;
|
|
293
320
|
const pauseRecording = (0, import_react.useCallback)(() => {
|
|
294
321
|
if (isRecordingRef.current && !isPausedRef.current && !isStoppingRef.current) {
|
|
295
322
|
workletNodeRef.current?.port.postMessage({ command: "pause" });
|
|
@@ -313,8 +340,10 @@ function useRecording(stream, options = {}) {
|
|
|
313
340
|
}, [startDurationLoop]);
|
|
314
341
|
(0, import_react.useEffect)(() => {
|
|
315
342
|
isUnmountedRef.current = false;
|
|
343
|
+
const epoch = mountEpochRef.current;
|
|
316
344
|
return () => {
|
|
317
345
|
isUnmountedRef.current = true;
|
|
346
|
+
mountEpochRef.current = epoch + 1;
|
|
318
347
|
if (audioTrackRef.current && onTrackEndedRef.current) {
|
|
319
348
|
audioTrackRef.current.removeEventListener("ended", onTrackEndedRef.current);
|
|
320
349
|
}
|
|
@@ -336,6 +365,28 @@ function useRecording(stream, options = {}) {
|
|
|
336
365
|
}
|
|
337
366
|
if (animationFrameRef.current !== null) {
|
|
338
367
|
cancelAnimationFrame(animationFrameRef.current);
|
|
368
|
+
animationFrameRef.current = null;
|
|
369
|
+
}
|
|
370
|
+
if (!isRecordingRef.current || isStoppingRef.current) return;
|
|
371
|
+
isRecordingRef.current = false;
|
|
372
|
+
isPausedRef.current = false;
|
|
373
|
+
workletNodeRef.current = null;
|
|
374
|
+
mediaStreamSourceRef.current = null;
|
|
375
|
+
audioTrackRef.current = null;
|
|
376
|
+
onTrackEndedRef.current = null;
|
|
377
|
+
let buffer = null;
|
|
378
|
+
try {
|
|
379
|
+
buffer = buildTakeBufferRef.current();
|
|
380
|
+
} catch (err) {
|
|
381
|
+
console.warn("[waveform-playlist] Failed to keep the interrupted take:", String(err));
|
|
382
|
+
}
|
|
383
|
+
setIsRecording(false);
|
|
384
|
+
setIsPaused(false);
|
|
385
|
+
if (buffer) setAudioBuffer(buffer);
|
|
386
|
+
try {
|
|
387
|
+
onTakeEndedRef.current?.(buffer);
|
|
388
|
+
} catch (err) {
|
|
389
|
+
console.warn("[waveform-playlist] Failed to hand over the interrupted take:", String(err));
|
|
339
390
|
}
|
|
340
391
|
};
|
|
341
392
|
}, []);
|
|
@@ -358,24 +409,28 @@ var import_core2 = require("@waveform-playlist/core");
|
|
|
358
409
|
function useMicrophoneAccess() {
|
|
359
410
|
const [stream, setStream] = (0, import_react2.useState)(null);
|
|
360
411
|
const [devices, setDevices] = (0, import_react2.useState)([]);
|
|
361
|
-
const [
|
|
412
|
+
const [hasLabels, setHasLabels] = (0, import_react2.useState)(false);
|
|
362
413
|
const [isLoading, setIsLoading] = (0, import_react2.useState)(false);
|
|
363
414
|
const [error, setError] = (0, import_react2.useState)(null);
|
|
364
415
|
const streamRef = (0, import_react2.useRef)(null);
|
|
365
416
|
const requestGenerationRef = (0, import_react2.useRef)(0);
|
|
366
417
|
const isUnmountedRef = (0, import_react2.useRef)(false);
|
|
418
|
+
const mountEpochRef = (0, import_react2.useRef)(0);
|
|
367
419
|
const enumerateDevices = (0, import_react2.useCallback)(async () => {
|
|
368
420
|
try {
|
|
369
|
-
const { devices: audioInputs } = await (0, import_core2.enumerateMicrophones)();
|
|
421
|
+
const { devices: audioInputs, hasLabels: labelled } = await (0, import_core2.enumerateMicrophones)();
|
|
370
422
|
setDevices(audioInputs);
|
|
423
|
+
setHasLabels(labelled);
|
|
371
424
|
} catch (err) {
|
|
372
|
-
console.
|
|
425
|
+
console.warn("[waveform-playlist] Failed to enumerate devices:", String(err));
|
|
373
426
|
setError(err instanceof Error ? err : new Error("Failed to enumerate devices"));
|
|
374
427
|
}
|
|
375
428
|
}, []);
|
|
376
429
|
const requestAccess = (0, import_react2.useCallback)(
|
|
377
430
|
async (deviceId, audioConstraints) => {
|
|
378
431
|
const generation = ++requestGenerationRef.current;
|
|
432
|
+
const mountEpoch = mountEpochRef.current;
|
|
433
|
+
const isStale = () => generation !== requestGenerationRef.current || mountEpoch !== mountEpochRef.current || isUnmountedRef.current;
|
|
379
434
|
setIsLoading(true);
|
|
380
435
|
setError(null);
|
|
381
436
|
try {
|
|
@@ -400,20 +455,35 @@ function useMicrophoneAccess() {
|
|
|
400
455
|
audio,
|
|
401
456
|
video: false
|
|
402
457
|
};
|
|
403
|
-
const
|
|
404
|
-
if (
|
|
458
|
+
const mediaDevices = typeof navigator !== "undefined" ? navigator.mediaDevices : void 0;
|
|
459
|
+
if (!mediaDevices || typeof mediaDevices.getUserMedia !== "function") {
|
|
460
|
+
throw new Error(
|
|
461
|
+
"Microphone access is not available: navigator.mediaDevices is undefined. It needs a secure context (https or localhost)."
|
|
462
|
+
);
|
|
463
|
+
}
|
|
464
|
+
const newStream = await mediaDevices.getUserMedia(constraints);
|
|
465
|
+
if (isStale()) {
|
|
405
466
|
newStream.getTracks().forEach((track) => track.stop());
|
|
406
467
|
return;
|
|
407
468
|
}
|
|
408
469
|
streamRef.current = newStream;
|
|
409
470
|
setStream(newStream);
|
|
410
|
-
|
|
471
|
+
const onEnded = () => {
|
|
472
|
+
if (streamRef.current !== newStream) return;
|
|
473
|
+
streamRef.current = null;
|
|
474
|
+
setStream(null);
|
|
475
|
+
setError(
|
|
476
|
+
new Error(
|
|
477
|
+
"The microphone was disconnected or its permission was revoked. Request microphone access again."
|
|
478
|
+
)
|
|
479
|
+
);
|
|
480
|
+
};
|
|
481
|
+
newStream.getTracks().forEach((track) => track.addEventListener("ended", onEnded, { once: true }));
|
|
411
482
|
await enumerateDevices();
|
|
412
483
|
} catch (err) {
|
|
413
|
-
console.
|
|
414
|
-
if (
|
|
484
|
+
console.warn("[waveform-playlist] Failed to access microphone:", String(err));
|
|
485
|
+
if (!isStale()) {
|
|
415
486
|
setError(err instanceof Error ? err : new Error("Failed to access microphone"));
|
|
416
|
-
setHasPermission(false);
|
|
417
487
|
}
|
|
418
488
|
} finally {
|
|
419
489
|
if (generation === requestGenerationRef.current) {
|
|
@@ -425,30 +495,35 @@ function useMicrophoneAccess() {
|
|
|
425
495
|
);
|
|
426
496
|
const stopStream = (0, import_react2.useCallback)(() => {
|
|
427
497
|
requestGenerationRef.current++;
|
|
498
|
+
setIsLoading(false);
|
|
428
499
|
if (streamRef.current) {
|
|
429
500
|
streamRef.current.getTracks().forEach((track) => track.stop());
|
|
430
501
|
streamRef.current = null;
|
|
431
502
|
setStream(null);
|
|
432
|
-
setHasPermission(false);
|
|
433
503
|
}
|
|
434
504
|
}, []);
|
|
435
505
|
(0, import_react2.useEffect)(() => {
|
|
436
506
|
isUnmountedRef.current = false;
|
|
507
|
+
const epoch = mountEpochRef.current;
|
|
437
508
|
enumerateDevices();
|
|
438
|
-
navigator
|
|
509
|
+
const mediaDevices = typeof navigator !== "undefined" ? navigator.mediaDevices : void 0;
|
|
510
|
+
mediaDevices?.addEventListener("devicechange", enumerateDevices);
|
|
439
511
|
return () => {
|
|
440
|
-
|
|
512
|
+
mediaDevices?.removeEventListener("devicechange", enumerateDevices);
|
|
441
513
|
isUnmountedRef.current = true;
|
|
514
|
+
mountEpochRef.current = epoch + 1;
|
|
515
|
+
setIsLoading(false);
|
|
442
516
|
if (streamRef.current) {
|
|
443
517
|
streamRef.current.getTracks().forEach((track) => track.stop());
|
|
444
518
|
streamRef.current = null;
|
|
519
|
+
setStream(null);
|
|
445
520
|
}
|
|
446
521
|
};
|
|
447
522
|
}, [enumerateDevices]);
|
|
448
523
|
return {
|
|
449
524
|
stream,
|
|
450
525
|
devices,
|
|
451
|
-
|
|
526
|
+
hasLabels,
|
|
452
527
|
isLoading,
|
|
453
528
|
requestAccess,
|
|
454
529
|
stopStream,
|
|
@@ -630,18 +705,29 @@ var import_core5 = require("@waveform-playlist/core");
|
|
|
630
705
|
var import_playout3 = require("@waveform-playlist/playout");
|
|
631
706
|
function useIntegratedRecording(arrangement, selectedTrackId, options = {}) {
|
|
632
707
|
const { currentTime = 0, audioConstraints, latencyOffset, ...recordingOptions } = options;
|
|
633
|
-
const
|
|
634
|
-
const [selectedDevice, setSelectedDevice] = (0, import_react4.useState)(null);
|
|
708
|
+
const audioResumedRef = (0, import_react4.useRef)(false);
|
|
635
709
|
const [hookError, setHookError] = (0, import_react4.useState)(null);
|
|
636
710
|
const recordingStartTimeRef = (0, import_react4.useRef)(0);
|
|
637
711
|
const recordingTrackIdRef = (0, import_react4.useRef)(null);
|
|
638
712
|
const [recordingTrackId, setRecordingTrackId] = (0, import_react4.useState)(null);
|
|
639
|
-
const
|
|
713
|
+
const startingEpochRef = (0, import_react4.useRef)(null);
|
|
714
|
+
const mountEpochRef = (0, import_react4.useRef)(0);
|
|
715
|
+
(0, import_react4.useEffect)(() => {
|
|
716
|
+
const epoch = mountEpochRef.current;
|
|
717
|
+
return () => {
|
|
718
|
+
mountEpochRef.current = epoch + 1;
|
|
719
|
+
};
|
|
720
|
+
}, []);
|
|
640
721
|
const selectedTrackIdRef = (0, import_react4.useRef)(selectedTrackId);
|
|
641
722
|
selectedTrackIdRef.current = selectedTrackId;
|
|
642
723
|
const currentTimeRef = (0, import_react4.useRef)(currentTime);
|
|
643
724
|
currentTimeRef.current = currentTime;
|
|
644
|
-
const
|
|
725
|
+
const audioConstraintsRef = (0, import_react4.useRef)(audioConstraints);
|
|
726
|
+
audioConstraintsRef.current = audioConstraints;
|
|
727
|
+
const finishTakeRef = (0, import_react4.useRef)(() => {
|
|
728
|
+
});
|
|
729
|
+
const { stream, devices, hasLabels, requestAccess, error: micError } = useMicrophoneAccess();
|
|
730
|
+
const selectedDevice = (0, import_react4.useMemo)(() => openDeviceId(stream), [stream]);
|
|
645
731
|
const {
|
|
646
732
|
meter,
|
|
647
733
|
resetPeak,
|
|
@@ -658,9 +744,18 @@ function useIntegratedRecording(arrangement, selectedTrackId, options = {}) {
|
|
|
658
744
|
pauseRecording,
|
|
659
745
|
resumeRecording,
|
|
660
746
|
error: recError
|
|
661
|
-
} =
|
|
747
|
+
} = useRecordingWithTakeEnded(
|
|
748
|
+
stream,
|
|
749
|
+
recordingOptions,
|
|
750
|
+
(buffer) => (
|
|
751
|
+
// The take ended without our stopRecording(): keep it (#822).
|
|
752
|
+
finishTakeRef.current(buffer)
|
|
753
|
+
)
|
|
754
|
+
);
|
|
755
|
+
const startRecRef = (0, import_react4.useRef)(startRec);
|
|
756
|
+
startRecRef.current = startRec;
|
|
662
757
|
const startRecording = (0, import_react4.useCallback)(async () => {
|
|
663
|
-
if (isRecordingRef.current ||
|
|
758
|
+
if (isRecordingRef.current || startingEpochRef.current === mountEpochRef.current) return false;
|
|
664
759
|
if (!selectedTrackIdRef.current) {
|
|
665
760
|
setHookError(
|
|
666
761
|
new Error("Cannot start recording: no track selected. Select or create a track first.")
|
|
@@ -672,39 +767,39 @@ function useIntegratedRecording(arrangement, selectedTrackId, options = {}) {
|
|
|
672
767
|
setHookError(refusal);
|
|
673
768
|
return false;
|
|
674
769
|
}
|
|
675
|
-
|
|
770
|
+
const mountEpoch = mountEpochRef.current;
|
|
771
|
+
startingEpochRef.current = mountEpoch;
|
|
772
|
+
const isStale = () => mountEpoch !== mountEpochRef.current;
|
|
676
773
|
try {
|
|
677
774
|
setHookError(null);
|
|
678
|
-
if (!
|
|
775
|
+
if (!audioResumedRef.current) {
|
|
679
776
|
await (0, import_playout3.resumeGlobalAudioContext)();
|
|
680
|
-
|
|
777
|
+
audioResumedRef.current = true;
|
|
681
778
|
}
|
|
779
|
+
if (isStale()) return false;
|
|
682
780
|
recordingStartTimeRef.current = currentTimeRef.current;
|
|
683
781
|
recordingTrackIdRef.current = selectedTrackIdRef.current;
|
|
684
782
|
setRecordingTrackId(selectedTrackIdRef.current);
|
|
685
|
-
const started = await
|
|
686
|
-
if (!started) setRecordingTrackId(null);
|
|
783
|
+
const started = await startRecRef.current();
|
|
784
|
+
if (!started && !isStale()) setRecordingTrackId(null);
|
|
687
785
|
return started;
|
|
688
786
|
} catch (err) {
|
|
689
|
-
|
|
690
|
-
|
|
787
|
+
if (!isStale()) {
|
|
788
|
+
setRecordingTrackId(null);
|
|
789
|
+
setHookError(err instanceof Error ? err : new Error(String(err)));
|
|
790
|
+
}
|
|
691
791
|
return false;
|
|
692
792
|
} finally {
|
|
693
|
-
|
|
793
|
+
if (startingEpochRef.current === mountEpoch) startingEpochRef.current = null;
|
|
694
794
|
}
|
|
695
|
-
}, [arrangement
|
|
696
|
-
const
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
setHookError(err instanceof Error ? err : new Error(String(err)));
|
|
702
|
-
return;
|
|
703
|
-
} finally {
|
|
795
|
+
}, [arrangement]);
|
|
796
|
+
const finishTake = (0, import_react4.useCallback)(
|
|
797
|
+
(buffer) => {
|
|
798
|
+
if (!buffer) return;
|
|
799
|
+
const trackId = recordingTrackIdRef.current;
|
|
800
|
+
recordingTrackIdRef.current = null;
|
|
704
801
|
setRecordingTrackId(null);
|
|
705
|
-
|
|
706
|
-
const trackId = recordingTrackIdRef.current;
|
|
707
|
-
if (buffer && trackId) {
|
|
802
|
+
if (!trackId) return;
|
|
708
803
|
if (!arrangement.getTracks().some((t) => t.id === trackId)) {
|
|
709
804
|
const err = new Error(
|
|
710
805
|
`Recording completed but track "${trackId}" no longer exists. The recorded audio could not be saved.`
|
|
@@ -750,19 +845,19 @@ function useIntegratedRecording(arrangement, selectedTrackId, options = {}) {
|
|
|
750
845
|
console.warn(`[waveform-playlist] ${notSaved.message}`);
|
|
751
846
|
setHookError(notSaved);
|
|
752
847
|
}
|
|
848
|
+
},
|
|
849
|
+
[arrangement, latencyOffset]
|
|
850
|
+
);
|
|
851
|
+
finishTakeRef.current = finishTake;
|
|
852
|
+
const stopRecording = (0, import_react4.useCallback)(async () => {
|
|
853
|
+
let buffer = null;
|
|
854
|
+
try {
|
|
855
|
+
buffer = await stopRec();
|
|
856
|
+
} catch (err) {
|
|
857
|
+
setHookError(err instanceof Error ? err : new Error(String(err)));
|
|
753
858
|
}
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
if (!hasPermission || devices.length === 0) return;
|
|
757
|
-
if (selectedDevice === null) {
|
|
758
|
-
setSelectedDevice(devices[0].deviceId);
|
|
759
|
-
} else if (!devices.some((d) => d.deviceId === selectedDevice)) {
|
|
760
|
-
const fallbackId = devices[0].deviceId;
|
|
761
|
-
setSelectedDevice(fallbackId);
|
|
762
|
-
resetPeak();
|
|
763
|
-
requestAccess(fallbackId, audioConstraints);
|
|
764
|
-
}
|
|
765
|
-
}, [hasPermission, devices, selectedDevice, resetPeak, requestAccess, audioConstraints]);
|
|
859
|
+
finishTakeRef.current(buffer);
|
|
860
|
+
}, [stopRec]);
|
|
766
861
|
const isRecordingRef = (0, import_react4.useRef)(isRecording);
|
|
767
862
|
isRecordingRef.current = isRecording;
|
|
768
863
|
const requestMicAccess = (0, import_react4.useCallback)(async () => {
|
|
@@ -772,13 +867,13 @@ function useIntegratedRecording(arrangement, selectedTrackId, options = {}) {
|
|
|
772
867
|
}
|
|
773
868
|
try {
|
|
774
869
|
setHookError(null);
|
|
775
|
-
await requestAccess(void 0,
|
|
870
|
+
await requestAccess(void 0, audioConstraintsRef.current);
|
|
776
871
|
await (0, import_playout3.resumeGlobalAudioContext)();
|
|
777
|
-
|
|
872
|
+
audioResumedRef.current = true;
|
|
778
873
|
} catch (err) {
|
|
779
874
|
setHookError(err instanceof Error ? err : new Error(String(err)));
|
|
780
875
|
}
|
|
781
|
-
}, [requestAccess
|
|
876
|
+
}, [requestAccess]);
|
|
782
877
|
const changeDevice = (0, import_react4.useCallback)(
|
|
783
878
|
async (deviceId) => {
|
|
784
879
|
if (isRecordingRef.current) {
|
|
@@ -787,16 +882,15 @@ function useIntegratedRecording(arrangement, selectedTrackId, options = {}) {
|
|
|
787
882
|
}
|
|
788
883
|
try {
|
|
789
884
|
setHookError(null);
|
|
790
|
-
setSelectedDevice(deviceId);
|
|
791
885
|
resetPeak();
|
|
792
|
-
await requestAccess(deviceId,
|
|
886
|
+
await requestAccess(deviceId, audioConstraintsRef.current);
|
|
793
887
|
await (0, import_playout3.resumeGlobalAudioContext)();
|
|
794
|
-
|
|
888
|
+
audioResumedRef.current = true;
|
|
795
889
|
} catch (err) {
|
|
796
890
|
setHookError(err instanceof Error ? err : new Error(String(err)));
|
|
797
891
|
}
|
|
798
892
|
},
|
|
799
|
-
[requestAccess,
|
|
893
|
+
[requestAccess, resetPeak]
|
|
800
894
|
);
|
|
801
895
|
return {
|
|
802
896
|
// Recording state
|
|
@@ -811,7 +905,7 @@ function useIntegratedRecording(arrangement, selectedTrackId, options = {}) {
|
|
|
811
905
|
// Microphone state
|
|
812
906
|
stream,
|
|
813
907
|
devices,
|
|
814
|
-
|
|
908
|
+
hasLabels,
|
|
815
909
|
selectedDevice,
|
|
816
910
|
// Recording controls
|
|
817
911
|
startRecording,
|
|
@@ -822,6 +916,10 @@ function useIntegratedRecording(arrangement, selectedTrackId, options = {}) {
|
|
|
822
916
|
changeDevice
|
|
823
917
|
};
|
|
824
918
|
}
|
|
919
|
+
function openDeviceId(stream) {
|
|
920
|
+
const track = stream?.getAudioTracks()[0];
|
|
921
|
+
return track?.getSettings?.().deviceId || null;
|
|
922
|
+
}
|
|
825
923
|
|
|
826
924
|
// src/hooks/useRecordingProgress.ts
|
|
827
925
|
var import_ui_components = require("@waveform-playlist/ui-components");
|