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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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474

// Voice Fraud Detection Frontend
class VoiceFraudDetectorUI {
    constructor() {
        this.isRecording = false;
        this.mediaRecorder = null;
        this.audioChunks = [];
        this.ws = null;
        this.audioContext = null;
        this.analyser = null;
        this.canvasContext = null;
        this.animationId = null;

        this.initializeElements();
        this.setupEventListeners();
        this.setupWebSocket();
    }

    initializeElements() {
        // Buttons
        this.startBtn = document.getElementById('startBtn');
        this.stopBtn = document.getElementById('stopBtn');
        this.testBtn = document.getElementById('testBtn');
        this.copyBtn = document.getElementById('copyBtn');
        this.clearBtn = document.getElementById('clearBtn');

        // Status elements
        this.statusText = document.getElementById('statusText');
        this.riskBar = document.getElementById('riskBar');

        // Results elements
        this.authenticityScore = document.getElementById('authenticityScore');
        this.syntheticScore = document.getElementById('syntheticScore');
        this.replayRisk = document.getElementById('replayRisk');
        this.confidence = document.getElementById('confidence');

        // Breakdown elements
        this.breakdownSpectral = document.getElementById('breakdownSpectral');
        this.breakdownPitch = document.getElementById('breakdownPitch');
        this.breakdownNoise = document.getElementById('breakdownNoise');
        this.breakdownProsody = document.getElementById('breakdownProsody');

        // Other elements
        this.transcriptionBox = document.getElementById('transcriptionBox');
        this.alertsContainer = document.getElementById('alertsContainer');
        this.sensitivitySlider = document.getElementById('sensitivity');
        this.sensitivityValue = document.getElementById('sensitivityValue');
        this.modelSelect = document.getElementById('modelSelect');
        this.audioVisualizer = document.getElementById('audioVisualizer');

        this.canvasContext = this.audioVisualizer.getContext('2d');
    }

    setupEventListeners() {
        this.startBtn.addEventListener('click', () => this.startRecording());
        this.stopBtn.addEventListener('click', () => this.stopRecording());
        this.testBtn.addEventListener('click', () => this.testConnection());
        this.copyBtn.addEventListener('click', () => this.copyTranscription());
        this.clearBtn.addEventListener('click', () => this.clearTranscription());

        this.sensitivitySlider.addEventListener('input', (e) => {
            const value = e.target.value;
            let label = '';
            if (value <= 3) label = 'Low';
            else if (value <= 7) label = 'Medium';
            else label = 'High';
            this.sensitivityValue.textContent = label;
        });
    }

    setupWebSocket() {
        const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
        const wsUrl = `${protocol}//${window.location.host}/ws`;

        console.log("πŸ”Œ Connecting to WebSocket:", wsUrl);

        this.ws = new WebSocket(wsUrl);

        this.ws.onopen = () => {
            console.log('βœ… WebSocket connected');
            this.addAlert('Connected to analysis server', 'success');
            this.updateStatus('IDLE', 'status-idle');
            
            // Send test message
            this.ws.send(JSON.stringify({ type: 'ping' }));
        };

        this.ws.onmessage = (event) => {
            try {
                const data = JSON.parse(event.data);
                console.log('πŸ“¨ Received:', data);
                this.handleAnalysisResults(data);
            } catch (error) {
                console.error('❌ Error parsing message:', error);
            }
        };

        this.ws.onerror = (error) => {
            console.error('❌ WebSocket error:', error);
            this.addAlert('Connection error. Check if server is running.', 'danger');
        };

        this.ws.onclose = () => {
            console.log('πŸ”Œ WebSocket disconnected');
            this.addAlert('Disconnected from server', 'warning');
            
            // Try to reconnect
            setTimeout(() => {
                console.log('πŸ”„ Attempting to reconnect...');
                this.setupWebSocket();
            }, 3000);
        };
    }

