@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/README.md
CHANGED
|
@@ -9,7 +9,7 @@ Audio recording support for waveform-playlist using AudioWorklet — mic capture
|
|
|
9
9
|
- **Sample-accurate VU metering** — a dedicated meter worklet measures peak/RMS on every sample, not just once per animation frame
|
|
10
10
|
- **Multi-channel recording** — auto-detects the microphone's actual channel count from the `MediaStream`
|
|
11
11
|
- **Overdub with latency compensation** — record against existing playback; the finalized clip's timeline position accounts for output latency and Tone.js scheduling lookahead, with an optional manual override
|
|
12
|
-
- **Device selection & hot-plug** — enumerate microphones, switch devices between takes (refused while actively recording — a mid-recording switch would silently record silence),
|
|
12
|
+
- **Device selection & hot-plug** — enumerate microphones, switch devices between takes (refused while actively recording — a mid-recording switch would silently record silence), no microphone is opened by the hooks themselves: when the open one is unplugged, `stream` is cleared and the app asks again
|
|
13
13
|
- **Pause/resume** — pauses the worklet itself, not just the UI
|
|
14
14
|
- **Hooks only** — no bundled UI components; wire the state into your own controls or `@waveform-playlist/ui-components`'s `SegmentedVUMeter`
|
|
15
15
|
|
|
@@ -188,7 +188,7 @@ The core AudioWorklet-based recording hook.
|
|
|
188
188
|
**Returns (`UseRecordingReturn`):**
|
|
189
189
|
- `isRecording: boolean`, `isPaused: boolean`
|
|
190
190
|
- `progress: RecordingProgressSource` — a source of `{ duration: number; peaks: (Int8Array | Int16Array)[] }` (seconds elapsed; one peaks entry per channel, growing live). Published once per animation frame while recording; the hook does not re-render for it. After a stop, `duration` is the recorded buffer's length
|
|
191
|
-
- `audioBuffer: AudioBuffer | null` — set after `stopRecording()` resolves
|
|
191
|
+
- `audioBuffer: AudioBuffer | null` — set after `stopRecording()` resolves, and when a take ends by itself (microphone unplugged, component hidden mid-take)
|
|
192
192
|
- `startRecording: () => Promise<boolean>` — resolves `true` when the capture pipeline actually started (`false`: no stream, already recording, worklet failure); check it before starting synchronized playback
|
|
193
193
|
- `stopRecording: () => Promise<AudioBuffer | null>` — awaits the worklet's final flush before resolving, so the last samples are never dropped
|
|
194
194
|
- `pauseRecording: () => void`, `resumeRecording: () => void` — pause/resume the worklet itself, not just the UI
|
|
@@ -232,7 +232,7 @@ Batteries-included hook: wires `useMicrophoneAccess` + `useMicrophoneLevel` + `u
|
|
|
232
232
|
|
|
233
233
|
### Types
|
|
234
234
|
|
|
235
|
-
`
|
|
235
|
+
`MicrophoneDevice`, `RecordingOptions`, `UseRecordingReturn`, `UseMicrophoneAccessReturn` are all exported from the package root for consumers building their own UI around these hooks.
|
|
236
236
|
|
|
237
237
|
## Architecture
|
|
238
238
|
|
package/dist/index.d.mts
CHANGED
|
@@ -6,17 +6,6 @@ import { UseThrottledValueOptions } from '@waveform-playlist/ui-components';
|
|
|
6
6
|
* Types for the recording package
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
interface RecordingState {
|
|
10
|
-
isRecording: boolean;
|
|
11
|
-
isPaused: boolean;
|
|
12
|
-
duration: number;
|
|
13
|
-
sampleRate: number;
|
|
14
|
-
}
|
|
15
|
-
interface RecordingData {
|
|
16
|
-
buffer: AudioBuffer | null;
|
|
17
|
-
peaks: (Int8Array | Int16Array)[];
|
|
18
|
-
duration: number;
|
|
19
|
-
}
|
|
20
9
|
interface MicrophoneDevice {
|
|
21
10
|
deviceId: string;
|
|
22
11
|
label: string;
|
|
@@ -41,16 +30,6 @@ interface RecordingOptions {
|
|
|
41
30
|
* Default: 16
|
|
42
31
|
*/
|
|
43
32
|
bits?: 8 | 16;
|
|
44
|
-
/**
|
|
45
|
-
* Specific device ID to use for recording
|
|
46
|
-
*/
|
|
47
|
-
deviceId?: string;
|
|
48
|
-
/**
|
|
49
|
-
* MediaTrackConstraints for audio recording
|
|
50
|
-
* Use this to customize echo cancellation, noise suppression, auto gain control, latency, etc.
|
|
51
|
-
* Default: Recording-optimized settings (all processing disabled, latency: 0 for low latency)
|
|
52
|
-
*/
|
|
53
|
-
audioConstraints?: MediaTrackConstraints;
|
|
54
33
|
}
|
|
55
34
|
interface UseRecordingReturn {
|
|
56
35
|
isRecording: boolean;
|
|
@@ -232,8 +211,13 @@ interface UseIntegratedRecordingReturn {
|
|
|
232
211
|
* (still loading, failed to load, or a clip that cannot be made from it). */
|
|
233
212
|
startRecording: () => Promise<boolean>;
|
|
234
213
|
/** Stop the take and add it to the track it was started on, with one
|
|
235
|
-
* `arrangement.updateTrack()`: one undo step.
|
|
236
|
-
|
|
214
|
+
* `arrangement.updateTrack()`: one undo step. Resolves when the take is on
|
|
215
|
+
* its track (or was refused, with `error` set); it never rejects.
|
|
216
|
+
*
|
|
217
|
+
* A take that ends without this call is added the same way: when the
|
|
218
|
+
* microphone is unplugged (`error` then says so), and when the component
|
|
219
|
+
* using the hook unmounts or is hidden mid-take. */
|
|
220
|
+
stopRecording: () => Promise<void>;
|
|
237
221
|
pauseRecording: () => void;
|
|
238
222
|
resumeRecording: () => void;
|
|
239
223
|
requestMicAccess: () => Promise<void>;
|
|
@@ -255,4 +239,4 @@ declare function useIntegratedRecording(arrangement: Arrangement, selectedTrackI
|
|
|
255
239
|
*/
|
|
256
240
|
declare function useRecordingProgress(progress: RecordingProgressSource, options?: UseThrottledValueOptions): RecordingProgress;
|
|
257
241
|
|
|
258
|
-
export { type IntegratedRecordingOptions, type MicrophoneDevice, type
|
|
242
|
+
export { type IntegratedRecordingOptions, type MicrophoneDevice, type RecordingOptions, type UseIntegratedRecordingReturn, type UseMicrophoneAccessReturn, type UseMicrophoneLevelOptions, type UseMicrophoneLevelReturn, type UseRecordingReturn, useIntegratedRecording, useMicrophoneAccess, useMicrophoneLevel, useRecording, useRecordingProgress };
|
package/dist/index.d.ts
CHANGED
|
@@ -6,17 +6,6 @@ import { UseThrottledValueOptions } from '@waveform-playlist/ui-components';
|
|
|
6
6
|
* Types for the recording package
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
interface RecordingState {
|
|
10
|
-
isRecording: boolean;
|
|
11
|
-
isPaused: boolean;
|
|
12
|
-
duration: number;
|
|
13
|
-
sampleRate: number;
|
|
14
|
-
}
|
|
15
|
-
interface RecordingData {
|
|
16
|
-
buffer: AudioBuffer | null;
|
|
17
|
-
peaks: (Int8Array | Int16Array)[];
|
|
18
|
-
duration: number;
|
|
19
|
-
}
|
|
20
9
|
interface MicrophoneDevice {
|
|
21
10
|
deviceId: string;
|
|
22
11
|
label: string;
|
|
@@ -41,16 +30,6 @@ interface RecordingOptions {
|
|
|
41
30
|
* Default: 16
|
|
42
31
|
*/
|
|
43
32
|
bits?: 8 | 16;
|
|
44
|
-
/**
|
|
45
|
-
* Specific device ID to use for recording
|
|
46
|
-
*/
|
|
47
|
-
deviceId?: string;
|
|
48
|
-
/**
|
|
49
|
-
* MediaTrackConstraints for audio recording
|
|
50
|
-
* Use this to customize echo cancellation, noise suppression, auto gain control, latency, etc.
|
|
51
|
-
* Default: Recording-optimized settings (all processing disabled, latency: 0 for low latency)
|
|
52
|
-
*/
|
|
53
|
-
audioConstraints?: MediaTrackConstraints;
|
|
54
33
|
}
|
|
55
34
|
interface UseRecordingReturn {
|
|
56
35
|
isRecording: boolean;
|
|
@@ -232,8 +211,13 @@ interface UseIntegratedRecordingReturn {
|
|
|
232
211
|
* (still loading, failed to load, or a clip that cannot be made from it). */
|
|
233
212
|
startRecording: () => Promise<boolean>;
|
|
234
213
|
/** Stop the take and add it to the track it was started on, with one
|
|
235
|
-
* `arrangement.updateTrack()`: one undo step.
|
|
236
|
-
|
|
214
|
+
* `arrangement.updateTrack()`: one undo step. Resolves when the take is on
|
|
215
|
+
* its track (or was refused, with `error` set); it never rejects.
|
|
216
|
+
*
|
|
217
|
+
* A take that ends without this call is added the same way: when the
|
|
218
|
+
* microphone is unplugged (`error` then says so), and when the component
|
|
219
|
+
* using the hook unmounts or is hidden mid-take. */
|
|
220
|
+
stopRecording: () => Promise<void>;
|
|
237
221
|
pauseRecording: () => void;
|
|
238
222
|
resumeRecording: () => void;
|
|
239
223
|
requestMicAccess: () => Promise<void>;
|
|
@@ -255,4 +239,4 @@ declare function useIntegratedRecording(arrangement: Arrangement, selectedTrackI
|
|
|
255
239
|
*/
|
|
256
240
|
declare function useRecordingProgress(progress: RecordingProgressSource, options?: UseThrottledValueOptions): RecordingProgress;
|
|
257
241
|
|
|
258
|
-
export { type IntegratedRecordingOptions, type MicrophoneDevice, type
|
|
242
|
+
export { type IntegratedRecordingOptions, type MicrophoneDevice, type RecordingOptions, type UseIntegratedRecordingReturn, type UseMicrophoneAccessReturn, type UseMicrophoneLevelOptions, type UseMicrophoneLevelReturn, type UseRecordingReturn, useIntegratedRecording, useMicrophoneAccess, useMicrophoneLevel, useRecording, useRecordingProgress };
|
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);
|
|
@@ -63,7 +66,10 @@ function useRecording(stream, options = {}) {
|
|
|
63
66
|
const finishStopRecordingRef = (0, import_react.useRef)(null);
|
|
64
67
|
const isStartingRef = (0, import_react.useRef)(false);
|
|
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();
|
|
@@ -101,7 +107,16 @@ function useRecording(stream, options = {}) {
|
|
|
101
107
|
setError(new Error("No microphone stream available"));
|
|
102
108
|
return false;
|
|
103
109
|
}
|
|
110
|
+
if (stream.getAudioTracks()[0]?.readyState === "ended") {
|
|
111
|
+
setError(
|
|
112
|
+
new Error(
|
|
113
|
+
"The microphone stream has ended (the device was disconnected or its permission revoked). Request microphone access again."
|
|
114
|
+
)
|
|
115
|
+
);
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
104
118
|
isStartingRef.current = true;
|
|
119
|
+
const mountEpoch = mountEpochRef.current;
|
|
105
120
|
try {
|
|
106
121
|
setError(null);
|
|
107
122
|
const context = (0, import_playout.getGlobalContext)();
|
|
@@ -109,7 +124,7 @@ function useRecording(stream, options = {}) {
|
|
|
109
124
|
await context.resume();
|
|
110
125
|
}
|
|
111
126
|
await loadWorklet();
|
|
112
|
-
if (isUnmountedRef.current) return false;
|
|
127
|
+
if (isUnmountedRef.current || mountEpoch !== mountEpochRef.current) return false;
|
|
113
128
|
const source = context.createMediaStreamSource(stream);
|
|
114
129
|
mediaStreamSourceRef.current = source;
|
|
115
130
|
const detectedChannelCount = stream.getAudioTracks()[0]?.getSettings().channelCount;
|
|
@@ -174,8 +189,9 @@ function useRecording(stream, options = {}) {
|
|
|
174
189
|
if (audioTrack) {
|
|
175
190
|
const onEnded = () => {
|
|
176
191
|
console.warn("[waveform-playlist] Audio track ended (mic unplugged or revoked)");
|
|
177
|
-
stopRecordingRef.current?.();
|
|
192
|
+
const stopped = stopRecordingRef.current?.();
|
|
178
193
|
setError(new Error("Microphone disconnected during recording"));
|
|
194
|
+
stopped?.then((buffer) => onTakeEndedRef.current?.(buffer));
|
|
179
195
|
};
|
|
180
196
|
onTrackEndedRef.current = onEnded;
|
|
181
197
|
audioTrack.addEventListener("ended", onEnded);
|
|
@@ -215,6 +231,18 @@ function useRecording(stream, options = {}) {
|
|
|
215
231
|
stopPromiseRef.current = stopPromise;
|
|
216
232
|
return stopPromise;
|
|
217
233
|
}, []);
|
|
234
|
+
const buildTakeBuffer = (0, import_react.useCallback)(() => {
|
|
235
|
+
const context = (0, import_playout.getGlobalContext)();
|
|
236
|
+
const rawContext = context.rawContext;
|
|
237
|
+
const numChannels = recordedChunksRef.current.length || channelCount;
|
|
238
|
+
const channelData = recordedChunksRef.current.map((chunks) => (0, import_core.concatenateAudioData)(chunks));
|
|
239
|
+
const totalSamples = channelData[0]?.length ?? 0;
|
|
240
|
+
if (totalSamples === 0) {
|
|
241
|
+
console.warn("[waveform-playlist] Recording stopped with 0 samples captured \u2014 discarding");
|
|
242
|
+
return null;
|
|
243
|
+
}
|
|
244
|
+
return (0, import_core.createAudioBuffer)(rawContext, channelData, rawContext.sampleRate, numChannels);
|
|
245
|
+
}, [channelCount]);
|
|
218
246
|
const finishStopRecording = (0, import_react.useCallback)(async () => {
|
|
219
247
|
try {
|
|
220
248
|
if (animationFrameRef.current !== null) {
|
|
@@ -262,16 +290,8 @@ function useRecording(stream, options = {}) {
|
|
|
262
290
|
workletNodeRef.current = null;
|
|
263
291
|
mediaStreamSourceRef.current = null;
|
|
264
292
|
}
|
|
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);
|
|
293
|
+
const buffer = buildTakeBuffer();
|
|
294
|
+
if (!buffer) return null;
|
|
275
295
|
setAudioBuffer(buffer);
|
|
276
296
|
durationRef.current = buffer.duration;
|
|
277
297
|
publishProgress();
|
|
@@ -287,9 +307,11 @@ function useRecording(stream, options = {}) {
|
|
|
287
307
|
setIsRecording(false);
|
|
288
308
|
setIsPaused(false);
|
|
289
309
|
}
|
|
290
|
-
}, [
|
|
310
|
+
}, [buildTakeBuffer, publishProgress]);
|
|
291
311
|
stopRecordingRef.current = stopRecording;
|
|
292
312
|
finishStopRecordingRef.current = finishStopRecording;
|
|
313
|
+
const buildTakeBufferRef = (0, import_react.useRef)(buildTakeBuffer);
|
|
314
|
+
buildTakeBufferRef.current = buildTakeBuffer;
|
|
293
315
|
const pauseRecording = (0, import_react.useCallback)(() => {
|
|
294
316
|
if (isRecordingRef.current && !isPausedRef.current && !isStoppingRef.current) {
|
|
295
317
|
workletNodeRef.current?.port.postMessage({ command: "pause" });
|
|
@@ -313,6 +335,7 @@ function useRecording(stream, options = {}) {
|
|
|
313
335
|
}, [startDurationLoop]);
|
|
314
336
|
(0, import_react.useEffect)(() => {
|
|
315
337
|
isUnmountedRef.current = false;
|
|
338
|
+
mountEpochRef.current += 1;
|
|
316
339
|
return () => {
|
|
317
340
|
isUnmountedRef.current = true;
|
|
318
341
|
if (audioTrackRef.current && onTrackEndedRef.current) {
|
|
@@ -336,6 +359,28 @@ function useRecording(stream, options = {}) {
|
|
|
336
359
|
}
|
|
337
360
|
if (animationFrameRef.current !== null) {
|
|
338
361
|
cancelAnimationFrame(animationFrameRef.current);
|
|
362
|
+
animationFrameRef.current = null;
|
|
363
|
+
}
|
|
364
|
+
if (!isRecordingRef.current || isStoppingRef.current) return;
|
|
365
|
+
isRecordingRef.current = false;
|
|
366
|
+
isPausedRef.current = false;
|
|
367
|
+
workletNodeRef.current = null;
|
|
368
|
+
mediaStreamSourceRef.current = null;
|
|
369
|
+
audioTrackRef.current = null;
|
|
370
|
+
onTrackEndedRef.current = null;
|
|
371
|
+
let buffer = null;
|
|
372
|
+
try {
|
|
373
|
+
buffer = buildTakeBufferRef.current();
|
|
374
|
+
} catch (err) {
|
|
375
|
+
console.warn("[waveform-playlist] Failed to keep the interrupted take:", String(err));
|
|
376
|
+
}
|
|
377
|
+
setIsRecording(false);
|
|
378
|
+
setIsPaused(false);
|
|
379
|
+
if (buffer) setAudioBuffer(buffer);
|
|
380
|
+
try {
|
|
381
|
+
onTakeEndedRef.current?.(buffer);
|
|
382
|
+
} catch (err) {
|
|
383
|
+
console.warn("[waveform-playlist] Failed to hand over the interrupted take:", String(err));
|
|
339
384
|
}
|
|
340
385
|
};
|
|
341
386
|
}, []);
|
|
@@ -369,7 +414,7 @@ function useMicrophoneAccess() {
|
|
|
369
414
|
const { devices: audioInputs } = await (0, import_core2.enumerateMicrophones)();
|
|
370
415
|
setDevices(audioInputs);
|
|
371
416
|
} catch (err) {
|
|
372
|
-
console.
|
|
417
|
+
console.warn("[waveform-playlist] Failed to enumerate devices:", String(err));
|
|
373
418
|
setError(err instanceof Error ? err : new Error("Failed to enumerate devices"));
|
|
374
419
|
}
|
|
375
420
|
}, []);
|
|
@@ -400,7 +445,13 @@ function useMicrophoneAccess() {
|
|
|
400
445
|
audio,
|
|
401
446
|
video: false
|
|
402
447
|
};
|
|
403
|
-
const
|
|
448
|
+
const mediaDevices = typeof navigator !== "undefined" ? navigator.mediaDevices : void 0;
|
|
449
|
+
if (!mediaDevices || typeof mediaDevices.getUserMedia !== "function") {
|
|
450
|
+
throw new Error(
|
|
451
|
+
"Microphone access is not available: navigator.mediaDevices is undefined. It needs a secure context (https or localhost)."
|
|
452
|
+
);
|
|
453
|
+
}
|
|
454
|
+
const newStream = await mediaDevices.getUserMedia(constraints);
|
|
404
455
|
if (generation !== requestGenerationRef.current || isUnmountedRef.current) {
|
|
405
456
|
newStream.getTracks().forEach((track) => track.stop());
|
|
406
457
|
return;
|
|
@@ -408,9 +459,21 @@ function useMicrophoneAccess() {
|
|
|
408
459
|
streamRef.current = newStream;
|
|
409
460
|
setStream(newStream);
|
|
410
461
|
setHasPermission(true);
|
|
462
|
+
const onEnded = () => {
|
|
463
|
+
if (streamRef.current !== newStream) return;
|
|
464
|
+
streamRef.current = null;
|
|
465
|
+
setStream(null);
|
|
466
|
+
setHasPermission(false);
|
|
467
|
+
setError(
|
|
468
|
+
new Error(
|
|
469
|
+
"The microphone was disconnected or its permission was revoked. Request microphone access again."
|
|
470
|
+
)
|
|
471
|
+
);
|
|
472
|
+
};
|
|
473
|
+
newStream.getTracks().forEach((track) => track.addEventListener("ended", onEnded, { once: true }));
|
|
411
474
|
await enumerateDevices();
|
|
412
475
|
} catch (err) {
|
|
413
|
-
console.
|
|
476
|
+
console.warn("[waveform-playlist] Failed to access microphone:", String(err));
|
|
414
477
|
if (generation === requestGenerationRef.current) {
|
|
415
478
|
setError(err instanceof Error ? err : new Error("Failed to access microphone"));
|
|
416
479
|
setHasPermission(false);
|
|
@@ -435,13 +498,16 @@ function useMicrophoneAccess() {
|
|
|
435
498
|
(0, import_react2.useEffect)(() => {
|
|
436
499
|
isUnmountedRef.current = false;
|
|
437
500
|
enumerateDevices();
|
|
438
|
-
navigator
|
|
501
|
+
const mediaDevices = typeof navigator !== "undefined" ? navigator.mediaDevices : void 0;
|
|
502
|
+
mediaDevices?.addEventListener("devicechange", enumerateDevices);
|
|
439
503
|
return () => {
|
|
440
|
-
|
|
504
|
+
mediaDevices?.removeEventListener("devicechange", enumerateDevices);
|
|
441
505
|
isUnmountedRef.current = true;
|
|
442
506
|
if (streamRef.current) {
|
|
443
507
|
streamRef.current.getTracks().forEach((track) => track.stop());
|
|
444
508
|
streamRef.current = null;
|
|
509
|
+
setStream(null);
|
|
510
|
+
setHasPermission(false);
|
|
445
511
|
}
|
|
446
512
|
};
|
|
447
513
|
}, [enumerateDevices]);
|
|
@@ -630,7 +696,7 @@ var import_core5 = require("@waveform-playlist/core");
|
|
|
630
696
|
var import_playout3 = require("@waveform-playlist/playout");
|
|
631
697
|
function useIntegratedRecording(arrangement, selectedTrackId, options = {}) {
|
|
632
698
|
const { currentTime = 0, audioConstraints, latencyOffset, ...recordingOptions } = options;
|
|
633
|
-
const
|
|
699
|
+
const audioResumedRef = (0, import_react4.useRef)(false);
|
|
634
700
|
const [selectedDevice, setSelectedDevice] = (0, import_react4.useState)(null);
|
|
635
701
|
const [hookError, setHookError] = (0, import_react4.useState)(null);
|
|
636
702
|
const recordingStartTimeRef = (0, import_react4.useRef)(0);
|
|
@@ -641,6 +707,10 @@ function useIntegratedRecording(arrangement, selectedTrackId, options = {}) {
|
|
|
641
707
|
selectedTrackIdRef.current = selectedTrackId;
|
|
642
708
|
const currentTimeRef = (0, import_react4.useRef)(currentTime);
|
|
643
709
|
currentTimeRef.current = currentTime;
|
|
710
|
+
const audioConstraintsRef = (0, import_react4.useRef)(audioConstraints);
|
|
711
|
+
audioConstraintsRef.current = audioConstraints;
|
|
712
|
+
const finishTakeRef = (0, import_react4.useRef)(() => {
|
|
713
|
+
});
|
|
644
714
|
const { stream, devices, hasPermission, requestAccess, error: micError } = useMicrophoneAccess();
|
|
645
715
|
const {
|
|
646
716
|
meter,
|
|
@@ -658,7 +728,16 @@ function useIntegratedRecording(arrangement, selectedTrackId, options = {}) {
|
|
|
658
728
|
pauseRecording,
|
|
659
729
|
resumeRecording,
|
|
660
730
|
error: recError
|
|
661
|
-
} =
|
|
731
|
+
} = useRecordingWithTakeEnded(
|
|
732
|
+
stream,
|
|
733
|
+
recordingOptions,
|
|
734
|
+
(buffer) => (
|
|
735
|
+
// The take ended without our stopRecording(): keep it (#822).
|
|
736
|
+
finishTakeRef.current(buffer)
|
|
737
|
+
)
|
|
738
|
+
);
|
|
739
|
+
const startRecRef = (0, import_react4.useRef)(startRec);
|
|
740
|
+
startRecRef.current = startRec;
|
|
662
741
|
const startRecording = (0, import_react4.useCallback)(async () => {
|
|
663
742
|
if (isRecordingRef.current || isStartingRef.current) return false;
|
|
664
743
|
if (!selectedTrackIdRef.current) {
|
|
@@ -675,14 +754,14 @@ function useIntegratedRecording(arrangement, selectedTrackId, options = {}) {
|
|
|
675
754
|
isStartingRef.current = true;
|
|
676
755
|
try {
|
|
677
756
|
setHookError(null);
|
|
678
|
-
if (!
|
|
757
|
+
if (!audioResumedRef.current) {
|
|
679
758
|
await (0, import_playout3.resumeGlobalAudioContext)();
|
|
680
|
-
|
|
759
|
+
audioResumedRef.current = true;
|
|
681
760
|
}
|
|
682
761
|
recordingStartTimeRef.current = currentTimeRef.current;
|
|
683
762
|
recordingTrackIdRef.current = selectedTrackIdRef.current;
|
|
684
763
|
setRecordingTrackId(selectedTrackIdRef.current);
|
|
685
|
-
const started = await
|
|
764
|
+
const started = await startRecRef.current();
|
|
686
765
|
if (!started) setRecordingTrackId(null);
|
|
687
766
|
return started;
|
|
688
767
|
} catch (err) {
|
|
@@ -692,19 +771,14 @@ function useIntegratedRecording(arrangement, selectedTrackId, options = {}) {
|
|
|
692
771
|
} finally {
|
|
693
772
|
isStartingRef.current = false;
|
|
694
773
|
}
|
|
695
|
-
}, [arrangement
|
|
696
|
-
const
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
setHookError(err instanceof Error ? err : new Error(String(err)));
|
|
702
|
-
return;
|
|
703
|
-
} finally {
|
|
774
|
+
}, [arrangement]);
|
|
775
|
+
const finishTake = (0, import_react4.useCallback)(
|
|
776
|
+
(buffer) => {
|
|
777
|
+
if (!buffer) return;
|
|
778
|
+
const trackId = recordingTrackIdRef.current;
|
|
779
|
+
recordingTrackIdRef.current = null;
|
|
704
780
|
setRecordingTrackId(null);
|
|
705
|
-
|
|
706
|
-
const trackId = recordingTrackIdRef.current;
|
|
707
|
-
if (buffer && trackId) {
|
|
781
|
+
if (!trackId) return;
|
|
708
782
|
if (!arrangement.getTracks().some((t) => t.id === trackId)) {
|
|
709
783
|
const err = new Error(
|
|
710
784
|
`Recording completed but track "${trackId}" no longer exists. The recorded audio could not be saved.`
|
|
@@ -750,19 +824,25 @@ function useIntegratedRecording(arrangement, selectedTrackId, options = {}) {
|
|
|
750
824
|
console.warn(`[waveform-playlist] ${notSaved.message}`);
|
|
751
825
|
setHookError(notSaved);
|
|
752
826
|
}
|
|
827
|
+
},
|
|
828
|
+
[arrangement, latencyOffset]
|
|
829
|
+
);
|
|
830
|
+
finishTakeRef.current = finishTake;
|
|
831
|
+
const stopRecording = (0, import_react4.useCallback)(async () => {
|
|
832
|
+
let buffer = null;
|
|
833
|
+
try {
|
|
834
|
+
buffer = await stopRec();
|
|
835
|
+
} catch (err) {
|
|
836
|
+
setHookError(err instanceof Error ? err : new Error(String(err)));
|
|
753
837
|
}
|
|
754
|
-
|
|
838
|
+
finishTakeRef.current(buffer);
|
|
839
|
+
}, [stopRec]);
|
|
755
840
|
(0, import_react4.useEffect)(() => {
|
|
756
841
|
if (!hasPermission || devices.length === 0) return;
|
|
757
|
-
if (selectedDevice === null) {
|
|
842
|
+
if (selectedDevice === null || !devices.some((d) => d.deviceId === selectedDevice)) {
|
|
758
843
|
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
844
|
}
|
|
765
|
-
}, [hasPermission, devices, selectedDevice
|
|
845
|
+
}, [hasPermission, devices, selectedDevice]);
|
|
766
846
|
const isRecordingRef = (0, import_react4.useRef)(isRecording);
|
|
767
847
|
isRecordingRef.current = isRecording;
|
|
768
848
|
const requestMicAccess = (0, import_react4.useCallback)(async () => {
|
|
@@ -772,13 +852,14 @@ function useIntegratedRecording(arrangement, selectedTrackId, options = {}) {
|
|
|
772
852
|
}
|
|
773
853
|
try {
|
|
774
854
|
setHookError(null);
|
|
775
|
-
|
|
855
|
+
setSelectedDevice(null);
|
|
856
|
+
await requestAccess(void 0, audioConstraintsRef.current);
|
|
776
857
|
await (0, import_playout3.resumeGlobalAudioContext)();
|
|
777
|
-
|
|
858
|
+
audioResumedRef.current = true;
|
|
778
859
|
} catch (err) {
|
|
779
860
|
setHookError(err instanceof Error ? err : new Error(String(err)));
|
|
780
861
|
}
|
|
781
|
-
}, [requestAccess
|
|
862
|
+
}, [requestAccess]);
|
|
782
863
|
const changeDevice = (0, import_react4.useCallback)(
|
|
783
864
|
async (deviceId) => {
|
|
784
865
|
if (isRecordingRef.current) {
|
|
@@ -789,14 +870,14 @@ function useIntegratedRecording(arrangement, selectedTrackId, options = {}) {
|
|
|
789
870
|
setHookError(null);
|
|
790
871
|
setSelectedDevice(deviceId);
|
|
791
872
|
resetPeak();
|
|
792
|
-
await requestAccess(deviceId,
|
|
873
|
+
await requestAccess(deviceId, audioConstraintsRef.current);
|
|
793
874
|
await (0, import_playout3.resumeGlobalAudioContext)();
|
|
794
|
-
|
|
875
|
+
audioResumedRef.current = true;
|
|
795
876
|
} catch (err) {
|
|
796
877
|
setHookError(err instanceof Error ? err : new Error(String(err)));
|
|
797
878
|
}
|
|
798
879
|
},
|
|
799
|
-
[requestAccess,
|
|
880
|
+
[requestAccess, resetPeak]
|
|
800
881
|
);
|
|
801
882
|
return {
|
|
802
883
|
// Recording state
|