Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

feat(mobile): Teach OpenWhispr your voice from meeting notes (#2386)

Chad Piha22105b08d8e2
diff --git a/openwhispr-mobile/app/(tabs)/(notes)/_layout.tsx b/openwhispr-mobile/app/(tabs)/(notes)/_layout.tsx
index 14d2157ac..2686f803c 100644
--- a/openwhispr-mobile/app/(tabs)/(notes)/_layout.tsx
+++ b/openwhispr-mobile/app/(tabs)/(notes)/_layout.tsx
@@ -19,7 +19,7 @@ export default function NotesLayout() {
<Stack.Screen name="voice-profile" options={{ title: 'Profile', headerLargeTitle: false }} />
<Stack.Screen
name="voice-enrollment"
- options={{ title: 'Enroll Voice', headerLargeTitle: false }}
+ options={{ title: 'Voice Sample', headerLargeTitle: false }}
/>
</Stack>
);
diff --git a/openwhispr-mobile/modules/speaker-diarization/ios/SpeakerDiarizationModule.swift b/openwhispr-mobile/modules/speaker-diarization/ios/SpeakerDiarizationModule.swift
index f7674b1fb..849b85273 100644
--- a/openwhispr-mobile/modules/speaker-diarization/ios/SpeakerDiarizationModule.swift
+++ b/openwhispr-mobile/modules/speaker-diarization/ios/SpeakerDiarizationModule.swift
@@ -26,7 +26,7 @@ public class SpeakerDiarizationModule: Module {
AsyncFunction("downloadModel") { (promise: Promise) in
Task {
do {
- _ = try await OfflineDiarizerModels.load() // downloads the offline-variant models if missing
+ _ = try await Self.loadModels()
promise.resolve(nil)
} catch {
promise.reject("MODEL_DOWNLOAD_ERROR", error.localizedDescription)
@@ -49,6 +49,8 @@ public class SpeakerDiarizationModule: Module {
for dir in Self.modelRepoSubdirs(base) where fm.fileExists(atPath: dir.path) {
try fm.removeItem(at: dir)
}
+ let marker = Self.downloadInProgressMarker()
+ if fm.fileExists(atPath: marker.path) { try fm.removeItem(at: marker) }
promise.resolve(nil)
} catch {
promise.reject("MODEL_DELETE_ERROR", error.localizedDescription)
@@ -69,7 +71,7 @@ public class SpeakerDiarizationModule: Module {
}
let manager = OfflineDiarizerManager(config: config)
- let models = try await OfflineDiarizerModels.load()
+ let models = try await Self.loadModels()
manager.initialize(models: models)
// Stream from the file (memory-mapped, chunked) rather than reading the whole recording
@@ -125,9 +127,32 @@ public class SpeakerDiarizationModule: Module {
]
}
+ // Each .mlmodelc is a folder that exists once its first file lands, so the artifacts alone
+ // can't tell a finished download from one that was cut off.
+ static func downloadInProgressMarker() -> URL {
+ OfflineDiarizerModels.defaultModelsDirectory()
+ .appendingPathComponent(".openwhispr-download-in-progress")
+ }
+
+ // Loads the offline models, downloading any that are missing. The marker stays behind if the
+ // app dies mid-download, so a half-written model never reads as ready; a successful load also
+ // finishes a download that was cut off.
+ static func loadModels() async throws -> OfflineDiarizerModels {
+ let marker = downloadInProgressMarker()
+ if !modelsCached() {
+ try? FileManager.default.createDirectory(
+ at: marker.deletingLastPathComponent(), withIntermediateDirectories: true)
+ FileManager.default.createFile(atPath: marker.path, contents: nil)
+ }
+ let models = try await OfflineDiarizerModels.load()
+ try? FileManager.default.removeItem(at: marker)
+ return models
+ }
+
static func modelsCached() -> Bool {
let base = OfflineDiarizerModels.defaultModelsDirectory()
let fm = FileManager.default
+ if fm.fileExists(atPath: downloadInProgressMarker().path) { return false }
let dirs = [base] + modelRepoSubdirs(base)
return requiredModelArtifacts.allSatisfy { file in
dirs.contains { fm.fileExists(atPath: $0.appendingPathComponent(file).path) }
diff --git a/openwhispr-mobile/package-lock.json b/openwhispr-mobile/package-lock.json
index fe6a62bac..a901cfcd7 100644
--- a/openwhispr-mobile/package-lock.json
+++ b/openwhispr-mobile/package-lock.json
@@ -54,7 +54,7 @@
"expo-superwall": "1.2.0",
"expo-symbols": "~55.0.9",
"expo-tracking-transparency": "~55.0.18",
- "expo-updates": "~55.0.31",
+ "expo-updates": "~55.0.32",
"expo-video": "~55.0.21",
"expo-web-browser": "~55.0.20",
"lucide-react-native": "^0.545.0",
@@ -9756,9 +9756,9 @@
}
},
"node_modules/expo-updates": {
- "version": "55.0.31",
- "resolved": "https://registry.npmjs.org/expo-updates/-/expo-updates-55.0.31.tgz",
- "integrity": "sha512-sMcxRw1xzwtZelXP92x1at9zJ1kjMVlwDZGehIBGInovh0oWnK38EHSmi/J6HaM90uk3+hQKBUva5+py9AHsuw==",
+ "version": "55.0.32",
+ "resolved": "https://registry.npmjs.org/expo-updates/-/expo-updates-55.0.32.tgz",
+ "integrity": "sha512-8yGODZMeBeVkZDzVEQrN5gMQ+Ifm3jovy2LspvJoOmJKfYWJpAiRXIq9ArU19vI/Js8oVurkzyofcMDAus/CrA==",
"license": "MIT",
"dependencies": {
"@expo/code-signing-certificates": "^0.0.6",
diff --git a/openwhispr-mobile/package.json b/openwhispr-mobile/package.json
index aa57ae364..6f068d928 100644
--- a/openwhispr-mobile/package.json
+++ b/openwhispr-mobile/package.json
@@ -75,7 +75,7 @@
"expo-superwall": "1.2.0",
"expo-symbols": "~55.0.9",
"expo-tracking-transparency": "~55.0.18",
- "expo-updates": "~55.0.31",
+ "expo-updates": "~55.0.32",
"expo-video": "~55.0.21",
"expo-web-browser": "~55.0.20",
"lucide-react-native": "^0.545.0",
diff --git a/openwhispr-mobile/src/components/notes/ConflictBanner.tsx b/openwhispr-mobile/src/components/notes/ConflictBanner.tsx
index 481caab2c..e0c9ffc7f 100644
--- a/openwhispr-mobile/src/components/notes/ConflictBanner.tsx
+++ b/openwhispr-mobile/src/components/notes/ConflictBanner.tsx
@@ -11,8 +11,8 @@ type ConflictBannerProps = {
};
/** Shown on the note editor when the note has a server copy parked from a push-time conflict
- * (see notesRepository.listConflictedNotes). Card/CTA layout matches VoiceProfilePromptCard and
- * ParakeetNudgeBanner — the app's existing inline-alert pattern. */
+ * (see notesRepository.listConflictedNotes). Card/CTA layout matches ParakeetNudgeBanner, the
+ * app's existing inline-alert pattern. */
export function ConflictBanner({ canUseServerCopy, onKeepMine, onUseServer }: ConflictBannerProps) {
return (
<View
diff --git a/openwhispr-mobile/src/components/notes/ThatsMeSheet.tsx b/openwhispr-mobile/src/components/notes/ThatsMeSheet.tsx
new file mode 100644
index 000000000..ed23cdf15
--- /dev/null
+++ b/openwhispr-mobile/src/components/notes/ThatsMeSheet.tsx
@@ -0,0 +1,157 @@
+import { useEffect, useRef, useState } from 'react';
+import type React from 'react';
+import { AccessibilityInfo, Modal, Pressable, ScrollView, View } from 'react-native';
+import { useSafeAreaInsets } from 'react-native-safe-area-context';
+import { Text } from '@/components/ui/Text';
+import { SystemIcon } from '@/components/ui/SystemIcon';
+import { GlassIconButton } from '@/components/ui/GlassIconButton';
+import { formatSpeakingTime, type VoiceSetupCandidate } from '@/lib/notes/voiceSetupPrompt';
+
+const CLAIMED_MESSAGE = 'Your next on-device meetings will label you as Me.';
+
+interface ThatsMeSheetProps {
+ visible: boolean;
+ candidates: VoiceSetupCandidate[];
+ /** True once your voice profile is saved. */
+ onClaim: (speakerId: number) => boolean;
+ onReadScript: () => void;
+ onClose: () => void;
+ /** iOS only: the sheet has finished sliding away. */
+ onDismissed?: () => void;
+}
+
+export function ThatsMeSheet({
+ visible,
+ candidates,
+ onClaim,
+ onReadScript,
+ onClose,
+ onDismissed,
+}: ThatsMeSheetProps): React.JSX.Element {
+ const insets = useSafeAreaInsets();
+ const [claimed, setClaimed] = useState(false);
+ // Two taps can land before the list re-renders. Once a claim has saved, a second one
+ // would fail against the new owner profile and undo the success state.
+ const claimedRef = useRef(false);
+
+ useEffect(() => {
+ if (!visible) return;
+ claimedRef.current = false;
+ setClaimed(false);
+ }, [visible]);
+
+ const claim = (speakerId: number): void => {
+ if (claimedRef.current) return;
+ if (!onClaim(speakerId)) return;
+ claimedRef.current = true;
+ setClaimed(true);
+ // The button that had focus is gone, so say what happened.
+ AccessibilityInfo.announceForAccessibility(`Got it. ${CLAIMED_MESSAGE}`);
+ };
+
+ return (
+ <Modal
+ visible={visible}
+ animationType="slide"
+ presentationStyle="pageSheet"
+ onRequestClose={onClose}
+ onDismiss={onDismissed}
+ >
+ <View className="flex-1 bg-systemBackground" testID="thats-me-sheet">
+ <View className="flex-row items-start justify-between gap-4 px-6 pb-4 pt-8">
+ <View className="flex-1">
+ <Text accessibilityRole="header" className="text-[22px] font-bold text-label">
+ Teach OpenWhispr your voice
+ </Text>
+ {claimed ? null : (
+ <>
+ <Text className="mt-1 text-[15px] leading-5 text-secondaryLabel">
+ Which speaker is you?
+ </Text>
+ <Text className="mt-2 text-[13px] leading-[18px] text-tertiaryLabel">
+ We'll use your voice from this meeting to make a voice profile. It stays on this
+ device and is only used to recognize you in meetings you record. You can delete it
+ in Voice Profiles.
+ </Text>
+ </>
+ )}
+ </View>
+ <GlassIconButton onPress={onClose} accessibilityLabel="Close">
+ <SystemIcon name="xmark" mdName="X" size={15} color="secondaryLabel" />
+ </GlassIconButton>
+ </View>
+
+ {claimed ? (
+ <View className="gap-3 px-6">
+ <Text className="text-[22px] font-semibold text-label">Got it</Text>
+ <Text className="text-[15px] leading-5 text-secondaryLabel">{CLAIMED_MESSAGE}</Text>
+ <Pressable
+ onPress={onClose}
+ accessibilityRole="button"
+ testID="thats-me-done"
+ className="mt-2 h-11 items-center justify-center rounded-lg bg-brand"
+ style={{ borderCurve: 'continuous' }}
+ >
+ <Text className="text-[15px] font-semibold text-white">Done</Text>
+ </Pressable>
+ </View>
+ ) : (
+ <ScrollView
+ contentContainerClassName="gap-3 px-6"
+ contentContainerStyle={{ paddingBottom: insets.bottom + 24 }}
+ >
+ {candidates.length === 0 ? (
+ <Text className="text-[15px] leading-5 text-secondaryLabel">
+ There's no voice sample from this meeting to use. Read a short script instead; it
+ takes about 20 seconds.
+ </Text>
+ ) : (
+ candidates.map((candidate) => (
+ <View
+ key={candidate.speakerId}
+ className="flex-row items-center gap-3 rounded-xl border border-separator bg-secondarySystemGroupedBackground p-3"
+ style={{ borderCurve: 'continuous' }}
+ >
+ <View className="min-w-0 flex-1">
+ <Text className="text-[16px] font-semibold text-label">{candidate.name}</Text>
+ <Text className="text-[13px] text-secondaryLabel">
+ {`Spoke for ${formatSpeakingTime(candidate.speechMs)}`}
+ </Text>
+ {candidate.sampleLine ? (
+ <Text numberOfLines={1} className="mt-1 text-[14px] text-label">
+ {`“${candidate.sampleLine}”`}
+ </Text>
+ ) : null}
+ </View>
+ <Pressable
+ onPress={() => claim(candidate.speakerId)}
+ accessibilityRole="button"
+ accessibilityLabel={`${candidate.name} is me`}
+ testID={`thats-me-${candidate.speakerId}`}
+ className="h-11 items-center justify-center rounded-lg bg-brand px-3"
+ style={({ pressed }) => ({
+ opacity: pressed ? 0.85 : 1,
+ borderCurve: 'continuous',
+ })}
+ >
+ <Text className="text-[14px] font-semibold text-white">That's me</Text>
+ </Pressable>
+ </View>
+ ))
+ )}
+ <Pressable
+ onPress={onReadScript}
+ accessibilityRole="button"
+ testID="thats-me-read-script"
+ className="min-h-11 items-center justify-center py-2"
+ >
+ <Text className="text-[15px] font-medium text-brand">
+ Read a short script instead
+ </Text>
+ </Pressable>
+ </ScrollView>
+ )}
+ </View>
+ </Modal>
+ );
+}
diff --git a/openwhispr-mobile/src/components/notes/VoiceEnrollmentRecorder.tsx b/openwhispr-mobile/src/components/notes/VoiceEnrollmentRecorder.tsx
index d4c5e042d..49e855ff4 100644
--- a/openwhispr-mobile/src/components/notes/VoiceEnrollmentRecorder.tsx
+++ b/openwhispr-mobile/src/components/notes/VoiceEnrollmentRecorder.tsx
@@ -1,311 +1,622 @@
import { useCallback, useEffect, useRef, useState } from 'react';
-import { Pressable, TextInput, View } from 'react-native';
+import type React from 'react';
+import { ActivityIndicator, Pressable, TextInput, View } from 'react-native';
+import Svg, { Circle } from 'react-native-svg';
import * as FileSystem from 'expo-file-system/legacy';
import { Text } from '@/components/ui/Text';
import { Button } from '@/components/ui/Button';
-import { GradientGlassSurface } from '@/components/ui/GradientGlassSurface';
+import { SystemIcon } from '@/components/ui/SystemIcon';
import { WaveformVisualizer } from '@/components/features/WaveformVisualizer';
import { useAudioRecording } from '@/hooks/useAudioRecording';
import { useAudioWaveform } from '@/hooks/useAudioWaveform';
-import { BRAND } from '@/config/colors';
-import type {
- EnrollVoiceProfileInput,
- ReenrollVoiceProfileInput,
-} from '@/services/diarization/VoiceprintService';
+import { BRAND, iosColor } from '@/config/colors';
import { SpeakerProfileOwnerAlreadyExistsError } from '@/data/local/notesRepository';
+import {
+ VOICE_ENROLLMENT_DIARIZER_MODEL_REQUIRED,
+ VoiceEnrollmentError,
+ type EnrollVoiceProfileInput,
+ type ReenrollVoiceProfileInput,
+} from '@/services/diarization/VoiceprintService';
+import {
+ VOICE_ENROLLMENT_UNEXPLAINED_FAILURE,
+ voiceEnrollmentFailureMessage,
+} from '@/lib/voiceEnrollmentMessages';
+import { isMicPermissionError, showMicPermissionAlert } from '@/lib/permissions';
+import { Sentry } from '@/lib/sentry';
-type VoiceEnrollmentSubmitInput = EnrollVoiceProfileInput | ReenrollVoiceProfileInput;
+type Phase =
+ | 'checking-model'
+ | 'needs-model'
+ | 'ready'
+ | 'recording'
+ | 'checking'
+ | 'download-failed'
+ | 'failed'
+ | 'done';
interface VoiceEnrollmentRecorderProps {
isOwner: boolean;
defaultDisplayName?: string;
profileId?: number;
- onSubmit: (input: VoiceEnrollmentSubmitInput) => Promise<void>;
- onCancel?: () => void;
+ isModelReady: () => Promise<boolean>;
+ isModelDownloading: () => boolean;
+ downloadModel: () => Promise<void>;
+ onSubmit: (input: EnrollVoiceProfileInput | ReenrollVoiceProfileInput) => Promise<void>;
+ onDone: () => void;
+ onCancel: () => void;
now?: () => Date;
}
-const MAX_TAKES = 2;
-const TARGET_SECONDS = 30;
+// Reading the script aloud is the consent, so it names what the profile is for.
+export const VOICE_ENROLLMENT_SCRIPT =
+ "I'm teaching OpenWhispr my voice so it can label me in my meeting notes. This voice profile stays on my device, is only used to recognize me in recordings I choose to process, and I can delete it at any time.";
+
+const MIC_PERMISSION_FAILURE =
+ "Couldn't start the microphone. Check that OpenWhispr can use it in Settings, then try again.";
+const MIC_START_FAILURE = "Couldn't start the microphone. Try again.";
+const STOP_FAILURE = "Couldn't finish the recording. Try again.";
-const formatError = (error: unknown): string => {
- if (error instanceof Error && error.message.trim()) return error.message;
- if (typeof error === 'object' && error && 'code' in error) return String(error.code);
- return 'Voice enrollment failed. Try another take.';
+// A call or Siri pauses the recorder, and the pause only shows as it no longer recording.
+const RECORDING_INTERRUPTED =
+ 'Your recording was interrupted, maybe by a call. Start again when you are ready.';
+
+// The check needs 10 s of speech, and normal reading has pauses between words, so Done
+// unlocks, and the ring fills, once there is room for them.
+const MIN_SECONDS = 15;
+const MAX_SECONDS = 30;
+// A stalled model request can hang for 30 minutes before it fails. Stop waiting long before
+// that; the download carries on, and Retry waits on the same one.
+const DOWNLOAD_WAIT_MS = 3 * 60 * 1000;
+const RING_SIZE = 88;
+const RING_STROKE = 4;
+const RING_RADIUS = (RING_SIZE - RING_STROKE) / 2;
+const RING_CIRCUMFERENCE = 2 * Math.PI * RING_RADIUS;
+
+const deleteRecording = (uri: string): void => {
+ FileSystem.deleteAsync(uri, { idempotent: true }).catch(() => undefined);
};
-const formatTime = (seconds: number): string =>
- `${Math.floor(seconds / 60)}:${String(seconds % 60).padStart(2, '0')}`;
+// Rejects when the wait times out or `signal` aborts (the recorder left).
+const waitForDownload = async (
+ download: Promise<void> | null,
+ signal: AbortSignal,
+): Promise<void> => {
+ if (!download) return;
+ if (signal.aborted) throw new Error('Left while waiting for the model download');
+ let timer: ReturnType<typeof setTimeout> | undefined;
+ let onAbort: (() => void) | undefined;
+ const timeout = new Promise<never>((_resolve, reject) => {
+ timer = setTimeout(() => reject(new Error('Model download timed out')), DOWNLOAD_WAIT_MS);
+ onAbort = () => reject(new Error('Left while waiting for the model download'));
+ signal.addEventListener('abort', onAbort);
+ });
+ try {
+ await Promise.race([download, timeout]);
+ } finally {
+ clearTimeout(timer);
+ if (onAbort) signal.removeEventListener('abort', onAbort);
+ }
+};
-const cleanupUris = async (uris: string[]): Promise<void> => {
- await Promise.all(
- uris.map((uri) => FileSystem.deleteAsync(uri, { idempotent: true }).catch(() => undefined)),
- );
+const reportFailure = (error: unknown, stage: string): void => {
+ Sentry.captureException(error, { tags: { feature: 'voice-enrollment', stage } });
+};
+
+// Also reports failures the message can't explain, so they can be diagnosed.
+const checkFailureMessage = (error: unknown): string => {
+ const message = voiceEnrollmentFailureMessage(error);
+ if (message === VOICE_ENROLLMENT_UNEXPLAINED_FAILURE) reportFailure(error, 'check');
+ return message;
+};
+
+type RecorderLike = { uri?: string | null; isRecording?: boolean } | null | undefined;
+
+// Reading a released native recorder throws, so a failed read means there is no file to track.
+const readRecorderUri = (recorder: RecorderLike): string | null => {
+ try {
+ return recorder?.uri || null;
+ } catch {
+ return null;
+ }
+};
+
+const readRecorderIsRecording = (recorder: RecorderLike): boolean => {
+ try {
+ return !!recorder?.isRecording;
+ } catch {
+ return false;
+ }
};
export function VoiceEnrollmentRecorder({
isOwner,
defaultDisplayName,
profileId,
+ isModelReady,
+ isModelDownloading,
+ downloadModel,
onSubmit,
+ onDone,
onCancel,
now = () => new Date(),
-}: VoiceEnrollmentRecorderProps) {
+}: VoiceEnrollmentRecorderProps): React.JSX.Element {
const recording = useAudioRecording();
const { currentAmplitude, waveformData } = useAudioWaveform(
recording.audioRecorder,
recording.isRecording,
);
+ const [phase, setPhase] = useState<Phase>('checking-model');
+ const [downloadStatus, setDownloadStatus] = useState<'idle' | 'downloading' | 'failed'>('idle');
const [displayName, setDisplayName] = useState(defaultDisplayName ?? (isOwner ? 'Me' : ''));
- // Consent is given by reading the on-screen script aloud, so it is stamped when
- // the first take starts rather than gated behind a separate checkbox.
- const [consentAcceptedAt, setConsentAcceptedAt] = useState<string | null>(null);
- const [recordingActive, setRecordingActive] = useState(false);
const [elapsedSeconds, setElapsedSeconds] = useState(0);
- const [takes, setTakes] = useState<Array<{ uri: string; mimeType: string }>>([]);
- const takesRef = useRef(takes);
- const cancelRecordingRef = useRef(recording.cancelRecording);
- const [error, setError] = useState<string | null>(null);
- const [showRetry, setShowRetry] = useState(false);
- const [saving, setSaving] = useState(false);
+ const [failure, setFailure] = useState<string | null>(null);
+ const consentAtRef = useRef<string | null>(null);
+ const downloadRef = useRef<Promise<void> | null>(null);
+ // The file the recorder is writing, captured while it starts. On unmount the recorder
+ // hook releases the native recorder before this component's cleanup runs, and a
+ // released recorder can't be asked for its uri any more.
+ const activeUriRef = useRef<string | null>(null);
+ // A finished recording not yet handed to onSubmit; deleted if you leave.
+ const pendingUriRef = useRef<string | null>(null);
+ const startingRef = useRef(false);
+ const stoppingRef = useRef(false);
+ const submittingRef = useRef(false);
+ const mountedRef = useRef(true);
+ const leftRef = useRef(new AbortController());
+ // The hook returns a new object each render; reading it through a ref keeps the
+ // callbacks below stable.
+ const recordingRef = useRef(recording);
+ recordingRef.current = recording;
- takesRef.current = takes;
- cancelRecordingRef.current = recording.cancelRecording;
+ const fail = useCallback((message: string): void => {
+ if (!mountedRef.current) return;
+ setFailure(message);
+ setPhase('failed');
+ }, []);
+
+ // Stops the recorder, then deletes the sample it was writing.
+ const discardActiveRecording = useCallback((): void => {
+ const uri = activeUriRef.current;
+ activeUriRef.current = null;
+ // The executor runs now, and turns a throw from a released recorder into a rejection.
+ new Promise((resolve) => resolve(recordingRef.current.cancelRecording()))
+ .catch(() => undefined)
+ .then(() => {
+ if (uri) deleteRecording(uri);
+ });
+ }, []);
useEffect(() => {
- setDisplayName(defaultDisplayName ?? (isOwner ? 'Me' : ''));
- }, [defaultDisplayName, isOwner]);
+ // Set here too, since Fast Refresh reruns effects on the same refs.
+ mountedRef.current = true;
+ leftRef.current = new AbortController();
+ return () => {
+ mountedRef.current = false;
+ leftRef.current.abort();
+ if (pendingUriRef.current) deleteRecording(pendingUriRef.current);
+ pendingUriRef.current = null;
+ // Releasing the recorder has already stopped it. A stop still in flight that
+ // resolves later finds the component gone and deletes the same file again.
+ discardActiveRecording();
+ };
+ }, [discardActiveRecording]);
+
+ const startDownload = useCallback((): void => {
+ setDownloadStatus('downloading');
+ const download = downloadModel();
+ downloadRef.current = download;
+ download.then(
+ () => {
+ if (downloadRef.current === download) downloadRef.current = null;
+ if (mountedRef.current) setDownloadStatus('idle');
+ },
+ () => {
+ if (mountedRef.current) setDownloadStatus('failed');
+ },
+ );
+ }, [downloadModel]);
+
+ useEffect(() => {
+ // Opened again while an earlier visit's download runs: you already said yes, so join it.
+ if (isModelDownloading()) {
+ startDownload();
+ setPhase('ready');
+ return;
+ }
+ let active = true;
+ isModelReady().then(
+ (ready) => active && setPhase(ready ? 'ready' : 'needs-model'),
+ () => active && setPhase('needs-model'),
+ );
+ return () => {
+ active = false;
+ };
+ }, [isModelDownloading, isModelReady, startDownload]);
- useEffect(
- () => () => {
- cleanupUris(takesRef.current.map((take) => take.uri)).catch(() => undefined);
- Promise.resolve(cancelRecordingRef.current()).catch(() => undefined);
+ const trimmedName = displayName.trim();
+ const asksForName = !isOwner && profileId === undefined;
+
+ const discardPending = (uri: string): void => {
+ pendingUriRef.current = null;
+ deleteRecording(uri);
+ };
+
+ const submit = useCallback(
+ async (uri: string): Promise<void> => {
+ submittingRef.current = true;
+ setPhase('checking');
+ // Held here while the model downloads so leaving deletes it.
+ pendingUriRef.current = uri;
+ try {
+ await waitForDownload(downloadRef.current, leftRef.current.signal);
+ } catch {
+ submittingRef.current = false;
+ // Kept so a retried download can check it without reading again.
+ if (mountedRef.current) setPhase('download-failed');
+ // Unless leaving has already deleted it.
+ else if (pendingUriRef.current === uri) discardPending(uri);
+ return;
+ }
+ // Left while stopping or waiting: nothing will check this recording.
+ if (!mountedRef.current) {
+ submittingRef.current = false;
+ discardPending(uri);
+ return;
+ }
+ // From here the enrollment service owns the file until the finally below.
+ pendingUriRef.current = null;
+ const consent = {
+ recordings: [{ uri, mimeType: 'audio/wav' }],
+ consentAccepted: true as const,
+ consentAcceptedAt: consentAtRef.current ?? now().toISOString(),
+ };
+ let keepRecording = false;
+ try {
+ // A retrain keeps the profile's current name, even one changed during the read.
+ await onSubmit(
+ profileId === undefined
+ ? { ...consent, displayName: trimmedName, isOwner }
+ : { ...consent, profileId },
+ );
+ if (mountedRef.current) setPhase('done');
+ } catch (caught) {
+ if (caught instanceof SpeakerProfileOwnerAlreadyExistsError) {
+ // onSubmit has already explained this one and left the screen.
+ return;
+ }
+ if (
+ caught instanceof VoiceEnrollmentError &&
+ caught.code === VOICE_ENROLLMENT_DIARIZER_MODEL_REQUIRED
+ ) {
+ // Kept so the read can be checked once the model is downloaded.
+ keepRecording = mountedRef.current;
+ if (keepRecording) {
+ pendingUriRef.current = uri;
+ setPhase('needs-model');
+ }
+ return;
+ }
+ fail(checkFailureMessage(caught));
+ } finally {
+ submittingRef.current = false;
+ if (!keepRecording) deleteRecording(uri);
+ }
},
- [],
+ [fail, isOwner, now, onSubmit, profileId, trimmedName],
);
+ const stop = useCallback(async (): Promise<void> => {
+ // Done and the 30 s auto-stop can land together; only the first one stops.
+ if (stoppingRef.current) return;
+ stoppingRef.current = true;
+ try {
+ const uri = await recordingRef.current.stopRecordingRaw();
+ if (!uri) {
+ discardActiveRecording();
+ fail(RECORDING_INTERRUPTED);
+ return;
+ }
+ // submit owns the file from here, so leaving no longer deletes it as the active one.
+ activeUriRef.current = null;
+ await submit(uri);
+ } catch (caught) {
+ discardActiveRecording();
+ reportFailure(caught, 'stop');
+ fail(STOP_FAILURE);
+ } finally {
+ stoppingRef.current = false;
+ }
+ }, [discardActiveRecording, fail, submit]);
+
+ // Paused by a call or Siri: say so now, not after reading on into a paused recorder.
+ const handleInterruption = useCallback((): void => {
+ const uri = activeUriRef.current;
+ activeUriRef.current = null;
+ // A paused recorder ignores cancel; stopping it finishes the file so it can be deleted.
+ Promise.resolve()
+ .then(() => recordingRef.current.audioRecorder.stop())
+ .catch(() => undefined)
+ .then(() => recordingRef.current.cancelRecording())
+ .catch(() => undefined)
+ .then(() => {
+ if (uri) deleteRecording(uri);
+ });
+ fail(RECORDING_INTERRUPTED);
+ }, [fail]);
+
useEffect(() => {
- if (!recordingActive) {
+ if (phase !== 'recording') {
setElapsedSeconds(0);
return;
}
- const interval = setInterval(() => setElapsedSeconds((seconds) => seconds + 1), 1000);
+ const interval = setInterval(() => {
+ if (!stoppingRef.current && !readRecorderIsRecording(recordingRef.current.audioRecorder)) {
+ clearInterval(interval);
+ handleInterruption();
+ return;
+ }
+ setElapsedSeconds((seconds) => seconds + 1);
+ }, 1000);
return () => clearInterval(interval);
- }, [recordingActive]);
+ }, [handleInterruption, phase]);
- const stopAndAcceptTake = useCallback(async () => {
- const uri = await recording.stopRecordingRaw();
- setRecordingActive(false);
- if (!uri) {
- setError('Recording did not produce an audio file.');
- return;
- }
- setError(null);
- setShowRetry(false);
- setTakes((current) => [...current, { uri, mimeType: 'audio/wav' }].slice(0, MAX_TAKES));
- }, [recording]);
+ useEffect(() => {
+ if (phase === 'recording' && elapsedSeconds >= MAX_SECONDS) stop().catch(() => undefined);
+ }, [elapsedSeconds, phase, stop]);
- const handleRecordPress = useCallback(async () => {
- if (saving || takes.length >= MAX_TAKES) return;
+ const start = useCallback(async (): Promise<void> => {
+ // The phase stays ready while the microphone opens; a second tap would prepare a
+ // second file that nothing deletes.
+ if (startingRef.current || activeUriRef.current) return;
+ startingRef.current = true;
+ setFailure(null);
+ consentAtRef.current ??= now().toISOString();
try {
- if (recordingActive) {
- await stopAndAcceptTake();
+ await recordingRef.current.startRecording();
+ activeUriRef.current = readRecorderUri(recordingRef.current.audioRecorder);
+ // Left before the microphone opened: stop it and drop what it started writing.
+ if (!mountedRef.current) {
+ discardActiveRecording();
return;
}
- setError(null);
- setShowRetry(false);
- if (!consentAcceptedAt) setConsentAcceptedAt(now().toISOString());
- await recording.startRecording();
- setRecordingActive(true);
+ setPhase('recording');
} catch (caught) {
- setRecordingActive(false);
- setError(formatError(caught));
- }
- }, [consentAcceptedAt, now, recording, recordingActive, saving, stopAndAcceptTake, takes.length]);
-
- const handleRemoveTakes = useCallback(() => {
- const uris = takes.map((take) => take.uri);
- setTakes([]);
- setError(null);
- setShowRetry(false);
- cleanupUris(uris).catch(() => undefined);
- }, [takes]);
-
- const handleSubmit = useCallback(async () => {
- const trimmedName = displayName.trim();
- if (!consentAcceptedAt || takes.length === 0) return;
- if (!isOwner && !trimmedName) return;
-
- setSaving(true);
- setError(null);
- const submittedTakes = takes;
- const submittedUris = submittedTakes.map((take) => take.uri);
- const baseInput = {
- recordings: submittedTakes,
- displayName: trimmedName || 'Me',
- isOwner,
- consentAccepted: true,
- consentAcceptedAt,
- };
-
- try {
- if (profileId === undefined) {
- await onSubmit(baseInput);
+ if (isMicPermissionError(caught)) {
+ // Same rule as dictation: only point at Settings when iOS couldn't ask itself.
+ if (!caught.nativePromptShown) showMicPermissionAlert();
+ fail(MIC_PERMISSION_FAILURE);
} else {
- await onSubmit({ ...baseInput, profileId });
+ reportFailure(caught, 'start');
+ fail(MIC_START_FAILURE);
}
- await cleanupUris(submittedUris);
- setTakes([]);
- setShowRetry(false);
- } catch (caught) {
- await cleanupUris(submittedUris);
- setTakes([]);
- setShowRetry(true);
- if (caught instanceof SpeakerProfileOwnerAlreadyExistsError) throw caught;
- setError(formatError(caught));
} finally {
- setSaving(false);
+ startingRef.current = false;
}
- }, [consentAcceptedAt, displayName, isOwner, onSubmit, profileId, takes]);
+ }, [discardActiveRecording, fail, now]);
- const handleCancel = useCallback(() => {
- cleanupUris(takes.map((take) => take.uri)).catch(() => undefined);
- onCancel?.();
- }, [onCancel, takes]);
+ // Downloads the model, then checks a recording that was waiting for it.
+ const downloadAndCheck = useCallback(async (): Promise<void> => {
+ // A second tap before the re-render would check, and delete, the same file twice.
+ if (submittingRef.current) return;
+ startDownload();
+ const uri = pendingUriRef.current;
+ if (uri) await submit(uri);
+ else setPhase('ready');
+ }, [startDownload, submit]);
- const trimmedName = displayName.trim();
- const avatarInitial = trimmedName ? trimmedName[0].toUpperCase() : '?';
- const canSave = takes.length > 0 && (isOwner || !!trimmedName) && !saving;
- const canRecordMore = takes.length < MAX_TAKES;
- const countLabel = recordingActive
- ? `Recording ${takes.length + 1} of ${MAX_TAKES}`
- : `${takes.length} of ${MAX_TAKES} recorded`;
+ if (phase === 'checking-model') {
+ return <ActivityIndicator className="mt-10" />;
+ }
- return (
- <View className="gap-5">
- <View className="gap-2">
- <Text className="text-[13px] uppercase tracking-wider text-secondaryLabel">Profile</Text>
- <View
- className="flex-row items-center gap-3 rounded-xl border border-separator bg-secondarySystemGroupedBackground p-2"
- style={{ borderCurve: 'continuous' }}
+ if (phase === 'needs-model') {
+ return (
+ <View className="gap-4">
+ <Text accessibilityRole="header" className="text-[20px] font-semibold text-label">
+ Download the speaker model
+ </Text>
+ <Text className="text-[15px] leading-5 text-secondaryLabel">
+ OpenWhispr needs a one-time download of about 100 MB to recognize voices. It runs entirely
+ on your device after that.
+ </Text>
+ <Button
+ testID="voice-enrollment-download"
+ onPress={() => {
+ downloadAndCheck().catch(() => undefined);
+ }}
>
- <View className="h-11 w-11 items-center justify-center overflow-hidden rounded-full bg-primary">
- <GradientGlassSurface shape="circle" />
- <Text className="text-[17px] font-semibold text-white">{avatarInitial}</Text>
- </View>
- <TextInput
- value={displayName}
- onChangeText={setDisplayName}
- placeholder={isOwner ? 'Me' : 'Speaker name'}
- placeholderTextColor="rgba(60,60,67,0.3)"
- testID="voice-enrollment-name"
- className="flex-1 text-[17px] font-semibold text-label"
- />
- </View>
+ Download
+ </Button>
+ <Button testID="voice-enrollment-not-now" variant="secondary" onPress={onCancel}>
+ Not Now
+ </Button>
</View>
+ );
+ }
- <View className="gap-2">
- <Text className="text-[13px] uppercase tracking-wider text-secondaryLabel">Read aloud</Text>
- <View
- className="rounded-xl border border-separator bg-secondarySystemGroupedBackground p-4"
- style={{ borderCurve: 'continuous' }}
- >
- <Text className="text-[15px] leading-6 text-label">
- Today I am creating a voice profile for meeting notes. This 10 second voiceprint stays
- on my device and helps label future transcripts. It is used only to recognize my voice
- in recordings I choose to process, and it is not shared with other people or services. I
- can delete them whenever I choose.
- </Text>
- </View>
+ if (phase === 'done') {
+ const labelled = isOwner ? `you as ${trimmedName}` : trimmedName;
+ return (
+ <View className="items-center gap-3 pt-6">
+ <SystemIcon
+ name="checkmark.circle.fill"
+ mdName="CircleCheck"
+ size={44}
+ color="systemGreen"
+ />
+ <Text className="text-[22px] font-semibold text-label">All set</Text>
+ <Text className="text-center text-[15px] leading-5 text-secondaryLabel">
+ {`Future on-device meetings will label ${labelled}.`}
+ </Text>
+ <Button testID="voice-enrollment-done" className="mt-2 self-stretch" onPress={onDone}>
+ Done
+ </Button>
</View>
+ );
+ }
- <View className="items-center gap-2">
- <WaveformVisualizer
- isRecording={recordingActive}
- height={120}
- color={BRAND}
- amplitude={currentAmplitude}
- waveformData={waveformData}
+ const needsName = asksForName && !trimmedName;
+ const canStop = elapsedSeconds >= MIN_SECONDS;
+ const ringProgress = Math.min(elapsedSeconds / MIN_SECONDS, 1);
+
+ return (
+ <View className="gap-5">
+ {asksForName ? (
+ <TextInput
+ value={displayName}
+ onChangeText={setDisplayName}
+ placeholder="Their name"
+ placeholderTextColor={iosColor('tertiaryLabel')}
+ editable={phase === 'ready' || phase === 'failed'}
+ testID="voice-enrollment-name"
+ className="rounded-xl border border-separator bg-secondarySystemGroupedBackground px-4 py-3 text-[17px] text-label"
+ style={{ borderCurve: 'continuous' }}
/>
- {recordingActive ? (
- <View className="flex-row items-center gap-2">
- <View className="h-2 w-2 rounded-full bg-systemRed" />
- <Text className="text-[14px] font-medium text-secondaryLabel">
- {`Recording · ${formatTime(elapsedSeconds)} / ${formatTime(TARGET_SECONDS)}`}
- </Text>
- </View>
- ) : null}
+ ) : null}
+
+ <Text className="text-[15px] leading-5 text-secondaryLabel">
+ Read this aloud in your normal voice. It takes about 20 seconds.
+ </Text>
+ <View
+ className="rounded-xl border border-separator bg-secondarySystemGroupedBackground p-4"
+ style={{ borderCurve: 'continuous' }}
+ >
+ <Text className="text-[19px] leading-7 text-label">{VOICE_ENROLLMENT_SCRIPT}</Text>
</View>
- <View className="gap-3">
- <Text className="text-center text-[14px] font-semibold text-label">{countLabel}</Text>
- {canRecordMore ? (
- recordingActive ? (
- <Pressable
- onPress={handleRecordPress}
- accessibilityRole="button"
- testID="voice-enrollment-record"
- className="h-14 flex-row items-center justify-center gap-2 rounded-full border border-separator bg-secondarySystemGroupedBackground active:opacity-80"
- style={{ borderCurve: 'continuous' }}
- >
- <View className="h-2.5 w-2.5 rounded-full bg-systemRed" />
- <Text className="text-base font-medium text-systemRed">Stop</Text>
- </Pressable>
- ) : (
- <Button
- onPress={handleRecordPress}
- disabled={saving}
- testID="voice-enrollment-record"
- accessibilityState={{ disabled: saving }}
- >
- {takes.length === 0 ? 'Record Take' : 'Record Another'}
- </Button>
- )
- ) : null}
- {canRecordMore ? (
- <Text className="text-center text-[13px] leading-5 text-tertiaryLabel">
- One recording is enough. Record another if you want better reliability.
+ {downloadStatus === 'downloading' ? (
+ <View className="flex-row items-center gap-2" testID="voice-enrollment-downloading">
+ <ActivityIndicator size="small" />
+ <Text className="text-[13px] text-secondaryLabel">Downloading speaker model…</Text>
+ </View>
+ ) : null}
+ {downloadStatus === 'failed' && phase !== 'download-failed' ? (
+ <View className="flex-row items-center justify-between">
+ <Text className="text-[13px] text-systemRed">
+ The speaker model didn&apos;t download.
</Text>
- ) : null}
- {takes.length > 0 || showRetry ? (
<Pressable
- onPress={handleRemoveTakes}
+ onPress={startDownload}
accessibilityRole="button"
- accessibilityLabel="Retry recording"
- testID="voice-enrollment-retry"
- className="items-center py-1"
+ testID="voice-enrollment-download-retry-inline"
+ className="min-h-11 justify-center px-2"
>
- <Text className="text-[14px] font-medium text-brand">Retry recording</Text>
+ <Text className="text-[14px] font-medium text-brand">Retry</Text>
</Pressable>
- ) : null}
- </View>
+ </View>
+ ) : null}
- {error ? (
+ {phase === 'failed' && failure ? (
<View
- className="rounded-xl border border-systemRed/30 bg-systemRed/10 p-3"
+ className="gap-2 rounded-xl border border-systemRed/30 bg-systemRed/10 p-3"
testID="voice-enrollment-error"
>
- <Text className="text-[14px] leading-5 text-systemRed">{error}</Text>
+ <Text className="text-[14px] leading-5 text-systemRed">{failure}</Text>
</View>
) : null}
- <View className="flex-row gap-3">
- {onCancel ? (
- <Button variant="secondary" className="flex-1" onPress={handleCancel}>
- Cancel
+ {phase === 'checking' ? (
+ <View className="items-center gap-2 py-6" testID="voice-enrollment-checking">
+ <ActivityIndicator />
+ <Text className="text-[15px] text-secondaryLabel">
+ {downloadStatus === 'downloading'
+ ? 'Waiting for the speaker model to finish downloading…'
+ : 'Checking your voice…'}
+ </Text>
+ </View>
+ ) : phase === 'download-failed' ? (
+ <View className="gap-3 py-2">
+ <Text className="text-[15px] leading-5 text-secondaryLabel">
+ The speaker model hasn&apos;t finished downloading, so your recording couldn&apos;t be
+ checked yet.
+ </Text>
+ <Button
+ testID="voice-enrollment-download-retry"
+ onPress={() => {
+ downloadAndCheck().catch(() => undefined);
+ }}
+ >
+ Retry
</Button>
- ) : null}
+ </View>
+ ) : phase === 'failed' ? (
<Button
- className="flex-1"
- onPress={handleSubmit}
- disabled={!canSave}
- loading={saving}
- testID="voice-enrollment-save"
- accessibilityState={{ disabled: !canSave }}
+ testID="voice-enrollment-try-again"
+ onPress={() => {
+ setFailure(null);
+ setPhase('ready');
+ }}
>
- Save
+ Try Again
</Button>
- </View>
+ ) : (
+ <View className="items-center gap-4">
+ {phase === 'recording' ? (
+ <WaveformVisualizer
+ isRecording
+ height={80}
+ color={BRAND}
+ amplitude={currentAmplitude}
+ waveformData={waveformData}
+ />
+ ) : null}
+ <Pressable
+ onPress={phase === 'ready' ? () => start().catch(() => undefined) : undefined}
+ disabled={phase !== 'ready' || needsName}
+ accessibilityRole="button"
+ accessibilityLabel={phase === 'recording' ? 'Recording' : 'Start recording'}
+ accessibilityState={{ disabled: phase !== 'ready' || needsName }}
+ testID="voice-enrollment-record"
+ style={{ width: RING_SIZE, height: RING_SIZE, opacity: needsName ? 0.4 : 1 }}
+ className="items-center justify-center"
+ >
+ <Svg width={RING_SIZE} height={RING_SIZE} style={{ position: 'absolute' }}>
+ <Circle
+ cx={RING_SIZE / 2}
+ cy={RING_SIZE / 2}
+ r={RING_RADIUS}
+ stroke={BRAND}
+ strokeOpacity={0.15}
+ strokeWidth={RING_STROKE}
+ fill="none"
+ />
+ <Circle
+ cx={RING_SIZE / 2}
+ cy={RING_SIZE / 2}
+ r={RING_RADIUS}
+ stroke={BRAND}
+ strokeWidth={RING_STROKE}
+ strokeDasharray={RING_CIRCUMFERENCE}
+ strokeDashoffset={RING_CIRCUMFERENCE * (1 - ringProgress)}
+ strokeLinecap="round"
+ fill="none"
+ transform={`rotate(-90 ${RING_SIZE / 2} ${RING_SIZE / 2})`}
+ />
+ </Svg>
+ <View className="h-16 w-16 items-center justify-center rounded-full bg-brand">
+ <SystemIcon name="mic.fill" mdName="Mic" size={26} color="#FFFFFF" />
+ </View>
+ </Pressable>
+ {phase === 'recording' ? (
+ <View className="items-center gap-2 self-stretch">
+ <Button
+ testID="voice-enrollment-stop"
+ className="self-stretch"
+ disabled={!canStop}
+ onPress={() => {
+ stop().catch(() => undefined);
+ }}
+ >
+ Done
+ </Button>
+ {canStop ? null : (
+ <Text className="text-[13px] text-tertiaryLabel">Keep reading</Text>
+ )}
+ </View>
+ ) : (
+ <Text className="text-[13px] text-tertiaryLabel">Tap to start reading</Text>
+ )}
+ </View>
+ )}
</View>
);
}
diff --git a/openwhispr-mobile/src/components/notes/VoiceProfileList.tsx b/openwhispr-mobile/src/components/notes/VoiceProfileList.tsx
index b5834a40e..8e3069228 100644
--- a/openwhispr-mobile/src/components/notes/VoiceProfileList.tsx
+++ b/openwhispr-mobile/src/components/notes/VoiceProfileList.tsx
@@ -16,11 +16,11 @@ interface VoiceProfileListProps {
onDeleteAll?: () => void;
}
-const formatEnrolledAt = (value: string | null): string => {
- if (!value) return 'Enrolled on this device';
+const formatAddedAt = (value: string | null): string => {
+ if (!value) return 'Added on this device';
const date = new Date(value);
- if (Number.isNaN(date.getTime())) return 'Enrolled on this device';
- return `Enrolled ${date.toLocaleDateString(undefined, {
+ if (Number.isNaN(date.getTime())) return 'Added on this device';
+ return `Added ${date.toLocaleDateString(undefined, {
month: 'short',
day: 'numeric',
})}`;
@@ -28,7 +28,7 @@ const formatEnrolledAt = (value: string | null): string => {
const formatMeta = (profile: SpeakerProfile): string => {
const samples = `${profile.sampleCount} ${profile.sampleCount === 1 ? 'sample' : 'samples'}`;
- return `${samples} · ${formatEnrolledAt(profile.createdAt)}`;
+ return `${samples} · ${formatAddedAt(profile.createdAt)}`;
};
export function VoiceProfileList({
@@ -57,18 +57,18 @@ export function VoiceProfileList({
<Text className="text-center text-[15px] leading-5 text-tertiaryLabel">
Record a short voice sample and OpenWhispr will recognize you in future meeting
transcripts instead of showing "Speaker 1". Works with on-device transcription.
- Voiceprints stay on this device and can be deleted anytime.
+ Voice samples stay on this device and can be deleted anytime.
</Text>
</View>
<Pressable
onPress={onEnrollOwner}
accessibilityRole="button"
- accessibilityLabel="Enroll Me"
+ accessibilityLabel="Teach Your Voice"
testID="voice-profile-enroll-owner-empty"
className="mt-1 h-10 items-center justify-center rounded-[10px] bg-brand px-4"
style={({ pressed }) => ({ opacity: pressed ? 0.85 : 1, borderCurve: 'continuous' })}
>
- <Text className="text-[15px] font-semibold text-white">Enroll Me</Text>
+ <Text className="text-[15px] font-semibold text-white">Teach Your Voice</Text>
</Pressable>
</View>
</GroupedList.Row>
@@ -105,7 +105,7 @@ export function VoiceProfileList({
className="rounded-md bg-tertiarySystemFill px-2 py-0.5"
testID={`voice-profile-owner-badge-${profile.id}`}
>
- <Text className="text-[12px] font-semibold text-secondaryLabel">Owner</Text>
+ <Text className="text-[12px] font-semibold text-secondaryLabel">You</Text>
</View>
) : null}
</View>
@@ -131,23 +131,23 @@ export function VoiceProfileList({
<GroupedList.Row
onPress={onEnrollOwner}
accessibilityRole="button"
- accessibilityLabel="Enroll Me"
+ accessibilityLabel="Teach Your Voice"
testID="voice-profile-enroll-owner"
contentInsetLeft={16}
>
- <Text className="text-[17px] font-medium text-brand">Enroll Me</Text>
+ <Text className="text-[17px] font-medium text-brand">Teach Your Voice</Text>
</GroupedList.Row>
)}
<GroupedList.Row
onPress={onEnrollSpeaker}
accessibilityRole="button"
- accessibilityLabel="Add Speaker"
+ accessibilityLabel="Add Someone's Voice"
testID="voice-profile-enroll-speaker"
contentInsetLeft={16}
>
<View className="flex-row items-center gap-2">
<SystemIcon name="plus" mdName="Plus" size={17} color="brand" />
- <Text className="text-[17px] font-medium text-brand">Add Speaker</Text>
+ <Text className="text-[17px] font-medium text-brand">Add Someone's Voice</Text>
</View>
</GroupedList.Row>
</GroupedList>
diff --git a/openwhispr-mobile/src/components/notes/VoiceProfilePromptCard.tsx b/openwhispr-mobile/src/components/notes/VoiceProfilePromptCard.tsx
deleted file mode 100644
index bfc1ceb54..000000000
--- a/openwhispr-mobile/src/components/notes/VoiceProfilePromptCard.tsx
+++ /dev/null
@@ -1,59 +0,0 @@
-import { Pressable, View } from 'react-native';
-import { Text } from '@/components/ui/Text';
-import { SystemIcon } from '@/components/ui/SystemIcon';
-
-interface VoiceProfilePromptCardProps {
- visible: boolean;
- onEnroll: () => void;
- onDismiss: () => void;
-}
-
-export function VoiceProfilePromptCard({
- visible,
- onEnroll,
- onDismiss,
-}: VoiceProfilePromptCardProps) {
- if (!visible) return null;
-
- return (
- <View
- className="mb-4 gap-3 rounded-xl border border-separator bg-secondarySystemGroupedBackground p-4"
- style={{ borderCurve: 'continuous' }}
- testID="voice-profile-prompt-card"
- >
- <View className="flex-row items-start gap-3">
- <View className="h-9 w-9 items-center justify-center rounded-lg bg-tertiarySystemFill">
- <SystemIcon name="waveform" mdName="AudioLines" size={20} color="brand" />
- </View>
- <View className="min-w-0 flex-1 gap-1">
- <Text className="text-[16px] font-semibold text-label">Enroll your voice</Text>
- <Text className="text-[14px] leading-5 text-secondaryLabel">
- Future meetings can label you more accurately. The voiceprint stays on this device.
- </Text>
- </View>
- </View>
- <View className="flex-row gap-3">
- <Pressable
- onPress={onEnroll}
- accessibilityRole="button"
- accessibilityLabel="Enroll Me"
- testID="voice-profile-prompt-enroll"
- className="h-9 items-center justify-center rounded-lg bg-brand px-4"
- style={({ pressed }) => ({ opacity: pressed ? 0.85 : 1, borderCurve: 'continuous' })}
- >
- <Text className="text-[14px] font-semibold text-white">Enroll Me</Text>
- </Pressable>
- <Pressable
- onPress={onDismiss}
- accessibilityRole="button"
- accessibilityLabel="Dismiss voice profile prompt"
- testID="voice-profile-prompt-dismiss"
- className="h-9 items-center justify-center rounded-lg bg-tertiarySystemFill px-4"
- style={({ pressed }) => ({ opacity: pressed ? 0.75 : 1, borderCurve: 'continuous' })}
- >
- <Text className="text-[14px] font-medium text-label">Dismiss</Text>
- </Pressable>
- </View>
- </View>
- );
-}
diff --git a/openwhispr-mobile/src/components/notes/VoiceSetupBanner.tsx b/openwhispr-mobile/src/components/notes/VoiceSetupBanner.tsx
new file mode 100644
index 000000000..32cbf9e54
--- /dev/null
+++ b/openwhispr-mobile/src/components/notes/VoiceSetupBanner.tsx
@@ -0,0 +1,48 @@
+import type React from 'react';
+import { Pressable, View } from 'react-native';
+import { Text } from '@/components/ui/Text';
+import { SystemIcon } from '@/components/ui/SystemIcon';
+
+interface VoiceSetupBannerProps {
+ onSetUp: () => void;
+ onDismiss: () => void;
+}
+
+export function VoiceSetupBanner({ onSetUp, onDismiss }: VoiceSetupBannerProps): React.JSX.Element {
+ return (
+ <View
+ className="mb-4 flex-row items-center gap-3 rounded-xl border border-separator bg-secondarySystemGroupedBackground p-3"
+ style={{ borderCurve: 'continuous' }}
+ testID="voice-setup-banner"
+ >
+ <View className="h-9 w-9 items-center justify-center rounded-lg bg-tertiarySystemFill">
+ <SystemIcon name="waveform" mdName="AudioLines" size={20} color="brand" />
+ </View>
+ <View className="min-w-0 flex-1">
+ <Text className="text-[15px] font-semibold text-label">Teach OpenWhispr your voice</Text>
+ <Text className="text-[13px] leading-[18px] text-secondaryLabel">
+ Your next on-device meetings will label you as Me.
+ </Text>
+ </View>
+ <Pressable
+ onPress={onSetUp}
+ accessibilityRole="button"
+ testID="voice-setup-banner-set-up"
+ className="h-11 items-center justify-center rounded-lg bg-brand px-3"
+ style={({ pressed }) => ({ opacity: pressed ? 0.85 : 1, borderCurve: 'continuous' })}
+ >
+ <Text className="text-[14px] font-semibold text-white">Set Up</Text>
+ </Pressable>
+ <Pressable
+ onPress={onDismiss}
+ accessibilityRole="button"
+ accessibilityLabel="Dismiss voice setup"
+ testID="voice-setup-banner-dismiss"
+ hitSlop={8}
+ className="h-11 w-8 items-center justify-center"
+ >
+ <SystemIcon name="xmark" mdName="X" size={13} color="secondaryLabel" />
+ </Pressable>
+ </View>
+ );
+}
diff --git a/openwhispr-mobile/src/components/notes/__tests__/VoiceEnrollmentRecorder.test.tsx b/openwhispr-mobile/src/components/notes/__tests__/VoiceEnrollmentRecorder.test.tsx
new file mode 100644
index 000000000..910d7c85a
--- /dev/null
+++ b/openwhispr-mobile/src/components/notes/__tests__/VoiceEnrollmentRecorder.test.tsx
@@ -0,0 +1,753 @@
+import { act, fireEvent, render, waitFor } from '@testing-library/react-native';
+import * as FileSystem from 'expo-file-system/legacy';
+import { VoiceEnrollmentRecorder } from '../VoiceEnrollmentRecorder';
+import { useAudioRecording } from '@/hooks/useAudioRecording';
+import {
+ VOICE_ENROLLMENT_DIARIZER_MODEL_REQUIRED,
+ VOICE_ENROLLMENT_LOW_SNR,
+ VOICE_ENROLLMENT_STORAGE_FAILED,
+ VoiceEnrollmentError,
+} from '@/services/diarization/VoiceprintService';
+import { SpeakerProfileOwnerAlreadyExistsError } from '@/data/local/notesRepository';
+import { MicPermissionError, showMicPermissionAlert } from '@/lib/permissions';
+import { Sentry } from '@/lib/sentry';
+
+jest.mock('@/components/ui/Text', () => ({ Text: require('react-native').Text }));
+jest.mock('@/components/ui/SystemIcon', () => ({ SystemIcon: () => null }));
+jest.mock('@/components/features/WaveformVisualizer', () => ({ WaveformVisualizer: () => null }));
+jest.mock('react-native-svg', () => ({
+ __esModule: true,
+ default: () => null,
+ Circle: () => null,
+}));
+jest.mock('@/hooks/useAudioWaveform', () => ({
+ useAudioWaveform: () => ({ currentAmplitude: 0, waveformData: [] }),
+}));
+jest.mock('expo-file-system/legacy', () => ({ deleteAsync: jest.fn(async () => undefined) }));
+jest.mock('@/components/ui/Button', () => {
+ const mockReact = require('react');
+ const mockText = require('react-native').Text;
+ return {
+ Button: function MockButton(props: any) {
+ return mockReact.createElement(
+ mockText,
+ {
+ onPress: props.disabled ? undefined : props.onPress,
+ testID: props.testID,
+ accessibilityState: { disabled: !!props.disabled },
+ },
+ props.children,
+ );
+ },
+ };
+});
+jest.mock('@/hooks/useAudioRecording', () => ({ useAudioRecording: jest.fn() }));
+jest.mock('@/lib/sentry', () => ({ Sentry: { captureException: jest.fn() } }));
+jest.mock('@/lib/permissions', () => ({
+ ...jest.requireActual('@/lib/permissions'),
+ showMicPermissionAlert: jest.fn(),
+}));
+
+const mockUseAudioRecording = useAudioRecording as jest.MockedFunction<typeof useAudioRecording>;
+const startRecording = jest.fn(async () => undefined);
+const stopRecordingRaw = jest.fn(async (): Promise<string | null> => 'file://sample.wav');
+const cancelRecording = jest.fn();
+
+const renderRecorder = (overrides: Partial<Parameters<typeof VoiceEnrollmentRecorder>[0]> = {}) => {
+ const props = {
+ isOwner: true,
+ isModelReady: jest.fn(async () => true),
+ isModelDownloading: jest.fn(() => false),
+ downloadModel: jest.fn(async () => undefined),
+ onSubmit: jest.fn(async () => undefined),
+ onDone: jest.fn(),
+ onCancel: jest.fn(),
+ now: () => new Date('2026-09-27T09:00:00.000Z'),
+ ...overrides,
+ };
+ return { props, ...render(<VoiceEnrollmentRecorder {...props} />) };
+};
+
+const recordFor = async (
+ utils: ReturnType<typeof renderRecorder>,
+ seconds: number,
+): Promise<void> => {
+ await waitFor(() => expect(utils.getByTestId('voice-enrollment-record')).toBeTruthy());
+ await act(async () => {
+ fireEvent.press(utils.getByTestId('voice-enrollment-record'));
+ });
+ await act(async () => {
+ jest.advanceTimersByTime(seconds * 1000);
+ });
+};
+
+// Mirrors the real hook: useAudioRecorder's releasing cleanup is declared before the
+// recorder component's own effects, so on unmount it releases the native recorder first,
+// and every property read after that throws NativeSharedObjectNotFoundException.
+const mockReleasingRecorder = (waitForStop: () => Promise<void> = async () => undefined) => {
+ let released = false;
+ let recordingNow = false;
+ const read = <T,>(value: T): T => {
+ if (released) throw new Error('NativeSharedObjectNotFoundException');
+ return value;
+ };
+ const audioRecorder = {
+ get uri() {
+ return read('file://partial.wav');
+ },
+ get isRecording() {
+ return read(recordingNow);
+ },
+ } as unknown as ReturnType<typeof useAudioRecording>['audioRecorder'];
+ const recording = {
+ ...mockUseAudioRecording(),
+ startRecording: jest.fn(async () => {
+ recordingNow = true;
+ }),
+ cancelRecording: jest.fn(async () => {
+ if (audioRecorder.isRecording) recordingNow = false;
+ }),
+ stopRecordingRaw: jest.fn(async () => {
+ if (!audioRecorder.isRecording) return null;
+ await waitForStop();
+ recordingNow = false;
+ return audioRecorder.uri ?? null;
+ }),
+ audioRecorder,
+ };
+ mockUseAudioRecording.mockImplementation(() => {
+ require('react').useEffect(
+ () => () => {
+ released = true;
+ },
+ [],
+ );
+ return recording;
+ });
+};
+
+beforeEach(() => {
+ jest.clearAllMocks();
+ jest.useFakeTimers();
+ mockUseAudioRecording.mockReturnValue({
+ isRecording: false,
+ isProcessing: false,
+ currentText: '',
+ isSupported: true,
+ startRecording,
+ stopRecording: jest.fn(),
+ stopRecordingRaw,
+ cancelRecording,
+ audioRecorder: { isRecording: true } as ReturnType<typeof useAudioRecording>['audioRecorder'],
+ });
+});
+afterEach(() => jest.useRealTimers());
+
+describe('VoiceEnrollmentRecorder', () => {
+ it('asks before downloading the speaker model, and Not Now downloads nothing', async () => {
+ const utils = renderRecorder({ isModelReady: jest.fn(async () => false) });
+ await waitFor(() => expect(utils.getByText('Download the speaker model')).toBeTruthy());
+ expect(utils.queryByTestId('voice-enrollment-record')).toBeNull();
+
+ fireEvent.press(utils.getByTestId('voice-enrollment-not-now'));
+
+ expect(utils.props.onCancel).toHaveBeenCalled();
+ expect(utils.props.downloadModel).not.toHaveBeenCalled();
+ });
+
+ it('lets you read while the model downloads, then checks once it has finished', async () => {
+ let finishDownload: () => void = () => {};
+ const downloadModel = jest.fn(() => new Promise<void>((resolve) => (finishDownload = resolve)));
+ const utils = renderRecorder({ isModelReady: jest.fn(async () => false), downloadModel });
+ await waitFor(() => expect(utils.getByTestId('voice-enrollment-download')).toBeTruthy());
+ fireEvent.press(utils.getByTestId('voice-enrollment-download'));
+ expect(utils.getByTestId('voice-enrollment-downloading')).toBeTruthy();
+
+ await recordFor(utils, 15);
+ await act(async () => {
+ fireEvent.press(utils.getByTestId('voice-enrollment-stop'));
+ });
+ expect(utils.getByTestId('voice-enrollment-checking')).toBeTruthy();
+ expect(utils.getByText(/waiting for the speaker model/i)).toBeTruthy();
+ expect(utils.props.onSubmit).not.toHaveBeenCalled();
+
+ await act(async () => finishDownload());
+ await waitFor(() => expect(utils.props.onSubmit).toHaveBeenCalledTimes(1));
+ });
+
+ it('deletes a recording waiting on the download when you leave, and never submits it', async () => {
+ let finishDownload: () => void = () => {};
+ const downloadModel = jest.fn(() => new Promise<void>((resolve) => (finishDownload = resolve)));
+ const utils = renderRecorder({ isModelReady: jest.fn(async () => false), downloadModel });
+ await waitFor(() => expect(utils.getByTestId('voice-enrollment-download')).toBeTruthy());
+ fireEvent.press(utils.getByTestId('voice-enrollment-download'));
+
+ await recordFor(utils, 15);
+ await act(async () => {
+ fireEvent.press(utils.getByTestId('voice-enrollment-stop'));
+ });
+ expect(utils.getByTestId('voice-enrollment-checking')).toBeTruthy();
+
+ utils.unmount();
+ expect(FileSystem.deleteAsync).toHaveBeenCalledWith('file://sample.wav', { idempotent: true });
+
+ await act(async () => finishDownload());
+ expect(utils.props.onSubmit).not.toHaveBeenCalled();
+ });
+
+ it('keeps the recording when the download fails and checks it after a retry', async () => {
+ const downloadModel = jest
+ .fn()
+ .mockRejectedValueOnce(new Error('offline'))
+ .mockResolvedValueOnce(undefined);
+ const utils = renderRecorder({ isModelReady: jest.fn(async () => false), downloadModel });
+ await waitFor(() => expect(utils.getByTestId('voice-enrollment-download')).toBeTruthy());
+ fireEvent.press(utils.getByTestId('voice-enrollment-download'));
+
+ await recordFor(utils, 15);
+ await act(async () => {
+ fireEvent.press(utils.getByTestId('voice-enrollment-stop'));
+ });
+ await waitFor(() => expect(utils.getByTestId('voice-enrollment-download-retry')).toBeTruthy());
+ expect(FileSystem.deleteAsync).not.toHaveBeenCalled();
+
+ await act(async () => {
+ fireEvent.press(utils.getByTestId('voice-enrollment-download-retry'));
+ });
+
+ await waitFor(() =>
+ expect(utils.props.onSubmit).toHaveBeenCalledWith(
+ expect.objectContaining({
+ recordings: [{ uri: 'file://sample.wav', mimeType: 'audio/wav' }],
+ }),
+ ),
+ );
+ });
+
+ it('stops waiting on a stalled download, and Retry checks the kept recording once it lands', async () => {
+ let finishDownload: () => void = () => {};
+ const stalled = new Promise<void>((resolve) => (finishDownload = resolve));
+ const downloadModel = jest.fn(() => stalled);
+ const utils = renderRecorder({ isModelReady: jest.fn(async () => false), downloadModel });
+ await waitFor(() => expect(utils.getByTestId('voice-enrollment-download')).toBeTruthy());
+ fireEvent.press(utils.getByTestId('voice-enrollment-download'));
+ await recordFor(utils, 15);
+ await act(async () => {
+ fireEvent.press(utils.getByTestId('voice-enrollment-stop'));
+ });
+
+ await act(async () => {
+ jest.advanceTimersByTime(3 * 60 * 1000);
+ });
+ expect(utils.getByTestId('voice-enrollment-download-retry')).toBeTruthy();
+ expect(FileSystem.deleteAsync).not.toHaveBeenCalled();
+
+ await act(async () => {
+ fireEvent.press(utils.getByTestId('voice-enrollment-download-retry'));
+ });
+ await act(async () => finishDownload());
+
+ await waitFor(() => expect(utils.props.onSubmit).toHaveBeenCalledTimes(1));
+ });
+
+ it('keeps Done off until 15 seconds and stops by itself at 30', async () => {
+ const utils = renderRecorder();
+ await recordFor(utils, 14);
+ expect(utils.getByTestId('voice-enrollment-stop').props.accessibilityState.disabled).toBe(true);
+ expect(utils.getByText('Keep reading')).toBeTruthy();
+
+ await act(async () => {
+ jest.advanceTimersByTime(1000);
+ });
+ expect(utils.getByTestId('voice-enrollment-stop').props.accessibilityState.disabled).toBe(
+ false,
+ );
+ expect(utils.queryByText('Keep reading')).toBeNull();
+
+ await act(async () => {
+ jest.advanceTimersByTime(14_000);
+ });
+ expect(stopRecordingRaw).not.toHaveBeenCalled();
+ await act(async () => {
+ jest.advanceTimersByTime(1000);
+ });
+ await waitFor(() => expect(stopRecordingRaw).toHaveBeenCalledTimes(1));
+ await waitFor(() => expect(utils.props.onSubmit).toHaveBeenCalledTimes(1));
+ });
+
+ it('stops and submits once when the 30 s limit lands while Done is still stopping', async () => {
+ let finishStop: (uri: string) => void = () => {};
+ stopRecordingRaw.mockImplementationOnce(
+ () => new Promise<string>((resolve) => (finishStop = resolve)),
+ );
+ const utils = renderRecorder();
+ await recordFor(utils, 29);
+ await act(async () => {
+ fireEvent.press(utils.getByTestId('voice-enrollment-stop'));
+ });
+ // Still recording while the stop is in flight, so the auto-stop fires as well.
+ await act(async () => {
+ jest.advanceTimersByTime(1000);
+ });
+ await act(async () => finishStop('file://sample.wav'));
+
+ await waitFor(() => expect(utils.props.onSubmit).toHaveBeenCalledTimes(1));
+ expect(stopRecordingRaw).toHaveBeenCalledTimes(1);
+ });
+
+ it('submits one sample with the consent time and shows success', async () => {
+ const utils = renderRecorder();
+ await recordFor(utils, 15);
+ await act(async () => {
+ fireEvent.press(utils.getByTestId('voice-enrollment-stop'));
+ });
+
+ await waitFor(() =>
+ expect(utils.props.onSubmit).toHaveBeenCalledWith({
+ recordings: [{ uri: 'file://sample.wav', mimeType: 'audio/wav' }],
+ displayName: 'Me',
+ isOwner: true,
+ consentAccepted: true,
+ consentAcceptedAt: '2026-09-27T09:00:00.000Z',
+ }),
+ );
+ expect(utils.getByText('Future on-device meetings will label you as Me.')).toBeTruthy();
+ fireEvent.press(utils.getByTestId('voice-enrollment-done'));
+ expect(utils.props.onDone).toHaveBeenCalled();
+ expect(FileSystem.deleteAsync).toHaveBeenCalledWith('file://sample.wav', { idempotent: true });
+ });
+
+ it('explains a failed sample and lets you try again', async () => {
+ const onSubmit = jest.fn(async () => {
+ throw new VoiceEnrollmentError(VOICE_ENROLLMENT_LOW_SNR, 'snr');
+ });
+ const utils = renderRecorder({ onSubmit });
+ await recordFor(utils, 15);
+ await act(async () => {
+ fireEvent.press(utils.getByTestId('voice-enrollment-stop'));
+ });
+
+ await waitFor(() =>
+ expect(utils.getByText('Too much background noise. Try somewhere quieter.')).toBeTruthy(),
+ );
+ expect(FileSystem.deleteAsync).toHaveBeenCalledWith('file://sample.wav', { idempotent: true });
+ expect(Sentry.captureException).not.toHaveBeenCalled();
+ fireEvent.press(utils.getByTestId('voice-enrollment-try-again'));
+ expect(utils.queryByTestId('voice-enrollment-error')).toBeNull();
+ expect(utils.getByTestId('voice-enrollment-record')).toBeTruthy();
+ });
+
+ it('reports a failure it cannot explain', async () => {
+ const error = new VoiceEnrollmentError(VOICE_ENROLLMENT_STORAGE_FAILED, 'disk');
+ const utils = renderRecorder({
+ onSubmit: jest.fn(async () => {
+ throw error;
+ }),
+ });
+ await recordFor(utils, 15);
+ await act(async () => {
+ fireEvent.press(utils.getByTestId('voice-enrollment-stop'));
+ });
+
+ await waitFor(() => expect(utils.getByText(/something went wrong/i)).toBeTruthy());
+ expect(Sentry.captureException).toHaveBeenCalledWith(error, expect.anything());
+ });
+
+ it('deletes the sample and shows no failure when you already have a voice profile', async () => {
+ const utils = renderRecorder({
+ onSubmit: jest.fn(async () => {
+ throw new SpeakerProfileOwnerAlreadyExistsError();
+ }),
+ });
+ await recordFor(utils, 15);
+ await act(async () => {
+ fireEvent.press(utils.getByTestId('voice-enrollment-stop'));
+ });
+
+ await waitFor(() =>
+ expect(FileSystem.deleteAsync).toHaveBeenCalledWith('file://sample.wav', {
+ idempotent: true,
+ }),
+ );
+ expect(utils.queryByTestId('voice-enrollment-error')).toBeNull();
+ });
+
+ it('keeps the read when the model turns out to be missing, and checks it after the download', async () => {
+ const onSubmit = jest
+ .fn()
+ .mockRejectedValueOnce(
+ new VoiceEnrollmentError(VOICE_ENROLLMENT_DIARIZER_MODEL_REQUIRED, 'x'),
+ )
+ .mockResolvedValueOnce(undefined);
+ const utils = renderRecorder({ onSubmit });
+ await recordFor(utils, 15);
+ await act(async () => {
+ fireEvent.press(utils.getByTestId('voice-enrollment-stop'));
+ });
+ await waitFor(() => expect(utils.getByTestId('voice-enrollment-download')).toBeTruthy());
+ expect(FileSystem.deleteAsync).not.toHaveBeenCalled();
+
+ await act(async () => {
+ fireEvent.press(utils.getByTestId('voice-enrollment-download'));
+ });
+
+ await waitFor(() => expect(onSubmit).toHaveBeenCalledTimes(2));
+ expect(onSubmit.mock.calls[1][0]).toEqual(
+ expect.objectContaining({
+ recordings: [{ uri: 'file://sample.wav', mimeType: 'audio/wav' }],
+ }),
+ );
+ expect(utils.props.downloadModel).toHaveBeenCalledTimes(1);
+ });
+
+ it('checks the kept recording once when Retry is tapped twice', async () => {
+ const downloadModel = jest
+ .fn()
+ .mockRejectedValueOnce(new Error('offline'))
+ .mockResolvedValue(undefined);
+ const utils = renderRecorder({ isModelReady: jest.fn(async () => false), downloadModel });
+ await waitFor(() => expect(utils.getByTestId('voice-enrollment-download')).toBeTruthy());
+ fireEvent.press(utils.getByTestId('voice-enrollment-download'));
+ await recordFor(utils, 15);
+ await act(async () => {
+ fireEvent.press(utils.getByTestId('voice-enrollment-stop'));
+ });
+ await waitFor(() => expect(utils.getByTestId('voice-enrollment-download-retry')).toBeTruthy());
+
+ await act(async () => {
+ const retry = utils.getByTestId('voice-enrollment-download-retry');
+ fireEvent.press(retry);
+ fireEvent.press(retry);
+ });
+
+ await waitFor(() => expect(utils.props.onSubmit).toHaveBeenCalledTimes(1));
+ expect(downloadModel).toHaveBeenCalledTimes(2);
+ });
+
+ it('retries a failed download from the read screen without checking anything', async () => {
+ const downloadModel = jest
+ .fn()
+ .mockRejectedValueOnce(new Error('offline'))
+ .mockResolvedValue(undefined);
+ const utils = renderRecorder({ isModelReady: jest.fn(async () => false), downloadModel });
+ await waitFor(() => expect(utils.getByTestId('voice-enrollment-download')).toBeTruthy());
+ await act(async () => {
+ fireEvent.press(utils.getByTestId('voice-enrollment-download'));
+ });
+ await waitFor(() =>
+ expect(utils.getByTestId('voice-enrollment-download-retry-inline')).toBeTruthy(),
+ );
+
+ await act(async () => {
+ fireEvent.press(utils.getByTestId('voice-enrollment-download-retry-inline'));
+ });
+
+ expect(downloadModel).toHaveBeenCalledTimes(2);
+ expect(utils.queryByTestId('voice-enrollment-download-retry-inline')).toBeNull();
+ expect(utils.props.onSubmit).not.toHaveBeenCalled();
+ });
+
+ it('keeps the profile name on a retrain', async () => {
+ const utils = renderRecorder({ isOwner: false, profileId: 5, defaultDisplayName: 'Alice' });
+ await recordFor(utils, 15);
+ await act(async () => {
+ fireEvent.press(utils.getByTestId('voice-enrollment-stop'));
+ });
+
+ await waitFor(() =>
+ expect(utils.props.onSubmit).toHaveBeenCalledWith({
+ recordings: [{ uri: 'file://sample.wav', mimeType: 'audio/wav' }],
+ profileId: 5,
+ consentAccepted: true,
+ consentAcceptedAt: '2026-09-27T09:00:00.000Z',
+ }),
+ );
+ expect(utils.queryByTestId('voice-enrollment-name')).toBeNull();
+ });
+
+ it('points to Settings when iOS can no longer ask for the microphone', async () => {
+ startRecording.mockRejectedValueOnce(
+ new MicPermissionError('denied', { canAskAgain: false, nativePromptShown: false }),
+ );
+ const utils = renderRecorder();
+ await waitFor(() => expect(utils.getByTestId('voice-enrollment-record')).toBeTruthy());
+ await act(async () => {
+ fireEvent.press(utils.getByTestId('voice-enrollment-record'));
+ });
+
+ expect(showMicPermissionAlert).toHaveBeenCalledTimes(1);
+ expect(utils.getByText(/check that OpenWhispr can use it in Settings/i)).toBeTruthy();
+ });
+
+ it('shows a failure and Try Again when the microphone cannot start', async () => {
+ startRecording.mockRejectedValueOnce(new Error('Microphone permission denied'));
+ const utils = renderRecorder();
+ await waitFor(() => expect(utils.getByTestId('voice-enrollment-record')).toBeTruthy());
+ await act(async () => {
+ fireEvent.press(utils.getByTestId('voice-enrollment-record'));
+ });
+
+ expect(utils.getByTestId('voice-enrollment-error')).toBeTruthy();
+ expect(utils.getByText("Couldn't start the microphone. Try again.")).toBeTruthy();
+ expect(utils.getByTestId('voice-enrollment-try-again')).toBeTruthy();
+ expect(showMicPermissionAlert).not.toHaveBeenCalled();
+ expect(Sentry.captureException).toHaveBeenCalledTimes(1);
+ });
+
+ it('says a call interrupted the read as soon as the recorder pauses', async () => {
+ const recorder = {
+ uri: 'file://partial.wav',
+ isRecording: true,
+ stop: jest.fn(async () => {}),
+ };
+ mockUseAudioRecording.mockReturnValue({
+ ...mockUseAudioRecording(),
+ audioRecorder: recorder as unknown as ReturnType<typeof useAudioRecording>['audioRecorder'],
+ });
+ const utils = renderRecorder();
+ await recordFor(utils, 5);
+
+ recorder.isRecording = false;
+ await act(async () => {
+ jest.advanceTimersByTime(1000);
+ });
+
+ expect(utils.getByText(/interrupted/i)).toBeTruthy();
+ expect(recorder.stop).toHaveBeenCalled();
+ await waitFor(() =>
+ expect(FileSystem.deleteAsync).toHaveBeenCalledWith('file://partial.wav', {
+ idempotent: true,
+ }),
+ );
+ expect(stopRecordingRaw).not.toHaveBeenCalled();
+ expect(utils.props.onSubmit).not.toHaveBeenCalled();
+ });
+
+ it('keeps the time you first started reading as the consent time across Try Again', async () => {
+ const now = jest
+ .fn()
+ .mockReturnValueOnce(new Date('2026-09-27T09:00:00.000Z'))
+ .mockReturnValue(new Date('2026-09-27T09:05:00.000Z'));
+ const onSubmit = jest
+ .fn()
+ .mockRejectedValueOnce(new VoiceEnrollmentError(VOICE_ENROLLMENT_LOW_SNR, 'snr'))
+ .mockResolvedValueOnce(undefined);
+ const utils = renderRecorder({ now, onSubmit });
+ await recordFor(utils, 15);
+ await act(async () => {
+ fireEvent.press(utils.getByTestId('voice-enrollment-stop'));
+ });
+ await waitFor(() => expect(utils.getByTestId('voice-enrollment-try-again')).toBeTruthy());
+ fireEvent.press(utils.getByTestId('voice-enrollment-try-again'));
+ await recordFor(utils, 15);
+ await act(async () => {
+ fireEvent.press(utils.getByTestId('voice-enrollment-stop'));
+ });
+
+ await waitFor(() => expect(onSubmit).toHaveBeenCalledTimes(2));
+ expect(onSubmit).toHaveBeenLastCalledWith(
+ expect.objectContaining({ consentAcceptedAt: '2026-09-27T09:00:00.000Z' }),
+ );
+ });
+
+ it('joins a download already running instead of asking again', async () => {
+ const downloadModel = jest.fn(() => new Promise<void>(() => {}));
+ const isModelReady = jest.fn(async () => false);
+ const utils = renderRecorder({
+ isModelReady,
+ isModelDownloading: jest.fn(() => true),
+ downloadModel,
+ });
+
+ await waitFor(() => expect(utils.getByTestId('voice-enrollment-record')).toBeTruthy());
+ expect(utils.queryByText('Download the speaker model')).toBeNull();
+ expect(utils.getByTestId('voice-enrollment-downloading')).toBeTruthy();
+ expect(downloadModel).toHaveBeenCalledTimes(1);
+ });
+
+ it('stops the download wait when you leave', async () => {
+ const downloadModel = jest.fn(() => new Promise<void>(() => {}));
+ const utils = renderRecorder({ isModelReady: jest.fn(async () => false), downloadModel });
+ await waitFor(() => expect(utils.getByTestId('voice-enrollment-download')).toBeTruthy());
+ fireEvent.press(utils.getByTestId('voice-enrollment-download'));
+ await recordFor(utils, 15);
+ await act(async () => {
+ fireEvent.press(utils.getByTestId('voice-enrollment-stop'));
+ });
+ expect(utils.getByTestId('voice-enrollment-checking')).toBeTruthy();
+
+ const timersWhileWaiting = jest.getTimerCount();
+ utils.unmount();
+ await act(async () => undefined);
+
+ expect(FileSystem.deleteAsync).toHaveBeenCalledTimes(1);
+ expect(FileSystem.deleteAsync).toHaveBeenCalledWith('file://sample.wav', { idempotent: true });
+ // The 3-minute wait timer goes with the screen (unmounting schedules one of its own).
+ expect(jest.getTimerCount()).toBeLessThanOrEqual(timersWhileWaiting);
+ });
+
+ it("labels a retrained profile of yours by the profile's name", async () => {
+ const utils = renderRecorder({ profileId: 3, defaultDisplayName: 'Chad' });
+ await recordFor(utils, 15);
+ await act(async () => {
+ fireEvent.press(utils.getByTestId('voice-enrollment-stop'));
+ });
+
+ await waitFor(() =>
+ expect(utils.getByText('Future on-device meetings will label you as Chad.')).toBeTruthy(),
+ );
+ });
+
+ it('cancels the recording and deletes it when you leave mid-read', async () => {
+ mockUseAudioRecording.mockReturnValue({
+ ...mockUseAudioRecording(),
+ audioRecorder: { uri: 'file://partial.wav', isRecording: true } as ReturnType<
+ typeof useAudioRecording
+ >['audioRecorder'],
+ });
+ const utils = renderRecorder();
+ await recordFor(utils, 5);
+ utils.unmount();
+ expect(cancelRecording).toHaveBeenCalled();
+
+ await act(async () => undefined);
+ expect(FileSystem.deleteAsync).toHaveBeenCalledWith('file://partial.wav', { idempotent: true });
+ });
+
+ it('deletes the recording and never submits it when you leave while it is stopping', async () => {
+ let finishStop: (uri: string) => void = () => {};
+ stopRecordingRaw.mockImplementationOnce(
+ () => new Promise<string>((resolve) => (finishStop = resolve)),
+ );
+ const utils = renderRecorder();
+ await recordFor(utils, 15);
+ await act(async () => {
+ fireEvent.press(utils.getByTestId('voice-enrollment-stop'));
+ });
+
+ utils.unmount();
+ await act(async () => finishStop('file://sample.wav'));
+
+ expect(FileSystem.deleteAsync).toHaveBeenCalledWith('file://sample.wav', { idempotent: true });
+ expect(utils.props.onSubmit).not.toHaveBeenCalled();
+ });
+
+ it('deletes the sample when you leave mid-read, after the recorder is released', async () => {
+ mockReleasingRecorder();
+ const utils = renderRecorder();
+ await recordFor(utils, 5);
+
+ utils.unmount();
+ await act(async () => undefined);
+
+ expect(FileSystem.deleteAsync).toHaveBeenCalledWith('file://partial.wav', { idempotent: true });
+ });
+
+ it('deletes the sample when you leave mid-stop, after the recorder is released', async () => {
+ let finishStop: () => void = () => {};
+ mockReleasingRecorder(() => new Promise<void>((resolve) => (finishStop = resolve)));
+ const utils = renderRecorder();
+ await recordFor(utils, 15);
+ await act(async () => {
+ fireEvent.press(utils.getByTestId('voice-enrollment-stop'));
+ });
+
+ utils.unmount();
+ await act(async () => finishStop());
+
+ expect(FileSystem.deleteAsync).toHaveBeenCalledWith('file://partial.wav', { idempotent: true });
+ expect(utils.props.onSubmit).not.toHaveBeenCalled();
+ });
+
+ it('never deletes a sample already handed to onSubmit when you leave', async () => {
+ let finishSubmit: () => void = () => {};
+ mockReleasingRecorder();
+ const onSubmit = jest.fn(() => new Promise<void>((resolve) => (finishSubmit = resolve)));
+ const utils = renderRecorder({ onSubmit });
+ await recordFor(utils, 15);
+ await act(async () => {
+ fireEvent.press(utils.getByTestId('voice-enrollment-stop'));
+ });
+ await waitFor(() => expect(onSubmit).toHaveBeenCalledTimes(1));
+
+ utils.unmount();
+ await act(async () => undefined);
+ expect(FileSystem.deleteAsync).not.toHaveBeenCalled();
+
+ await act(async () => finishSubmit());
+ expect(FileSystem.deleteAsync).toHaveBeenCalledTimes(1);
+ });
+
+ it.each([
+ ['returns no file', async () => null, /interrupted/i],
+ [
+ 'fails',
+ async () => {
+ throw new Error('stop failed');
+ },
+ /couldn't finish the recording/i,
+ ],
+ ])(
+ 'deletes the sample and shows a failure when stopping %s',
+ async (_label, stopResult, message) => {
+ stopRecordingRaw.mockImplementationOnce(stopResult);
+ mockUseAudioRecording.mockReturnValue({
+ ...mockUseAudioRecording(),
+ audioRecorder: { uri: 'file://partial.wav', isRecording: true } as ReturnType<
+ typeof useAudioRecording
+ >['audioRecorder'],
+ });
+ const utils = renderRecorder();
+ await recordFor(utils, 15);
+ await act(async () => {
+ fireEvent.press(utils.getByTestId('voice-enrollment-stop'));
+ });
+
+ expect(utils.getByTestId('voice-enrollment-error')).toBeTruthy();
+ expect(utils.getByText(message)).toBeTruthy();
+ expect(FileSystem.deleteAsync).toHaveBeenCalledWith('file://partial.wav', {
+ idempotent: true,
+ });
+ expect(utils.props.onSubmit).not.toHaveBeenCalled();
+ },
+ );
+
+ it('starts one recording when the mic is tapped twice while it opens', async () => {
+ let finishStart: () => void = () => {};
+ startRecording.mockImplementationOnce(
+ () => new Promise<undefined>((resolve) => (finishStart = () => resolve(undefined))),
+ );
+ const utils = renderRecorder();
+ await waitFor(() => expect(utils.getByTestId('voice-enrollment-record')).toBeTruthy());
+
+ await act(async () => {
+ fireEvent.press(utils.getByTestId('voice-enrollment-record'));
+ fireEvent.press(utils.getByTestId('voice-enrollment-record'));
+ });
+ await act(async () => finishStart());
+
+ expect(startRecording).toHaveBeenCalledTimes(1);
+ expect(utils.getByTestId('voice-enrollment-stop')).toBeTruthy();
+ });
+
+ it("needs a name before recording someone else's voice", async () => {
+ const utils = renderRecorder({ isOwner: false });
+ await waitFor(() => expect(utils.getByTestId('voice-enrollment-record')).toBeTruthy());
+ expect(utils.getByTestId('voice-enrollment-record').props.accessibilityState.disabled).toBe(
+ true,
+ );
+
+ fireEvent.changeText(utils.getByTestId('voice-enrollment-name'), 'Alice');
+ await recordFor(utils, 15);
+ await act(async () => {
+ fireEvent.press(utils.getByTestId('voice-enrollment-stop'));
+ });
+
+ await waitFor(() =>
+ expect(utils.props.onSubmit).toHaveBeenCalledWith(
+ expect.objectContaining({ displayName: 'Alice', isOwner: false }),
+ ),
+ );
+ expect(utils.getByText('Future on-device meetings will label Alice.')).toBeTruthy();
+ });
+});
diff --git a/openwhispr-mobile/src/components/notes/__tests__/VoiceProfiles.test.tsx b/openwhispr-mobile/src/components/notes/__tests__/VoiceProfiles.test.tsx
index dba6b7663..703761147 100644
--- a/openwhispr-mobile/src/components/notes/__tests__/VoiceProfiles.test.tsx
+++ b/openwhispr-mobile/src/components/notes/__tests__/VoiceProfiles.test.tsx
@@ -1,10 +1,6 @@
-import { act, fireEvent, render, waitFor } from '@testing-library/react-native';
-import * as FileSystem from 'expo-file-system/legacy';
+import { fireEvent, render } from '@testing-library/react-native';
import type { SpeakerProfile } from '@/data/types';
import { VoiceProfileList } from '../VoiceProfileList';
-import { VoiceEnrollmentRecorder } from '../VoiceEnrollmentRecorder';
-import { VoiceProfilePromptCard } from '../VoiceProfilePromptCard';
-import { useAudioRecording } from '@/hooks/useAudioRecording';
jest.mock('@/components/ui/Text', () => ({ Text: require('react-native').Text }));
jest.mock('@/components/ui/SystemIcon', () => ({ SystemIcon: () => null }));
@@ -12,31 +8,6 @@ jest.mock('@/components/ui/SwipeableCard', () => ({
SwipeableCard: ({ children }: any) => children,
}));
jest.mock('../SectionHeader', () => ({ SectionHeader: () => null }));
-jest.mock('@/components/ui/GradientGlassSurface', () => ({ GradientGlassSurface: () => null }));
-jest.mock('@/components/features/WaveformVisualizer', () => ({ WaveformVisualizer: () => null }));
-jest.mock('expo-file-system/legacy', () => ({
- deleteAsync: jest.fn(async () => undefined),
-}));
-jest.mock('@/components/ui/Button', () => {
- const mockReact = require('react');
- const mockText = require('react-native').Text;
- return {
- Button: function MockButton(props: any) {
- return mockReact.createElement(
- mockText,
- {
- onPress: props.disabled ? undefined : props.onPress,
- testID: props.testID,
- accessibilityState: props.accessibilityState,
- },
- props.children,
- );
- },
- };
-});
-jest.mock('@/hooks/useAudioRecording', () => ({
- useAudioRecording: jest.fn(),
-}));
const profile = (overrides: Partial<SpeakerProfile> = {}): SpeakerProfile =>
({
@@ -59,27 +30,26 @@ const baseListProps = {
onDelete: jest.fn(),
};
-const mockUseAudioRecording = useAudioRecording as jest.MockedFunction<typeof useAudioRecording>;
-
describe('VoiceProfileList', () => {
beforeEach(() => {
jest.clearAllMocks();
});
- it('renders owner badge and opens the profile on row press', () => {
+ it('renders your badge and opens the profile on row press', () => {
const onOpenProfile = jest.fn();
const { getByText, getByTestId } = render(
<VoiceProfileList profiles={[profile()]} {...baseListProps} onOpenProfile={onOpenProfile} />,
);
- expect(getByText('Owner')).toBeTruthy();
+ expect(getByText('You')).toBeTruthy();
+ expect(getByText(/Added /)).toBeTruthy();
fireEvent.press(getByTestId('voice-profile-row-1'));
expect(onOpenProfile).toHaveBeenCalledWith(expect.objectContaining({ id: 1 }));
});
- it('offers owner enrollment in the empty state', () => {
+ it('offers to teach your voice in the empty state', () => {
const onEnrollOwner = jest.fn();
const { getByTestId, getByText } = render(
<VoiceProfileList profiles={[]} {...baseListProps} onEnrollOwner={onEnrollOwner} />,
@@ -87,15 +57,14 @@ describe('VoiceProfileList', () => {
expect(getByText('Label speakers automatically')).toBeTruthy();
expect(getByText(/works with on-device transcription/i)).toBeTruthy();
- // Worded like the Enroll Me row and prompt card.
- expect(getByText('Enroll Me')).toBeTruthy();
+ expect(getByText('Teach Your Voice')).toBeTruthy();
fireEvent.press(getByTestId('voice-profile-enroll-owner-empty'));
expect(onEnrollOwner).toHaveBeenCalled();
});
- it('hides the owner enrollment action once an owner exists', () => {
- const { queryByTestId, getByTestId } = render(
+ it("offers to add someone's voice and hides Teach Your Voice once you have one", () => {
+ const { queryByTestId, getByText } = render(
<VoiceProfileList
profiles={[profile(), profile({ id: 2, displayName: 'Alice', isOwner: 0 })]}
{...baseListProps}
@@ -103,98 +72,6 @@ describe('VoiceProfileList', () => {
);
expect(queryByTestId('voice-profile-enroll-owner')).toBeNull();
- expect(getByTestId('voice-profile-enroll-speaker')).toBeTruthy();
- });
-});
-
-describe('VoiceProfilePromptCard', () => {
- it('shows only when no owner profile prompt is visible and supports dismiss', () => {
- const onDismiss = jest.fn();
- const { queryByTestId, rerender, getByTestId } = render(
- <VoiceProfilePromptCard visible={false} onEnroll={() => {}} onDismiss={onDismiss} />,
- );
-
- expect(queryByTestId('voice-profile-prompt-card')).toBeNull();
-
- rerender(<VoiceProfilePromptCard visible onEnroll={() => {}} onDismiss={onDismiss} />);
- fireEvent.press(getByTestId('voice-profile-prompt-dismiss'));
-
- expect(onDismiss).toHaveBeenCalled();
- });
-});
-
-describe('VoiceEnrollmentRecorder', () => {
- const startRecording = jest.fn(async () => undefined);
- const stopRecordingRaw = jest.fn(async () => 'file://take1.wav');
-
- beforeEach(() => {
- jest.clearAllMocks();
- mockUseAudioRecording.mockReturnValue({
- isRecording: false,
- isProcessing: false,
- currentText: '',
- isSupported: true,
- startRecording,
- stopRecording: jest.fn(),
- stopRecordingRaw,
- cancelRecording: jest.fn(),
- audioRecorder: {} as ReturnType<typeof useAudioRecording>['audioRecorder'],
- });
- });
-
- it('records a take and submits with the consent timestamp from the first take', async () => {
- const onSubmit = jest.fn(async () => undefined);
- const now = () => new Date('2026-06-19T09:00:00.000Z');
- const { getByTestId, getByText } = render(
- <VoiceEnrollmentRecorder isOwner onSubmit={onSubmit} now={now} />,
- );
-
- fireEvent.press(getByTestId('voice-enrollment-save'));
- expect(onSubmit).not.toHaveBeenCalled();
-
- fireEvent.press(getByTestId('voice-enrollment-record'));
- await waitFor(() => expect(startRecording).toHaveBeenCalledTimes(1));
- await waitFor(() => expect(getByText('Stop')).toBeTruthy());
- fireEvent.press(getByTestId('voice-enrollment-record'));
- await waitFor(() => expect(stopRecordingRaw).toHaveBeenCalledTimes(1));
- await waitFor(() => expect(getByText('1 of 2 recorded')).toBeTruthy());
- fireEvent.press(getByTestId('voice-enrollment-save'));
-
- await waitFor(() =>
- expect(onSubmit).toHaveBeenCalledWith(
- expect.objectContaining({
- consentAccepted: true,
- consentAcceptedAt: '2026-06-19T09:00:00.000Z',
- recordings: [{ uri: 'file://take1.wav', mimeType: 'audio/wav' }],
- }),
- ),
- );
- });
-
- it('shows retry on quality failure and enables save after an accepted take', async () => {
- const onSubmit = jest.fn(async () => {
- throw new Error('Speech activity is below 10000ms.');
- });
- const { getByTestId, getByText } = render(
- <VoiceEnrollmentRecorder isOwner onSubmit={onSubmit} />,
- );
-
- fireEvent.press(getByTestId('voice-enrollment-record'));
- await waitFor(() => expect(getByText('Stop')).toBeTruthy());
- fireEvent.press(getByTestId('voice-enrollment-record'));
- await waitFor(() => expect(stopRecordingRaw).toHaveBeenCalledTimes(1));
-
- expect(getByTestId('voice-enrollment-save').props.accessibilityState.disabled).toBe(false);
-
- await act(async () => {
- fireEvent.press(getByText('Save'));
- });
-
- await waitFor(() => expect(getByText('Speech activity is below 10000ms.')).toBeTruthy());
- expect(FileSystem.deleteAsync).toHaveBeenCalledWith('file://take1.wav', {
- idempotent: true,
- });
- expect(getByText('0 of 2 recorded')).toBeTruthy();
- expect(getByTestId('voice-enrollment-retry')).toBeTruthy();
+ expect(getByText("Add Someone's Voice")).toBeTruthy();
});
});
diff --git a/openwhispr-mobile/src/components/notes/__tests__/VoiceSetup.test.tsx b/openwhispr-mobile/src/components/notes/__tests__/VoiceSetup.test.tsx
new file mode 100644
index 000000000..1583e9f62
--- /dev/null
+++ b/openwhispr-mobile/src/components/notes/__tests__/VoiceSetup.test.tsx
@@ -0,0 +1,137 @@
+import { act, fireEvent, render } from '@testing-library/react-native';
+import { AccessibilityInfo } from 'react-native';
+import { VoiceSetupBanner } from '../VoiceSetupBanner';
+import { ThatsMeSheet } from '../ThatsMeSheet';
+
+jest.mock('react-native-safe-area-context', () => ({
+ useSafeAreaInsets: () => ({ top: 0, right: 0, bottom: 0, left: 0 }),
+}));
+jest.mock('@/components/ui/Text', () => ({ Text: require('react-native').Text }));
+jest.mock('@/components/ui/SystemIcon', () => ({ SystemIcon: () => null }));
+jest.mock('@/components/ui/GlassIconButton', () => ({
+ GlassIconButton: ({ onPress, accessibilityLabel }: any) =>
+ require('react').createElement(require('react-native').Pressable, {
+ onPress,
+ accessibilityLabel,
+ }),
+}));
+
+const candidates = [
+ { speakerId: 11, name: 'Speaker 2', speechMs: 65000, sampleLine: 'Let us ship on Friday.' },
+ { speakerId: 10, name: 'Speaker 1', speechMs: 12000, sampleLine: 'Sounds good.' },
+];
+
+describe('VoiceSetupBanner', () => {
+ it('explains the benefit and offers set up and dismiss', () => {
+ const onSetUp = jest.fn();
+ const onDismiss = jest.fn();
+ const { getByText, getByTestId } = render(
+ <VoiceSetupBanner onSetUp={onSetUp} onDismiss={onDismiss} />,
+ );
+
+ expect(getByText('Teach OpenWhispr your voice')).toBeTruthy();
+ expect(getByText('Your next on-device meetings will label you as Me.')).toBeTruthy();
+ expect(getByTestId('voice-setup-banner-dismiss').props.accessibilityLabel).toBe(
+ 'Dismiss voice setup',
+ );
+ fireEvent.press(getByTestId('voice-setup-banner-set-up'));
+ fireEvent.press(getByTestId('voice-setup-banner-dismiss'));
+
+ expect(onSetUp).toHaveBeenCalledTimes(1);
+ expect(onDismiss).toHaveBeenCalledTimes(1);
+ });
+});
+
+describe('ThatsMeSheet', () => {
+ const baseProps = {
+ visible: true,
+ candidates,
+ onClaim: jest.fn(() => true),
+ onReadScript: jest.fn(),
+ onClose: jest.fn(),
+ };
+
+ beforeEach(() => jest.clearAllMocks());
+
+ it('lists each speaker with how long they spoke and what they said', () => {
+ const { getByText } = render(<ThatsMeSheet {...baseProps} />);
+
+ expect(getByText('Which speaker is you?')).toBeTruthy();
+ expect(getByText('Speaker 2')).toBeTruthy();
+ expect(getByText('Spoke for 1 min')).toBeTruthy();
+ expect(getByText('Spoke for 12 sec')).toBeTruthy();
+ expect(getByText('“Let us ship on Friday.”')).toBeTruthy();
+ });
+
+ it('claims the tapped speaker and confirms, out loud too', () => {
+ const announce = jest.spyOn(AccessibilityInfo, 'announceForAccessibility');
+ const { getByTestId, getByText } = render(<ThatsMeSheet {...baseProps} />);
+
+ fireEvent.press(getByTestId('thats-me-11'));
+
+ expect(baseProps.onClaim).toHaveBeenCalledWith(11);
+ expect(getByText('Got it')).toBeTruthy();
+ expect(announce).toHaveBeenCalledWith(expect.stringContaining('Got it'));
+ fireEvent.press(getByTestId('thats-me-done'));
+ expect(baseProps.onClose).toHaveBeenCalled();
+ });
+
+ it("keeps the first claim when That's me is tapped twice before it re-renders", () => {
+ // The second call would fail: the first one already saved the owner profile.
+ const onClaim = jest.fn().mockReturnValueOnce(true).mockReturnValue(false);
+ const { getByTestId, getByText } = render(<ThatsMeSheet {...baseProps} onClaim={onClaim} />);
+ const button = getByTestId('thats-me-11');
+
+ act(() => {
+ fireEvent.press(button);
+ fireEvent.press(button);
+ });
+
+ expect(onClaim).toHaveBeenCalledTimes(1);
+ expect(getByText('Got it')).toBeTruthy();
+ });
+
+ it('stays on the list when the claim fails', () => {
+ const onClaim = jest.fn(() => false);
+ const { getByTestId, queryByText } = render(<ThatsMeSheet {...baseProps} onClaim={onClaim} />);
+
+ fireEvent.press(getByTestId('thats-me-11'));
+
+ expect(queryByText('Got it')).toBeNull();
+ expect(getByTestId('thats-me-read-script')).toBeTruthy();
+ });
+
+ it('offers only the script when no speaker has a sample', () => {
+ const { getByTestId, queryByTestId, getByText } = render(
+ <ThatsMeSheet {...baseProps} candidates={[]} />,
+ );
+
+ expect(queryByTestId('thats-me-11')).toBeNull();
+ expect(
+ getByText(
+ "There's no voice sample from this meeting to use. Read a short script instead; it takes about 20 seconds.",
+ ),
+ ).toBeTruthy();
+ fireEvent.press(getByTestId('thats-me-read-script'));
+ expect(baseProps.onReadScript).toHaveBeenCalled();
+ });
+
+ it('shows the privacy note that the tap agrees to above the speakers', () => {
+ const { getByText, toJSON } = render(<ThatsMeSheet {...baseProps} />);
+ const note =
+ "We'll use your voice from this meeting to make a voice profile. It stays on this device and is only used to recognize you in meetings you record. You can delete it in Voice Profiles.";
+ expect(getByText(note)).toBeTruthy();
+ // Visible before any speaker, so it's on screen when you tap That's me.
+ const rendered = JSON.stringify(toJSON());
+ expect(rendered.indexOf(note)).toBeLessThan(rendered.indexOf('Speaker 2'));
+ });
+
+ it('tells the screen once the sheet has finished closing', () => {
+ const onDismissed = jest.fn();
+ const { UNSAFE_getByType } = render(<ThatsMeSheet {...baseProps} onDismissed={onDismissed} />);
+
+ UNSAFE_getByType(require('react-native').Modal).props.onDismiss();
+
+ expect(onDismissed).toHaveBeenCalledTimes(1);
+ });
+});
diff --git a/openwhispr-mobile/src/data/local/__tests__/notesRepository.audioOwnership.test.ts b/openwhispr-mobile/src/data/local/__tests__/notesRepository.audioOwnership.test.ts
index a1efb51b4..3c58327ec 100644
--- a/openwhispr-mobile/src/data/local/__tests__/notesRepository.audioOwnership.test.ts
+++ b/openwhispr-mobile/src/data/local/__tests__/notesRepository.audioOwnership.test.ts
@@ -3,6 +3,8 @@ jest.mock('expo-file-system/legacy', () => ({
deleteAsync: jest.fn().mockResolvedValue(undefined),
}));
import * as FileSystem from 'expo-file-system/legacy';
+import { eq } from 'drizzle-orm';
+import { notes } from '@/db/schema';
import { createMemoryRepository } from './testDb';
beforeEach(() => {
@@ -28,3 +30,26 @@ it('removes the recording owned by the deleted note', () => {
repo.hardDeleteNote(note.id);
expect(FileSystem.deleteAsync).toHaveBeenCalledWith(uri, { idempotent: true });
});
+
+it('restores a missing recording path without moving the note in lists', () => {
+ const { repo, db } = createMemoryRepository();
+ const note = repo.createNote('Local', '');
+ db.update(notes).set({ updatedAt: '2026-01-01 00:00:00' }).where(eq(notes.id, note.id)).run();
+ const uri = `${FileSystem.documentDirectory}meeting-${note.id}.wav`;
+
+ repo.restoreMeetingRecordingPath(note.id, uri);
+
+ expect(repo.getNoteById(note.id)).toEqual(
+ expect.objectContaining({ sourceFile: uri, updatedAt: '2026-01-01 00:00:00' }),
+ );
+});
+
+it('never replaces a recording path a note already has', () => {
+ const { repo } = createMemoryRepository();
+ const note = repo.createNote('Remote', '');
+ repo.updateNoteMeta(note.id, { sourceFile: 'https://cdn.example/a.wav' });
+
+ repo.restoreMeetingRecordingPath(note.id, `${FileSystem.documentDirectory}meeting-1.wav`);
+
+ expect(repo.getNoteById(note.id)?.sourceFile).toBe('https://cdn.example/a.wav');
+});
diff --git a/openwhispr-mobile/src/data/local/__tests__/notesRepository.remoteTranscript.test.ts b/openwhispr-mobile/src/data/local/__tests__/notesRepository.remoteTranscript.test.ts
index 8e922510a..b44663fd3 100644
--- a/openwhispr-mobile/src/data/local/__tests__/notesRepository.remoteTranscript.test.ts
+++ b/openwhispr-mobile/src/data/local/__tests__/notesRepository.remoteTranscript.test.ts
@@ -1,3 +1,7 @@
+jest.mock('expo-file-system/legacy', () => ({
+ documentDirectory: 'file:///app/documents/',
+ deleteAsync: jest.fn().mockResolvedValue(undefined),
+}));
import { eq } from 'drizzle-orm';
import { notes, speakers, transcriptSegments } from '@/db/schema';
import { serializeSegmentsForSync } from '@/lib/notes/remoteTranscript';
@@ -314,6 +318,40 @@ describe('applyRemoteNote transcript integration', () => {
expect(seg[0].text).toBe('Only one line now.');
});
+ it('keeps the recording this device holds when the pull echoes back a null source file', () => {
+ const { repo, db } = createMemoryRepository();
+ repo.applyRemoteNote(remoteNote({ transcript: desktopRaw }), noFolder);
+ const [note] = repo.getAllNotes();
+ const recording = `file:///app/documents/meeting-${note.id}.wav`;
+ db.update(notes).set({ sourceFile: recording }).where(eq(notes.id, note.id)).run();
+
+ repo.applyRemoteNote(remoteNote({ updated_at: '2026-07-09T11:00:00.000Z' }), noFolder);
+ expect(repo.getNoteById(note.id)?.sourceFile).toBe(recording);
+
+ repo.applyRemoteNote(
+ remoteNote({
+ source_file: 'https://cdn.example/a.wav',
+ updated_at: '2026-07-09T12:00:00.000Z',
+ }),
+ noFolder,
+ );
+ expect(repo.getNoteById(note.id)?.sourceFile).toBe('https://cdn.example/a.wav');
+ });
+
+ it.each([
+ ['a file name another device stored', 'file-1.wav'],
+ ["a file this device doesn't manage", 'file:///app/documents/imported.wav'],
+ ])('clears %s when the pull echoes back a null source file', (_case, sourceFile) => {
+ const { repo, db } = createMemoryRepository();
+ repo.applyRemoteNote(remoteNote({ transcript: desktopRaw }), noFolder);
+ const [note] = repo.getAllNotes();
+ db.update(notes).set({ sourceFile }).where(eq(notes.id, note.id)).run();
+
+ repo.applyRemoteNote(remoteNote({ updated_at: '2026-07-09T11:00:00.000Z' }), noFolder);
+
+ expect(repo.getNoteById(note.id)?.sourceFile).toBeNull();
+ });
+
it('does not rebuild when the transcript is unchanged (segment ids stable)', () => {
const { repo } = createMemoryRepository();
repo.applyRemoteNote(remoteNote({ transcript: desktopRaw }), noFolder);
diff --git a/openwhispr-mobile/src/data/local/__tests__/notesRepository.voiceProfiles.test.ts b/openwhispr-mobile/src/data/local/__tests__/notesRepository.voiceProfiles.test.ts
index dd1f4a25d..17a2238b0 100644
--- a/openwhispr-mobile/src/data/local/__tests__/notesRepository.voiceProfiles.test.ts
+++ b/openwhispr-mobile/src/data/local/__tests__/notesRepository.voiceProfiles.test.ts
@@ -284,6 +284,162 @@ describe('LocalNotesRepository speaker profiles', () => {
);
});
+ it('creates the owner profile and links the speaker in one write', () => {
+ const { repo, db } = createMemoryRepository();
+ const note = createMeeting(db);
+ const spkr = createSpeaker(db, { noteId: note.id, speakerLabel: 'SPEAKER_00' });
+
+ const profile = repo.createOwnerProfileForSpeaker(
+ spkr.id,
+ {
+ displayName: 'Me',
+ embedding: [0.1, 0.2],
+ sampleCount: 1,
+ consentAt: '2026-06-19T00:00:00.000Z',
+ },
+ { displayName: 'Me', speakerStatus: 'locked', speakerLocked: 1, speakerLockSource: 'user' },
+ );
+
+ expect(profile).toEqual(expect.objectContaining({ displayName: 'Me', isOwner: 1 }));
+ const speakerRow = db.select().from(speakers).where(eq(speakers.id, spkr.id)).get();
+ expect(speakerRow).toEqual(
+ expect.objectContaining({
+ displayName: 'Me',
+ speakerStatus: 'locked',
+ speakerLocked: 1,
+ speakerLockSource: 'user',
+ profileId: profile.id,
+ pendingSync: 1,
+ }),
+ );
+ expect(repo.getSpeakerProfiles()).toHaveLength(1);
+ });
+
+ const claimInput = {
+ displayName: 'Me',
+ embedding: [0.6, 0.8],
+ sampleCount: 1,
+ consentAt: '2026-06-19T00:00:00.000Z',
+ };
+
+ it('refuses with the owner error, and links nothing, when an owner profile already exists', () => {
+ const { repo, db } = createMemoryRepository();
+ const note = createMeeting(db);
+ const spkr = createSpeaker(db, { noteId: note.id, speakerLabel: 'SPEAKER_00' });
+ repo.createSpeakerProfile({ ...claimInput, isOwner: 1, email: null });
+
+ expect(() =>
+ repo.createOwnerProfileForSpeaker(spkr.id, claimInput, { displayName: 'Me' }),
+ ).toThrow(SpeakerProfileOwnerAlreadyExistsError);
+
+ expect(repo.getSpeakerProfiles()).toHaveLength(1);
+ const speakerRow = db.select().from(speakers).where(eq(speakers.id, spkr.id)).get();
+ expect(speakerRow).toEqual(expect.objectContaining({ profileId: null, displayName: null }));
+ });
+
+ it.each([
+ ['queues a shared note for sync', 0, 1],
+ ['leaves a private note out of sync', 1, 0],
+ ])('%s', (_label, isPrivate, expectedPendingSync) => {
+ const { repo, db } = createMemoryRepository();
+ const note = createMeeting(db);
+ db.update(notes).set({ isPrivate, pendingSync: 0 }).where(eq(notes.id, note.id)).run();
+ const spkr = createSpeaker(db, { noteId: note.id, speakerLabel: 'SPEAKER_00' });
+
+ repo.createOwnerProfileForSpeaker(spkr.id, claimInput, { displayName: 'Me' });
+
+ const noteRow = db.select().from(notes).where(eq(notes.id, note.id)).get();
+ expect(noteRow?.pendingSync).toBe(expectedPendingSync);
+ });
+
+ it('rolls back the new owner profile when the same-transaction speaker write fails', () => {
+ const { repo, db } = createMemoryRepository();
+ const note = createMeeting(db);
+ const spkr = createSpeaker(db, { noteId: note.id, speakerLabel: 'SPEAKER_00' });
+
+ expect(() =>
+ repo.createOwnerProfileForSpeaker(
+ spkr.id,
+ {
+ displayName: 'Me',
+ embedding: [0.1, 0.2],
+ sampleCount: 1,
+ consentAt: '2026-06-19T00:00:00.000Z',
+ },
+ // speaker_locked is NOT NULL at the DB level; forcing it to null makes the
+ // second write of the transaction fail so this proves the profile insert
+ // (the first write) is rolled back with it instead of left orphaned —
+ // otherwise the single-owner unique index would permanently block every
+ // future claim once the in-memory meeting sample is gone.
+ { speakerLocked: null } as unknown as Partial<Speaker>,
+ ),
+ ).toThrow();
+
+ expect(repo.getSpeakerProfiles()).toEqual([]);
+ expect(db.select().from(speakerProfiles).all()).toEqual([]);
+ const speakerRow = db.select().from(speakers).where(eq(speakers.id, spkr.id)).get();
+ expect(speakerRow).toEqual(expect.objectContaining({ profileId: null, displayName: null }));
+ });
+
+ it.each([
+ ['does not exist', () => 9999],
+ [
+ 'was deleted',
+ (db: TestDb, noteId: number) => {
+ const spkr = createSpeaker(db, { noteId, speakerLabel: 'SPEAKER_00' });
+ db.update(speakers)
+ .set({ deletedAt: '2026-06-19T00:00:00.000Z' })
+ .where(eq(speakers.id, spkr.id))
+ .run();
+ return spkr.id;
+ },
+ ],
+ ])('saves no owner profile when the speaker %s', (_label, speakerIdFor) => {
+ const { repo, db } = createMemoryRepository();
+ const note = createMeeting(db);
+ const speakerId = speakerIdFor(db, note.id);
+
+ expect(() =>
+ repo.createOwnerProfileForSpeaker(
+ speakerId,
+ {
+ displayName: 'Me',
+ embedding: [0.1, 0.2],
+ sampleCount: 1,
+ consentAt: '2026-06-19T00:00:00.000Z',
+ },
+ { displayName: 'Me' },
+ ),
+ ).toThrow();
+
+ expect(db.select().from(speakerProfiles).all()).toEqual([]);
+ const claimedRows = db
+ .select()
+ .from(speakers)
+ .all()
+ .filter((row) => row.profileId !== null || row.displayName !== null);
+ expect(claimedRows).toEqual([]);
+ });
+
+ it('numbers speakers added by a re-diarization after the ones the note has', () => {
+ const { repo, db } = createMemoryRepository();
+ const note = createMeeting(db);
+ createSpeaker(db, { noteId: note.id, speakerLabel: 'SPEAKER_00', sortOrder: 0 });
+ createSpeaker(db, { noteId: note.id, speakerLabel: 'SPEAKER_01', sortOrder: 1 });
+
+ repo.upsertSpeakers(note.id, [
+ {
+ noteId: note.id,
+ speakerLabel: 'SPEAKER_02',
+ speakerStatus: 'provisional',
+ speakerLocked: 0,
+ },
+ ]);
+
+ const added = repo.getSpeakers(note.id).find((row) => row.speakerLabel === 'SPEAKER_02');
+ expect(added?.sortOrder).toBe(2);
+ });
+
it('hard-deletes voice profiles during account-switch data wipe', () => {
const { repo, db } = createMemoryRepository();
repo.createSpeakerProfile({
diff --git a/openwhispr-mobile/src/data/local/notesRepository.ts b/openwhispr-mobile/src/data/local/notesRepository.ts
index 1b1e69af7..b2cdb08c4 100644
--- a/openwhispr-mobile/src/data/local/notesRepository.ts
+++ b/openwhispr-mobile/src/data/local/notesRepository.ts
@@ -871,7 +871,11 @@ export class LocalNotesRepository implements NotesRepository {
content: remote.content,
folderId: localFolderId,
noteType: remote.note_type,
- sourceFile: remote.source_file,
+ // Device-local recordings push as null, so a null coming back must not erase the
+ // path this device still plays and reprocesses from.
+ sourceFile:
+ remote.source_file ??
+ (isManagedMeetingAudioUri(local.id, local.sourceFile) ? local.sourceFile : null),
audioDurationSeconds: remote.audio_duration_seconds,
calendarEventId: remote.calendar_event_id ?? null,
participants: remote.participants ?? null,
@@ -1460,9 +1464,22 @@ export class LocalNotesRepository implements NotesRepository {
upsertSpeakers(noteId: number, rows: NewSpeaker[]): void {
this.database.transaction((tx) => {
+ // Numbered after the note's existing speakers, so a re-diarization that adds one
+ // never shows a second "Speaker 1".
+ const nextSortOrder = tx
+ .select({ sortOrder: speakers.sortOrder })
+ .from(speakers)
+ .where(and(eq(speakers.noteId, noteId), isNull(speakers.deletedAt)))
+ .all()
+ .reduce((next, row) => Math.max(next, row.sortOrder + 1), 0);
rows.forEach((row, index) => {
tx.insert(speakers)
- .values({ ...row, noteId, sortOrder: row.sortOrder ?? index, pendingSync: 1 })
+ .values({
+ ...row,
+ noteId,
+ sortOrder: row.sortOrder ?? nextSortOrder + index,
+ pendingSync: 1,
+ })
.run();
});
});
@@ -1590,6 +1607,49 @@ export class LocalNotesRepository implements NotesRepository {
}
}
+ createOwnerProfileForSpeaker(
+ speakerId: number,
+ profileInput: Omit<NewSpeakerProfile, 'isOwner'>,
+ speakerPatch: Partial<Speaker>,
+ ): SpeakerProfile {
+ this.validateSpeakerProfileEmbedding(profileInput.embedding);
+
+ let result: { profile: SpeakerProfile; noteId: number };
+ try {
+ result = this.database.transaction((tx) => {
+ const row = tx
+ .insert(speakerProfiles)
+ .values({
+ ...profileInput,
+ isOwner: 1,
+ embedding: encodeSpeakerProfileEmbedding(profileInput.embedding),
+ })
+ .returning()
+ .get();
+ const profile = this.mapSpeakerProfile(row);
+ const [linked] = tx
+ .update(speakers)
+ .set({
+ ...speakerPatch,
+ profileId: profile.id,
+ pendingSync: 1,
+ updatedAt: sql`datetime('now')`,
+ })
+ .where(and(eq(speakers.id, speakerId), isNull(speakers.deletedAt)))
+ .returning({ noteId: speakers.noteId })
+ .all();
+ // Throwing rolls back the insert, so no owner profile is saved without its speaker.
+ if (!linked) throw new Error(`Speaker ${speakerId} not found`);
+ return { profile, noteId: linked.noteId };
+ });
+ } catch (error) {
+ mapSpeakerProfileOwnerConstraint(error);
+ }
+
+ this.markNoteTranscriptDirty(result.noteId);
+ return result.profile;
+ }
+
updateSpeakerProfile(id: number, updates: Partial<SpeakerProfile>): void {
const { embedding, ...rest } = updates;
delete rest.id;
@@ -1680,6 +1740,14 @@ export class LocalNotesRepository implements NotesRepository {
.run();
}
+ restoreMeetingRecordingPath(noteId: number, sourceFile: string): void {
+ this.database
+ .update(notes)
+ .set({ sourceFile })
+ .where(and(eq(notes.id, noteId), isNull(notes.sourceFile)))
+ .run();
+ }
+
updateNoteCalendarContext(noteId: number, updates: MeetingCalendarContextUpdate): void {
const row = this.database
.select({ isPrivate: notes.isPrivate })
diff --git a/openwhispr-mobile/src/data/synced/SyncedNotesRepository.ts b/openwhispr-mobile/src/data/synced/SyncedNotesRepository.ts
index 7f658189c..0fbf717c2 100644
--- a/openwhispr-mobile/src/data/synced/SyncedNotesRepository.ts
+++ b/openwhispr-mobile/src/data/synced/SyncedNotesRepository.ts
@@ -335,6 +335,16 @@ export class SyncedNotesRepository implements NotesRepository {
createSpeakerProfile(input: NewSpeakerProfile): SpeakerProfile {
return this.local.createSpeakerProfile(input);
}
+ createOwnerProfileForSpeaker(
+ speakerId: number,
+ profileInput: Omit<NewSpeakerProfile, 'isOwner'>,
+ speakerPatch: Partial<Speaker>,
+ ): SpeakerProfile {
+ // Also writes the speaker row (see LocalNotesRepository), so nudge sync like updateSpeaker.
+ const profile = this.local.createOwnerProfileForSpeaker(speakerId, profileInput, speakerPatch);
+ requestSync('after-write');
+ return profile;
+ }
updateSpeakerProfile(id: number, updates: Partial<SpeakerProfile>): void {
this.local.updateSpeakerProfile(id, updates);
}
@@ -358,6 +368,10 @@ export class SyncedNotesRepository implements NotesRepository {
this.local.updateNoteMeta(noteId, updates);
}
+ restoreMeetingRecordingPath(noteId: number, sourceFile: string): void {
+ this.local.restoreMeetingRecordingPath(noteId, sourceFile);
+ }
+
updateNoteCalendarContext(noteId: number, updates: MeetingCalendarContextUpdate): void {
const note = this.local.getNoteById(noteId);
this.local.updateNoteCalendarContext(noteId, updates);
diff --git a/openwhispr-mobile/src/data/synced/__tests__/SyncedNotesRepository.transcriptSync.test.ts b/openwhispr-mobile/src/data/synced/__tests__/SyncedNotesRepository.transcriptSync.test.ts
index 6076cf231..d151a053b 100644
--- a/openwhispr-mobile/src/data/synced/__tests__/SyncedNotesRepository.transcriptSync.test.ts
+++ b/openwhispr-mobile/src/data/synced/__tests__/SyncedNotesRepository.transcriptSync.test.ts
@@ -10,6 +10,7 @@ const makeLocal = () =>
({
replaceSegments: jest.fn(),
updateSpeaker: jest.fn(),
+ createOwnerProfileForSpeaker: jest.fn(() => ({ id: 3 })),
mergeSpeakers: jest.fn(),
moveNoteToSpace: jest.fn(),
getNotesBySpace: jest.fn(() => []),
@@ -32,6 +33,19 @@ describe('SyncedNotesRepository — transcript writes are syncable', () => {
expect(mockRequestSync).toHaveBeenCalledWith('after-write');
});
+ it('createOwnerProfileForSpeaker delegates and nudges a debounced sync for the speaker row', () => {
+ const local = makeLocal();
+ const input = { displayName: 'Me', embedding: [1], consentAt: 'now' };
+ const profile = new SyncedNotesRepository(local).createOwnerProfileForSpeaker(5, input, {
+ displayName: 'Me',
+ });
+ expect(local.createOwnerProfileForSpeaker).toHaveBeenCalledWith(5, input, {
+ displayName: 'Me',
+ });
+ expect(profile).toEqual({ id: 3 });
+ expect(mockRequestSync).toHaveBeenCalledWith('after-write');
+ });
+
it('mergeSpeakers delegates and nudges a debounced sync', () => {
const local = makeLocal();
new SyncedNotesRepository(local).mergeSpeakers(1, 2, 3, {});
diff --git a/openwhispr-mobile/src/data/types.ts b/openwhispr-mobile/src/data/types.ts
index c8698d251..e11981cff 100644
--- a/openwhispr-mobile/src/data/types.ts
+++ b/openwhispr-mobile/src/data/types.ts
@@ -407,6 +407,17 @@ export interface NotesRepository {
getSpeakerProfiles(): SpeakerProfile[];
getSpeakerProfileById(id: number): SpeakerProfile | null;
createSpeakerProfile(input: NewSpeakerProfile): SpeakerProfile;
+ /**
+ * Atomically creates the owner speaker profile and applies `speakerPatch` (plus the new
+ * profile's id) to the speaker row in one transaction, so a failure partway through never
+ * leaves an owner profile with no speaker linked to it, which would block every later
+ * claim until the profile was deleted. Used by the "claim this speaker as me" flow.
+ */
+ createOwnerProfileForSpeaker(
+ speakerId: number,
+ profileInput: Omit<NewSpeakerProfile, 'isOwner'>,
+ speakerPatch: Partial<Speaker>,
+ ): SpeakerProfile;
updateSpeakerProfile(id: number, updates: Partial<SpeakerProfile>): void;
deleteSpeakerProfile(id: number): void;
deleteAllSpeakerProfiles(): void;
@@ -418,6 +429,8 @@ export interface NotesRepository {
setTranscriptionStatus(noteId: number, status: TranscriptionStatus): void;
/** Update local-only meeting/diarization columns NoteUpdate cannot reach. */
updateNoteMeta(noteId: number, updates: MeetingNoteUpdate): void;
+ /** Points a meeting with no recording path back at its recording, keeping its place in lists. */
+ restoreMeetingRecordingPath(noteId: number, sourceFile: string): void;
/** Update selected calendar context; public notes mark pending sync, private notes stay local-only. */
updateNoteCalendarContext(noteId: number, updates: MeetingCalendarContextUpdate): void;
}
diff --git a/openwhispr-mobile/src/lib/__tests__/meetingRecovery.test.ts b/openwhispr-mobile/src/lib/__tests__/meetingRecovery.test.ts
index 2d947b596..60d4cee86 100644
--- a/openwhispr-mobile/src/lib/__tests__/meetingRecovery.test.ts
+++ b/openwhispr-mobile/src/lib/__tests__/meetingRecovery.test.ts
@@ -1,4 +1,5 @@
jest.mock('expo-sqlite/localStorage/install', () => ({}));
+jest.mock('expo-file-system/legacy', () => ({ documentDirectory: 'file:///docs/' }));
jest.mock('@/data', () => ({ notesRepository: { getAllNotes: jest.fn(() => []) } }));
jest.mock('@/store/useNotesStore', () => ({ useNotesStore: { getState: jest.fn() } }));
jest.mock('@/lib/sentry', () => ({ Sentry: { captureException: jest.fn() } }));
@@ -13,6 +14,7 @@ import {
MEETING_RESUME_MARKER_PREFIX,
createdBeforeRuntime,
recoverOrphanedMeetings,
+ repairMeetingRecordings,
waitForAppActive,
type MeetingRecoveryDeps,
} from '@/lib/meetingRecovery';
@@ -253,3 +255,44 @@ describe('createdBeforeRuntime', () => {
).toBe(true);
});
});
+
+describe('repairMeetingRecordings', () => {
+ const run = async (files: string[], notes: Note[]) => {
+ const deps = {
+ listRecordingFiles: jest.fn(async () => files),
+ getNote: jest.fn((id: number) => notes.find((candidate) => candidate.id === id) ?? null),
+ recordingUri: (id: number) => `file:///docs/meeting-${id}.wav`,
+ restorePath: jest.fn(),
+ deleteFile: jest.fn(),
+ };
+ await repairMeetingRecordings(deps);
+ return deps;
+ };
+
+ it('points a meeting whose path a sync erased back at its recording', async () => {
+ const deps = await run(['meeting-4.wav'], [note({ id: 4, sourceFile: null })]);
+
+ expect(deps.restorePath).toHaveBeenCalledWith(4, 'file:///docs/meeting-4.wav');
+ expect(deps.deleteFile).not.toHaveBeenCalled();
+ });
+
+ it('deletes a recording whose note is gone for good', async () => {
+ const deps = await run(['meeting-9.wav'], []);
+
+ expect(deps.deleteFile).toHaveBeenCalledWith('file:///docs/meeting-9.wav');
+ expect(deps.restorePath).not.toHaveBeenCalled();
+ });
+
+ it('leaves notes that still point somewhere, other note types, and other files alone', async () => {
+ const deps = await run(
+ ['meeting-4.wav', 'meeting-5.wav', 'SQLite', 'meeting-x.wav', 'meeting-6.wav.tmp'],
+ [
+ note({ id: 4 }),
+ note({ id: 5, noteType: 'upload', sourceFile: null } as Partial<Note> & { id: number }),
+ ],
+ );
+
+ expect(deps.restorePath).not.toHaveBeenCalled();
+ expect(deps.deleteFile).not.toHaveBeenCalled();
+ });
+});
diff --git a/openwhispr-mobile/src/lib/__tests__/voiceEnrollmentMessages.test.ts b/openwhispr-mobile/src/lib/__tests__/voiceEnrollmentMessages.test.ts
new file mode 100644
index 000000000..81656e4ad
--- /dev/null
+++ b/openwhispr-mobile/src/lib/__tests__/voiceEnrollmentMessages.test.ts
@@ -0,0 +1,121 @@
+import {
+ VOICE_ENROLLMENT_DIARIZATION_FAILED,
+ VOICE_ENROLLMENT_LOW_SNR,
+ VOICE_ENROLLMENT_LOW_SPEECH_RATIO,
+ VOICE_ENROLLMENT_MULTIPLE_SPEAKERS,
+ VOICE_ENROLLMENT_NO_MEANINGFUL_SPEAKER,
+ VOICE_ENROLLMENT_NO_SPEECH,
+ VOICE_ENROLLMENT_PROFILE_NOT_FOUND,
+ VOICE_ENROLLMENT_SHORT_SPEECH,
+ VoiceEnrollmentError,
+ type EnrollmentQualityFailureCode,
+} from '@/services/diarization/VoiceprintService';
+import {
+ VOICE_ENROLLMENT_UNEXPLAINED_FAILURE,
+ voiceEnrollmentFailureMessage,
+} from '../voiceEnrollmentMessages';
+
+const failure = (code: ConstructorParameters<typeof VoiceEnrollmentError>[0]) =>
+ voiceEnrollmentFailureMessage(new VoiceEnrollmentError(code, 'raw service text'));
+
+describe('voiceEnrollmentFailureMessage', () => {
+ it('asks for the whole script when too little speech was heard', () => {
+ expect(failure(VOICE_ENROLLMENT_SHORT_SPEECH)).toBe(
+ "We didn't hear enough. Read the whole script.",
+ );
+ expect(failure(VOICE_ENROLLMENT_NO_MEANINGFUL_SPEAKER)).toBe(
+ "We couldn't pick out your voice. Read the whole script with your phone close by.",
+ );
+ });
+
+ it('points at the microphone when nothing was heard at all', () => {
+ expect(failure(VOICE_ENROLLMENT_NO_SPEECH)).toBe(
+ "We couldn't hear you. Check that nothing is covering the microphone, then try again.",
+ );
+ });
+
+ it('says so when the profile being retrained was deleted', () => {
+ expect(failure(VOICE_ENROLLMENT_PROFILE_NOT_FOUND)).toBe(
+ 'This voice profile was deleted. Go back to Voice Profiles to add it again.',
+ );
+ });
+
+ it('explains noise, pauses and extra voices', () => {
+ expect(failure(VOICE_ENROLLMENT_LOW_SNR)).toBe(
+ 'Too much background noise. Try somewhere quieter.',
+ );
+ expect(failure(VOICE_ENROLLMENT_LOW_SPEECH_RATIO)).toBe(
+ 'Lots of pauses. Read at your normal pace.',
+ );
+ expect(failure(VOICE_ENROLLMENT_MULTIPLE_SPEAKERS)).toBe(
+ 'We heard more than one voice. Try again on your own.',
+ );
+ });
+
+ describe('when the recording was quiet', () => {
+ const levelFailure = (code: EnrollmentQualityFailureCode, peakDb: number) =>
+ voiceEnrollmentFailureMessage(
+ new VoiceEnrollmentError(code, 'raw service text', {
+ ok: false,
+ code,
+ reason: 'raw service text',
+ speechActivity: {
+ durationMs: 16_906,
+ analyzedMs: 16_906,
+ speechActivityMs: 3_397,
+ speechRatio: 0.2,
+ peakDb,
+ averageDb: -34,
+ noiseFloorDb: -37.3,
+ thresholdDb: -30,
+ noSpeechLikely: false,
+ reason: 'speech_activity_detected',
+ confidence: 0.65,
+ },
+ }),
+ );
+ const quiet = 'We could barely hear you. Speak up or hold your phone closer.';
+
+ it('blames the volume, not pauses or length', () => {
+ // Levels logged from a simulator read through a Mac mic at 29% input volume.
+ expect(levelFailure(VOICE_ENROLLMENT_LOW_SPEECH_RATIO, -22.6)).toBe(quiet);
+ expect(levelFailure(VOICE_ENROLLMENT_SHORT_SPEECH, -22.6)).toBe(quiet);
+ expect(levelFailure(VOICE_ENROLLMENT_NO_MEANINGFUL_SPEAKER, -22.6)).toBe(quiet);
+ });
+
+ it('keeps the pause and length messages when the voice was loud enough', () => {
+ expect(levelFailure(VOICE_ENROLLMENT_LOW_SPEECH_RATIO, -9.4)).toBe(
+ 'Lots of pauses. Read at your normal pace.',
+ );
+ expect(levelFailure(VOICE_ENROLLMENT_SHORT_SPEECH, -9.4)).toBe(
+ "We didn't hear enough. Read the whole script.",
+ );
+ expect(levelFailure(VOICE_ENROLLMENT_NO_MEANINGFUL_SPEAKER, -9.4)).toBe(
+ "We couldn't pick out your voice. Read the whole script with your phone close by.",
+ );
+ });
+
+ it('draws the line at a loudest moment of -18 dB', () => {
+ expect(levelFailure(VOICE_ENROLLMENT_LOW_SPEECH_RATIO, -18)).toBe(
+ 'Lots of pauses. Read at your normal pace.',
+ );
+ expect(levelFailure(VOICE_ENROLLMENT_LOW_SPEECH_RATIO, -18.1)).toBe(quiet);
+ });
+
+ it("keeps the noise and second-voice messages, which a quiet voice doesn't explain", () => {
+ expect(levelFailure(VOICE_ENROLLMENT_LOW_SNR, -30)).toBe(
+ 'Too much background noise. Try somewhere quieter.',
+ );
+ expect(levelFailure(VOICE_ENROLLMENT_MULTIPLE_SPEAKERS, -30)).toBe(
+ 'We heard more than one voice. Try again on your own.',
+ );
+ });
+ });
+
+ it('falls back to a general message for anything else', () => {
+ const general = VOICE_ENROLLMENT_UNEXPLAINED_FAILURE;
+ expect(failure(VOICE_ENROLLMENT_DIARIZATION_FAILED)).toBe(general);
+ expect(voiceEnrollmentFailureMessage(new Error('boom'))).toBe(general);
+ expect(voiceEnrollmentFailureMessage(null)).toBe(general);
+ });
+});
diff --git a/openwhispr-mobile/src/lib/meetingRecovery.ts b/openwhispr-mobile/src/lib/meetingRecovery.ts
index eef660eaf..7d2eb4db0 100644
--- a/openwhispr-mobile/src/lib/meetingRecovery.ts
+++ b/openwhispr-mobile/src/lib/meetingRecovery.ts
@@ -1,5 +1,6 @@
import 'expo-sqlite/localStorage/install';
import { AppState, type AppStateStatus } from 'react-native';
+import * as FileSystem from 'expo-file-system/legacy';
import type { Note } from '@/data';
import { notesRepository } from '@/data';
import { canTransition } from '@/lib/diarization/transcriptionStatus';
@@ -85,6 +86,34 @@ export async function recoverOrphanedMeetings(deps: MeetingRecoveryDeps): Promis
}
}
+const MEETING_RECORDING_FILE = /^meeting-(\d+)\.wav$/;
+
+export interface MeetingRecordingRepairDeps {
+ /** File names in the directory meeting recordings are saved to. */
+ listRecordingFiles(): Promise<string[]>;
+ getNote(noteId: number): Note | null;
+ recordingUri(noteId: number): string;
+ restorePath(noteId: number, uri: string): void;
+ deleteFile(uri: string): void;
+}
+
+/**
+ * Sync pulls used to erase a meeting's recording path, leaving the note with no playback
+ * or Retry and its recording on disk with nothing pointing at it. Points each such note
+ * back at its recording, and deletes recordings whose note is gone for good (hard-deleted
+ * notes can only have deleted their recording through that path).
+ */
+export async function repairMeetingRecordings(deps: MeetingRecordingRepairDeps): Promise<void> {
+ for (const name of await deps.listRecordingFiles()) {
+ const noteId = Number(MEETING_RECORDING_FILE.exec(name)?.[1]);
+ if (!noteId) continue;
+ const note = deps.getNote(noteId);
+ const uri = deps.recordingUri(noteId);
+ if (!note) deps.deleteFile(uri);
+ else if (note.noteType === 'meeting' && note.sourceFile == null) deps.restorePath(noteId, uri);
+ }
+}
+
interface AppStateLike {
currentState: AppStateStatus;
addEventListener(type: 'change', listener: (state: AppStateStatus) => void): { remove(): void };
@@ -129,21 +158,40 @@ let hasStarted = false;
export function startMeetingRecoveryOnce(): void {
if (hasStarted) return;
hasStarted = true;
- recoverOrphanedMeetings({
- listNotes: () =>
- notesRepository
- .getAllNotes()
- .filter((note) => createdBeforeRuntime(note, RUNTIME_STARTED_AT_MS)),
- markFailed: (noteId) => useNotesStore.getState().transitionStatus(noteId, 'failed'),
- waitUntilActive: () => waitForAppActive(AppState),
- resume: (noteId) => useNotesStore.getState().retryMeetingTranscription(noteId),
- storage: localStorage,
- reportError: (error, noteId) =>
- Sentry.captureException(error, {
- tags: { feature: 'meeting-recovery' },
- extra: { noteId },
+ const directory = FileSystem.documentDirectory;
+ // A recording found again can make its meeting resumable, so this runs first.
+ const repaired = directory
+ ? repairMeetingRecordings({
+ listRecordingFiles: () => FileSystem.readDirectoryAsync(directory),
+ getNote: (noteId) => notesRepository.getNoteById(noteId),
+ recordingUri: (noteId) => `${directory}meeting-${noteId}.wav`,
+ restorePath: (noteId, uri) => notesRepository.restoreMeetingRecordingPath(noteId, uri),
+ deleteFile: (uri) => {
+ FileSystem.deleteAsync(uri, { idempotent: true }).catch(() => undefined);
+ },
+ }).catch((error) => {
+ Sentry.captureException(error, { tags: { feature: 'meeting-recovery' } });
+ })
+ : Promise.resolve();
+ repaired
+ .then(() =>
+ recoverOrphanedMeetings({
+ listNotes: () =>
+ notesRepository
+ .getAllNotes()
+ .filter((note) => createdBeforeRuntime(note, RUNTIME_STARTED_AT_MS)),
+ markFailed: (noteId) => useNotesStore.getState().transitionStatus(noteId, 'failed'),
+ waitUntilActive: () => waitForAppActive(AppState),
+ resume: (noteId) => useNotesStore.getState().retryMeetingTranscription(noteId),
+ storage: localStorage,
+ reportError: (error, noteId) =>
+ Sentry.captureException(error, {
+ tags: { feature: 'meeting-recovery' },
+ extra: { noteId },
+ }),
}),
- }).catch((error) => {
- Sentry.captureException(error, { tags: { feature: 'meeting-recovery' } });
- });
+ )
+ .catch((error) => {
+ Sentry.captureException(error, { tags: { feature: 'meeting-recovery' } });
+ });
}
diff --git a/openwhispr-mobile/src/lib/notes/__tests__/voiceSetupPrompt.test.ts b/openwhispr-mobile/src/lib/notes/__tests__/voiceSetupPrompt.test.ts
new file mode 100644
index 000000000..c6cc3aea3
--- /dev/null
+++ b/openwhispr-mobile/src/lib/notes/__tests__/voiceSetupPrompt.test.ts
@@ -0,0 +1,179 @@
+import type { Segment, Speaker } from '@/data/types';
+import {
+ formatSpeakingTime,
+ shouldOfferVoiceSetup,
+ voiceSetupCandidates,
+} from '../voiceSetupPrompt';
+
+const speaker = (overrides: Partial<Speaker> = {}): Speaker =>
+ ({
+ id: 10,
+ noteId: 7,
+ speakerLabel: 'speaker_0',
+ displayName: null,
+ profileId: null,
+ color: null,
+ sortOrder: 0,
+ speakerStatus: 'provisional',
+ speakerLocked: 0,
+ speakerLockSource: null,
+ clientId: null,
+ remoteId: null,
+ deletedAt: null,
+ pendingSync: 0,
+ createdAt: null,
+ updatedAt: null,
+ ...overrides,
+ }) as Speaker;
+
+const segment = (overrides: Partial<Segment> = {}): Segment =>
+ ({
+ id: 1,
+ noteId: 7,
+ startMs: 0,
+ endMs: 6000,
+ text: 'Hello there.',
+ speakerLabel: 'speaker_0',
+ sortOrder: 0,
+ clientId: null,
+ remoteId: null,
+ deletedAt: null,
+ pendingSync: 0,
+ createdAt: null,
+ updatedAt: null,
+ ...overrides,
+ }) as Segment;
+
+const offer = (overrides: Partial<Parameters<typeof shouldOfferVoiceSetup>[0]> = {}) =>
+ shouldOfferVoiceSetup({
+ isOnDeviceMeeting: true,
+ transcriptStatus: 'done',
+ hasOwnerProfile: false,
+ dismissed: false,
+ candidateCount: 1,
+ ...overrides,
+ });
+
+describe('shouldOfferVoiceSetup', () => {
+ it('offers setup on a finished on-device meeting with a speaker you can claim', () => {
+ expect(offer()).toBe(true);
+ });
+
+ it('never offers it for a meeting not recorded on this device', () => {
+ expect(offer({ isOnDeviceMeeting: false })).toBe(false);
+ });
+
+ it('waits until the transcript is done', () => {
+ expect(offer({ transcriptStatus: 'diarizing' })).toBe(false);
+ });
+
+ it('stops once you have a voice profile or dismissed the banner', () => {
+ expect(offer({ hasOwnerProfile: true })).toBe(false);
+ expect(offer({ dismissed: true })).toBe(false);
+ });
+
+ it('stays hidden when no speaker can be claimed, such as after a restart', () => {
+ expect(offer({ candidateCount: 0 })).toBe(false);
+ });
+});
+
+describe('voiceSetupCandidates', () => {
+ const speakers = [
+ speaker({ id: 10, speakerLabel: 'speaker_0', sortOrder: 0 }),
+ speaker({ id: 11, speakerLabel: 'speaker_1', sortOrder: 1 }),
+ ];
+ const segments = [
+ segment({ id: 1, speakerLabel: 'speaker_0', startMs: 0, endMs: 6000, text: 'Short.' }),
+ segment({
+ id: 2,
+ speakerLabel: 'speaker_0',
+ startMs: 6000,
+ endMs: 12000,
+ text: 'The longest thing I said today.',
+ }),
+ segment({
+ id: 3,
+ speakerLabel: 'speaker_1',
+ startMs: 12000,
+ endMs: 40000,
+ text: 'I talked for a long while.',
+ }),
+ ];
+ const embeddingsByLabel = { speaker_0: [0.1, 0.2], speaker_1: [0.3, 0.4] };
+ const profileIds = new Set<number>([4]);
+ const candidates = (
+ overrides: Partial<Parameters<typeof voiceSetupCandidates>[0]> = {},
+ ): ReturnType<typeof voiceSetupCandidates> =>
+ voiceSetupCandidates({ segments, speakers, embeddingsByLabel, profileIds, ...overrides });
+
+ it('lists speakers with 10 s of speech and a sample, longest speaker first', () => {
+ expect(candidates()).toEqual([
+ {
+ speakerId: 11,
+ name: 'Speaker 2',
+ speechMs: 28000,
+ sampleLine: 'I talked for a long while.',
+ },
+ {
+ speakerId: 10,
+ name: 'Speaker 1',
+ speechMs: 12000,
+ sampleLine: 'The longest thing I said today.',
+ },
+ ]);
+ });
+
+ it('leaves out speakers under 10 s of speech', () => {
+ const short = [segment({ speakerLabel: 'speaker_0', startMs: 0, endMs: 9999 })];
+ expect(candidates({ segments: short })).toEqual([]);
+ });
+
+ it('includes a speaker with exactly 10 s of speech', () => {
+ const enough = [segment({ speakerLabel: 'speaker_0', startMs: 0, endMs: 10_000 })];
+ expect(candidates({ segments: enough }).map((candidate) => candidate.speakerId)).toEqual([10]);
+ });
+
+ it('leaves out speakers without a sample from this meeting', () => {
+ expect(candidates({ embeddingsByLabel: { speaker_1: [] } })).toEqual([]);
+ expect(candidates({ embeddingsByLabel: undefined })).toEqual([]);
+ });
+
+ it('leaves out speakers already linked to a voice profile', () => {
+ const linked = [speaker({ id: 11, speakerLabel: 'speaker_1', profileId: 4 })];
+ expect(candidates({ speakers: linked })).toEqual([]);
+ });
+
+ it('lists a speaker whose voice profile was deleted, so it can be claimed again', () => {
+ const orphaned = [
+ speaker({ id: 11, speakerLabel: 'speaker_1', sortOrder: 1, displayName: 'Me', profileId: 9 }),
+ ];
+ expect(candidates({ speakers: orphaned }).map((candidate) => candidate.speakerId)).toEqual([
+ 11,
+ ]);
+ });
+
+ it('lists a speaker you already named, by that name', () => {
+ const named = [speaker({ id: 11, speakerLabel: 'speaker_1', displayName: 'Chad' })];
+ expect(candidates({ speakers: named }).map((candidate) => candidate.name)).toEqual(['Chad']);
+ });
+
+ it('leaves out a sample that could never match anyone', () => {
+ expect(candidates({ embeddingsByLabel: { speaker_0: [0, 0], speaker_1: [NaN, 1] } })).toEqual(
+ [],
+ );
+ });
+
+ it('names speakers the way the transcript does, even when an earlier one is left out', () => {
+ const [only] = candidates({ embeddingsByLabel: { speaker_1: [0.3, 0.4] } });
+ expect(only.name).toBe('Speaker 2');
+ });
+});
+
+describe('formatSpeakingTime', () => {
+ it('reads as a duration', () => {
+ expect(formatSpeakingTime(12_000)).toBe('12 sec');
+ expect(formatSpeakingTime(59_400)).toBe('59 sec');
+ expect(formatSpeakingTime(65_000)).toBe('1 min');
+ expect(formatSpeakingTime(28 * 60_000)).toBe('28 min');
+ });
+});
diff --git a/openwhispr-mobile/src/lib/notes/voiceSetupPrompt.ts b/openwhispr-mobile/src/lib/notes/voiceSetupPrompt.ts
new file mode 100644
index 000000000..b9f4265a7
--- /dev/null
+++ b/openwhispr-mobile/src/lib/notes/voiceSetupPrompt.ts
@@ -0,0 +1,85 @@
+import type { Segment, Speaker } from '@/data/types';
+import { getSpeakerDisplayName } from '@/lib/diarization/transcriptDisplay';
+import { hasFiniteNonZeroNorm } from '@/lib/diarization/voiceprints';
+import { ENROLLMENT_MIN_SPEECH_ACTIVITY_MS } from '@/services/diarization/VoiceprintService';
+
+export interface VoiceSetupCandidate {
+ speakerId: number;
+ name: string;
+ speechMs: number;
+ sampleLine: string;
+}
+
+/**
+ * The meeting-note banner. It only shows when That's me can work: the meeting was diarized
+ * on this device and a speaker's sample is still held from it (samples don't survive a
+ * restart), so it never opens onto an empty sheet.
+ */
+export function shouldOfferVoiceSetup({
+ isOnDeviceMeeting,
+ transcriptStatus,
+ hasOwnerProfile,
+ dismissed,
+ candidateCount,
+}: {
+ isOnDeviceMeeting: boolean;
+ transcriptStatus: string;
+ hasOwnerProfile: boolean;
+ dismissed: boolean;
+ candidateCount: number;
+}): boolean {
+ return (
+ isOnDeviceMeeting &&
+ transcriptStatus === 'done' &&
+ !hasOwnerProfile &&
+ !dismissed &&
+ candidateCount > 0
+ );
+}
+
+/**
+ * Speakers who said enough, with a sample still held from this meeting, longest first. A
+ * speaker still pointing at a deleted profile counts as unlinked, so you can claim it again.
+ */
+export function voiceSetupCandidates({
+ segments,
+ speakers,
+ embeddingsByLabel,
+ profileIds,
+}: {
+ segments: Segment[];
+ speakers: Speaker[];
+ embeddingsByLabel: Record<string, number[]> | undefined;
+ profileIds: ReadonlySet<number>;
+}): VoiceSetupCandidate[] {
+ return speakers
+ .filter(
+ (speaker) =>
+ (speaker.profileId == null || !profileIds.has(speaker.profileId)) &&
+ hasFiniteNonZeroNorm(embeddingsByLabel?.[speaker.speakerLabel]),
+ )
+ .map((speaker) => {
+ const own = segments.filter((segment) => segment.speakerLabel === speaker.speakerLabel);
+ const speechMs = own.reduce(
+ (total, segment) => total + Math.max(0, segment.endMs - segment.startMs),
+ 0,
+ );
+ const sampleLine = own
+ .map((segment) => segment.text.trim())
+ .reduce((longest, text) => (text.length > longest.length ? text : longest), '');
+ return {
+ speakerId: speaker.id,
+ name: getSpeakerDisplayName(speaker, speaker.sortOrder),
+ speechMs,
+ sampleLine,
+ };
+ })
+ .filter((candidate) => candidate.speechMs >= ENROLLMENT_MIN_SPEECH_ACTIVITY_MS)
+ .sort((a, b) => b.speechMs - a.speechMs);
+}
+
+/** "45 sec", "3 min": how long someone spoke, not a position in the recording. */
+export function formatSpeakingTime(ms: number): string {
+ const seconds = Math.round(ms / 1000);
+ return seconds < 60 ? `${seconds} sec` : `${Math.round(seconds / 60)} min`;
+}
diff --git a/openwhispr-mobile/src/lib/voiceEnrollmentMessages.ts b/openwhispr-mobile/src/lib/voiceEnrollmentMessages.ts
new file mode 100644
index 000000000..fca741889
--- /dev/null
+++ b/openwhispr-mobile/src/lib/voiceEnrollmentMessages.ts
@@ -0,0 +1,64 @@
+import {
+ VOICE_ENROLLMENT_LOW_SNR,
+ VOICE_ENROLLMENT_LOW_SPEECH_RATIO,
+ VOICE_ENROLLMENT_MULTIPLE_SPEAKERS,
+ VOICE_ENROLLMENT_NO_MEANINGFUL_SPEAKER,
+ VOICE_ENROLLMENT_NO_SPEECH,
+ VOICE_ENROLLMENT_PROFILE_NOT_FOUND,
+ VOICE_ENROLLMENT_SHORT_SPEECH,
+ VoiceEnrollmentError,
+} from '@/services/diarization/VoiceprintService';
+
+/** Shown for failures we can't explain; callers report these so they can be diagnosed. */
+export const VOICE_ENROLLMENT_UNEXPLAINED_FAILURE =
+ 'Something went wrong checking your voice. Try again.';
+
+/** Alert title and message for teaching your voice when you already have a profile. */
+export const VOICE_ALREADY_TAUGHT_ALERT = [
+ "You've already taught OpenWhispr your voice",
+ 'Open it in Voice Profiles and choose Retrain Voice.',
+] as const;
+
+// The speech detector never sets its bar above -30 dB, so a voice whose loudest moment
+// stays below this reads as pauses or too little speech. Measured: failed reads peaked
+// at -22 to -24 dB, a normal read at -9 dB.
+const QUIET_PEAK_DB = -18;
+
+const LEVEL_FAILURES = new Set<string>([
+ VOICE_ENROLLMENT_LOW_SPEECH_RATIO,
+ VOICE_ENROLLMENT_SHORT_SPEECH,
+ VOICE_ENROLLMENT_NO_MEANINGFUL_SPEAKER,
+]);
+
+const wasTooQuiet = (error: VoiceEnrollmentError): boolean => {
+ const quality = error.quality;
+ const peakDb = quality && !quality.ok ? quality.speechActivity?.peakDb : undefined;
+ return peakDb !== undefined && peakDb < QUIET_PEAK_DB;
+};
+
+/** What to tell someone whose voice sample failed, in words they can act on. */
+export function voiceEnrollmentFailureMessage(error: unknown): string {
+ if (!(error instanceof VoiceEnrollmentError)) return VOICE_ENROLLMENT_UNEXPLAINED_FAILURE;
+ const { code } = error;
+ if (LEVEL_FAILURES.has(code) && wasTooQuiet(error)) {
+ return 'We could barely hear you. Speak up or hold your phone closer.';
+ }
+ switch (code) {
+ case VOICE_ENROLLMENT_NO_SPEECH:
+ return "We couldn't hear you. Check that nothing is covering the microphone, then try again.";
+ case VOICE_ENROLLMENT_SHORT_SPEECH:
+ return "We didn't hear enough. Read the whole script.";
+ case VOICE_ENROLLMENT_NO_MEANINGFUL_SPEAKER:
+ return "We couldn't pick out your voice. Read the whole script with your phone close by.";
+ case VOICE_ENROLLMENT_LOW_SNR:
+ return 'Too much background noise. Try somewhere quieter.';
+ case VOICE_ENROLLMENT_LOW_SPEECH_RATIO:
+ return 'Lots of pauses. Read at your normal pace.';
+ case VOICE_ENROLLMENT_MULTIPLE_SPEAKERS:
+ return 'We heard more than one voice. Try again on your own.';
+ case VOICE_ENROLLMENT_PROFILE_NOT_FOUND:
+ return 'This voice profile was deleted. Go back to Voice Profiles to add it again.';
+ default:
+ return VOICE_ENROLLMENT_UNEXPLAINED_FAILURE;
+ }
+}
diff --git a/openwhispr-mobile/src/screens/DiarizationModelScreen.tsx b/openwhispr-mobile/src/screens/DiarizationModelScreen.tsx
index c83e2cdeb..1955c92b5 100644
--- a/openwhispr-mobile/src/screens/DiarizationModelScreen.tsx
+++ b/openwhispr-mobile/src/screens/DiarizationModelScreen.tsx
@@ -17,22 +17,42 @@ export default function DiarizationModelScreen() {
const isDiarizerAvailable = useNotesStore((state) => state.isDiarizerAvailable);
const isDiarizerModelReady = useNotesStore((state) => state.isDiarizerModelReady);
const downloadDiarizerModel = useNotesStore((state) => state.downloadDiarizerModel);
+ const isDiarizerModelDownloading = useNotesStore((state) => state.isDiarizerModelDownloading);
const deleteDiarizerModel = useNotesStore((state) => state.deleteDiarizerModel);
const [status, setStatus] = useState<ModelStatus>('checking');
const [error, setError] = useState<string | null>(null);
+ // Starts the download, or follows one another screen already started.
+ const download = useCallback(async () => {
+ setError(null);
+ setStatus('downloading');
+ try {
+ await downloadDiarizerModel();
+ safeHaptics('success');
+ setStatus('ready');
+ } catch (caught) {
+ setError(caught instanceof Error ? caught.message : 'Download failed');
+ setStatus('error');
+ Sentry.captureException(caught, { tags: { feature: 'diarization-model-settings' } });
+ }
+ }, [downloadDiarizerModel]);
+
const refresh = useCallback(async () => {
try {
if (!(await isDiarizerAvailable())) {
setStatus('unavailable');
return;
}
+ if (isDiarizerModelDownloading()) {
+ await download();
+ return;
+ }
setStatus((await isDiarizerModelReady()) ? 'ready' : 'idle');
} catch (caught) {
setStatus('unavailable');
Sentry.captureException(caught, { tags: { feature: 'diarization-model-settings' } });
}
- }, [isDiarizerAvailable, isDiarizerModelReady]);
+ }, [download, isDiarizerAvailable, isDiarizerModelDownloading, isDiarizerModelReady]);
useEffect(() => {
refresh();
@@ -40,18 +60,8 @@ export default function DiarizationModelScreen() {
const handleDownload = useCallback(async () => {
safeHaptics('light');
- setError(null);
- setStatus('downloading');
- try {
- await downloadDiarizerModel();
- safeHaptics('success');
- setStatus('ready');
- } catch (caught) {
- setError(caught instanceof Error ? caught.message : 'Download failed');
- setStatus('error');
- Sentry.captureException(caught, { tags: { feature: 'diarization-model-settings' } });
- }
- }, [downloadDiarizerModel]);
+ await download();
+ }, [download]);
const handleDelete = useCallback(() => {
confirmDestructive(
diff --git a/openwhispr-mobile/src/screens/NoteEditorScreen.tsx b/openwhispr-mobile/src/screens/NoteEditorScreen.tsx
index 43af5c0fd..bb42273e0 100644
--- a/openwhispr-mobile/src/screens/NoteEditorScreen.tsx
+++ b/openwhispr-mobile/src/screens/NoteEditorScreen.tsx
@@ -37,6 +37,11 @@ import {
import { NoteShareSheet } from '@/components/notes/NoteShareSheet';
import { NoteActionsMenu } from '@/components/notes/NoteActionsMenu';
import { ConflictBanner } from '@/components/notes/ConflictBanner';
+import { VoiceSetupBanner } from '@/components/notes/VoiceSetupBanner';
+import { ThatsMeSheet } from '@/components/notes/ThatsMeSheet';
+import { shouldOfferVoiceSetup, voiceSetupCandidates } from '@/lib/notes/voiceSetupPrompt';
+import { SpeakerProfileOwnerAlreadyExistsError } from '@/data/local/notesRepository';
+import { VOICE_ALREADY_TAUGHT_ALERT } from '@/lib/voiceEnrollmentMessages';
import { NoteChatSheet } from '@/components/notes/NoteChatSheet';
import { isDictationAgentEnabled } from '@/lib/dictationAgent';
import { SpeakerTranscript } from '@/components/notes/SpeakerTranscript';
@@ -143,6 +148,15 @@ export default function NoteEditorScreen() {
const spaceFolders = useNotesStore((s) => s.spaceFolders);
const spaces = useNotesStore((s) => s.spaces);
const getSpaceFolders = useNotesStore((s) => s.getSpaceFolders);
+ const voiceProfiles = useNotesStore((s) => s.voiceProfiles);
+ const meetingSpeakerEmbeddings = useNotesStore((s) => s.meetingSpeakerEmbeddingsByNoteId);
+ const claimSpeakerAsMe = useNotesStore((s) => s.claimSpeakerAsMe);
+ const loadVoiceProfiles = useNotesStore((s) => s.loadVoiceProfiles);
+ // Until the config loads, treat the banner as dismissed so it can't flash.
+ const voiceSetupDismissed = useConfigStore(
+ (s) => !s.config || !!s.config.voiceSetupBannerDismissedAt,
+ );
+ const updateConfig = useConfigStore((s) => s.updateConfig);
const note = useMemo<Note | null>(() => {
if (Number.isNaN(noteId)) return null;
return notes.find((n) => n.id === noteId) ?? getNoteById(noteId);
@@ -313,6 +327,35 @@ export default function NoteEditorScreen() {
() => groupTranscriptSegments(transcriptSegments, speakers),
[speakers, transcriptSegments],
);
+ const [voiceSetupVisible, setVoiceSetupVisible] = useState(false);
+ const readScriptAfterSheetRef = useRef(false);
+ const voiceCandidates = useMemo(
+ () =>
+ voiceSetupCandidates({
+ segments: transcriptSegments,
+ speakers,
+ embeddingsByLabel: note ? meetingSpeakerEmbeddings[note.id] : undefined,
+ profileIds: new Set(voiceProfiles.map((profile) => profile.id)),
+ }),
+ [meetingSpeakerEmbeddings, note, speakers, transcriptSegments, voiceProfiles],
+ );
+ const showVoiceSetupBanner = shouldOfferVoiceSetup({
+ isOnDeviceMeeting:
+ note?.noteType === 'meeting' && isManagedMeetingAudioUri(note.id, note.sourceFile),
+ transcriptStatus,
+ hasOwnerProfile: voiceProfiles.some((profile) => profile.isOwner === 1),
+ dismissed: voiceSetupDismissed,
+ candidateCount: voiceCandidates.length,
+ });
+ // The banner and That's me need your profiles, and a meeting opened straight from
+ // recording never passes a screen that loads them.
+ useEffect(() => {
+ loadVoiceProfiles();
+ }, [loadVoiceProfiles]);
+ useEffect(() => {
+ setVoiceSetupVisible(false);
+ readScriptAfterSheetRef.current = false;
+ }, [noteId]);
const hasTranscriptSegments = transcriptSegments.length > 0;
const usesSegmentTranscript = isAudioTranscript && hasTranscriptSegments;
const transcriptText = useMemo(
@@ -445,6 +488,52 @@ export default function NoteEditorScreen() {
resolveConflictKeepMine(noteId);
}, [noteId, resolveConflictKeepMine]);
+ const handleClaimVoice = useCallback(
+ (speakerId: number): boolean => {
+ try {
+ claimSpeakerAsMe(noteId, speakerId);
+ safeHaptics('success');
+ return true;
+ } catch (error) {
+ if (error instanceof SpeakerProfileOwnerAlreadyExistsError) {
+ // A profile this screen didn't know about: reloading hides the banner.
+ loadVoiceProfiles();
+ Alert.alert(...VOICE_ALREADY_TAUGHT_ALERT);
+ } else {
+ Alert.alert(
+ "Couldn't save your voice",
+ 'Read a short script instead to teach OpenWhispr your voice.',
+ );
+ }
+ return false;
+ }
+ },
+ [claimSpeakerAsMe, loadVoiceProfiles, noteId],
+ );
+
+ const openVoiceScript = useCallback(() => {
+ router.push(`/(tabs)/(notes)/voice-enrollment?owner=1&noteId=${noteId}`);
+ }, [noteId, router]);
+
+ // iOS drops a push made while a page sheet is still sliding away, so it waits for the
+ // sheet's onDismiss; Android's Modal has no onDismiss.
+ const handleReadVoiceScript = useCallback(() => {
+ setVoiceSetupVisible(false);
+ if (Platform.OS === 'ios') readScriptAfterSheetRef.current = true;
+ else openVoiceScript();
+ }, [openVoiceScript]);
+
+ const handleVoiceSetupDismissed = useCallback(() => {
+ if (!readScriptAfterSheetRef.current) return;
+ readScriptAfterSheetRef.current = false;
+ openVoiceScript();
+ }, [openVoiceScript]);
+
+ const dismissVoiceSetup = useCallback(() => {
+ safeHaptics('light');
+ updateConfig({ voiceSetupBannerDismissedAt: new Date().toISOString() });
+ }, [updateConfig]);
+
const maybeLearnCorrections = useCallback(
(newContent: string) => {
if (!autoLearnEnabledRef.current) return;
@@ -1252,6 +1341,13 @@ export default function NoteEditorScreen() {
/>
) : null}
+ {showVoiceSetupBanner ? (
+ <VoiceSetupBanner
+ onSetUp={() => setVoiceSetupVisible(true)}
+ onDismiss={dismissVoiceSetup}
+ />
+ ) : null}
+
{bodyTabs.length > 1 ? (
<View
className="mb-4 flex-row bg-tertiarySystemFill p-0.5"
@@ -1500,6 +1596,14 @@ export default function NoteEditorScreen() {
{transcriptSheetVisible ? speakerSheets : null}
</TranscriptSheet>
{transcriptSheetVisible ? null : speakerSheets}
+ <ThatsMeSheet
+ visible={voiceSetupVisible}
+ candidates={voiceCandidates}
+ onClaim={handleClaimVoice}
+ onReadScript={handleReadVoiceScript}
+ onClose={() => setVoiceSetupVisible(false)}
+ onDismissed={handleVoiceSetupDismissed}
+ />
</View>
);
}
diff --git a/openwhispr-mobile/src/screens/NotesListScreen.tsx b/openwhispr-mobile/src/screens/NotesListScreen.tsx
index b4b885f2a..d0945e7c0 100644
--- a/openwhispr-mobile/src/screens/NotesListScreen.tsx
+++ b/openwhispr-mobile/src/screens/NotesListScreen.tsx
@@ -18,10 +18,8 @@ import { groupNotesByDate } from '@/lib/groupNotesByDate';
import { safeHaptics } from '@/lib/utils';
import { confirmDestructive } from '@/lib/alerts';
import { SyncStatusLabel } from '@/components/notes/SyncStatusLabel';
-import { VoiceProfilePromptCard } from '@/components/notes/VoiceProfilePromptCard';
import { useManualSyncRefresh } from '@/hooks/useManualSyncRefresh';
import { useMoveNote } from '@/hooks/useMoveNote';
-import { useConfigStore } from '@/store/useConfigStore';
export default function NotesListScreen() {
const params = useLocalSearchParams<{ folderId?: string; spaceId?: string }>();
@@ -50,8 +48,6 @@ export default function NotesListScreen() {
voiceProfiles,
loadVoiceProfiles,
} = useNotesStore();
- const config = useConfigStore((s) => s.config);
- const updateConfig = useConfigStore((s) => s.updateConfig);
const [newFolderVisible, setNewFolderVisible] = useState(false);
@@ -224,24 +220,11 @@ export default function NotesListScreen() {
[handleCompose],
);
- const hasOwnerProfile = voiceProfiles.some((profile) => profile.isOwner === 1);
- const showVoiceProfilePrompt = !hasOwnerProfile && !config?.voiceProfilePromptDismissedAt;
-
const openVoiceProfiles = useCallback(() => {
safeHaptics('selection');
router.push('/(tabs)/(notes)/voice-profiles');
}, []);
- const openOwnerEnrollment = useCallback(() => {
- safeHaptics('selection');
- router.push('/(tabs)/(notes)/voice-enrollment?owner=1');
- }, []);
-
- const dismissVoiceProfilePrompt = useCallback(() => {
- safeHaptics('light');
- updateConfig({ voiceProfilePromptDismissedAt: new Date().toISOString() });
- }, [updateConfig]);
-
return (
<View className="flex-1 bg-systemBackground">
<NotesTopBar
@@ -261,11 +244,6 @@ export default function NotesListScreen() {
>
<Text className="mb-2 text-[15px] text-tertiaryLabel">{`${totalCount} ${totalCount === 1 ? 'Note' : 'Notes'}`}</Text>
<SyncStatusLabel />
- <VoiceProfilePromptCard
- visible={showVoiceProfilePrompt && !searchQuery}
- onEnroll={openOwnerEnrollment}
- onDismiss={dismissVoiceProfilePrompt}
- />
<View className="mb-2">
<GroupedList>
@@ -282,8 +260,8 @@ export default function NotesListScreen() {
<Text className="text-[16px] font-medium text-label">Voice Profiles</Text>
<Text className="mt-0.5 text-[13px] text-secondaryLabel">
{voiceProfiles.length === 0
- ? 'Enroll voices for meeting labels'
- : `${voiceProfiles.length} enrolled`}
+ ? 'Teach OpenWhispr your voice'
+ : `${voiceProfiles.length} ${voiceProfiles.length === 1 ? 'voice' : 'voices'}`}
</Text>
</View>
<SystemIcon
diff --git a/openwhispr-mobile/src/screens/VoiceEnrollmentScreen.tsx b/openwhispr-mobile/src/screens/VoiceEnrollmentScreen.tsx
index 20e4af228..b1fbcec27 100644
--- a/openwhispr-mobile/src/screens/VoiceEnrollmentScreen.tsx
+++ b/openwhispr-mobile/src/screens/VoiceEnrollmentScreen.tsx
@@ -1,33 +1,50 @@
-import { useCallback, useEffect, useMemo } from 'react';
+import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { Alert, ScrollView, View } from 'react-native';
import { useLocalSearchParams, useRouter } from 'expo-router';
import { Text } from '@/components/ui/Text';
+import { Button } from '@/components/ui/Button';
import { VoiceEnrollmentRecorder } from '@/components/notes/VoiceEnrollmentRecorder';
import { useNotesStore } from '@/store/useNotesStore';
+import { notesRepository } from '@/data';
+import type { SpeakerProfile } from '@/data/types';
import { SpeakerProfileOwnerAlreadyExistsError } from '@/data/local/notesRepository';
import {
- VOICE_ENROLLMENT_DIARIZER_MODEL_REQUIRED,
+ VOICE_ENROLLMENT_PROFILE_NOT_FOUND,
VoiceEnrollmentError,
+ type EnrollVoiceProfileInput,
+ type ReenrollVoiceProfileInput,
} from '@/services/diarization/VoiceprintService';
-import type {
- EnrollVoiceProfileInput,
- ReenrollVoiceProfileInput,
-} from '@/services/diarization/VoiceprintService';
+import { VOICE_ALREADY_TAUGHT_ALERT } from '@/lib/voiceEnrollmentMessages';
type SubmitInput = EnrollVoiceProfileInput | ReenrollVoiceProfileInput;
export default function VoiceEnrollmentScreen() {
- const params = useLocalSearchParams<{ owner?: string; profileId?: string }>();
+ const params = useLocalSearchParams<{ owner?: string; profileId?: string; noteId?: string }>();
const router = useRouter();
- const profileId = params.profileId ? Number(params.profileId) : null;
const profiles = useNotesStore((state) => state.voiceProfiles);
+ // Teaching your voice when you already have a profile retrains that one, instead of a
+ // full read that ends in "already taught". Read once, from the database since the store
+ // may not have loaded profiles yet, so saving a new one mid-screen doesn't turn this into
+ // a retrain.
+ const [ownerProfileIdAtOpen] = useState(() =>
+ params.owner !== '0' && !params.profileId
+ ? (notesRepository.getSpeakerProfiles().find((profile) => profile.isOwner === 1)?.id ?? null)
+ : null,
+ );
+ const profileId = params.profileId ? Number(params.profileId) : ownerProfileIdAtOpen;
+ const noteId = params.noteId ? Number(params.noteId) : null;
+ const [profilesLoaded, setProfilesLoaded] = useState(false);
const loadVoiceProfiles = useNotesStore((state) => state.loadVoiceProfiles);
const enrollVoiceProfile = useNotesStore((state) => state.enrollVoiceProfile);
const reenrollVoiceProfile = useNotesStore((state) => state.reenrollVoiceProfile);
+ const relabelMeetingSpeakers = useNotesStore((state) => state.relabelMeetingSpeakers);
+ const isDiarizerModelReady = useNotesStore((state) => state.isDiarizerModelReady);
const downloadDiarizerModel = useNotesStore((state) => state.downloadDiarizerModel);
+ const isDiarizerModelDownloading = useNotesStore((state) => state.isDiarizerModelDownloading);
useEffect(() => {
loadVoiceProfiles();
+ setProfilesLoaded(true);
}, [loadVoiceProfiles]);
const existingProfile = useMemo(
@@ -36,57 +53,71 @@ export default function VoiceEnrollmentScreen() {
[profileId, profiles],
);
const isOwner = existingProfile ? existingProfile.isOwner === 1 : params.owner !== '0';
+ const title = existingProfile
+ ? isOwner
+ ? 'Retrain Your Voice'
+ : `Retrain ${existingProfile.displayName}'s Voice`
+ : isOwner
+ ? 'Teach OpenWhispr Your Voice'
+ : "Add Someone's Voice";
+
+ // A second tap on Done would go back past the screen that opened this one.
+ const leftRef = useRef(false);
+ const leave = useCallback(() => {
+ if (leftRef.current) return;
+ leftRef.current = true;
+ if (router.canGoBack()) router.back();
+ else router.replace('/(tabs)/(notes)/voice-profiles');
+ }, [router]);
const handleSubmit = useCallback(
async (input: SubmitInput) => {
+ let profile: SpeakerProfile;
try {
- if (existingProfile) {
- await reenrollVoiceProfile({ ...input, profileId: existingProfile.id });
- } else {
- await enrollVoiceProfile(input as EnrollVoiceProfileInput);
- }
- if (router.canGoBack()) router.back();
- else router.replace('/(tabs)/(notes)/voice-profiles');
+ profile = existingProfile
+ ? await reenrollVoiceProfile({ ...input, profileId: existingProfile.id })
+ : await enrollVoiceProfile(input as EnrollVoiceProfileInput);
} catch (error) {
if (error instanceof SpeakerProfileOwnerAlreadyExistsError) {
- Alert.alert(
- 'Me is already enrolled',
- 'Open Voice Profiles and choose Re-enroll Me to update the owner voice profile.',
- );
- return;
- }
- if (
+ Alert.alert(...VOICE_ALREADY_TAUGHT_ALERT);
+ leave();
+ } else if (
error instanceof VoiceEnrollmentError &&
- error.code === VOICE_ENROLLMENT_DIARIZER_MODEL_REQUIRED
+ error.code === VOICE_ENROLLMENT_PROFILE_NOT_FOUND
) {
- Alert.alert(
- 'Download diarization model',
- 'Voice enrollment needs the on-device diarization model. Download it now, then record another take.',
- [
- { text: 'Cancel', style: 'cancel' },
- {
- text: 'Download',
- onPress: () => {
- downloadDiarizerModel().catch((downloadError) => {
- Alert.alert(
- 'Download failed',
- downloadError instanceof Error
- ? downloadError.message
- : 'Could not download the model.',
- );
- });
- },
- },
- ],
- );
- throw error;
+ // Deleted while you read, e.g. by a sync: reloading shows that it's gone instead
+ // of offering Try Again for a profile no read can save.
+ loadVoiceProfiles();
}
throw error;
}
+ // Started from a meeting note: label that meeting with the new voice as well.
+ if (noteId != null) relabelMeetingSpeakers(noteId, profile.id);
},
- [downloadDiarizerModel, enrollVoiceProfile, existingProfile, reenrollVoiceProfile, router],
+ [
+ enrollVoiceProfile,
+ existingProfile,
+ leave,
+ loadVoiceProfiles,
+ noteId,
+ reenrollVoiceProfile,
+ relabelMeetingSpeakers,
+ ],
);
+ if (profileId != null && !existingProfile) {
+ // Profiles load in the first effect; after that a missing one was deleted.
+ if (!profilesLoaded) return <View className="flex-1 bg-systemBackground" />;
+ return (
+ <View className="flex-1 gap-4 bg-systemBackground p-4" testID="voice-enrollment-missing">
+ <Text className="text-[15px] leading-5 text-secondaryLabel">
+ This voice profile no longer exists.
+ </Text>
+ <Button onPress={leave}>Back</Button>
+ </View>
+ );
+ }
+
return (
<View className="flex-1 bg-systemBackground">
<ScrollView
@@ -95,16 +126,18 @@ export default function VoiceEnrollmentScreen() {
contentContainerStyle={{ padding: 16, paddingBottom: 40 }}
>
<Text accessibilityRole="header" className="mb-5 text-2xl font-bold text-label">
- {existingProfile ? 'Re-enroll Voice' : 'Enroll Voice'}
+ {title}
</Text>
<VoiceEnrollmentRecorder
isOwner={isOwner}
profileId={existingProfile?.id}
- defaultDisplayName={existingProfile?.displayName ?? (isOwner ? 'Me' : '')}
+ defaultDisplayName={existingProfile?.displayName}
+ isModelReady={isDiarizerModelReady}
+ isModelDownloading={isDiarizerModelDownloading}
+ downloadModel={downloadDiarizerModel}
onSubmit={handleSubmit}
- onCancel={() => {
- if (router.canGoBack()) router.back();
- }}
+ onDone={leave}
+ onCancel={leave}
/>
</ScrollView>
</View>
diff --git a/openwhispr-mobile/src/screens/VoiceProfileDetailScreen.tsx b/openwhispr-mobile/src/screens/VoiceProfileDetailScreen.tsx
index e66cca0b0..1d9cdbe07 100644
--- a/openwhispr-mobile/src/screens/VoiceProfileDetailScreen.tsx
+++ b/openwhispr-mobile/src/screens/VoiceProfileDetailScreen.tsx
@@ -120,7 +120,7 @@ export default function VoiceProfileDetailScreen() {
}
}, [email, profile, updateVoiceProfile]);
- const reenroll = useCallback(() => {
+ const retrain = useCallback(() => {
if (!profile) return;
router.push(`/(tabs)/(notes)/voice-enrollment?profileId=${profile.id}`);
}, [profile, router]);
@@ -158,7 +158,7 @@ export default function VoiceProfileDetailScreen() {
</Text>
{isOwner ? (
<View className="rounded-md bg-tertiarySystemFill px-2 py-0.5">
- <Text className="text-[12px] font-semibold text-secondaryLabel">Owner</Text>
+ <Text className="text-[12px] font-semibold text-secondaryLabel">You</Text>
</View>
) : null}
</View>
@@ -189,31 +189,30 @@ export default function VoiceProfileDetailScreen() {
Adding an email lets meeting notes link this speaker to calendar invites later.
</Text>
- <SectionHeader label="Voiceprint" />
+ <SectionHeader label="Voice sample" />
<GroupedList dividerInset={16}>
<View className="min-h-[46px] flex-row items-center justify-between px-4 py-3">
<Text className="text-[16px] text-secondaryLabel">Samples</Text>
<Text className="text-[16px] font-medium text-label">{profile.sampleCount}</Text>
</View>
<View className="min-h-[46px] flex-row items-center justify-between px-4 py-3">
- <Text className="text-[16px] text-secondaryLabel">Enrolled</Text>
+ <Text className="text-[16px] text-secondaryLabel">Added</Text>
<Text className="text-[16px] font-medium text-label">
{formatDate(profile.createdAt)}
</Text>
</View>
<GroupedList.Row
- onPress={reenroll}
+ onPress={retrain}
accessibilityRole="button"
- accessibilityLabel="Re-enroll Voice"
+ accessibilityLabel="Retrain Voice"
testID="voice-profile-reenroll"
contentInsetLeft={16}
>
- <Text className="text-[17px] font-medium text-brand">Re-enroll Voice</Text>
+ <Text className="text-[17px] font-medium text-brand">Retrain Voice</Text>
</GroupedList.Row>
</GroupedList>
<Text className="mt-2 px-4 text-[13px] leading-5 text-secondaryLabel">
- Re-recording replaces this speaker's voiceprint. Past transcripts keep their existing
- labels.
+ Retraining replaces this voice sample. Past transcripts keep their existing labels.
</Text>
<View className="mt-6">
diff --git a/openwhispr-mobile/src/screens/__tests__/DiarizationModelScreen.test.tsx b/openwhispr-mobile/src/screens/__tests__/DiarizationModelScreen.test.tsx
new file mode 100644
index 000000000..368d71df2
--- /dev/null
+++ b/openwhispr-mobile/src/screens/__tests__/DiarizationModelScreen.test.tsx
@@ -0,0 +1,48 @@
+import { act, render, waitFor } from '@testing-library/react-native';
+import DiarizationModelScreen from '../DiarizationModelScreen';
+
+let mockDownloading = false;
+let mockFinishDownload: () => void = () => {};
+const mockDownload = jest.fn(() => new Promise<void>((resolve) => (mockFinishDownload = resolve)));
+const mockState = {
+ isDiarizerAvailable: jest.fn(async () => true),
+ isDiarizerModelReady: jest.fn(async () => false),
+ isDiarizerModelDownloading: jest.fn(() => mockDownloading),
+ downloadDiarizerModel: mockDownload,
+ deleteDiarizerModel: jest.fn(async () => undefined),
+};
+
+jest.mock('@/components/ui/Text', () => ({ Text: require('react-native').Text }));
+jest.mock('@/components/ui/SystemIcon', () => ({ SystemIcon: () => null }));
+jest.mock('expo-router', () => ({ useRouter: () => ({ back: jest.fn() }) }));
+jest.mock('@/store/useNotesStore', () => ({
+ useNotesStore: (selector: (state: typeof mockState) => unknown) => selector(mockState),
+}));
+jest.mock('@/lib/utils', () => ({ safeHaptics: jest.fn() }));
+jest.mock('@/lib/alerts', () => ({ confirmDestructive: jest.fn() }));
+jest.mock('@/lib/sentry', () => ({ Sentry: { captureException: jest.fn() } }));
+
+beforeEach(() => {
+ jest.clearAllMocks();
+ mockDownloading = false;
+});
+
+describe('DiarizationModelScreen', () => {
+ it('offers the download when the model is missing', async () => {
+ const { findByText, queryByText } = render(<DiarizationModelScreen />);
+ expect(await findByText('Speaker model')).toBeTruthy();
+ expect(queryByText('Downloading…')).toBeNull();
+ expect(mockDownload).not.toHaveBeenCalled();
+ });
+
+ it('shows a download another screen started, then the downloaded model', async () => {
+ mockDownloading = true;
+ const { findByText, getByText } = render(<DiarizationModelScreen />);
+
+ expect(await findByText('Downloading…')).toBeTruthy();
+ expect(mockDownload).toHaveBeenCalledTimes(1);
+
+ await act(async () => mockFinishDownload());
+ await waitFor(() => expect(getByText('Downloaded')).toBeTruthy());
+ });
+});
diff --git a/openwhispr-mobile/src/screens/__tests__/NoteEditorScreen.calendarContext.test.tsx b/openwhispr-mobile/src/screens/__tests__/NoteEditorScreen.calendarContext.test.tsx
index d4ac8c060..471d245d3 100644
--- a/openwhispr-mobile/src/screens/__tests__/NoteEditorScreen.calendarContext.test.tsx
+++ b/openwhispr-mobile/src/screens/__tests__/NoteEditorScreen.calendarContext.test.tsx
@@ -1,5 +1,5 @@
import React from 'react';
-import { Alert } from 'react-native';
+import { Alert, Modal, Platform } from 'react-native';
import { act, fireEvent, render, waitFor, within } from '@testing-library/react-native';
import NoteEditorScreen from '@/screens/NoteEditorScreen';
import { ReasoningService } from '@/services/reasoning/ReasoningService';
@@ -9,7 +9,7 @@ import { formatTranscriptForExport } from '@/lib/diarization/transcriptDisplay';
import { makeContentHash } from '@/lib/utils';
import { clearLocalReasoningReadinessCache, LocalReasoningError } from '@/lib/localReasoning';
import { extractCorrections } from '@/lib/correctionLearner';
-import type { Action, Note, Segment, Speaker } from '@/data/types';
+import type { Action, Note, Segment, Speaker, SpeakerProfile } from '@/data/types';
import type { Folder, Space } from '@/data';
import type { UserConfig } from '@/types';
@@ -25,6 +25,8 @@ const mockAddLearnedWords = jest.fn();
const mockUpdateConfig = jest.fn(async (updates: Record<string, unknown>) => {
mockConfigState.config = { ...mockConfigState.config, ...updates };
});
+const mockClaimSpeakerAsMe = jest.fn();
+const mockLoadVoiceProfiles = jest.fn();
let mockNote: Note;
let mockRouteNoteId = '7';
@@ -56,6 +58,10 @@ const mockNotesState = {
moveNoteToSpace: jest.fn(),
createFolder: jest.fn(),
getSpaceFolders: jest.fn(() => [] as Folder[]),
+ voiceProfiles: [] as SpeakerProfile[],
+ meetingSpeakerEmbeddingsByNoteId: {} as Record<number, Record<string, number[]>>,
+ claimSpeakerAsMe: mockClaimSpeakerAsMe,
+ loadVoiceProfiles: mockLoadVoiceProfiles,
};
const mockActionsState = {
@@ -85,10 +91,11 @@ jest.mock('react-native-safe-area-context', () => ({
useSafeAreaInsets: () => ({ top: 0, right: 0, bottom: 0, left: 0 }),
}));
+const mockPush = jest.fn();
jest.mock('expo-router', () => ({
useLocalSearchParams: () => ({ id: mockRouteNoteId }),
useRouter: () => ({
- push: jest.fn(),
+ push: mockPush,
canGoBack: () => true,
back: jest.fn(),
replace: jest.fn(),
@@ -99,6 +106,12 @@ jest.mock('expo-clipboard', () => ({
setStringAsync: jest.fn(async () => undefined),
}));
+jest.mock('@/lib/transcriptAudio', () => ({
+ ...jest.requireActual('@/lib/transcriptAudio'),
+ isManagedMeetingAudioUri: (noteId: number, uri?: string | null) =>
+ uri === `managed://meeting-${noteId}.wav`,
+}));
+
jest.mock('@/store/useNotesStore', () => ({
useNotesStore: (selector?: (state: typeof mockNotesState) => unknown) =>
selector ? selector(mockNotesState) : mockNotesState,
@@ -524,6 +537,9 @@ beforeEach(() => {
mockSpeakers = [speaker()];
mockConfigState.config.autoGenerateNoteTitle = false;
mockConfigState.config.appleLocalIntelligenceEnabled = true;
+ mockConfigState.config.voiceSetupBannerDismissedAt = undefined;
+ mockNotesState.voiceProfiles = [];
+ mockNotesState.meetingSpeakerEmbeddingsByNoteId = {};
mockUpdateConfig.mockClear();
(ReasoningService.processText as jest.Mock).mockResolvedValue({
text: 'Generated calendar-aware notes',
@@ -1601,3 +1617,188 @@ describe('NoteEditorScreen clearing generated notes', () => {
expect(mockUpdateNote).toHaveBeenLastCalledWith(7, { enhancedContent: '## Rewritten' });
});
});
+
+describe('NoteEditorScreen voice setup', () => {
+ let alertSpy: jest.SpyInstance;
+ const longSegments = (): Segment[] => [
+ segment({
+ id: 20,
+ speakerLabel: 'speaker_0',
+ startMs: 0,
+ endMs: 15000,
+ text: 'I will own the launch.',
+ }),
+ ];
+
+ beforeEach(() => {
+ alertSpy = jest.spyOn(Alert, 'alert').mockImplementation(() => undefined);
+ mockNote = note({ sourceFile: 'managed://meeting-7.wav', transcriptionStatus: 'done' });
+ mockNotesState.notes = [mockNote];
+ mockSegments = longSegments();
+ mockSpeakers = [speaker()];
+ mockNotesState.meetingSpeakerEmbeddingsByNoteId = { 7: { speaker_0: [0.1, 0.2] } };
+ });
+ afterEach(() => alertSpy.mockRestore());
+
+ it('offers voice setup on a meeting recorded on this device', () => {
+ const { getByTestId } = render(<NoteEditorScreen />);
+ expect(getByTestId('voice-setup-banner')).toBeTruthy();
+ });
+
+ it('loads your voice profiles, which a meeting opened straight from recording never did', () => {
+ render(<NoteEditorScreen />);
+ expect(mockLoadVoiceProfiles).toHaveBeenCalled();
+ });
+
+ it('does not offer it on a meeting recorded elsewhere, or once you have a profile', () => {
+ mockNote = note({ sourceFile: null });
+ mockNotesState.notes = [mockNote];
+ const { queryByTestId, rerender } = render(<NoteEditorScreen />);
+ expect(queryByTestId('voice-setup-banner')).toBeNull();
+
+ mockNote = note({ sourceFile: 'managed://meeting-7.wav' });
+ mockNotesState.notes = [mockNote];
+ mockNotesState.voiceProfiles = [{ id: 1, isOwner: 1 } as SpeakerProfile];
+ rerender(<NoteEditorScreen />);
+ expect(queryByTestId('voice-setup-banner')).toBeNull();
+ });
+
+ it("doesn't offer it once this meeting's samples are gone, such as after a restart", () => {
+ mockNotesState.meetingSpeakerEmbeddingsByNoteId = {};
+ const { queryByTestId } = render(<NoteEditorScreen />);
+ expect(queryByTestId('voice-setup-banner')).toBeNull();
+ });
+
+ it('waits for settings to load, so a dismissed banner never flashes', () => {
+ const loaded = mockConfigState.config;
+ mockConfigState.config = null as unknown as Partial<UserConfig>;
+ try {
+ const { queryByTestId } = render(<NoteEditorScreen />);
+ expect(queryByTestId('voice-setup-banner')).toBeNull();
+ } finally {
+ mockConfigState.config = loaded;
+ }
+ });
+
+ it('hides the banner for good when dismissed, under its own setting', () => {
+ const { getByTestId } = render(<NoteEditorScreen />);
+ fireEvent.press(getByTestId('voice-setup-banner-dismiss'));
+ expect(mockUpdateConfig).toHaveBeenCalledWith({
+ voiceSetupBannerDismissedAt: expect.any(String),
+ });
+ });
+
+ it('still shows for someone who dismissed the old notes-list card', () => {
+ (mockConfigState.config as Record<string, unknown>).voiceProfilePromptDismissedAt =
+ '2026-09-01T00:00:00.000Z';
+ try {
+ const { getByTestId } = render(<NoteEditorScreen />);
+ expect(getByTestId('voice-setup-banner')).toBeTruthy();
+ } finally {
+ delete (mockConfigState.config as Record<string, unknown>).voiceProfilePromptDismissedAt;
+ }
+ });
+
+ it('makes the tapped speaker your voice', () => {
+ const { getByTestId, getByText } = render(<NoteEditorScreen />);
+ fireEvent.press(getByTestId('voice-setup-banner-set-up'));
+ fireEvent.press(getByTestId('thats-me-10'));
+
+ expect(mockClaimSpeakerAsMe).toHaveBeenCalledWith(7, 10);
+ expect(getByText('Got it')).toBeTruthy();
+ });
+
+ it('explains and keeps the script offer when the sample is gone', () => {
+ mockClaimSpeakerAsMe.mockImplementationOnce(() => {
+ throw new Error("This meeting's voice sample is no longer available.");
+ });
+ const { getByTestId, queryByText } = render(<NoteEditorScreen />);
+ fireEvent.press(getByTestId('voice-setup-banner-set-up'));
+ fireEvent.press(getByTestId('thats-me-10'));
+
+ expect(alertSpy).toHaveBeenCalledWith(
+ "Couldn't save your voice",
+ 'Read a short script instead to teach OpenWhispr your voice.',
+ );
+ expect(queryByText('Got it')).toBeNull();
+ expect(getByTestId('thats-me-read-script')).toBeTruthy();
+ });
+
+ it("doesn't suggest the script when you already have a voice profile", () => {
+ const { SpeakerProfileOwnerAlreadyExistsError } = jest.requireActual(
+ '@/data/local/notesRepository',
+ );
+ mockClaimSpeakerAsMe.mockImplementationOnce(() => {
+ throw new SpeakerProfileOwnerAlreadyExistsError();
+ });
+ const { getByTestId } = render(<NoteEditorScreen />);
+ fireEvent.press(getByTestId('voice-setup-banner-set-up'));
+ fireEvent.press(getByTestId('thats-me-10'));
+
+ expect(alertSpy).toHaveBeenCalledWith(
+ "You've already taught OpenWhispr your voice",
+ 'Open it in Voice Profiles and choose Retrain Voice.',
+ );
+ // Reloading picks up the profile, which hides the banner.
+ expect(mockLoadVoiceProfiles).toHaveBeenCalledTimes(2);
+ });
+
+ it('opens the script for this meeting once the sheet has finished closing', () => {
+ const { getByTestId, UNSAFE_getAllByType } = render(<NoteEditorScreen />);
+ fireEvent.press(getByTestId('voice-setup-banner-set-up'));
+ const sheet = UNSAFE_getAllByType(Modal).find(
+ (modal) => modal.props.presentationStyle === 'pageSheet' && modal.props.visible,
+ );
+ fireEvent.press(getByTestId('thats-me-read-script'));
+
+ // Pushing while the sheet slides away gets dropped on iOS.
+ expect(mockPush).not.toHaveBeenCalled();
+ act(() => sheet?.props.onDismiss());
+
+ expect(mockPush).toHaveBeenCalledWith('/(tabs)/(notes)/voice-enrollment?owner=1&noteId=7');
+ });
+
+ it('opens the script right away on Android, which has no sheet onDismiss', () => {
+ const platform = Platform.OS;
+ Platform.OS = 'android';
+ try {
+ const { getByTestId } = render(<NoteEditorScreen />);
+ fireEvent.press(getByTestId('voice-setup-banner-set-up'));
+ fireEvent.press(getByTestId('thats-me-read-script'));
+
+ expect(mockPush).toHaveBeenCalledWith('/(tabs)/(notes)/voice-enrollment?owner=1&noteId=7');
+ } finally {
+ Platform.OS = platform;
+ }
+ });
+
+ it("doesn't open the script for another note after switching mid-close", () => {
+ const { getByTestId, UNSAFE_getAllByType, rerender } = render(<NoteEditorScreen />);
+ fireEvent.press(getByTestId('voice-setup-banner-set-up'));
+ const sheet = UNSAFE_getAllByType(Modal).find(
+ (modal) => modal.props.presentationStyle === 'pageSheet' && modal.props.visible,
+ );
+ fireEvent.press(getByTestId('thats-me-read-script'));
+
+ mockRouteNoteId = '8';
+ mockNote = note({ id: 8, sourceFile: 'managed://meeting-8.wav' });
+ mockNotesState.notes = [mockNote];
+ rerender(<NoteEditorScreen />);
+ act(() => sheet?.props.onDismiss());
+
+ expect(mockPush).not.toHaveBeenCalled();
+ });
+
+ it('closes the sheet when another note opens', () => {
+ const { getByTestId, queryByTestId, rerender } = render(<NoteEditorScreen />);
+ fireEvent.press(getByTestId('voice-setup-banner-set-up'));
+ expect(getByTestId('thats-me-sheet')).toBeTruthy();
+
+ mockRouteNoteId = '8';
+ mockNote = note({ id: 8, sourceFile: 'managed://meeting-8.wav' });
+ mockNotesState.notes = [mockNote];
+ rerender(<NoteEditorScreen />);
+
+ expect(queryByTestId('thats-me-sheet')).toBeNull();
+ });
+});
diff --git a/openwhispr-mobile/src/screens/__tests__/NoteEditorScreen.conflictBanner.test.tsx b/openwhispr-mobile/src/screens/__tests__/NoteEditorScreen.conflictBanner.test.tsx
index 173741a12..009d49cbc 100644
--- a/openwhispr-mobile/src/screens/__tests__/NoteEditorScreen.conflictBanner.test.tsx
+++ b/openwhispr-mobile/src/screens/__tests__/NoteEditorScreen.conflictBanner.test.tsx
@@ -3,7 +3,15 @@ import { act, fireEvent, render } from '@testing-library/react-native';
import NoteEditorScreen from '@/screens/NoteEditorScreen';
import * as Clipboard from 'expo-clipboard';
import { exportNote } from '@/lib/noteExport';
-import type { Action, ConflictedNote, Note, RemoteNote, Segment, Speaker } from '@/data/types';
+import type {
+ Action,
+ ConflictedNote,
+ Note,
+ RemoteNote,
+ Segment,
+ Speaker,
+ SpeakerProfile,
+} from '@/data/types';
import type { Folder, Space } from '@/data';
const mockUpdateNote = jest.fn();
@@ -48,6 +56,10 @@ const mockNotesState = {
moveNoteToSpace: jest.fn(),
createFolder: jest.fn(),
getSpaceFolders: jest.fn(() => [] as Folder[]),
+ voiceProfiles: [] as SpeakerProfile[],
+ meetingSpeakerEmbeddingsByNoteId: {} as Record<number, Record<string, number[]>>,
+ claimSpeakerAsMe: jest.fn(),
+ loadVoiceProfiles: jest.fn(),
};
const mockActionsState = {
diff --git a/openwhispr-mobile/src/screens/__tests__/VoiceEnrollmentScreen.test.tsx b/openwhispr-mobile/src/screens/__tests__/VoiceEnrollmentScreen.test.tsx
new file mode 100644
index 000000000..28b4a1a75
--- /dev/null
+++ b/openwhispr-mobile/src/screens/__tests__/VoiceEnrollmentScreen.test.tsx
@@ -0,0 +1,173 @@
+import { Alert } from 'react-native';
+import { fireEvent, render, waitFor } from '@testing-library/react-native';
+import VoiceEnrollmentScreen from '../VoiceEnrollmentScreen';
+import { SpeakerProfileOwnerAlreadyExistsError } from '@/data/local/notesRepository';
+
+let mockParams: Record<string, string> = {};
+let mockRecorderProps: any = null;
+const mockBack = jest.fn();
+const mockEnroll = jest.fn();
+const mockReenroll = jest.fn();
+// What the database holds; the store may not have loaded it yet.
+let mockSavedProfiles: any[] = [];
+const mockState = {
+ voiceProfiles: [] as any[],
+ loadVoiceProfiles: jest.fn(),
+ enrollVoiceProfile: mockEnroll,
+ reenrollVoiceProfile: mockReenroll,
+ relabelMeetingSpeakers: jest.fn(),
+ isDiarizerModelReady: jest.fn(async () => true),
+ isDiarizerModelDownloading: jest.fn(() => false),
+ downloadDiarizerModel: jest.fn(async () => undefined),
+};
+
+jest.mock('@/components/ui/Text', () => ({ Text: require('react-native').Text }));
+jest.mock('@/components/ui/Button', () => ({
+ Button: ({ onPress, children }: any) =>
+ require('react').createElement(require('react-native').Text, { onPress }, children),
+}));
+jest.mock('expo-router', () => ({
+ useLocalSearchParams: () => mockParams,
+ useRouter: () => ({ back: mockBack, canGoBack: () => true, replace: jest.fn() }),
+}));
+jest.mock('@/store/useNotesStore', () => ({
+ useNotesStore: (selector: (state: typeof mockState) => unknown) => selector(mockState),
+}));
+jest.mock('@/data/local/notesRepository', () => ({
+ SpeakerProfileOwnerAlreadyExistsError: class extends Error {},
+}));
+jest.mock('@/data', () => ({
+ notesRepository: { getSpeakerProfiles: () => mockSavedProfiles },
+}));
+jest.mock('@/components/notes/VoiceEnrollmentRecorder', () => ({
+ VoiceEnrollmentRecorder: (props: any) => {
+ mockRecorderProps = props;
+ return null;
+ },
+}));
+
+beforeEach(() => {
+ jest.clearAllMocks();
+ mockParams = {};
+ mockRecorderProps = null;
+ mockState.voiceProfiles = [];
+ mockSavedProfiles = [];
+ mockState.loadVoiceProfiles.mockImplementation(() => {
+ mockState.voiceProfiles = mockSavedProfiles;
+ });
+});
+
+describe('VoiceEnrollmentScreen', () => {
+ it('teaches your own voice by default', () => {
+ mockParams = { owner: '1' };
+ const { getByText } = render(<VoiceEnrollmentScreen />);
+ expect(getByText('Teach OpenWhispr Your Voice')).toBeTruthy();
+ expect(mockRecorderProps).toMatchObject({ isOwner: true, profileId: undefined });
+ });
+
+ it("adds someone else's voice", () => {
+ mockParams = { owner: '0' };
+ const { getByText } = render(<VoiceEnrollmentScreen />);
+ expect(getByText("Add Someone's Voice")).toBeTruthy();
+ expect(mockRecorderProps).toMatchObject({ isOwner: false, profileId: undefined });
+ });
+
+ it('retrains an existing profile, keeping it', async () => {
+ mockParams = { profileId: '2' };
+ mockSavedProfiles = [{ id: 2, displayName: 'Me', isOwner: 1 }];
+ mockState.voiceProfiles = mockSavedProfiles;
+ mockReenroll.mockResolvedValueOnce({ id: 2 });
+ const { getByText } = render(<VoiceEnrollmentScreen />);
+ expect(getByText('Retrain Your Voice')).toBeTruthy();
+ expect(mockRecorderProps).toMatchObject({ isOwner: true, profileId: 2 });
+
+ await mockRecorderProps.onSubmit({ consentAccepted: true });
+
+ expect(mockReenroll).toHaveBeenCalledWith(expect.objectContaining({ profileId: 2 }));
+ expect(mockEnroll).not.toHaveBeenCalled();
+ });
+
+ it('retrains your existing profile instead of starting a read that would be refused', () => {
+ mockParams = { owner: '1' };
+ // Not loaded into the store yet, as when you come straight from a meeting.
+ mockSavedProfiles = [{ id: 4, displayName: 'Me', isOwner: 1 }];
+ const { getByText } = render(<VoiceEnrollmentScreen />);
+ expect(getByText('Retrain Your Voice')).toBeTruthy();
+ expect(mockRecorderProps).toMatchObject({ isOwner: true, profileId: 4 });
+ });
+
+ it('shows a profile that loads after the first render', () => {
+ mockParams = { profileId: '2' };
+ mockSavedProfiles = [{ id: 2, displayName: 'Alice', isOwner: 0 }];
+ const { getByText } = render(<VoiceEnrollmentScreen />);
+ expect(getByText("Retrain Alice's Voice")).toBeTruthy();
+ expect(mockRecorderProps).toMatchObject({ isOwner: false, profileId: 2 });
+ });
+
+ it('says so and offers Back when the profile was deleted', () => {
+ mockParams = { profileId: '2' };
+ const { getByTestId, getByText } = render(<VoiceEnrollmentScreen />);
+ expect(getByTestId('voice-enrollment-missing')).toBeTruthy();
+ expect(mockRecorderProps).toBeNull();
+ fireEvent.press(getByText('Back'));
+ expect(mockBack).toHaveBeenCalled();
+ });
+
+ it('labels the meeting it was opened from once your voice is saved', async () => {
+ mockParams = { owner: '1', noteId: '7' };
+ mockEnroll.mockResolvedValueOnce({ id: 3 });
+ render(<VoiceEnrollmentScreen />);
+
+ await mockRecorderProps.onSubmit({});
+
+ expect(mockState.relabelMeetingSpeakers).toHaveBeenCalledWith(7, 3);
+ });
+
+ it('reloads profiles when the one being retrained was deleted, so the screen says it is gone', async () => {
+ const { VOICE_ENROLLMENT_PROFILE_NOT_FOUND, VoiceEnrollmentError } = jest.requireActual(
+ '@/services/diarization/VoiceprintService',
+ );
+ mockParams = { profileId: '2' };
+ mockSavedProfiles = [{ id: 2, displayName: 'Alice', isOwner: 0 }];
+ render(<VoiceEnrollmentScreen />);
+ mockSavedProfiles = [];
+ mockReenroll.mockRejectedValueOnce(
+ new VoiceEnrollmentError(VOICE_ENROLLMENT_PROFILE_NOT_FOUND, 'gone'),
+ );
+
+ await expect(mockRecorderProps.onSubmit({})).rejects.toBeInstanceOf(VoiceEnrollmentError);
+
+ expect(mockState.loadVoiceProfiles).toHaveBeenCalledTimes(2);
+ });
+
+ it('goes back once when Done is tapped twice', () => {
+ mockParams = { owner: '1' };
+ render(<VoiceEnrollmentScreen />);
+
+ mockRecorderProps.onDone();
+ mockRecorderProps.onDone();
+
+ expect(mockBack).toHaveBeenCalledTimes(1);
+ });
+
+ it('explains when you already taught it your voice', async () => {
+ const alertSpy = jest.spyOn(Alert, 'alert').mockImplementation(() => undefined);
+ mockParams = { owner: '1' };
+ mockEnroll.mockRejectedValueOnce(new SpeakerProfileOwnerAlreadyExistsError());
+ render(<VoiceEnrollmentScreen />);
+
+ await expect(mockRecorderProps.onSubmit({})).rejects.toBeInstanceOf(
+ SpeakerProfileOwnerAlreadyExistsError,
+ );
+ await waitFor(() =>
+ expect(alertSpy).toHaveBeenCalledWith(
+ "You've already taught OpenWhispr your voice",
+ 'Open it in Voice Profiles and choose Retrain Voice.',
+ ),
+ );
+ // Reading again would only be refused again.
+ expect(mockBack).toHaveBeenCalled();
+ expect(mockState.relabelMeetingSpeakers).not.toHaveBeenCalled();
+ alertSpy.mockRestore();
+ });
+});
diff --git a/openwhispr-mobile/src/services/diarization/VoiceprintService.ts b/openwhispr-mobile/src/services/diarization/VoiceprintService.ts
index e44de3d7f..504ae1f5e 100644
--- a/openwhispr-mobile/src/services/diarization/VoiceprintService.ts
+++ b/openwhispr-mobile/src/services/diarization/VoiceprintService.ts
@@ -132,6 +132,8 @@ export type EnrollmentQualityResult =
code: EnrollmentQualityFailureCode;
reason: string;
speakerDurationsMs?: Record<number, number>;
+ /** Kept on level-based rejections so the message can tell a quiet voice from pauses. */
+ speechActivity?: SpeechActivityAnalysis;
};
export interface EnrollmentQualityInput {
@@ -159,6 +161,7 @@ export type SpeechActivityQualityResult =
ok: false;
code: EnrollmentQualityFailureCode;
reason: string;
+ speechActivity?: SpeechActivityAnalysis;
};
export interface EnrollmentAudioTools {
@@ -189,6 +192,11 @@ export interface IdentifyNoteSpeakersDeps {
export interface IdentifyNoteSpeakersOptions {
preferredProfileEmails?: string[];
+ /**
+ * Only labels speakers with this profile, and only speakers not already linked to a
+ * profile that still exists, so labels the user rejected or chose stay as they are.
+ */
+ onlyProfileId?: number;
}
export interface VoiceprintSpeakerIdentificationDecision {
@@ -262,11 +270,14 @@ export const evaluateEnrollmentQuality = (
.map(([speakerId]) => Number(speakerId));
if (meaningfulSpeakerIds.length === 0) {
- return failQuality(
- VOICE_ENROLLMENT_NO_MEANINGFUL_SPEAKER,
- `No speaker had at least ${meaningfulSpeakerMs}ms of attributed speech.`,
- speakerDurationsMs,
- );
+ return {
+ ...failQuality(
+ VOICE_ENROLLMENT_NO_MEANINGFUL_SPEAKER,
+ `No speaker had at least ${meaningfulSpeakerMs}ms of attributed speech.`,
+ speakerDurationsMs,
+ ),
+ speechActivity: speechQuality.speechActivity,
+ };
}
if (meaningfulSpeakerIds.length > 1) {
return failQuality(
@@ -280,11 +291,14 @@ export const evaluateEnrollmentQuality = (
const acceptedSpeakerMs = speakerDurationsMs[speakerId] ?? 0;
const usableSpeechMs = Math.max(speechQuality.speechActivity.speechActivityMs, acceptedSpeakerMs);
if (usableSpeechMs < minSpeechActivityMs) {
- return failQuality(
- VOICE_ENROLLMENT_SHORT_SPEECH,
- `Detected ${Math.round(usableSpeechMs)}ms of usable speech; at least ${minSpeechActivityMs}ms is required.`,
- speakerDurationsMs,
- );
+ return {
+ ...failQuality(
+ VOICE_ENROLLMENT_SHORT_SPEECH,
+ `Detected ${Math.round(usableSpeechMs)}ms of usable speech; at least ${minSpeechActivityMs}ms is required.`,
+ speakerDurationsMs,
+ ),
+ speechActivity: speechQuality.speechActivity,
+ };
}
const embedding = diarization.embeddings[speakerId];
@@ -336,19 +350,25 @@ export const evaluateSpeechActivityQuality = (
);
}
if (speechActivity.speechActivityMs < minSpeechActivityMs) {
- return failQuality(
- VOICE_ENROLLMENT_SHORT_SPEECH,
- `Speech activity is below ${minSpeechActivityMs}ms.`,
- );
+ return {
+ ...failQuality(
+ VOICE_ENROLLMENT_SHORT_SPEECH,
+ `Speech activity is below ${minSpeechActivityMs}ms.`,
+ ),
+ speechActivity,
+ };
}
if (speechActivity.speechRatio < minSpeechRatio) {
- return failQuality(
- VOICE_ENROLLMENT_LOW_SPEECH_RATIO,
- `Speech ratio is below ${minSpeechRatio}.`,
- );
+ return {
+ ...failQuality(VOICE_ENROLLMENT_LOW_SPEECH_RATIO, `Speech ratio is below ${minSpeechRatio}.`),
+ speechActivity,
+ };
}
if (speechActivity.peakDb - speechActivity.noiseFloorDb < minSnrDb) {
- return failQuality(VOICE_ENROLLMENT_LOW_SNR, `SNR proxy is below ${minSnrDb}dB.`);
+ return {
+ ...failQuality(VOICE_ENROLLMENT_LOW_SNR, `SNR proxy is below ${minSnrDb}dB.`),
+ speechActivity,
+ };
}
return { ok: true, speechActivity };
@@ -593,6 +613,10 @@ const buildIdentificationPatch = (
return {};
};
+// Rewriting a speaker with the values it already has would still mark the note for sync.
+const changesSpeaker = (speaker: Speaker, patch: Partial<Speaker>): boolean =>
+ Object.entries(patch).some(([key, value]) => speaker[key as keyof Speaker] !== value);
+
export const identifyNoteSpeakers = (
noteId: number,
speakerEmbeddingsByLabel: Record<string, number[]>,
@@ -617,9 +641,18 @@ export const identifyNoteSpeakers = (
const decisions: VoiceprintSpeakerIdentificationDecision[] = [];
const updatedSpeakerIds: number[] = [];
+ const { onlyProfileId } = options;
+
speakers.forEach((speaker) => {
const match = matchesByLabel[speaker.speakerLabel];
if (!match) return;
+ if (
+ onlyProfileId !== undefined &&
+ (match.profileId !== onlyProfileId ||
+ (speaker.profileId !== null && profilesById.has(speaker.profileId)))
+ ) {
+ return;
+ }
const profile = getMatchedProfile(match, profilesById);
const patch = buildIdentificationPatch(speaker, profile, match.decision);
@@ -631,7 +664,7 @@ export const identifyNoteSpeakers = (
profileId: match.profileId,
});
- if (Object.keys(patch).length === 0) return;
+ if (!changesSpeaker(speaker, patch)) return;
deps.repo.updateSpeaker(speaker.id, patch);
updatedSpeakerIds.push(speaker.id);
});
diff --git a/openwhispr-mobile/src/services/diarization/__tests__/VoiceprintService.test.ts b/openwhispr-mobile/src/services/diarization/__tests__/VoiceprintService.test.ts
index 097361077..ee0f6a6f0 100644
--- a/openwhispr-mobile/src/services/diarization/__tests__/VoiceprintService.test.ts
+++ b/openwhispr-mobile/src/services/diarization/__tests__/VoiceprintService.test.ts
@@ -19,6 +19,7 @@ import {
VOICE_ENROLLMENT_NO_USABLE_EMBEDDING,
VOICE_ENROLLMENT_SPEECH_ANALYSIS_FAILED,
VOICE_ENROLLMENT_STORAGE_FAILED,
+ VOICE_ENROLLMENT_NO_MEANINGFUL_SPEAKER,
VOICE_ENROLLMENT_SHORT_SPEECH,
VOICE_ENROLLMENT_TRANSCODE_FAILED,
type EnrollVoiceProfileDeps,
@@ -187,6 +188,39 @@ describe('evaluateEnrollmentQuality', () => {
);
});
+ it.each([
+ ['low speech ratio', { speechRatio: 0.2 }, VOICE_ENROLLMENT_LOW_SPEECH_RATIO],
+ ['low SNR', { peakDb: -20, noiseFloorDb: -28 }, VOICE_ENROLLMENT_LOW_SNR],
+ ])('keeps the level analysis on a %s rejection', (_name, overrides, code) => {
+ const speechActivity = { ...passingAnalysis, ...overrides };
+
+ expect(evaluateEnrollmentQuality({ speechActivity, diarization: diarization() })).toMatchObject(
+ { ok: false, code, speechActivity },
+ );
+ });
+
+ it('keeps the level analysis on a short-speech rejection', () => {
+ const speechActivity = { ...passingAnalysis, speechActivityMs: 5_000 };
+
+ expect(
+ evaluateEnrollmentQuality({
+ speechActivity,
+ diarization: diarization({ segments: [{ start: 0, end: 5, speakerId: 0 }] }),
+ }),
+ ).toMatchObject({ ok: false, code: VOICE_ENROLLMENT_SHORT_SPEECH, speechActivity });
+ });
+
+ it('keeps the level analysis when no single speaker was heard for long enough', () => {
+ const speechActivity = passingAnalysis;
+
+ expect(
+ evaluateEnrollmentQuality({
+ speechActivity,
+ diarization: diarization({ segments: [{ start: 0, end: 1, speakerId: 0 }] }),
+ }),
+ ).toMatchObject({ ok: false, code: VOICE_ENROLLMENT_NO_MEANINGFUL_SPEAKER, speechActivity });
+ });
+
it('accepts VAD-short analysis when diarization attributes enough single-speaker speech', () => {
expect(
evaluateEnrollmentQuality({
@@ -285,6 +319,7 @@ describe('enrollVoiceProfile', () => {
await expect(enrollVoiceProfile(enrollInput, deps)).rejects.toMatchObject({
code: VOICE_ENROLLMENT_LOW_SPEECH_RATIO,
+ quality: { speechActivity: { speechRatio: 0.1 } },
});
expect(calls.diarize).toEqual([]);
});
@@ -488,6 +523,25 @@ describe('identifyNoteSpeakers', () => {
return { deps, calls };
};
+ it('writes nothing to a speaker that already carries the label, which would resync the note', () => {
+ const { deps, calls } = makeIdentificationDeps({
+ speakers: [
+ speaker({
+ displayName: 'Alice',
+ speakerStatus: 'confirmed',
+ speakerLocked: 0,
+ speakerLockSource: null,
+ profileId: 10,
+ }),
+ ],
+ });
+
+ const result = identifyNoteSpeakers(7, { speaker_0: [1, 0] }, deps);
+
+ expect(calls.updateSpeaker).toEqual([]);
+ expect(result.updatedSpeakerIds).toEqual([]);
+ });
+
it('auto-labels a high-confidence unlocked speaker and sets profileId', () => {
const { deps, calls } = makeIdentificationDeps();
diff --git a/openwhispr-mobile/src/store/__tests__/useNotesStore.meeting.test.ts b/openwhispr-mobile/src/store/__tests__/useNotesStore.meeting.test.ts
index b8cce8290..4647255a6 100644
--- a/openwhispr-mobile/src/store/__tests__/useNotesStore.meeting.test.ts
+++ b/openwhispr-mobile/src/store/__tests__/useNotesStore.meeting.test.ts
@@ -61,6 +61,7 @@ jest.mock('@/data', () => ({
getSpeakerProfiles: jest.fn(() => []),
getSpeakerProfileById: jest.fn(() => null),
createSpeakerProfile: jest.fn(),
+ createOwnerProfileForSpeaker: jest.fn(),
updateSpeakerProfile: jest.fn(),
deleteSpeakerProfile: jest.fn(),
deleteAllSpeakerProfiles: jest.fn(),
@@ -113,6 +114,8 @@ import { ReasoningService } from '@/services/reasoning/ReasoningService';
import { generateLocalMeetingNotes } from '@/lib/notes/localMeetingNotes';
import * as localReasoning from '@/lib/localReasoning';
import type { Speaker } from '@/data/types';
+import { SpeakerProfileOwnerAlreadyExistsError } from '@/data/local/notesRepository';
+import * as Sentry from '@sentry/react-native';
const speaker = (overrides: Partial<Speaker>): Speaker =>
({
@@ -967,6 +970,56 @@ describe('voice profile enrollment', () => {
expect(downloadModel).toHaveBeenCalledTimes(1);
});
+ it('shares one model download between callers and is not ready until it finishes', async () => {
+ let finish: () => void = () => {};
+ const downloadModel = jest.fn(() => new Promise<void>((resolve) => (finish = resolve)));
+ mockGetDiarizer.mockReturnValue({
+ diarize: jest.fn(),
+ isModelDownloaded: jest.fn(async () => true),
+ downloadModel,
+ });
+
+ const first = useNotesStore.getState().downloadDiarizerModel();
+ const second = useNotesStore.getState().downloadDiarizerModel();
+ await expect(useNotesStore.getState().isDiarizerModelReady()).resolves.toBe(false);
+ finish();
+ await Promise.all([first, second]);
+
+ expect(downloadModel).toHaveBeenCalledTimes(1);
+ await expect(useNotesStore.getState().isDiarizerModelReady()).resolves.toBe(true);
+ });
+
+ it('waits for a speaker-model download already running before diarizing a meeting', async () => {
+ let finish: () => void = () => {};
+ const diarize = jest.fn(async (_wavUri: string, _count?: number) => ({
+ segments: [],
+ embeddings: {},
+ }));
+ mockGetDiarizer.mockReturnValue({
+ diarize,
+ isModelDownloaded: jest.fn(async () => false),
+ downloadModel: jest.fn(() => new Promise<void>((resolve) => (finish = resolve))),
+ });
+ mockProcessMeeting.mockImplementation(
+ async (_input: unknown, deps: { diarizer: { diarize: typeof diarize } }) => {
+ await deps.diarizer.diarize('file://meeting.wav', 2);
+ return { speakerEmbeddingsByLabel: {} };
+ },
+ );
+
+ const download = useNotesStore.getState().downloadDiarizerModel();
+ expect(useNotesStore.getState().isDiarizerModelDownloading()).toBe(true);
+ const pipeline = useNotesStore.getState().runMeetingPipeline(7, 'file://meeting.wav', 2);
+ await new Promise((resolve) => setTimeout(resolve, 0));
+ expect(diarize).not.toHaveBeenCalled();
+
+ finish();
+ await download;
+ await pipeline;
+ expect(diarize).toHaveBeenCalledWith('file://meeting.wav', 2);
+ expect(useNotesStore.getState().isDiarizerModelDownloading()).toBe(false);
+ });
+
it('deletes the diarizer model through the explicit delete action', async () => {
const deleteModel = jest.fn(async () => undefined);
mockGetDiarizer.mockReturnValueOnce({
@@ -1125,6 +1178,219 @@ describe('speaker mutations', () => {
expect(notesRepository.updateSpeaker).not.toHaveBeenCalled();
});
+
+ describe('claimSpeakerAsMe', () => {
+ let profiles: Array<Record<string, unknown>>;
+ let rows: Speaker[];
+ const ownerProfile = {
+ id: 3,
+ displayName: 'Me',
+ isOwner: 1,
+ email: null,
+ embedding: [0.6, 0.8],
+ sampleCount: 1,
+ };
+
+ beforeEach(() => {
+ profiles = [];
+ useNotesStore.setState({
+ meetingSpeakerEmbeddingsByNoteId: { 7: { SPEAKER_01: [3, 4] } },
+ });
+ (notesRepository.getTranscriptionStatus as jest.Mock).mockReturnValue('done');
+ (notesRepository.getSpeakerProfiles as jest.Mock).mockImplementation(() => profiles);
+ rows = [speaker({ id: 10, speakerLabel: 'SPEAKER_01' })];
+ (notesRepository.getSpeakers as jest.Mock).mockImplementation(() => rows);
+ // Like the repository: the claimed speaker comes back locked to the new profile.
+ (notesRepository.createOwnerProfileForSpeaker as jest.Mock).mockImplementation(
+ (speakerId: number, _input: unknown, patch: Partial<Speaker>) => {
+ profiles = [ownerProfile];
+ rows = rows.map((row) =>
+ row.id === speakerId ? { ...row, ...patch, profileId: ownerProfile.id } : row,
+ );
+ return ownerProfile;
+ },
+ );
+ });
+
+ it('creates your profile and links the speaker in a single atomic write', () => {
+ useNotesStore.getState().claimSpeakerAsMe(7, 10);
+
+ expect(notesRepository.createOwnerProfileForSpeaker).toHaveBeenCalledWith(
+ 10,
+ {
+ displayName: 'Me',
+ // Stored normalized, like a guided-read sample.
+ embedding: [0.6, 0.8],
+ sampleCount: 1,
+ consentAt: expect.any(String),
+ },
+ {
+ displayName: 'Me',
+ speakerStatus: 'locked',
+ speakerLocked: 1,
+ speakerLockSource: 'user',
+ },
+ );
+ expect(notesRepository.updateSpeaker).not.toHaveBeenCalled();
+ expect(useNotesStore.getState().voiceProfiles).toEqual([ownerProfile]);
+ expect(useNotesStore.getState().transcriptRevision).toBe(1);
+ });
+
+ it('labels another speaker in the same meeting who has your voice', () => {
+ useNotesStore.setState({
+ meetingSpeakerEmbeddingsByNoteId: { 7: { SPEAKER_01: [3, 4], SPEAKER_02: [3, 4] } },
+ });
+ rows = [
+ speaker({ id: 10, speakerLabel: 'SPEAKER_01' }),
+ speaker({ id: 11, speakerLabel: 'SPEAKER_02', sortOrder: 1 }),
+ ];
+
+ useNotesStore.getState().claimSpeakerAsMe(7, 10);
+
+ expect(notesRepository.updateSpeaker).toHaveBeenCalledTimes(1);
+ expect(notesRepository.updateSpeaker).toHaveBeenCalledWith(
+ 11,
+ expect.objectContaining({ displayName: 'Me', profileId: 3 }),
+ );
+ });
+
+ it("leaves a speaker linked to someone else's profile, or a rejected suggestion, alone", () => {
+ const bob = { id: 4, displayName: 'Bob', isOwner: 0, email: null, embedding: [0, 1] };
+ profiles = [bob];
+ useNotesStore.setState({
+ meetingSpeakerEmbeddingsByNoteId: {
+ 7: { SPEAKER_01: [3, 4], SPEAKER_02: [3, 4], SPEAKER_03: [0, 1] },
+ },
+ });
+ rows = [
+ speaker({ id: 10, speakerLabel: 'SPEAKER_01' }),
+ // Auto-labelled Bob, though the voice matches yours.
+ speaker({
+ id: 11,
+ speakerLabel: 'SPEAKER_02',
+ sortOrder: 1,
+ displayName: 'Bob',
+ profileId: 4,
+ speakerStatus: 'confirmed',
+ }),
+ // You rejected "Bob?" here; claiming must not suggest Bob again.
+ speaker({ id: 12, speakerLabel: 'SPEAKER_03', sortOrder: 2 }),
+ ];
+ (notesRepository.createOwnerProfileForSpeaker as jest.Mock).mockImplementation(
+ (speakerId: number, _input: unknown, patch: Partial<Speaker>) => {
+ profiles = [bob, ownerProfile];
+ rows = rows.map((row) =>
+ row.id === speakerId ? { ...row, ...patch, profileId: ownerProfile.id } : row,
+ );
+ return ownerProfile;
+ },
+ );
+
+ useNotesStore.getState().claimSpeakerAsMe(7, 10);
+
+ expect(notesRepository.updateSpeaker).not.toHaveBeenCalled();
+ });
+
+ it('keeps the claim when labelling the rest of the meeting fails', () => {
+ const failure = new Error('disk full');
+ (notesRepository.updateSpeaker as jest.Mock).mockImplementationOnce(() => {
+ throw failure;
+ });
+ useNotesStore.setState({
+ meetingSpeakerEmbeddingsByNoteId: { 7: { SPEAKER_01: [3, 4], SPEAKER_02: [3, 4] } },
+ });
+ rows = [
+ speaker({ id: 10, speakerLabel: 'SPEAKER_01' }),
+ speaker({ id: 11, speakerLabel: 'SPEAKER_02', sortOrder: 1 }),
+ ];
+
+ expect(() => useNotesStore.getState().claimSpeakerAsMe(7, 10)).not.toThrow();
+
+ expect(useNotesStore.getState().voiceProfiles).toEqual([ownerProfile]);
+ expect(Sentry.captureException).toHaveBeenCalledWith(failure, expect.anything());
+ });
+
+ it('refuses without writing when you already have a profile', () => {
+ profiles = [{ id: 1, isOwner: 1 }];
+
+ expect(() => useNotesStore.getState().claimSpeakerAsMe(7, 10)).toThrow(
+ SpeakerProfileOwnerAlreadyExistsError,
+ );
+ expect(notesRepository.createOwnerProfileForSpeaker).not.toHaveBeenCalled();
+ });
+
+ it('refuses without writing while the meeting is being processed again', () => {
+ (notesRepository.getTranscriptionStatus as jest.Mock).mockReturnValue('diarizing');
+
+ expect(() => useNotesStore.getState().claimSpeakerAsMe(7, 10)).toThrow();
+ expect(notesRepository.createOwnerProfileForSpeaker).not.toHaveBeenCalled();
+ });
+
+ it.each([
+ ['gone', {}],
+ ['unusable', { 7: { SPEAKER_01: [0, 0] } }],
+ ])('refuses without writing when the meeting sample is %s', (_label, embeddings) => {
+ useNotesStore.setState({ meetingSpeakerEmbeddingsByNoteId: embeddings });
+
+ expect(() => useNotesStore.getState().claimSpeakerAsMe(7, 10)).toThrow();
+ expect(notesRepository.createOwnerProfileForSpeaker).not.toHaveBeenCalled();
+ });
+
+ it('refuses when the speaker is not on the note', () => {
+ expect(() => useNotesStore.getState().claimSpeakerAsMe(7, 99)).toThrow();
+ expect(notesRepository.createOwnerProfileForSpeaker).not.toHaveBeenCalled();
+ });
+ });
+
+ describe('relabelMeetingSpeakers', () => {
+ beforeEach(() => {
+ (notesRepository.getSpeakerProfiles as jest.Mock).mockReturnValue([
+ { id: 3, displayName: 'Me', isOwner: 1, email: null, embedding: [0.6, 0.8] },
+ ]);
+ (notesRepository.getSpeakers as jest.Mock).mockReturnValue([
+ speaker({ id: 10, speakerLabel: 'SPEAKER_01' }),
+ ]);
+ });
+
+ it('labels a meeting still held in memory with a newly added voice', () => {
+ useNotesStore.setState({ meetingSpeakerEmbeddingsByNoteId: { 7: { SPEAKER_01: [3, 4] } } });
+
+ useNotesStore.getState().relabelMeetingSpeakers(7, 3);
+
+ expect(notesRepository.updateSpeaker).toHaveBeenCalledWith(
+ 10,
+ expect.objectContaining({ displayName: 'Me', profileId: 3 }),
+ );
+ expect(useNotesStore.getState().transcriptRevision).toBe(1);
+ });
+
+ it('does nothing for a meeting whose samples are gone', () => {
+ useNotesStore.setState({ meetingSpeakerEmbeddingsByNoteId: {} });
+
+ useNotesStore.getState().relabelMeetingSpeakers(7, 3);
+
+ expect(notesRepository.updateSpeaker).not.toHaveBeenCalled();
+ expect(useNotesStore.getState().transcriptRevision).toBe(0);
+ });
+
+ it('writes nothing when the speaker already has the new label', () => {
+ useNotesStore.setState({ meetingSpeakerEmbeddingsByNoteId: { 7: { SPEAKER_01: [3, 4] } } });
+ (notesRepository.getSpeakers as jest.Mock).mockReturnValue([
+ speaker({
+ id: 10,
+ speakerLabel: 'SPEAKER_01',
+ displayName: 'Me',
+ profileId: 3,
+ speakerStatus: 'confirmed',
+ }),
+ ]);
+
+ useNotesStore.getState().relabelMeetingSpeakers(7, 3);
+
+ expect(notesRepository.updateSpeaker).not.toHaveBeenCalled();
+ expect(useNotesStore.getState().transcriptRevision).toBe(0);
+ });
+ });
});
it('does not open a synced source_file that is not the recording owned by this note', async () => {
diff --git a/openwhispr-mobile/src/store/useNotesStore.ts b/openwhispr-mobile/src/store/useNotesStore.ts
index cacc206ab..0ee69732c 100644
--- a/openwhispr-mobile/src/store/useNotesStore.ts
+++ b/openwhispr-mobile/src/store/useNotesStore.ts
@@ -5,6 +5,7 @@ import { notesRepository, spacesRepository } from '@/data';
import type { Note, Folder, NoteUpdate, Space } from '@/data';
import type { ConflictedNote, Segment, Speaker, SpeakerProfile } from '@/data/types';
import type { CalendarParticipant } from '@/data/calendarTypes';
+import type { Diarizer } from '@/lib/diarization/diarizer';
import { useAuthStore } from '@/store/useAuthStore';
import { useProcessingModeStore } from '@/store/useProcessingModeStore';
import { useConfigStore } from '@/store/useConfigStore';
@@ -19,8 +20,11 @@ import { buildMergeTargetPatch, buildRenameSpeakerPatch } from '@/lib/diarizatio
import {
buildConfirmedSpeakerPatch,
buildRejectedSuggestionPatch,
+ hasFiniteNonZeroNorm,
+ l2Normalize,
runningMeanEmbedding,
} from '@/lib/diarization/voiceprints';
+import { SpeakerProfileOwnerAlreadyExistsError } from '@/data/local/notesRepository';
import {
buildActionSystemPrompt,
isDefaultGenerateNotesAction,
@@ -113,6 +117,37 @@ const reloadVoiceProfiles = (): Pick<NotesStore, 'voiceProfiles'> => ({
voiceProfiles: notesRepository.getSpeakerProfiles(),
});
+/** Labels a meeting's speakers from the saved voice profiles; true when any speaker changed. */
+const identifyMeetingSpeakers = (
+ noteId: number,
+ speakerEmbeddingsByLabel: Record<string, number[]>,
+ onlyProfileId?: number,
+): boolean => {
+ const note = notesRepository.getNoteById(noteId);
+ const preferredProfileEmails = getCalendarParticipantEmails(
+ parseCalendarParticipants(note?.participants ?? null) ?? [],
+ );
+ const identification = identifyNoteSpeakers(
+ noteId,
+ speakerEmbeddingsByLabel,
+ { repo: notesRepository },
+ { preferredProfileEmails, onlyProfileId },
+ );
+ return identification.updatedSpeakerIds.length > 0;
+};
+
+let diarizerModelDownload: Promise<void> | null = null;
+
+// Diarizing with the model missing downloads it natively, into the folder a download
+// already running is writing, so it waits for that download first.
+const diarizerAfterModelDownload = (diarizer: Diarizer): Diarizer => ({
+ ...diarizer,
+ diarize: async (wavUri, numberOfSpeakers) => {
+ await diarizerModelDownload?.catch(() => undefined);
+ return diarizer.diarize(wavUri, numberOfSpeakers);
+ },
+});
+
const DEFAULT_FOLDER_ID = 1;
const MEETINGS_FOLDER_NAME = 'meetings';
@@ -393,6 +428,7 @@ interface NotesStore {
isDiarizerModelReady: () => Promise<boolean>;
isLocalAsrModelReady: () => Promise<boolean>;
downloadDiarizerModel: () => Promise<void>;
+ isDiarizerModelDownloading: () => boolean;
deleteDiarizerModel: () => Promise<void>;
getNoteSegments: (noteId: number) => Segment[];
getNoteSpeakers: (noteId: number) => Speaker[];
@@ -412,6 +448,13 @@ interface NotesStore {
) => void;
rejectSpeakerSuggestion: (noteId: number, speakerId: number) => void;
renameSpeaker: (noteId: number, speakerId: number, displayName: string) => void;
+ /** Makes a speaker from a meeting just processed on this device your voice profile. */
+ claimSpeakerAsMe: (noteId: number, speakerId: number) => void;
+ /**
+ * Labels a meeting processed since launch with a newly added or retrained voice profile,
+ * leaving speakers already linked to another profile alone.
+ */
+ relabelMeetingSpeakers: (noteId: number, profileId: number) => void;
mergeSpeakers: (noteId: number, sourceSpeakerId: number, targetSpeakerId: number) => void;
}
@@ -704,7 +747,7 @@ export const useNotesStore = create<NotesStore>((set, get) => ({
language: opts.language,
wordTimestamps: opts.wordTimestamps,
}),
- diarizer: getDiarizer(),
+ diarizer: diarizerAfterModelDownload(getDiarizer()),
repo: notesRepository,
},
);
@@ -717,21 +760,7 @@ export const useNotesStore = create<NotesStore>((set, get) => ({
[noteId]: result.speakerEmbeddingsByLabel,
},
}));
- const note = notesRepository.getNoteById(noteId);
- const preferredProfileEmails = getCalendarParticipantEmails(
- parseCalendarParticipants(note?.participants ?? null) ?? [],
- );
- const identification = identifyNoteSpeakers(
- noteId,
- result.speakerEmbeddingsByLabel,
- {
- repo: notesRepository,
- },
- {
- preferredProfileEmails,
- },
- );
- if (identification.updatedSpeakerIds.length > 0) {
+ if (identifyMeetingSpeakers(noteId, result.speakerEmbeddingsByLabel)) {
set((state) => ({ transcriptRevision: state.transcriptRevision + 1 }));
}
await finalizeMeetingNoteContent(noteId);
@@ -771,7 +800,9 @@ export const useNotesStore = create<NotesStore>((set, get) => ({
isDiarizerAvailable: async () => loadDiarizer().isAvailable(),
- isDiarizerModelReady: async () => loadDiarizer().isModelDownloaded(),
+ // A download in flight has created some model files, which reads as downloaded.
+ isDiarizerModelReady: async () =>
+ diarizerModelDownload === null && loadDiarizer().isModelDownloaded(),
// True only if the local ASR model the meeting path would route to is actually downloaded.
isLocalAsrModelReady: async () => {
@@ -780,9 +811,16 @@ export const useNotesStore = create<NotesStore>((set, get) => ({
return LocalTranscriptionService.isReadyForLanguage();
},
- downloadDiarizerModel: async () => {
- await loadDiarizer().downloadModel();
+ // Shared, so a second caller can't start another download into the same model folder.
+ downloadDiarizerModel: () => {
+ diarizerModelDownload ??= loadDiarizer()
+ .downloadModel()
+ .finally(() => {
+ diarizerModelDownload = null;
+ });
+ return diarizerModelDownload;
},
+ isDiarizerModelDownloading: () => diarizerModelDownload !== null,
deleteDiarizerModel: async () => {
await loadDiarizer().deleteModel();
@@ -899,6 +937,49 @@ export const useNotesStore = create<NotesStore>((set, get) => ({
notesRepository.updateSpeaker(speaker.id, patch);
set((state) => ({ transcriptRevision: state.transcriptRevision + 1 }));
},
+ claimSpeakerAsMe: (noteId, speakerId) => {
+ if (notesRepository.getSpeakerProfiles().some((profile) => profile.isOwner === 1)) {
+ throw new SpeakerProfileOwnerAlreadyExistsError();
+ }
+ // A retry rewrites the speakers under new labels before it replaces the samples.
+ if (notesRepository.getTranscriptionStatus(noteId) !== 'done') {
+ throw new Error('This meeting is still being processed.');
+ }
+ const speaker = notesRepository.getSpeakers(noteId).find((row) => row.id === speakerId);
+ if (!speaker) throw new Error('Speaker not found');
+ // Only held in memory for meetings processed since the app started.
+ const embeddings = get().meetingSpeakerEmbeddingsByNoteId[noteId] ?? {};
+ const embedding = embeddings[speaker.speakerLabel];
+ if (!hasFiniteNonZeroNorm(embedding)) {
+ throw new Error("This meeting's voice sample is no longer available.");
+ }
+
+ const profile = notesRepository.createOwnerProfileForSpeaker(
+ speaker.id,
+ {
+ displayName: 'Me',
+ embedding: l2Normalize(embedding),
+ sampleCount: 1,
+ consentAt: new Date().toISOString(),
+ },
+ buildRenameSpeakerPatch(speaker, 'Me'),
+ );
+ set((state) => ({
+ ...reloadVoiceProfiles(),
+ transcriptRevision: state.transcriptRevision + 1,
+ }));
+ // The claim is saved; labelling the rest of the meeting is a bonus that must not undo it.
+ try {
+ get().relabelMeetingSpeakers(noteId, profile.id);
+ } catch (error) {
+ Sentry.captureException(error, { tags: { feature: 'voice-setup' } });
+ }
+ },
+ relabelMeetingSpeakers: (noteId, profileId) => {
+ const embeddings = get().meetingSpeakerEmbeddingsByNoteId[noteId];
+ if (!embeddings || !identifyMeetingSpeakers(noteId, embeddings, profileId)) return;
+ set((state) => ({ transcriptRevision: state.transcriptRevision + 1 }));
+ },
mergeSpeakers: (noteId, sourceSpeakerId, targetSpeakerId) => {
if (sourceSpeakerId === targetSpeakerId) {
throw new Error('Cannot merge a speaker into itself');
diff --git a/openwhispr-mobile/src/sync/__tests__/syncEngine.test.ts b/openwhispr-mobile/src/sync/__tests__/syncEngine.test.ts
index 5e0baa1eb..7877258b0 100644
--- a/openwhispr-mobile/src/sync/__tests__/syncEngine.test.ts
+++ b/openwhispr-mobile/src/sync/__tests__/syncEngine.test.ts
@@ -47,12 +47,16 @@ jest.mock('@/store/useConfigStore', () => ({
useConfigStore: { subscribe: jest.fn(() => () => {}), getState: () => mockConfigState },
}));
+const mockLoadVoiceProfiles = jest.fn();
+const mockSetNotesState = jest.fn();
jest.mock('@/store/useNotesStore', () => ({
useNotesStore: {
+ setState: (...args: unknown[]) => mockSetNotesState(...args),
getState: () => ({
loadFolders: jest.fn(),
loadSpaces: jest.fn(),
loadNotes: jest.fn(),
+ loadVoiceProfiles: mockLoadVoiceProfiles,
}),
},
}));
@@ -690,6 +694,9 @@ describe('subscription cache reset on account switch (fix round 1, Finding 4)',
requestSync('foreground');
await flush();
expect(notesRepository.wipeAllSyncableData).toHaveBeenCalled();
+ // The wipe removed user-1's voice profiles, so the store must drop them too.
+ expect(mockLoadVoiceProfiles).toHaveBeenCalled();
+ expect(mockSetNotesState).toHaveBeenCalledWith({ meetingSpeakerEmbeddingsByNoteId: {} });
// A second foreground trigger for user-2, past the 30s foreground
// throttle. With the cache reset during the switch run above,
diff --git a/openwhispr-mobile/src/sync/syncEngine.ts b/openwhispr-mobile/src/sync/syncEngine.ts
index a30cb1d9b..7d27a7dae 100644
--- a/openwhispr-mobile/src/sync/syncEngine.ts
+++ b/openwhispr-mobile/src/sync/syncEngine.ts
@@ -181,6 +181,10 @@ function reconcileSyncIdentity(auth: ReturnType<typeof useAuthStore.getState>):
level: 'info',
});
notesRepository.wipeAllSyncableData();
+ // The wipe also deleted the previous user's voice profiles; their meeting voice
+ // samples held in memory go with them.
+ useNotesStore.getState().loadVoiceProfiles();
+ useNotesStore.setState({ meetingSpeakerEmbeddingsByNoteId: {} });
resetSubscriptionCache();
// Immediately clear in-memory dictionary + snippets so any UI/transcription
// path that reads them between now and the post-pull load() doesn't see the
diff --git a/openwhispr-mobile/src/types/index.ts b/openwhispr-mobile/src/types/index.ts
index f0e96cd26..ec17e44ef 100644
--- a/openwhispr-mobile/src/types/index.ts
+++ b/openwhispr-mobile/src/types/index.ts
@@ -24,7 +24,8 @@ export interface UserConfig {
languages?: string[];
cloudBackupEnabled?: boolean;
usageAnalyticsEnabled?: boolean;
- voiceProfilePromptDismissedAt?: string;
+ // Its own key, so dismissing the old notes-list voice card doesn't also hide this.
+ voiceSetupBannerDismissedAt?: string;
// One-time Parakeet nudges on the Home banner: pick a language to unlock the
// faster on-device model / download the faster model for a qualifying language.
parakeetAutoLanguageNudgeDismissedAt?: string;