    async startRecording() {
        try {
            console.log('🎀 Starting recording...');
            
            const stream = await navigator.mediaDevices.getUserMedia({
                audio: {
                    channelCount: 1,
                    sampleRate: 16000,
                    echoCancellation: true,
                    noiseSuppression: true
                }
            });

            this.setupAudioVisualizer(stream);

            // Create MediaRecorder with proper MIME type
            const options = { mimeType: 'audio/webm;codecs=opus' };
            this.mediaRecorder = new MediaRecorder(stream, options);

            this.mediaRecorder.ondataavailable = (event) => {
                if (event.data.size > 0) {
                    this.processAudioChunk(event.data);
                }
            };

            // Start recording with 500ms chunks (more reliable)
            this.mediaRecorder.start(500);

            this.isRecording = true;
            this.startBtn.disabled = true;
            this.stopBtn.disabled = false;
            this.updateStatus('LISTENING', 'status-listening');
            this.addAlert('Recording started. Speak now...', 'info');

            console.log('βœ… Recording started');

        } catch (error) {
            console.error('❌ Error accessing microphone:', error);
            this.addAlert('Cannot access microphone. Please check permissions.', 'danger');
        }
    }

    setupAudioVisualizer(stream) {
        try {
            this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
            this.analyser = this.audioContext.createAnalyser();
            const source = this.audioContext.createMediaStreamSource(stream);

            source.connect(this.analyser);
            this.analyser.fftSize = 256;

            this.drawVisualizer();
        } catch (error) {
            console.error('❌ Error setting up visualizer:', error);
        }
    }

    drawVisualizer() {
        if (!this.analyser) return;

        const bufferLength = this.analyser.frequencyBinCount;
        const dataArray = new Uint8Array(bufferLength);

        const draw = () => {
            this.animationId = requestAnimationFrame(draw);
            this.analyser.getByteFrequencyData(dataArray);

            // Clear canvas
            this.canvasContext.fillStyle = 'rgba(0, 0, 0, 0.1)';
            this.canvasContext.fillRect(0, 0, this.audioVisualizer.width, this.audioVisualizer.height);

            // Draw bars
            const barWidth = (this.audioVisualizer.width / bufferLength) * 2.5;
            let x = 0;

            for (let i = 0; i < bufferLength; i++) {
                const barHeight = dataArray[i];
                
                // Create gradient
                const gradient = this.canvasContext.createLinearGradient(
                    0, this.audioVisualizer.height - barHeight,
                    0, this.audioVisualizer.height
                );

                // Color based on frequency range
                if (i < bufferLength / 3) {
                    gradient.addColorStop(0, '#3498db');
                    gradient.addColorStop(1, '#2980b9');
                } else if (i < (bufferLength * 2) / 3) {
                    gradient.addColorStop(0, '#2ecc71');
                    gradient.addColorStop(1, '#27ae60');
                } else {
                    gradient.addColorStop(0, '#e74c3c');
                    gradient.addColorStop(1, '#c0392b');
                }

                this.canvasContext.fillStyle = gradient;
                this.canvasContext.fillRect(
                    x,
                    this.audioVisualizer.height - barHeight,
                    barWidth,
                    barHeight
                );

                x += barWidth + 1;
            }
        };

        draw();
    }

    async processAudioChunk(audioBlob) {
        if (this.ws.readyState !== WebSocket.OPEN) {
            console.warn('WebSocket not ready');
            return;
        }

        try {
            // Convert blob to ArrayBuffer
            const arrayBuffer = await audioBlob.arrayBuffer();
            
            // Convert to base64
            const base64Audio = this.arrayBufferToBase64(arrayBuffer);
            
            // Send to server
            const message = {
                type: 'audio',
                data: base64Audio,
                timestamp: Date.now(),
                sensitivity: this.sensitivitySlider.value,
                model: this.modelSelect.value
            };

            this.ws.send(JSON.stringify(message));
            console.log('πŸ“€ Sent audio chunk:', audioBlob.size, 'bytes');
            
        } catch (error) {
            console.error('❌ Error processing audio chunk:', error);
        }
    }

