Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@
"dev": "vite",
"build": "tsc && vite build",
"lint": "eslint src --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview"
"preview": "vite preview",
"prepare": "npm run build"
},
"peerDependencies": {
"react": "^18.2.0",
Expand Down
22 changes: 20 additions & 2 deletions src/hooks/useVoiceVisualizer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ function useVoiceVisualizer({
onResumedAudioPlayback,
onErrorPlayingAudio,
shouldHandleBeforeUnload = true,
timeslice,
onChunkAvailable,
}: useVoiceVisualizerParams = {}): Controls {
const [isRecordingInProgress, setIsRecordingInProgress] = useState(false);
const [isPausedRecording, setIsPausedRecording] = useState(false);
Expand Down Expand Up @@ -172,7 +174,12 @@ function useVoiceVisualizer({
"dataavailable",
handleDataAvailable,
);
mediaRecorderRef.current.start();
// Start recording with timeslice if provided, otherwise normal recording
if (timeslice) {
mediaRecorderRef.current.start(timeslice);
} else {
mediaRecorderRef.current.start();
}
if (onStartRecording) onStartRecording();

recordingFrame();
Expand All @@ -194,6 +201,13 @@ function useVoiceVisualizer({
};

const handleDataAvailable = (event: BlobEvent) => {
// If timeslice is set, only emit chunks - don't store locally
if (timeslice && onChunkAvailable) {
onChunkAvailable(event.data);
return;
}

// Standard behavior (no timeslice): store blob and process for playback
if (!mediaRecorderRef.current) return;

mediaRecorderRef.current = null;
Expand Down Expand Up @@ -234,7 +248,11 @@ function useVoiceVisualizer({
if (audioContextRef.current && audioContextRef.current.state !== "closed") {
void audioContextRef.current.close();
}
_setIsProcessingAudioOnComplete(true);

// Only process blob for playback if not in timeslice mode
if (!timeslice) {
_setIsProcessingAudioOnComplete(true);
}
setRecordingTime(0);
setIsPausedRecording(false);
if (onStopRecording) onStopRecording();
Expand Down
2 changes: 2 additions & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ export interface useVoiceVisualizerParams {
onResumedAudioPlayback?: () => void;
onErrorPlayingAudio?: (error: Error) => void;
shouldHandleBeforeUnload?: boolean;
timeslice?: number;
onChunkAvailable?: (chunk: Blob) => void;
}

export interface UseWebWorkerParams<T> {
Expand Down