1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
import { useState, useCallback, useRef, useEffect } from "react";
export type MicrophoneStatus =
| "idle"
| "requesting"
| "ready"
| "recording"
| "denied"
| "error";
export interface MicrophoneState {
status: MicrophoneStatus;
error: string | null;
sampleRate: number;
channels: number;
volume: number;
}
interface UseMicrophoneOptions {
sampleRate?: number;
onAudioData?: (samples: Float32Array) => void;
}
function getErrorMessage(err: unknown): { message: string; status: MicrophoneStatus } {
if (err instanceof DOMException) {
switch (err.name) {
case "NotAllowedError":
case "PermissionDeniedError":
return { message: "Microphone permission denied", status: "denied" };
case "NotFoundError":
return { message: "No microphone found", status: "error" };
case "NotReadableError":
case "TrackStartError":
return { message: "Microphone is in use by another application", status: "error" };
case "OverconstrainedError":
return { message: "Microphone does not support requested settings", status: "error" };
case "AbortError":
return { message: "Microphone access was aborted", status: "error" };
case "SecurityError":
return { message: "Microphone access blocked (requires HTTPS)", status: "error" };
default:
return { message: `Microphone error: ${err.name} - ${err.message}`, status: "error" };
}
}
if (err instanceof Error) {
return { message: err.message, status: "error" };
}
return { message: "Failed to access microphone", status: "error" };
}
export function useMicrophone(options: UseMicrophoneOptions = {}) {
const { onAudioData } = options;
const [state, setState] = useState<MicrophoneState>({
status: "idle",
error: null,
sampleRate: 48000,
channels: 1,
volume: 0,
});
const streamRef = useRef<MediaStream | null>(null);
const audioContextRef = useRef<AudioContext | null>(null);
const processorRef = useRef<ScriptProcessorNode | null>(null);
const sourceRef = useRef<MediaStreamAudioSourceNode | null>(null);
const onAudioDataRef = useRef(onAudioData);
// Keep callback ref updated
useEffect(() => {
onAudioDataRef.current = onAudioData;
}, [onAudioData]);
// Check if microphone permission is already granted
const checkPermission = useCallback(async (): Promise<boolean> => {
try {
const result = await navigator.permissions.query({
name: "microphone" as PermissionName,
});
return result.state === "granted";
} catch {
return false;
}
}, []);
// Request microphone permission without starting recording
const requestPermission = useCallback(async (): Promise<boolean> => {
setState((s) => ({ ...s, status: "requesting", error: null }));
// Check for secure context
if (typeof window !== "undefined" && !window.isSecureContext) {
setState((s) => ({ ...s, status: "error", error: "Microphone requires HTTPS (or localhost)" }));
return false;
}
try {
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
// Permission granted - stop the stream immediately
stream.getTracks().forEach((track) => track.stop());
setState((s) => ({ ...s, status: "ready", error: null }));
return true;
} catch (err) {
const { message, status } = getErrorMessage(err);
setState((s) => ({ ...s, status, error: message }));
return false;
}
}, []);
const start = useCallback(async (): Promise<boolean> => {
if (state.status === "recording") return true;
setState((s) => ({ ...s, status: "requesting", error: null }));
// Check for secure context
if (typeof window !== "undefined" && !window.isSecureContext) {
setState((s) => ({ ...s, status: "error", error: "Microphone requires HTTPS (or localhost)" }));
return false;
}
let stream: MediaStream;
try {
stream = await navigator.mediaDevices.getUserMedia({ audio: true });
} catch (err) {
const { message, status } = getErrorMessage(err);
setState((s) => ({ ...s, status, error: message }));
return false;
}
try {
streamRef.current = stream;
// Create audio context
const AudioContextClass = window.AudioContext || (window as unknown as { webkitAudioContext: typeof AudioContext }).webkitAudioContext;
const audioContext = new AudioContextClass();
audioContextRef.current = audioContext;
// Resume audio context if it's suspended
if (audioContext.state === "suspended") {
await audioContext.resume();
}
// Create source from microphone
const source = audioContext.createMediaStreamSource(stream);
sourceRef.current = source;
// Use ScriptProcessor for audio processing
const bufferSize = 4096;
const processor = audioContext.createScriptProcessor(bufferSize, 1, 1);
processorRef.current = processor;
processor.onaudioprocess = (event) => {
const inputData = event.inputBuffer.getChannelData(0);
const samples = new Float32Array(inputData.length);
samples.set(inputData);
// Calculate RMS volume (0-1 range)
let sum = 0;
for (let i = 0; i < samples.length; i++) {
sum += samples[i] * samples[i];
}
const rms = Math.sqrt(sum / samples.length);
// Normalize and clamp to 0-1 range (typical speech is around 0.1-0.3 RMS)
const normalizedVolume = Math.min(1, rms * 3);
setState((s) => ({ ...s, volume: normalizedVolume }));
if (onAudioDataRef.current) {
onAudioDataRef.current(samples);
}
};
source.connect(processor);
processor.connect(audioContext.destination);
setState((s) => ({
...s,
status: "recording",
sampleRate: audioContext.sampleRate,
error: null,
}));
return true;
} catch (err) {
stream.getTracks().forEach((track) => track.stop());
streamRef.current = null;
const { message, status } = getErrorMessage(err);
setState((s) => ({ ...s, status, error: message }));
return false;
}
}, [state.status]);
const stop = useCallback(() => {
if (processorRef.current && sourceRef.current) {
try {
sourceRef.current.disconnect(processorRef.current);
processorRef.current.disconnect();
} catch {
// Already disconnected
}
processorRef.current = null;
sourceRef.current = null;
}
if (streamRef.current) {
streamRef.current.getTracks().forEach((track) => track.stop());
streamRef.current = null;
}
if (audioContextRef.current) {
audioContextRef.current.close();
audioContextRef.current = null;
}
setState((s) => ({ ...s, status: "idle", error: null, volume: 0 }));
}, []);
// Cleanup on unmount
useEffect(() => {
return () => {
if (processorRef.current && sourceRef.current) {
try {
sourceRef.current.disconnect(processorRef.current);
processorRef.current.disconnect();
} catch {
// Already disconnected
}
}
if (streamRef.current) {
streamRef.current.getTracks().forEach((track) => track.stop());
}
if (audioContextRef.current) {
audioContextRef.current.close();
}
};
}, []);
return {
...state,
start,
stop,
checkPermission,
requestPermission,
isRecording: state.status === "recording",
isDenied: state.status === "denied",
};
}
|