    arrayBufferToBase64(buffer) {
        let binary = '';
        const bytes = new Uint8Array(buffer);
        for (let i = 0; i < bytes.byteLength; i++) {
            binary += String.fromCharCode(bytes[i]);
        }
        return btoa(binary);
    }

    handleAnalysisResults(data) {
        // Handle errors
        if (data.error) {
            console.error('❌ Server error:', data.error);
            this.addAlert(`Server error: ${data.error}`, 'danger');
            return;
        }

        // Update fraud scores
        if (data.fraud_score !== undefined) {
            const fraudPercent = Math.round(data.fraud_score * 100);
            this.updateRiskBar(fraudPercent);

            // Update all score displays
            this.authenticityScore.textContent = `${100 - fraudPercent}%`;
            this.syntheticScore.textContent = `${fraudPercent}%`;
            this.replayRisk.textContent = `${Math.round(data.replay_risk * 100 || 0)}%`;
            this.confidence.textContent = `${Math.round((data.confidence || 0.5) * 100)}%`;

            // Update breakdown
            this.breakdownSpectral.textContent = data.breakdown?.spectral || 'Normal';
            this.breakdownPitch.textContent = data.breakdown?.pitch || 'Natural';
            this.breakdownNoise.textContent = data.breakdown?.noise || 'Clean';
            this.breakdownProsody.textContent = data.breakdown?.prosody || 'Human-like';

            // Update status based on fraud score
            if (fraudPercent > 70) {
                this.updateStatus('FRAUD DETECTED', 'status-fraud');
                this.addAlert(`🚨 High fraud risk detected (${fraudPercent}%)`, 'danger');
                
                if (document.getElementById('enableAlerts').checked) {
                    this.playAlertSound();
                }
            } else if (fraudPercent > 30) {
                this.updateStatus('SUSPICIOUS', 'status-processing');
                this.addAlert(`⚠️ Moderate fraud risk (${fraudPercent}%)`, 'warning');
            } else {
                this.updateStatus('GENUINE', 'status-genuine');
                this.addAlert(`βœ… Voice appears genuine (${fraudPercent}% risk)`, 'success');
            }
        }

        // Update transcription
        if (data.transcription) {
            this.updateTranscription(data.transcription);
        }

        // Handle alerts from server
        if (data.alert) {
            this.addAlert(data.alert.message, data.alert.type);
        }
    }

    updateRiskBar(percentage) {
        this.riskBar.style.width = `${percentage}%`;
        
        // Update color based on percentage
        if (percentage < 30) {
            this.riskBar.style.background = 'linear-gradient(to right, #2ecc71, #27ae60)';
        } else if (percentage < 70) {
            this.riskBar.style.background = 'linear-gradient(to right, #f1c40f, #f39c12)';
        } else {
            this.riskBar.style.background = 'linear-gradient(to right, #e74c3c, #c0392b)';
        }
    }

    updateTranscription(text) {
        if (!document.getElementById('enableTranscription').checked) return;
        
        const placeholder = this.transcriptionBox.querySelector('.placeholder');
        if (placeholder) placeholder.remove();
        
        const div = document.createElement('div');
        div.className = 'transcription-line';
        div.textContent = `[${new Date().toLocaleTimeString()}] ${text}`;
        
        this.transcriptionBox.appendChild(div);
        this.transcriptionBox.scrollTop = this.transcriptionBox.scrollHeight;
    }

    updateStatus(text, className) {
        this.statusText.textContent = text;
        this.statusText.className = className;
    }

