@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
tsx 176 lines 7.05 KB
Raw
1 import { runAtomCommand } from "@t3tools/client-runtime/state/runtime";
2 import { useAtomValue } from "@effect/atom-react";
3 import { AudioModule, RecordingPresets, type RecordingStatus } from "expo-audio";
4 import { File } from "expo-file-system";
5 import { activateKeepAwakeAsync, deactivateKeepAwake } from "expo-keep-awake";
6 import { useSyncExternalStore } from "react";
7 import { Platform } from "react-native";
8 import { Atom, AsyncResult } from "effect/reactivity";
9 import { makeMutable } from "react-native-reanimated";
10
11 import {
12 type MicrophoneEntry,
13 microphoneOrder,
14 preferredMicrophoneInput,
15 rememberMicrophones,
16 } from "../../lib/microphonePriority";
17 import { getLocalVoiceTranscriber } from "../../native/voiceTranscription";
18 import { createVoiceRecorder } from "../../native/voiceRecorder";
19 import { mobilePreferencesAtom, saveMobilePreferences } from "../../state/preferences";
20 import { getNativeShowcaseScene } from "../showcase/nativeShowcaseScene";
21 import {
22 voiceInputBlocksSubmission,
23 type VoiceTranscriber,
24 } from "@t3tools/client-runtime/voice-input";
25 import { createLazyVoiceRecorder } from "./lazyVoiceRecorder";
26 import { VOICE_WAVEFORM_SAMPLE_COUNT } from "./voiceInputMetering";
27 import { VoiceInputSession } from "./voiceInputSession";
28 import { createVoiceRecordingPermission } from "./voiceRecordingPermission";
29 import { voiceRouteAtom } from "./voiceDestination";
30 import { makePermissionStatusAtom } from "../../lib/permissionStatusAtom";
31 import {
32 acquireAudioSession,
33 activateAudioSession,
34 releaseAudioSession,
35 } from "../../lib/audioSession";
36
37 // The native constructor expects platform-flattened options, as built by useAudioRecorder.
38 const { ios: IOS_RECORDING_OPTIONS, android: ANDROID_RECORDING_OPTIONS } =
39 RecordingPresets.HIGH_QUALITY;
40 const VOICE_RECORDING_OPTIONS = {
41 extension: RecordingPresets.HIGH_QUALITY.extension,
42 sampleRate: RecordingPresets.HIGH_QUALITY.sampleRate,
43 numberOfChannels: RecordingPresets.HIGH_QUALITY.numberOfChannels,
44 bitRate: RecordingPresets.HIGH_QUALITY.bitRate,
45 isMeteringEnabled: true,
46 ...(Platform.OS === "ios" ? IOS_RECORDING_OPTIONS : ANDROID_RECORDING_OPTIONS),
47 };
48
49 function keepScreenAwake(tag: string): () => void {
50 const activation = activateKeepAwakeAsync(tag);
51 void activation.catch(() => {});
52 // Release after activation settles, even if the recording ends immediately.
53 return () => void activation.then(() => deactivateKeepAwake(tag)).catch(() => {});
54 }
55
56 export const deviceMicrophoneOrder = (saved?: ReadonlyArray<MicrophoneEntry>) =>
57 microphoneOrder(saved, Platform.OS === "android" ? "android" : "ios");
58
59 /** "checking" until the first availability probe settles; re-probed on foreground. */
60 export const localVoiceTranscriberAtom = makePermissionStatusAtom<
61 VoiceTranscriber | null | "checking"
62 >({
63 label: "mobile:voice-input:transcriber",
64 initial: "checking",
65 read: () => getLocalVoiceTranscriber().catch(() => null),
66 }).pipe(Atom.keepAlive);
67
68 const voiceInputAvailableAtom = Atom.make((get) => {
69 const route = get(voiceRouteAtom);
70 if (route) return route.transcriber !== null;
71 const transcriber = get(localVoiceTranscriberAtom);
72 return transcriber !== null && transcriber !== "checking";
73 });
74
75 /**
76 * The app has one microphone and one dictation at a time, so the session
77 * lives for the registry's lifetime rather than any screen's.
78 */
79 const voiceInputAtom = Atom.make((get) => {
80 const registry = get.registry;
81 // Mounted so the recorder can read known microphones synchronously.
82 get.mount(mobilePreferencesAtom);
83 const audioLevels = makeMutable<ReadonlyArray<number>>(
84 Array<number>(VOICE_WAVEFORM_SAMPLE_COUNT).fill(0),
85 );
86 let keepAwakeSequence = 0;
87 let audioToken: ReturnType<typeof acquireAudioSession> | null = null;
88 const permission = createVoiceRecordingPermission(() => {
89 void session.controller.appMovedToBackground();
90 });
91 const recorder = createLazyVoiceRecorder({
92 getDestination: () => (registry.get(voiceRouteAtom) === null ? "local" : "environment"),
93 create: (destination) =>
94 destination === "local"
95 ? createVoiceRecorder(VOICE_RECORDING_OPTIONS)
96 : new AudioModule.AudioRecorder(VOICE_RECORDING_OPTIONS),
97 onStatus: (status: RecordingStatus) => {
98 session.controller.handleRecorderStatus({
99 isFinished: status.isFinished,
100 hasError: status.hasError || status.mediaServicesDidReset === true,
101 error: status.error,
102 url: status.url,
103 });
104 },
105 // Read when recording starts so preference changes do not re-render every composer.
106 selectInput: (inputs) => {
107 const preferences = registry.get(mobilePreferencesAtom);
108 if (!AsyncResult.isSuccess(preferences)) return null;
109 const known = deviceMicrophoneOrder(preferences.value.microphones);
110 const order = rememberMicrophones(known, inputs);
111 if (order !== known) {
112 void runAtomCommand(registry, saveMobilePreferences, {
113 transform: (current) => ({
114 microphones: rememberMicrophones(deviceMicrophoneOrder(current.microphones), inputs),
115 }),
116 });
117 }
118 return preferredMicrophoneInput(inputs, order);
119 },
120 });
121 const session: VoiceInputSession = new VoiceInputSession({
122 recorder,
123 getTranscriber: () => {
124 const route = registry.get(voiceRouteAtom);
125 if (route) return route.transcriber;
126 const transcriber = registry.get(localVoiceTranscriberAtom);
127 return transcriber === "checking" ? null : transcriber;
128 },
129 requestPermission: permission.requestPermission,
130 configureRecording: async () => {
131 const token = acquireAudioSession("recording");
132 audioToken = token;
133 try {
134 await activateAudioSession(token);
135 } catch (error) {
136 await releaseAudioSession(token).catch(() => {});
137 throw error;
138 }
139 },
140 releaseRecording: async () => {
141 if (audioToken) await releaseAudioSession(audioToken);
142 audioToken = null;
143 },
144 deleteRecording: (uri) => new File(uri).delete(),
145 sampleRecording: () => {
146 const status = recorder.getStatus();
147 return status?.isRecording
148 ? { durationMillis: status.durationMillis, metering: status.metering }
149 : null;
150 },
151 publishAudioLevels: (levels) => audioLevels.set(levels),
152 keepAwake: () => keepScreenAwake(`voice-input:${++keepAwakeSequence}`),
153 });
154 get.subscribe(voiceRouteAtom, () => session.controller.cancel());
155 get.addFinalizer(() => {
156 permission.dispose();
157 session.dispose();
158 recorder.release();
159 });
160 return { session, audioLevels };
161 }).pipe(Atom.keepAlive, Atom.withLabel("mobile:voice-input"));
162
163 export function useGlobalVoiceInput() {
164 const { session, audioLevels } = useAtomValue(voiceInputAtom);
165 const isTranscriberAvailable = useAtomValue(voiceInputAvailableAtom);
166 const snapshot = useSyncExternalStore(session.subscribe, session.getSnapshot);
167 return {
168 ...snapshot,
169 isAvailable: isTranscriberAvailable || getNativeShowcaseScene() !== null,
170 audioLevels,
171 isBusy: voiceInputBlocksSubmission(snapshot.state),
172 session,
173 stop: () => session.controller.stop(),
174 cancel: () => session.controller.cancel(),
175 };
176 }