videogen/apps/mobile/components/voices/useVoiceRecorder.ts
caelum0x d56639a594
Some checks failed
build-images / web (push) Has been cancelled
vision2026-ci / Python SDK resource tests (push) Has been cancelled
build-images / api (push) Has been cancelled
build-images / worker-gpu (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Has been cancelled
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Has been cancelled
codeql / Analyze (javascript-typescript) (push) Has been cancelled
codeql / Analyze (python) (push) Has been cancelled
perf / API hot-path micro-smoke (budgeted) (push) Has been cancelled
perf / Desktop engine crates (release build check) (push) Has been cancelled
vision2026-ci / API phase modules (ruff + pytest) (push) Has been cancelled
vision2026-ci / Alembic upgrade + drift check (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Has been cancelled
Menivor — open source release (MIT), free to use and fork
Paid, hosted product at menivor.com, now retired as a company project.
Fresh squashed history of the cleaned working tree (secret-scanned,
no .env, no customer data, no outreach logs). Built on
MoneyPrinterTurbo (MIT, harry0703) — see NOTICE.
2026-12-04 02:10:26 +03:00

128 lines
4.1 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* Thin hook around expo-av's `Audio.Recording` for capturing a 30–60s brand
* voice sample. Owns the recorder lifecycle (permission -> record -> stop) and
* exposes a live duration, so the UI can guide the creator toward a usable
* sample length. `stop()` resolves with a `LocalFile` ready for the shared
* upload flow (lib/api.ts::presignAndUpload).
*
* A single recorder may exist at a time (an expo-av constraint), so the hook
* keeps the active instance in a ref and always unloads it on stop / unmount.
*/
import { useCallback, useEffect, useRef, useState } from "react";
import { Audio } from "expo-av";
import * as FileSystem from "expo-file-system/legacy";
import type { LocalFile } from "@/lib/api";
import { describeError } from "@/components/me/errors";
export type RecorderStatus = "idle" | "recording" | "stopping";
export interface VoiceRecorder {
status: RecorderStatus;
/** Elapsed recording time in milliseconds (0 while idle). */
durationMs: number;
error: string | null;
/** Requests mic permission and begins recording. */
start: () => Promise<void>;
/** Stops + unloads, returning the captured file (or null if nothing ran). */
stop: () => Promise<LocalFile | null>;
}
/** Derives a `LocalFile` from a finished recording's URI. */
async function toLocalFile(uri: string): Promise<LocalFile> {
// expo-av writes an .m4a container on both platforms by default.
const name = `voice-sample-${Date.now()}.m4a`;
let size = 0;
try {
const info = await FileSystem.getInfoAsync(uri);
if (info.exists && typeof info.size === "number") size = info.size;
} catch {
size = 0;
}
return { uri, name, mimeType: "audio/mp4", size };
}
export function useVoiceRecorder(): VoiceRecorder {
const [status, setStatus] = useState<RecorderStatus>("idle");
const [durationMs, setDurationMs] = useState(0);
const [error, setError] = useState<string | null>(null);
const recordingRef = useRef<Audio.Recording | null>(null);
// Best-effort unload if the component unmounts mid-recording.
useEffect(() => {
return () => {
const active = recordingRef.current;
recordingRef.current = null;
if (active) {
active.stopAndUnloadAsync().catch(() => {
/* best-effort cleanup */
});
}
};
}, []);
const start = useCallback(async () => {
if (recordingRef.current) return;
setError(null);
setDurationMs(0);
try {
const permission = await Audio.requestPermissionsAsync();
if (!permission.granted) {
throw new Error("Microphone permission is required to record a voice sample.");
}
await Audio.setAudioModeAsync({
allowsRecordingIOS: true,
playsInSilentModeIOS: true,
});
const { recording } = await Audio.Recording.createAsync(
Audio.RecordingOptionsPresets.HIGH_QUALITY,
(recordingStatus) => {
if (recordingStatus.isRecording) {
setDurationMs(recordingStatus.durationMillis ?? 0);
}
},
250,
);
recordingRef.current = recording;
setStatus("recording");
} catch (err) {
recordingRef.current = null;
setStatus("idle");
setError(describeError(err, "Could not start recording."));
}
}, []);
const stop = useCallback(async (): Promise<LocalFile | null> => {
const active = recordingRef.current;
if (!active) return null;
setStatus("stopping");
try {
await active.stopAndUnloadAsync();
// Reset iOS audio session so subsequent playback isn't stuck in record mode.
await Audio.setAudioModeAsync({ allowsRecordingIOS: false }).catch(() => {
/* non-fatal */
});
const uri = active.getURI();
recordingRef.current = null;
setStatus("idle");
if (!uri) {
setError("Recording produced no audio file.");
return null;
}
return await toLocalFile(uri);
} catch (err) {
recordingRef.current = null;
setStatus("idle");
setError(describeError(err, "Could not stop recording."));
return null;
}
}, []);
return { status, durationMs, error, start, stop };
}