@arbidocs/react 0.4.109 → 0.4.111
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 +1 -1
- package/dist/artifacts/index.cjs +13 -13
- package/dist/artifacts/index.js +2 -2
- package/dist/{chunk-P4A5EG5B.js → chunk-5L46Z7HZ.js} +443 -36
- package/dist/chunk-5L46Z7HZ.js.map +1 -0
- package/dist/{chunk-EEW2SH76.cjs → chunk-XD4P4KRH.cjs} +451 -35
- package/dist/chunk-XD4P4KRH.cjs.map +1 -0
- package/dist/index.cjs +419 -338
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +480 -463
- package/dist/index.d.ts +480 -463
- package/dist/index.js +158 -115
- package/dist/index.js.map +1 -1
- package/dist/ui/index.cjs +139 -139
- package/dist/ui/index.js +3 -3
- package/package.json +3 -3
- package/dist/chunk-EEW2SH76.cjs.map +0 -1
- package/dist/chunk-P4A5EG5B.js.map +0 -1
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var chunkWYZJLZ67_cjs = require('./chunk-WYZJLZ67.cjs');
|
|
4
3
|
var chunkTBELZWGV_cjs = require('./chunk-TBELZWGV.cjs');
|
|
4
|
+
var chunkWYZJLZ67_cjs = require('./chunk-WYZJLZ67.cjs');
|
|
5
5
|
var chunkHJDR5HEO_cjs = require('./chunk-HJDR5HEO.cjs');
|
|
6
6
|
var chunkS6A6FSG7_cjs = require('./chunk-S6A6FSG7.cjs');
|
|
7
7
|
var React = require('react');
|
|
@@ -709,8 +709,256 @@ function classifyMediaError(err, kind) {
|
|
|
709
709
|
}
|
|
710
710
|
return `Could not access the ${kind}. Please try again.`;
|
|
711
711
|
}
|
|
712
|
+
|
|
713
|
+
// src/documents/chunked-recorder.ts
|
|
714
|
+
var AUDIO_MIME_PREFERENCE = [
|
|
715
|
+
"audio/webm;codecs=opus",
|
|
716
|
+
"audio/ogg;codecs=opus",
|
|
717
|
+
"audio/mp4;codecs=mp4a.40.2",
|
|
718
|
+
"audio/mp4"
|
|
719
|
+
];
|
|
720
|
+
var VIDEO_MIME_PREFERENCE = [
|
|
721
|
+
"video/webm;codecs=av01,opus",
|
|
722
|
+
"video/webm;codecs=av1,opus",
|
|
723
|
+
"video/webm;codecs=vp9,opus",
|
|
724
|
+
"video/webm;codecs=vp8,opus",
|
|
725
|
+
"video/webm",
|
|
726
|
+
"video/mp4"
|
|
727
|
+
];
|
|
728
|
+
function pickRecorderMimeType(kind, isTypeSupported = (t) => typeof MediaRecorder !== "undefined" && MediaRecorder.isTypeSupported(t)) {
|
|
729
|
+
const list = kind === "audio" ? AUDIO_MIME_PREFERENCE : VIDEO_MIME_PREFERENCE;
|
|
730
|
+
for (const type of list) {
|
|
731
|
+
try {
|
|
732
|
+
if (isTypeSupported(type)) return type;
|
|
733
|
+
} catch {
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
return list[list.length - 1];
|
|
737
|
+
}
|
|
738
|
+
function createChunkedRecorder(options) {
|
|
739
|
+
const now = options.now ?? (() => Date.now());
|
|
740
|
+
const mode = options.mode ?? "restart";
|
|
741
|
+
let running = false;
|
|
742
|
+
let index = 0;
|
|
743
|
+
let timer = null;
|
|
744
|
+
let tail = Promise.resolve();
|
|
745
|
+
const recorderInit = () => ({
|
|
746
|
+
mimeType: options.mimeType,
|
|
747
|
+
...options.audioBitsPerSecond ? { audioBitsPerSecond: options.audioBitsPerSecond } : {},
|
|
748
|
+
...options.videoBitsPerSecond ? { videoBitsPerSecond: options.videoBitsPerSecond } : {}
|
|
749
|
+
});
|
|
750
|
+
const fail = (err) => {
|
|
751
|
+
running = false;
|
|
752
|
+
if (timer !== null) clearTimeout(timer);
|
|
753
|
+
timer = null;
|
|
754
|
+
options.onError?.(err);
|
|
755
|
+
};
|
|
756
|
+
const beginPart = () => {
|
|
757
|
+
let recorder;
|
|
758
|
+
try {
|
|
759
|
+
recorder = new MediaRecorder(options.stream, recorderInit());
|
|
760
|
+
} catch (err) {
|
|
761
|
+
fail(err);
|
|
762
|
+
return void 0;
|
|
763
|
+
}
|
|
764
|
+
const chunks = [];
|
|
765
|
+
const part = { recorder, startedAt: now(), chunks };
|
|
766
|
+
recorder.ondataavailable = (e) => {
|
|
767
|
+
if (e.data.size > 0) chunks.push(e.data);
|
|
768
|
+
};
|
|
769
|
+
recorder.onstart = () => {
|
|
770
|
+
part.startedAt = now();
|
|
771
|
+
};
|
|
772
|
+
try {
|
|
773
|
+
recorder.start();
|
|
774
|
+
} catch (err) {
|
|
775
|
+
fail(err);
|
|
776
|
+
return void 0;
|
|
777
|
+
}
|
|
778
|
+
return part;
|
|
779
|
+
};
|
|
780
|
+
const endPart = (part) => new Promise((resolve) => {
|
|
781
|
+
const { recorder, startedAt, chunks } = part;
|
|
782
|
+
const durationMs = Math.max(0, now() - startedAt);
|
|
783
|
+
const partIndex = index++;
|
|
784
|
+
const emit = () => {
|
|
785
|
+
if (chunks.length) {
|
|
786
|
+
const type = recorder.mimeType || options.mimeType;
|
|
787
|
+
options.onChunk({
|
|
788
|
+
blob: new Blob(chunks, { type }),
|
|
789
|
+
mimeType: type,
|
|
790
|
+
index: partIndex,
|
|
791
|
+
startedAt,
|
|
792
|
+
durationMs,
|
|
793
|
+
standalone: true
|
|
794
|
+
});
|
|
795
|
+
}
|
|
796
|
+
resolve();
|
|
797
|
+
};
|
|
798
|
+
if (recorder.state === "inactive") {
|
|
799
|
+
emit();
|
|
800
|
+
return;
|
|
801
|
+
}
|
|
802
|
+
recorder.onstop = emit;
|
|
803
|
+
recorder.stop();
|
|
804
|
+
});
|
|
805
|
+
let open;
|
|
806
|
+
const rotate = () => {
|
|
807
|
+
if (!running || !open) return;
|
|
808
|
+
const finished = open;
|
|
809
|
+
open = beginPart();
|
|
810
|
+
tail = tail.then(() => endPart(finished));
|
|
811
|
+
if (running) timer = setTimeout(rotate, options.chunkMs);
|
|
812
|
+
};
|
|
813
|
+
let sliceStartedAt = 0;
|
|
814
|
+
let sliceRecorder = null;
|
|
815
|
+
let stopResolve = null;
|
|
816
|
+
const startTimeslice = () => {
|
|
817
|
+
let recorder;
|
|
818
|
+
try {
|
|
819
|
+
recorder = new MediaRecorder(options.stream, recorderInit());
|
|
820
|
+
} catch (err) {
|
|
821
|
+
fail(err);
|
|
822
|
+
return;
|
|
823
|
+
}
|
|
824
|
+
sliceRecorder = recorder;
|
|
825
|
+
sliceStartedAt = now();
|
|
826
|
+
recorder.ondataavailable = (e) => {
|
|
827
|
+
const at = now();
|
|
828
|
+
const startedAt = sliceStartedAt;
|
|
829
|
+
sliceStartedAt = at;
|
|
830
|
+
if (!e.data.size) return;
|
|
831
|
+
const type = recorder.mimeType || options.mimeType;
|
|
832
|
+
const partIndex = index++;
|
|
833
|
+
options.onChunk({
|
|
834
|
+
blob: e.data,
|
|
835
|
+
mimeType: type,
|
|
836
|
+
index: partIndex,
|
|
837
|
+
startedAt,
|
|
838
|
+
durationMs: Math.max(0, at - startedAt),
|
|
839
|
+
standalone: partIndex === 0
|
|
840
|
+
});
|
|
841
|
+
};
|
|
842
|
+
recorder.onstop = () => {
|
|
843
|
+
stopResolve?.();
|
|
844
|
+
stopResolve = null;
|
|
845
|
+
};
|
|
846
|
+
try {
|
|
847
|
+
recorder.start();
|
|
848
|
+
} catch (err) {
|
|
849
|
+
sliceRecorder = null;
|
|
850
|
+
fail(err);
|
|
851
|
+
return;
|
|
852
|
+
}
|
|
853
|
+
timer = setInterval(() => {
|
|
854
|
+
if (recorder.state === "recording") recorder.requestData();
|
|
855
|
+
}, options.chunkMs);
|
|
856
|
+
};
|
|
857
|
+
return {
|
|
858
|
+
get running() {
|
|
859
|
+
return running;
|
|
860
|
+
},
|
|
861
|
+
start() {
|
|
862
|
+
if (running) return;
|
|
863
|
+
running = true;
|
|
864
|
+
index = 0;
|
|
865
|
+
if (mode === "timeslice") {
|
|
866
|
+
startTimeslice();
|
|
867
|
+
return;
|
|
868
|
+
}
|
|
869
|
+
open = beginPart();
|
|
870
|
+
if (running) timer = setTimeout(rotate, options.chunkMs);
|
|
871
|
+
},
|
|
872
|
+
stop() {
|
|
873
|
+
if (!running && !open && !sliceRecorder) return tail;
|
|
874
|
+
running = false;
|
|
875
|
+
if (timer !== null) {
|
|
876
|
+
clearTimeout(timer);
|
|
877
|
+
clearInterval(timer);
|
|
878
|
+
}
|
|
879
|
+
timer = null;
|
|
880
|
+
if (mode === "timeslice") {
|
|
881
|
+
const recorder = sliceRecorder;
|
|
882
|
+
sliceRecorder = null;
|
|
883
|
+
if (!recorder || recorder.state === "inactive") return tail;
|
|
884
|
+
tail = new Promise((resolve) => {
|
|
885
|
+
stopResolve = resolve;
|
|
886
|
+
recorder.stop();
|
|
887
|
+
});
|
|
888
|
+
return tail;
|
|
889
|
+
}
|
|
890
|
+
const finished = open;
|
|
891
|
+
open = void 0;
|
|
892
|
+
if (finished) tail = tail.then(() => endPart(finished));
|
|
893
|
+
return tail;
|
|
894
|
+
}
|
|
895
|
+
};
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
// src/documents/audio-segmenter.ts
|
|
899
|
+
var DEFAULT_SEGMENTER_CONFIG = {
|
|
900
|
+
voiceThreshold: 0.3,
|
|
901
|
+
silenceMs: 700,
|
|
902
|
+
maxMs: 1e4,
|
|
903
|
+
minVoiceMs: 600
|
|
904
|
+
};
|
|
905
|
+
var INITIAL_SEGMENTER_STATE = { phase: "idle" };
|
|
906
|
+
function levelFromRms(rms) {
|
|
907
|
+
return Math.sqrt(Math.min(1, rms * 8));
|
|
908
|
+
}
|
|
909
|
+
function end(state, at, reason, config) {
|
|
910
|
+
return {
|
|
911
|
+
type: "end",
|
|
912
|
+
at,
|
|
913
|
+
startedAt: state.startedAt,
|
|
914
|
+
voiceMs: state.voiceMs,
|
|
915
|
+
keep: state.voiceMs >= config.minVoiceMs,
|
|
916
|
+
reason
|
|
917
|
+
};
|
|
918
|
+
}
|
|
919
|
+
function stepSegmenter(state, level, t, config = DEFAULT_SEGMENTER_CONFIG) {
|
|
920
|
+
const voice = level >= config.voiceThreshold;
|
|
921
|
+
if (state.phase === "idle") {
|
|
922
|
+
if (!voice) return { state, events: [] };
|
|
923
|
+
return {
|
|
924
|
+
state: { phase: "voice", startedAt: t, lastVoiceAt: t, lastSampleAt: t, voiceMs: 0 },
|
|
925
|
+
events: [{ type: "start", at: t }]
|
|
926
|
+
};
|
|
927
|
+
}
|
|
928
|
+
const dt = Math.max(0, t - state.lastSampleAt);
|
|
929
|
+
const next = {
|
|
930
|
+
...state,
|
|
931
|
+
lastSampleAt: t,
|
|
932
|
+
lastVoiceAt: voice ? t : state.lastVoiceAt,
|
|
933
|
+
voiceMs: state.voiceMs + (voice ? dt : 0)
|
|
934
|
+
};
|
|
935
|
+
if (t - next.startedAt >= config.maxMs) {
|
|
936
|
+
const events = [end(next, t, "max", config)];
|
|
937
|
+
if (!voice) return { state: { phase: "idle" }, events };
|
|
938
|
+
events.push({ type: "start", at: t });
|
|
939
|
+
return {
|
|
940
|
+
state: { phase: "voice", startedAt: t, lastVoiceAt: t, lastSampleAt: t, voiceMs: 0 },
|
|
941
|
+
events
|
|
942
|
+
};
|
|
943
|
+
}
|
|
944
|
+
const silence = config.silenceMs + (next.voiceMs < config.minVoiceMs ? config.holdShortMs ?? 0 : 0);
|
|
945
|
+
if (!voice && t - next.lastVoiceAt >= silence) {
|
|
946
|
+
return { state: { phase: "idle" }, events: [end(next, t, "silence", config)] };
|
|
947
|
+
}
|
|
948
|
+
return { state: next, events: [] };
|
|
949
|
+
}
|
|
950
|
+
function flushSegmenter(state, t, config = DEFAULT_SEGMENTER_CONFIG) {
|
|
951
|
+
if (state.phase === "idle") return { state, events: [] };
|
|
952
|
+
return { state: { phase: "idle" }, events: [end(state, t, "flush", config)] };
|
|
953
|
+
}
|
|
712
954
|
var LEVEL_UPDATE_INTERVAL_MS = 50;
|
|
713
|
-
function
|
|
955
|
+
function pickSegmentMimeType() {
|
|
956
|
+
if (MediaRecorder.isTypeSupported("audio/webm;codecs=opus")) return "audio/webm;codecs=opus";
|
|
957
|
+
return MediaRecorder.isTypeSupported("audio/webm") ? "audio/webm" : "audio/mp4";
|
|
958
|
+
}
|
|
959
|
+
function useAudioRecorder(options = {}) {
|
|
960
|
+
const optionsRef = React.useRef(options);
|
|
961
|
+
optionsRef.current = options;
|
|
714
962
|
const [state, setState] = React.useState("idle");
|
|
715
963
|
const [level, setLevel] = React.useState(0);
|
|
716
964
|
const [elapsedMs, setElapsedMs] = React.useState(0);
|
|
@@ -720,6 +968,7 @@ function useAudioRecorder() {
|
|
|
720
968
|
const mediaRecorderRef = React.useRef(null);
|
|
721
969
|
const chunksRef = React.useRef([]);
|
|
722
970
|
const streamRef = React.useRef(null);
|
|
971
|
+
const ownsStreamRef = React.useRef(false);
|
|
723
972
|
const stateRef = React.useRef("idle");
|
|
724
973
|
stateRef.current = state;
|
|
725
974
|
const segmentStartRef = React.useRef(0);
|
|
@@ -727,60 +976,195 @@ function useAudioRecorder() {
|
|
|
727
976
|
const audioContextRef = React.useRef(null);
|
|
728
977
|
const analyserRef = React.useRef(null);
|
|
729
978
|
const levelRafRef = React.useRef(null);
|
|
979
|
+
const levelIntervalRef = React.useRef(null);
|
|
980
|
+
const segmentingRef = React.useRef(false);
|
|
981
|
+
const segmenterStateRef = React.useRef(INITIAL_SEGMENTER_STATE);
|
|
982
|
+
const openSegmentRef = React.useRef(null);
|
|
983
|
+
const segmentMimeTypeRef = React.useRef(null);
|
|
984
|
+
const chunkedRef = React.useRef(null);
|
|
985
|
+
const segmenterConfig = React.useCallback(
|
|
986
|
+
() => ({ ...DEFAULT_SEGMENTER_CONFIG, ...optionsRef.current.segmenter }),
|
|
987
|
+
[]
|
|
988
|
+
);
|
|
989
|
+
const beginSegment = React.useCallback(() => {
|
|
990
|
+
const stream = streamRef.current;
|
|
991
|
+
const mt = segmentMimeTypeRef.current;
|
|
992
|
+
if (!stream || !mt) return;
|
|
993
|
+
const recorder = new MediaRecorder(stream, { mimeType: mt });
|
|
994
|
+
const chunks = [];
|
|
995
|
+
recorder.ondataavailable = (e) => {
|
|
996
|
+
if (e.data.size > 0) chunks.push(e.data);
|
|
997
|
+
};
|
|
998
|
+
recorder.start();
|
|
999
|
+
openSegmentRef.current = { recorder, chunks, startedAt: Date.now() };
|
|
1000
|
+
}, []);
|
|
1001
|
+
const endSegment = React.useCallback((keep, voiceMs) => {
|
|
1002
|
+
const open = openSegmentRef.current;
|
|
1003
|
+
openSegmentRef.current = null;
|
|
1004
|
+
if (!open) return;
|
|
1005
|
+
const { recorder, chunks, startedAt } = open;
|
|
1006
|
+
const durationMs = Date.now() - startedAt;
|
|
1007
|
+
recorder.onstop = () => {
|
|
1008
|
+
const onSegment = optionsRef.current.onSegment;
|
|
1009
|
+
if (!keep || !chunks.length || !onSegment) return;
|
|
1010
|
+
const type = recorder.mimeType || segmentMimeTypeRef.current || "audio/webm";
|
|
1011
|
+
onSegment({
|
|
1012
|
+
blob: new Blob(chunks, { type }),
|
|
1013
|
+
mimeType: type,
|
|
1014
|
+
startedAt,
|
|
1015
|
+
durationMs,
|
|
1016
|
+
voiceMs
|
|
1017
|
+
});
|
|
1018
|
+
};
|
|
1019
|
+
if (recorder.state !== "inactive") recorder.stop();
|
|
1020
|
+
}, []);
|
|
1021
|
+
const applySegmenterEvents = React.useCallback(
|
|
1022
|
+
(events) => {
|
|
1023
|
+
for (const event of events) {
|
|
1024
|
+
if (event.type === "start") beginSegment();
|
|
1025
|
+
else endSegment(event.keep, event.voiceMs);
|
|
1026
|
+
}
|
|
1027
|
+
},
|
|
1028
|
+
[beginSegment, endSegment]
|
|
1029
|
+
);
|
|
1030
|
+
const flushOpenSegment = React.useCallback(() => {
|
|
1031
|
+
if (!segmentingRef.current) return;
|
|
1032
|
+
const flushed = flushSegmenter(segmenterStateRef.current, performance.now(), segmenterConfig());
|
|
1033
|
+
segmenterStateRef.current = flushed.state;
|
|
1034
|
+
applySegmenterEvents(flushed.events);
|
|
1035
|
+
endSegment(false, 0);
|
|
1036
|
+
}, [segmenterConfig, applySegmenterEvents, endSegment]);
|
|
730
1037
|
const stopMeter = React.useCallback(() => {
|
|
731
1038
|
if (levelRafRef.current !== null) cancelAnimationFrame(levelRafRef.current);
|
|
732
1039
|
levelRafRef.current = null;
|
|
1040
|
+
if (levelIntervalRef.current !== null) clearInterval(levelIntervalRef.current);
|
|
1041
|
+
levelIntervalRef.current = null;
|
|
733
1042
|
analyserRef.current = null;
|
|
734
1043
|
void audioContextRef.current?.close().catch(() => {
|
|
735
1044
|
});
|
|
736
1045
|
audioContextRef.current = null;
|
|
737
1046
|
}, []);
|
|
738
|
-
const startMeter = React.useCallback(
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
1047
|
+
const startMeter = React.useCallback(
|
|
1048
|
+
(stream) => {
|
|
1049
|
+
const audioContext = new AudioContext();
|
|
1050
|
+
const source = audioContext.createMediaStreamSource(stream);
|
|
1051
|
+
const analyser = audioContext.createAnalyser();
|
|
1052
|
+
analyser.fftSize = 256;
|
|
1053
|
+
source.connect(analyser);
|
|
1054
|
+
audioContextRef.current = audioContext;
|
|
1055
|
+
analyserRef.current = analyser;
|
|
1056
|
+
const data = new Uint8Array(analyser.frequencyBinCount);
|
|
1057
|
+
const segmenting = segmentingRef.current;
|
|
1058
|
+
let lastUpdate = 0;
|
|
1059
|
+
const tick = (t) => {
|
|
1060
|
+
const active = analyserRef.current;
|
|
1061
|
+
if (!active) return;
|
|
1062
|
+
if (t - lastUpdate >= LEVEL_UPDATE_INTERVAL_MS) {
|
|
1063
|
+
lastUpdate = t;
|
|
1064
|
+
active.getByteTimeDomainData(data);
|
|
1065
|
+
let sumSquares = 0;
|
|
1066
|
+
for (let i = 0; i < data.length; i++) {
|
|
1067
|
+
const centered = (data[i] - 128) / 128;
|
|
1068
|
+
sumSquares += centered * centered;
|
|
1069
|
+
}
|
|
1070
|
+
const rms = Math.sqrt(sumSquares / data.length);
|
|
1071
|
+
const nextLevel = levelFromRms(rms);
|
|
1072
|
+
setLevel(nextLevel);
|
|
1073
|
+
setElapsedMs(accumulatedMsRef.current + (t - segmentStartRef.current));
|
|
1074
|
+
if (segmenting) {
|
|
1075
|
+
const step = stepSegmenter(segmenterStateRef.current, nextLevel, t, segmenterConfig());
|
|
1076
|
+
segmenterStateRef.current = step.state;
|
|
1077
|
+
applySegmenterEvents(step.events);
|
|
1078
|
+
}
|
|
758
1079
|
}
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
1080
|
+
if (!segmenting) levelRafRef.current = requestAnimationFrame(tick);
|
|
1081
|
+
};
|
|
1082
|
+
if (segmenting) {
|
|
1083
|
+
levelIntervalRef.current = setInterval(
|
|
1084
|
+
() => tick(performance.now()),
|
|
1085
|
+
LEVEL_UPDATE_INTERVAL_MS
|
|
1086
|
+
);
|
|
1087
|
+
} else {
|
|
1088
|
+
levelRafRef.current = requestAnimationFrame(tick);
|
|
762
1089
|
}
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
}, []);
|
|
1090
|
+
},
|
|
1091
|
+
[segmenterConfig, applySegmenterEvents]
|
|
1092
|
+
);
|
|
767
1093
|
const stopTracks = React.useCallback(() => {
|
|
768
|
-
streamRef.current?.getTracks().forEach((t) => t.stop());
|
|
1094
|
+
if (ownsStreamRef.current) streamRef.current?.getTracks().forEach((t) => t.stop());
|
|
769
1095
|
streamRef.current = null;
|
|
1096
|
+
ownsStreamRef.current = false;
|
|
770
1097
|
}, []);
|
|
771
1098
|
React.useEffect(
|
|
772
1099
|
() => () => {
|
|
1100
|
+
void chunkedRef.current?.stop();
|
|
1101
|
+
chunkedRef.current = null;
|
|
1102
|
+
flushOpenSegment();
|
|
1103
|
+
endSegment(false, 0);
|
|
1104
|
+
segmentingRef.current = false;
|
|
1105
|
+
segmenterStateRef.current = INITIAL_SEGMENTER_STATE;
|
|
773
1106
|
stopTracks();
|
|
774
1107
|
stopMeter();
|
|
775
1108
|
},
|
|
776
|
-
[stopTracks, stopMeter]
|
|
1109
|
+
[flushOpenSegment, endSegment, stopTracks, stopMeter]
|
|
777
1110
|
);
|
|
1111
|
+
React.useEffect(() => {
|
|
1112
|
+
const onPageHide = () => flushOpenSegment();
|
|
1113
|
+
window.addEventListener("pagehide", onPageHide);
|
|
1114
|
+
return () => window.removeEventListener("pagehide", onPageHide);
|
|
1115
|
+
}, [flushOpenSegment]);
|
|
778
1116
|
const start = React.useCallback(async () => {
|
|
779
1117
|
if (stateRef.current === "recording" || stateRef.current === "paused") return;
|
|
780
1118
|
setError(null);
|
|
781
1119
|
try {
|
|
782
|
-
const
|
|
1120
|
+
const { track, onSegment } = optionsRef.current;
|
|
1121
|
+
const stream = track ? new MediaStream([track]) : await navigator.mediaDevices.getUserMedia({ audio: true });
|
|
783
1122
|
streamRef.current = stream;
|
|
1123
|
+
ownsStreamRef.current = !track;
|
|
1124
|
+
const { chunkMs } = optionsRef.current;
|
|
1125
|
+
if (onSegment && chunkMs) {
|
|
1126
|
+
const mt2 = optionsRef.current.mimeType ?? pickRecorderMimeType("audio");
|
|
1127
|
+
const chunked = createChunkedRecorder({
|
|
1128
|
+
stream,
|
|
1129
|
+
mimeType: mt2,
|
|
1130
|
+
chunkMs,
|
|
1131
|
+
audioBitsPerSecond: optionsRef.current.audioBitsPerSecond,
|
|
1132
|
+
onChunk: (chunk) => optionsRef.current.onSegment?.({
|
|
1133
|
+
blob: chunk.blob,
|
|
1134
|
+
mimeType: chunk.mimeType,
|
|
1135
|
+
startedAt: chunk.startedAt,
|
|
1136
|
+
durationMs: chunk.durationMs,
|
|
1137
|
+
voiceMs: chunk.durationMs,
|
|
1138
|
+
index: chunk.index
|
|
1139
|
+
}),
|
|
1140
|
+
onError: (err) => setError(classifyMediaError(err, "microphone"))
|
|
1141
|
+
});
|
|
1142
|
+
chunkedRef.current = chunked;
|
|
1143
|
+
segmentingRef.current = false;
|
|
1144
|
+
setMimeType(mt2);
|
|
1145
|
+
setBlob(null);
|
|
1146
|
+
accumulatedMsRef.current = 0;
|
|
1147
|
+
segmentStartRef.current = performance.now();
|
|
1148
|
+
chunked.start();
|
|
1149
|
+
stateRef.current = "recording";
|
|
1150
|
+
setState("recording");
|
|
1151
|
+
return;
|
|
1152
|
+
}
|
|
1153
|
+
if (onSegment) {
|
|
1154
|
+
const mt2 = pickSegmentMimeType();
|
|
1155
|
+
segmentMimeTypeRef.current = mt2;
|
|
1156
|
+
segmentingRef.current = true;
|
|
1157
|
+
segmenterStateRef.current = INITIAL_SEGMENTER_STATE;
|
|
1158
|
+
setMimeType(mt2);
|
|
1159
|
+
setBlob(null);
|
|
1160
|
+
accumulatedMsRef.current = 0;
|
|
1161
|
+
segmentStartRef.current = performance.now();
|
|
1162
|
+
stateRef.current = "recording";
|
|
1163
|
+
setState("recording");
|
|
1164
|
+
startMeter(stream);
|
|
1165
|
+
return;
|
|
1166
|
+
}
|
|
1167
|
+
segmentingRef.current = false;
|
|
784
1168
|
const mt = MediaRecorder.isTypeSupported("audio/webm") ? "audio/webm" : "audio/mp4";
|
|
785
1169
|
const recorder = new MediaRecorder(stream, { mimeType: mt });
|
|
786
1170
|
chunksRef.current = [];
|
|
@@ -796,11 +1180,13 @@ function useAudioRecorder() {
|
|
|
796
1180
|
setState("recording");
|
|
797
1181
|
startMeter(stream);
|
|
798
1182
|
} catch (err) {
|
|
1183
|
+
segmentingRef.current = false;
|
|
799
1184
|
stopTracks();
|
|
800
1185
|
setError(classifyMediaError(err, "microphone"));
|
|
801
1186
|
}
|
|
802
1187
|
}, [startMeter, stopTracks]);
|
|
803
1188
|
const pause = React.useCallback(() => {
|
|
1189
|
+
if (segmentingRef.current || chunkedRef.current) return;
|
|
804
1190
|
const recorder = mediaRecorderRef.current;
|
|
805
1191
|
if (!recorder || recorder.state !== "recording") return;
|
|
806
1192
|
recorder.pause();
|
|
@@ -810,6 +1196,7 @@ function useAudioRecorder() {
|
|
|
810
1196
|
setState("paused");
|
|
811
1197
|
}, [stopMeter]);
|
|
812
1198
|
const resume = React.useCallback(() => {
|
|
1199
|
+
if (segmentingRef.current || chunkedRef.current) return;
|
|
813
1200
|
const recorder = mediaRecorderRef.current;
|
|
814
1201
|
if (!recorder || recorder.state !== "paused") return;
|
|
815
1202
|
recorder.resume();
|
|
@@ -818,6 +1205,26 @@ function useAudioRecorder() {
|
|
|
818
1205
|
if (streamRef.current) startMeter(streamRef.current);
|
|
819
1206
|
}, [startMeter]);
|
|
820
1207
|
const stop = React.useCallback(() => {
|
|
1208
|
+
const chunked = chunkedRef.current;
|
|
1209
|
+
if (chunked) {
|
|
1210
|
+
chunkedRef.current = null;
|
|
1211
|
+
void chunked.stop();
|
|
1212
|
+
setElapsedMs(accumulatedMsRef.current + (performance.now() - segmentStartRef.current));
|
|
1213
|
+
stopTracks();
|
|
1214
|
+
stateRef.current = "stopped";
|
|
1215
|
+
setState("stopped");
|
|
1216
|
+
return;
|
|
1217
|
+
}
|
|
1218
|
+
if (segmentingRef.current) {
|
|
1219
|
+
flushOpenSegment();
|
|
1220
|
+
segmentingRef.current = false;
|
|
1221
|
+
stopMeter();
|
|
1222
|
+
stopTracks();
|
|
1223
|
+
setLevel(0);
|
|
1224
|
+
stateRef.current = "stopped";
|
|
1225
|
+
setState("stopped");
|
|
1226
|
+
return;
|
|
1227
|
+
}
|
|
821
1228
|
const recorder = mediaRecorderRef.current;
|
|
822
1229
|
if (!recorder || recorder.state === "inactive") return;
|
|
823
1230
|
const finalElapsed = recorder.state === "paused" ? accumulatedMsRef.current : accumulatedMsRef.current + (performance.now() - segmentStartRef.current);
|
|
@@ -831,7 +1238,7 @@ function useAudioRecorder() {
|
|
|
831
1238
|
setState("stopped");
|
|
832
1239
|
};
|
|
833
1240
|
recorder.stop();
|
|
834
|
-
}, [stopMeter, stopTracks]);
|
|
1241
|
+
}, [stopMeter, stopTracks, flushOpenSegment]);
|
|
835
1242
|
const reset = React.useCallback(() => {
|
|
836
1243
|
setState("idle");
|
|
837
1244
|
setBlob(null);
|
|
@@ -4013,6 +4420,7 @@ function DocumentCard({
|
|
|
4013
4420
|
);
|
|
4014
4421
|
}
|
|
4015
4422
|
|
|
4423
|
+
exports.AUDIO_MIME_PREFERENCE = AUDIO_MIME_PREFERENCE;
|
|
4016
4424
|
exports.AgentBadge = AgentBadge;
|
|
4017
4425
|
exports.AiMarkdown = AiMarkdown;
|
|
4018
4426
|
exports.ArbiBotHeadIcon = ArbiBotHeadIcon;
|
|
@@ -4041,6 +4449,7 @@ exports.CommandItem = CommandItem;
|
|
|
4041
4449
|
exports.CommandList = CommandList;
|
|
4042
4450
|
exports.CommandSeparator = CommandSeparator;
|
|
4043
4451
|
exports.CommandShortcut = CommandShortcut;
|
|
4452
|
+
exports.DEFAULT_SEGMENTER_CONFIG = DEFAULT_SEGMENTER_CONFIG;
|
|
4044
4453
|
exports.DictateProvider = DictateProvider;
|
|
4045
4454
|
exports.DocIcon = DocIcon;
|
|
4046
4455
|
exports.DocumentCard = DocumentCard;
|
|
@@ -4054,6 +4463,7 @@ exports.HoverCard = HoverCard;
|
|
|
4054
4463
|
exports.HoverCardContent = HoverCardContent;
|
|
4055
4464
|
exports.HoverCardTrigger = HoverCardTrigger;
|
|
4056
4465
|
exports.HtmlIcon = HtmlIcon;
|
|
4466
|
+
exports.INITIAL_SEGMENTER_STATE = INITIAL_SEGMENTER_STATE;
|
|
4057
4467
|
exports.IconToggle = IconToggle;
|
|
4058
4468
|
exports.ImgIcon = ImgIcon;
|
|
4059
4469
|
exports.InlineMicButton = InlineMicButton;
|
|
@@ -4118,6 +4528,7 @@ exports.TooltipTrigger = TooltipTrigger;
|
|
|
4118
4528
|
exports.TxtIcon = TxtIcon;
|
|
4119
4529
|
exports.UserEntity = UserEntity;
|
|
4120
4530
|
exports.UserMenu = UserMenu;
|
|
4531
|
+
exports.VIDEO_MIME_PREFERENCE = VIDEO_MIME_PREFERENCE;
|
|
4121
4532
|
exports.VISIBILITY_ICONS = VISIBILITY_ICONS;
|
|
4122
4533
|
exports.VerifiedBadge = VerifiedBadge;
|
|
4123
4534
|
exports.VidIcon = VidIcon;
|
|
@@ -4130,6 +4541,8 @@ exports.classifyMediaError = classifyMediaError;
|
|
|
4130
4541
|
exports.configureEntityRegistry = configureEntityRegistry;
|
|
4131
4542
|
exports.configureRagLabels = configureRagLabels;
|
|
4132
4543
|
exports.configureStatusRegistry = configureStatusRegistry;
|
|
4544
|
+
exports.createChunkedRecorder = createChunkedRecorder;
|
|
4545
|
+
exports.flushSegmenter = flushSegmenter;
|
|
4133
4546
|
exports.formatElapsed = formatElapsed;
|
|
4134
4547
|
exports.getDocIcon = getDocIcon;
|
|
4135
4548
|
exports.getEntityColor = getEntityColor2;
|
|
@@ -4140,8 +4553,11 @@ exports.getStatusVariant = getStatusVariant;
|
|
|
4140
4553
|
exports.getTagTypeIcon = getTagTypeIcon;
|
|
4141
4554
|
exports.getTagTypeLabel = getTagTypeLabel;
|
|
4142
4555
|
exports.getWorkspaceColor = getWorkspaceColor;
|
|
4556
|
+
exports.levelFromRms = levelFromRms;
|
|
4557
|
+
exports.pickRecorderMimeType = pickRecorderMimeType;
|
|
4143
4558
|
exports.returnFocusToMark = returnFocusToMark;
|
|
4559
|
+
exports.stepSegmenter = stepSegmenter;
|
|
4144
4560
|
exports.toggleVariants = toggleVariants;
|
|
4145
4561
|
exports.useAudioRecorder = useAudioRecorder;
|
|
4146
|
-
//# sourceMappingURL=chunk-
|
|
4147
|
-
//# sourceMappingURL=chunk-
|
|
4562
|
+
//# sourceMappingURL=chunk-XD4P4KRH.cjs.map
|
|
4563
|
+
//# sourceMappingURL=chunk-XD4P4KRH.cjs.map
|