    addAlert(message, type = 'info') {
        const alertDiv = document.createElement('div');
        alertDiv.className = `alert alert-${type}`;

        let icon = 'info-circle';
        if (type === 'warning') icon = 'exclamation-triangle';
        if (type === 'danger') icon = 'exclamation-circle';
        if (type === 'success') icon = 'check-circle';

        alertDiv.innerHTML = `
            <i class="fas fa-${icon}"></i>
            <span>${message}</span>
            <button class="alert-close"><i class="fas fa-times"></i></button>
        `;

        this.alertsContainer.appendChild(alertDiv);

        // Auto-remove info alerts after 5 seconds
        if (type === 'info') {
            setTimeout(() => {
                if (alertDiv.parentNode) {
                    alertDiv.style.opacity = '0';
                    setTimeout(() => alertDiv.remove(), 300);
                }
            }, 5000);
        }

        // Add close button functionality
        alertDiv.querySelector('.alert-close').addEventListener('click', () => {
            alertDiv.style.opacity = '0';
            setTimeout(() => alertDiv.remove(), 300);
        });

        // Scroll to bottom
        this.alertsContainer.scrollTop = this.alertsContainer.scrollHeight;
    }

    stopRecording() {
        if (this.mediaRecorder && this.isRecording) {
            console.log('πŸ›‘ Stopping recording...');
            
            this.mediaRecorder.stop();
            this.mediaRecorder.stream.getTracks().forEach(track => track.stop());

            if (this.animationId) {
                cancelAnimationFrame(this.animationId);
            }

            if (this.audioContext) {
                this.audioContext.close();
            }

            this.isRecording = false;
            this.startBtn.disabled = false;
            this.stopBtn.disabled = true;
            this.updateStatus('IDLE', 'status-idle');
            this.addAlert('Recording stopped', 'info');

            // Clear visualizer
            this.canvasContext.clearRect(0, 0, this.audioVisualizer.width, this.audioVisualizer.height);
            
            console.log('βœ… Recording stopped');
        }
    }

    testConnection() {
        if (this.ws.readyState === WebSocket.OPEN) {
            this.addAlert('Sending test request...', 'info');
            this.ws.send(JSON.stringify({ type: 'test' }));
        } else {
            this.addAlert('WebSocket not connected', 'danger');
        }
    }

    playAlertSound() {
        try {
            const audio = new Audio('https://assets.mixkit.co/sfx/preview/mixkit-alarm-digital-clock-beep-989.mp3');
            audio.volume = 0.3;
            audio.play();
        } catch (error) {
            console.error('Error playing alert sound:', error);
        }
    }

    copyTranscription() {
        const text = this.transcriptionBox.textContent;
        navigator.clipboard.writeText(text).then(() => {
            this.addAlert('Transcription copied to clipboard', 'success');
        });
    }

    clearTranscription() {
        this.transcriptionBox.innerHTML = '<div class="placeholder">Speech will appear here...</div>';
        this.addAlert('Transcription cleared', 'info');
    }
}

// Initialize when page loads
document.addEventListener('DOMContentLoaded', () => {
    console.log('πŸš€ Initializing Voice Fraud Detection System...');
    
    window.voiceDetector = new VoiceFraudDetectorUI();

    // Add keyboard shortcuts
    document.addEventListener('keydown', (e) => {
        // Ctrl+Space to start/stop recording
        if (e.ctrlKey && e.code === 'Space') {
            e.preventDefault();
            if (window.voiceDetector.isRecording) {
                window.voiceDetector.stopRecording();
            } else {
                window.voiceDetector.startRecording();
            }
        }

        // Escape to stop recording
        if (e.code === 'Escape' && window.voiceDetector.isRecording) {
            window.voiceDetector.stopRecording();
        }
    });

    console.log('%cπŸ” Voice Fraud Detection System Ready', 'color: #3498db; font-size: 16px; font-weight: bold;');
    console.log('%c🎯 Press Ctrl+Space to start/stop recording', 'color: #2ecc71;');
    console.log('%cπŸ›‘ Press Escape to stop recording', 'color: #e74c3c;');
});