File size: 12,159 Bytes
ae238b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import React, { useState, useRef, useEffect } from 'react';
import { useTranscriptionStore } from '../stores/transcriptionStore';
import { useAudioAnalyzer } from '../hooks/useAudioAnalyzer';

interface MediaRecorderProps {
  onComplete: () => void;
  onCancel: () => void;
}

const MediaRecorder: React.FC<MediaRecorderProps> = ({ onComplete, onCancel }) => {
  const { recordingType, setRecordedBlob } = useTranscriptionStore();

  const [isRecording, setIsRecording] = useState(false);
  const [recordingTime, setRecordingTime] = useState(0);
  const [stream, setStream] = useState<MediaStream | null>(null);
  const [error, setError] = useState<string | null>(null);
  const [permissionState, setPermissionState] = useState<'prompt' | 'granted' | 'denied'>('prompt');
  const [currentMicrophone, setCurrentMicrophone] = useState<string | null>(null);

  const mediaRecorderRef = useRef<MediaRecorder | null>(null);
  const videoRef = useRef<HTMLVideoElement>(null);
  const chunksRef = useRef<Blob[]>([]);
  const timerRef = useRef<number | null>(null);

  const isVideo = recordingType === 'video';

  // Audio analyzer for real-time waveform
  const { audioData, connectToStream, disconnect } = useAudioAnalyzer(256);

  // Get microphone device info
  const getMicrophoneInfo = async (mediaStream: MediaStream) => {
    try {
      // Get all available audio input devices
      const devices = await navigator.mediaDevices.enumerateDevices();
      const audioInputDevices = devices.filter(device => device.kind === 'audioinput');

      // Get the audio track from the current stream
      const audioTrack = mediaStream.getAudioTracks()[0];

      if (audioTrack) {
        // Get the device settings
        const settings = audioTrack.getSettings();
        const deviceId = settings.deviceId;

        // Find the matching device in our list
        const currentDevice = audioInputDevices.find(device => device.deviceId === deviceId);

        if (currentDevice && currentDevice.label) {
          setCurrentMicrophone(currentDevice.label);
        } else {
          // Fallback to device ID if label is not available
          setCurrentMicrophone(`Microphone (${deviceId?.substring(0, 8)}...)`);
        }
      }
    } catch (err) {
      console.error('Error getting microphone info:', err);
      setCurrentMicrophone('Unknown microphone');
    }
  };

  // Request permissions and setup media stream
  const requestPermissions = async () => {
    try {
      setError(null);

      const constraints: MediaStreamConstraints = {
        audio: {
          echoCancellation: true,
          noiseSuppression: true,
          autoGainControl: true,
        },
        video: isVideo ? {
          width: { ideal: 1280 },
          height: { ideal: 720 },
          facingMode: 'user'
        } : false
      };

      const mediaStream = await navigator.mediaDevices.getUserMedia(constraints);
      setStream(mediaStream);
      setPermissionState('granted');

      // Get microphone device information
      await getMicrophoneInfo(mediaStream);

      // Show video preview if recording video
      if (isVideo && videoRef.current) {
        videoRef.current.srcObject = mediaStream;
        videoRef.current.play();
      }

      // Connect audio analyzer for waveform visualization
      connectToStream(mediaStream);

    } catch (err) {
      console.error('Error accessing media devices:', err);
      setPermissionState('denied');

      if (err instanceof DOMException) {
        switch (err.name) {
          case 'NotAllowedError':
            setError('Permission denied. Please allow access to your microphone' + (isVideo ? ' and camera' : '') + '.');
            break;
          case 'NotFoundError':
            setError('No ' + (isVideo ? 'camera or ' : '') + 'microphone found.');
            break;
          case 'NotReadableError':
            setError('Media device is already in use by another application.');
            break;
          default:
            setError('Failed to access media devices: ' + err.message);
        }
      } else {
        setError('An unexpected error occurred while accessing media devices.');
      }
    }
  };

  // Start recording
  const startRecording = () => {
    if (!stream) return;

    try {
      chunksRef.current = [];

      // Try different MIME types in order of preference
      const mimeTypes = isVideo
        ? ['video/webm;codecs=vp9,opus', 'video/webm;codecs=vp8,opus', 'video/webm']
        : ['audio/webm;codecs=opus', 'audio/webm', 'audio/mp4', ''];

      let selectedMimeType = '';
      for (const mimeType of mimeTypes) {
        if (mimeType === '' || window.MediaRecorder.isTypeSupported(mimeType)) {
          selectedMimeType = mimeType;
          break;
        }
      }

      const options: MediaRecorderOptions = selectedMimeType ? { mimeType: selectedMimeType } : {};
      const mediaRecorder = new window.MediaRecorder(stream, options);
      mediaRecorderRef.current = mediaRecorder;

      mediaRecorder.ondataavailable = (event) => {
        if (event.data.size > 0) {
          chunksRef.current.push(event.data);
        }
      };

      mediaRecorder.onstop = () => {
        const blob = new Blob(chunksRef.current, {
          type: isVideo ? 'video/webm' : 'audio/webm'
        });

        setRecordedBlob(blob);
        onComplete();
      };

      mediaRecorder.start();
      setIsRecording(true);
      setRecordingTime(0);

      // Start timer
      timerRef.current = setInterval(() => {
        setRecordingTime(prev => prev + 1);
      }, 1000);

    } catch (err) {
      console.error('Error starting recording:', err);
      setError('Failed to start recording: ' + (err instanceof Error ? err.message : 'Unknown error'));
    }
  };

  // Stop recording
  const stopRecording = () => {
    if (mediaRecorderRef.current && isRecording) {
      mediaRecorderRef.current.stop();
      setIsRecording(false);

      if (timerRef.current) {
        clearInterval(timerRef.current);
        timerRef.current = null;
      }
    }
  };

  // Format recording time
  const formatTime = (seconds: number) => {
    const mins = Math.floor(seconds / 60);
    const secs = seconds % 60;
    return `${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
  };

  // Cleanup on unmount and when recording stops
  useEffect(() => {
    return () => {
      if (stream) {
        stream.getTracks().forEach(track => track.stop());
      }
      if (timerRef.current) {
        clearInterval(timerRef.current);
      }
    };
  }, [stream]);

  // Cleanup stream when recording stops externally
  useEffect(() => {
    if (!recordingType && stream) {
      stream.getTracks().forEach(track => track.stop());
      setStream(null);
      setCurrentMicrophone(null); // Clear microphone info
      disconnect(); // Also disconnect audio analyzer
    }
  }, [recordingType, stream, disconnect]);

  // Auto-request permissions when component mounts
  useEffect(() => {
    if (permissionState === 'prompt') {
      requestPermissions();
    }
  }, []);

  return (
    <div className="flex flex-col items-center justify-center min-h-[400px] bg-gray-900 rounded-lg border-2 border-dashed border-gray-600 p-8">

      {/* Header */}
      <div className="mb-6 text-center">
        <h3 className="text-xl font-semibold text-white mb-2">
          Record {isVideo ? 'Video' : 'Audio'}
        </h3>
        <p className="text-gray-400 text-sm">
          {permissionState === 'prompt' && 'Requesting permissions...'}
          {permissionState === 'denied' && 'Permission required to record'}
          {permissionState === 'granted' && !isRecording && 'Ready to record'}
          {isRecording && `Recording... ${formatTime(recordingTime)}`}
        </p>

        {/* Microphone Device Info */}
        {permissionState === 'granted' && currentMicrophone && (
          <div className="mt-2 flex items-center justify-center gap-2 text-xs text-gray-300">
            <svg className="w-4 h-4 text-blue-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
              <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M19 11a7 7 0 01-7 7m0 0a7 7 0 01-7-7m7 7v4m0 0H8m4 0h4m-4-8a3 3 0 01-3-3V5a3 3 0 116 0v6a3 3 0 01-3 3z" />
            </svg>
            <span className="truncate max-w-xs" title={currentMicrophone}>
              {currentMicrophone}
            </span>
          </div>
        )}
      </div>

      {/* Video Preview (only for video recording) */}
      {isVideo && permissionState === 'granted' && (
        <div className="mb-6">
          <video
            ref={videoRef}
            className="w-80 h-60 bg-black rounded-lg object-cover"
            muted
            playsInline
          />
        </div>
      )}

      {/* Audio Visualization */}
      {permissionState === 'granted' && (
        <div className="mb-6 flex items-center justify-center">
          <div className="w-80 h-20 bg-gray-800 rounded-lg flex items-center justify-center">
            <div className="flex items-center space-x-1">
              {/* Real-time audio visualization bars */}
              {Array.from({ length: 32 }, (_, i) => {
                // Use a wider frequency range for better distribution
                // Map across 60% of the frequency spectrum for voice and some harmonics
                const voiceRangeEnd = Math.floor(audioData.length * 0.6);
                const dataIndex = Math.floor((i / 32) * voiceRangeEnd);
                const amplitude = audioData[dataIndex] || 0;

                // Apply logarithmic scaling to prevent saturation and better distribute levels
                const normalizedAmplitude = amplitude / 255;
                const logScaled = Math.log10(1 + normalizedAmplitude * 9) / Math.log10(10); // Log scale 0-1
                const height = Math.max(4, logScaled * 60); // Scale to 4-60px

                return (
                  <div
                    key={i}
                    className="w-1 bg-blue-500 rounded-full transition-all duration-75"
                    style={{
                      height: `${height}px`
                    }}
                  />
                );
              })}
            </div>
          </div>
        </div>
      )}

      {/* Error Display */}
      {error && (
        <div className="mb-4 p-3 bg-red-900/20 border border-red-500 rounded-lg">
          <p className="text-red-300 text-sm">{error}</p>
        </div>
      )}

      {/* Controls */}
      <div className="flex gap-4">
        {permissionState === 'denied' && (
          <button
            onClick={requestPermissions}
            className="px-6 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded-lg transition-colors"
          >
            Request Permission
          </button>
        )}

        {permissionState === 'granted' && !isRecording && (
          <button
            onClick={startRecording}
            disabled={!stream}
            className="px-6 py-2 bg-red-600 hover:bg-red-700 disabled:bg-gray-600 text-white rounded-lg transition-colors flex items-center gap-2"
          >
            <div className="w-4 h-4 bg-white rounded-full"></div>
            Start Recording
          </button>
        )}

        {isRecording && (
          <button
            onClick={stopRecording}
            className="px-6 py-2 bg-gray-600 hover:bg-gray-700 text-white rounded-lg transition-colors flex items-center gap-2"
          >
            <div className="w-4 h-4 bg-white"></div>
            Stop Recording
          </button>
        )}

        <button
          onClick={onCancel}
          disabled={isRecording}
          className="px-6 py-2 bg-gray-700 hover:bg-gray-600 disabled:bg-gray-800 text-white rounded-lg transition-colors"
        >
          Cancel
        </button>
      </div>

      {/* Tips */}
      <div className="mt-6 text-center">
        <p className="text-gray-400 text-xs max-w-md">
          Speak clearly and minimize background noise for best transcription results.
        </p>
      </div>
    </div>
  );
};

export default MediaRecorder;