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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>OmniVoice | Realtime Audio Interface</title>
    <!-- FontAwesome for Icons -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <!-- Google Fonts -->
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;600;800&family=Space+Grotesk:wght@500;700" rel="stylesheet">
    
    <style>
        :root {
            --bg-color: #050505;
            --surface-color: #121212;
            --primary-accent: #00f2ff;
            --secondary-accent: #7000ff;
            --text-color: #e0e0e0;
            --glass-border: rgba(255, 255, 255, 0.1);
            --glass-bg: rgba(20, 20, 20, 0.6);
            --font-main: 'Inter', sans-serif;
            --font-display: 'Space Grotesk', sans-serif;
        }

        * {
            box-sizing: box-box;
            margin: 0;
            padding: 0;
        }

        body {
            background-color: var(--bg-color);
            color: var(--text-color);
            font-family: var(--font-main);
            height: 100vh;
            overflow: hidden;
            display: flex;
            flex-direction: column;
            background-image: 
                radial-gradient(circle at 50% 10%, rgba(0, 242, 255, 0.15) 0%, transparent 40%),
                radial-gradient(circle at 80% 80%, rgba(112, 0, 255, 0.15) 0%, transparent 40%);
        }

        /* --- Header --- */
        header {
            padding: 1.5rem 2rem;
            display: flex;
            justify-content: space-between;
            align-items: center;
            z-index: 10;
        }

        .brand {
            font-family: var(--font-display);
            font-weight: 700;
            font-size: 1.5rem;
            letter-spacing: -1px;
            color: white;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .brand i { color: var(--primary-accent); }

        .anycoder-link {
            font-size: 0.85rem;
            background: rgba(255,255,255,0.05);
            padding: 8px 16px;
            border-radius: 20px;
            text-decoration: none;
            color: #888;
            border: 1px solid var(--glass-border);
            transition: all 0.3s ease;
            font-weight: 600;
        }

        .anycoder-link:hover {
            background: rgba(0, 242, 255, 0.1);
            color: var(--primary-accent);
            border-color: var(--primary-accent);
        }

        /* --- Main Interface --- */
        main {
            flex: 1;
            position: relative;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            overflow: hidden;
        }

        /* Visualizer Container */
        .visualizer-container {
            position: relative;
            width: 400px;
            height: 400px;
            margin-bottom: 2rem;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        canvas {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            z-index: 2;
        }

        .core-glow {
            width: 100px;
            height: 100px;
            background: radial-gradient(circle, var(--primary-accent) 0%, transparent 70%);
            border-radius: 50%;
            opacity: 0.5;
            filter: blur(20px);
            animation: pulse 2s infinite ease-in-out;
            z-index: 1;
        }

        @keyframes pulse {
            0% { transform: scale(1); opacity: 0.4; }
            50% { transform: scale(1.2); opacity: 0.6; }
            100% { transform: scale(1); opacity: 0.4; }
        }

        /* --- Controls & UI --- */
        .ui-panel {
            width: 90%;
            max-width: 600px;
            background: var(--glass-bg);
            backdrop-filter: blur(12px);
            border: 1px solid var(--glass-border);
            border-radius: 24px;
            padding: 2rem;
            display: flex;
            flex-direction: column;
            gap: 1.5rem;
            box-shadow: 0 20px 50px rgba(0,0,0,0.5);
            z-index: 5;
        }

        .status-bar {
            display: flex;
            justify-content: space-between;
            font-size: 0.9rem;
            color: #666;
            text-transform: uppercase;
            letter-spacing: 1px;
            font-weight: 600;
        }

        .status-indicator {
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .status-dot {
            width: 8px;
            height: 8px;
            background: #333;
            border-radius: 50%;
            transition: background 0.3s;
        }

        .status-dot.active { background: var(--primary-accent); box-shadow: 0 0 10px var(--primary-accent); }
        .status-dot.processing { background: var(--secondary-accent); box-shadow: 0 0 10px var(--secondary-accent); }

        .transcript-area {
            height: 120px;
            overflow-y: auto;
            font-size: 1rem;
            line-height: 1.6;
            color: #ccc;
            border-left: 2px solid var(--glass-border);
            padding-left: 1rem;
        }

        .transcript-area::-webkit-scrollbar { width: 4px; }
        .transcript-area::-webkit-scrollbar-thumb { background: #333; border-radius: 2px; }

        .controls {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 1rem;
        }

        .btn {
            background: transparent;
            border: 1px solid var(--glass-border);
            color: white;
            padding: 1rem;
            border-radius: 12px;
            font-family: var(--font-display);
            font-weight: 700;
            cursor: pointer;
            transition: all 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275);
            display: flex;
            justify-content: center;
            align-items: center;
            gap: 10px;
            text-transform: uppercase;
            font-size: 0.9rem;
        }

        .btn:hover {
            background: rgba(255,255,255,0.05);
            transform: translateY(-2px);
        }

        .btn-primary {
            background: var(--primary-accent);
            color: #000;
            border: none;
            box-shadow: 0 0 20px rgba(0, 242, 255, 0.3);
        }

        .btn-primary:hover {
            background: #fff;
            box-shadow: 0 0 30px rgba(0, 242, 255, 0.6);
        }

        .btn-danger {
            background: rgba(255, 50, 50, 0.1);
            border-color: rgba(255, 50, 50, 0.3);
            color: #ff5050;
        }
        
        .btn-danger:hover {
            background: rgba(255, 50, 50, 0.2);
        }

        /* --- Responsive --- */
        @media (max-width: 600px) {
            .visualizer-container { width: 300px; height: 300px; }
            .ui-panel { width: 95%; padding: 1.5rem; }
            .controls { grid-template-columns: 1fr; }
            .brand { font-size: 1.2rem; }
        }
    </style>
</head>
<body>

    <header>
        <div class="brand">
            <i class="fa-solid fa-wave-square"></i>
            OmniVoice
        </div>
        <a href="https://huggingface.co/spaces/akhaliq/anycoder" class="anycoder-link" target="_blank">
            Built with anycoder <i class="fa-solid fa-arrow-up-right-from-square"></i>
        </a>
    </header>

    <main>
        <div class="visualizer-container">
            <div class="core-glow"></div>
            <canvas id="visualizer"></canvas>
        </div>

        <div class="ui-panel">
            <div class="status-bar">
                <div class="status-indicator">
                    <div class="status-dot" id="mic-status"></div>
                    <span id="status-text">SYSTEM IDLE</span>
                </div>
                <div class="volume-indicator">
                    <i class="fa-solid fa-volume-high"></i>
                </div>
            </div>

            <div class="transcript-area" id="transcript">
                <span style="opacity: 0.5;">Waiting for audio input...</span>
            </div>

            <div class="controls">
                <button class="btn btn-primary" id="start-btn">
                    <i class="fa-solid fa-microphone"></i> Listen
                </button>
                <button class="btn btn-danger" id="stop-btn">
                    <i class="fa-solid fa-stop"></i> Stop
                </button>
                <button class="btn" id="speak-btn">
                    <i class="fa-solid fa-volume-high"></i> Test Voice
                </button>
            </div>
        </div>
    </main>

    <script>
        // --- Audio Visualizer Logic ---
        const canvas = document.getElementById('visualizer');
        const ctx = canvas.getContext('2d');
        let audioContext, analyser, microphone, javascriptNode;
        let isListening = false;
        let animationId;

        // Resize canvas for high DPI
        function resizeCanvas() {
            const container = canvas.parentElement;
            canvas.width = container.clientWidth;
            canvas.height = container.clientHeight;
        }
        window.addEventListener('resize', resizeCanvas);
        resizeCanvas();

        // --- Voice Interaction Logic (Speech Recognition) ---
        const transcriptEl = document.getElementById('transcript');
        const statusText = document.getElementById('status-text');
        const micStatus = document.getElementById('mic-status');
        
        // Check browser support
        const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
        let recognizer;

        if (SpeechRecognition) {
            recognizer = new SpeechRecognition();
            recognizer.continuous = true;
            recognizer.intermediateResults = true;

            recognizer.onresult = (event) => {
                let interimTranscript = '';
                let finalTranscript = '';

                for (let i = event.resultIndex; i < event.results.length; ++i) {
                    if (event.results[i].isFinal) {
                        finalTranscript += event.results[i][0].transcript;
                        processVoiceCommand(finalTranscript);
                    } else {
                        interimTranscript += event.results[i][0].transcript;
                    }
                }

                if (interimTranscript) {
                    transcriptEl.innerText = `Listening: ${interimTranscript}`;
                    micStatus.classList.add('processing');
                }
                
                if (finalTranscript) {
                    transcriptEl.innerText = `You said: "${finalTranscript}"`;
                    micStatus.classList.remove('processing');
                    micStatus.classList.add('active');
                }
            };

            recognizer.onerror = (event) => {
                console.error('Speech recognition error', event.error);
                transcriptEl.innerText = "Error: " + event.error;
            };
            
            recognizer.onend = () => {
                micStatus.classList.remove('active');
                statusText.innerText = "SYSTEM IDLE";
            };
        } else {
            transcriptEl.innerText = "Speech Recognition not supported in this browser.";
        }

        // --- Audio Context / Visualizer ---
        async function startVisualizer() {
            try {
                audioContext = new (window.AudioContext || window.webkitAudioContext)();
                analyser = audioContext.createAnalyser();
                
                const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
                microphone = audioContext.createMediaStreamSource(stream);
                microphone.connect(analyser);
                
                analyser.fftSize = 2048;
                isListening = true;
                draw();
                
                statusText.innerText = "AUDIO INPUT ACTIVE";
                micStatus.classList.add('active');
                transcriptEl.innerText = "Visualizing audio frequency...";
                
            } catch (err) {
                console.error('The audioContext failed', err);
                transcriptEl.innerText = "Error: Microphone access denied or blocked.";
            }
        }

        function stopVisualizer() {
            if (audioContext) {
                audioContext.close();
                audioContext = null;
            }
            isListening = false;
            if (animationId) clearTimeout(animationId);
            micStatus.classList.remove('active');
            statusText.innerText = "SYSTEM IDLE";
            
            // Clear canvas
            ctx.clearRect(0, 0, canvas.width, canvas.height);
        }

        function draw() {
            if (!isListening) return;
            
            const width = canvas.width;
            const height = canvas.height;
            const centerX = width / 2;
            const centerY = height / 2;
            
            ctx.clearRect(0, 0, width, height);
            
            const bufferLength = analyser.frequencyBinCount;
            const dataArray = new Uint8Array(bufferLength);
            analyser.getByteFrequencyData(dataArray);
            
            // Calculate average volume for pulsing effect
            let sum = 0;
            for(let i = 0; i < bufferLength; i++) {
                sum += dataArray[i];
            }
            const average = sum / bufferLength;
            const radius = 50 + (average * 0.5); // Dynamic radius

            // Draw Circular Spectrum
            ctx.beginPath();
            ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI);
            ctx.strokeStyle = "rgba(0, 242, 255, 0.2)";
            ctx.lineWidth = 2;
            ctx.stroke();

            // Draw Frequency Bars
            const bars = 60; // Number of bars to draw
            const step = (Math.PI * 2) / bars;
            
            for (let i = 0; i < bars; i++) {
                // Get data from lower frequencies mostly
                const value = dataArray[i * 4]; 
                const barHeight = value * 1.5;
                
                const angle = i * step;
                
                // Calculate start and end points for the bar
                const x1 = centerX + Math.cos(angle) * radius;
                const y1 = centerY + Math.sin(angle) * radius;
                const x2 = centerX + Math.cos(angle) * (radius + barHeight);
                const y2 = centerY + Math.sin(angle) * (radius + barHeight);
                
                ctx.beginPath();
                ctx.moveTo(x1, y1);
                ctx.lineTo(x2, y2);
                
                // Gradient color based on height
                const hue = (i / bars) * 360;
                ctx.strokeStyle = `hsl(${hue}, 100%, 50%)`;
                ctx.lineWidth = 3;
                ctx.lineCap = 'round';
                ctx.stroke();
            }

            animationId = requestAnimationFrame(draw);
        }

        // --- Voice Command Simulation ---
        function processVoiceCommand(text) {
            // Simple keyword matching to simulate "AI" response
            text = text.toLowerCase();
            let response = "";

            if (text.includes("hello") || text.includes("hi")) {
                response = "Hello! I am ready to assist you with audio processing.";
            } else if (text.includes("time")) {
                response = "The current time is " + new Date().toLocaleTimeString();
            } else if (text.includes("status")) {
                response = "System status: All audio inputs are functioning normally.";
            } else if (text.includes("stop")) {
                response = "Stopping audio processing...";
                stopVisualizer();
                if(recognizer) recognizer.stop();
            } else if (text.includes("start") || text.includes("listen")) {
                response = "Starting audio capture mode.";
                startVisualizer();
                if(recognizer) recognizer.start();
            } else {
                response = "I heard: " + text + ". I am currently a client-side demo, but I can visualize your voice!";
            }

            speak(response);
        }

        // --- Text to Speech ---
        function speak(text) {
            if ('speechSynthesis' in window) {
                const utterance = new SpeechSynthesisUtterance(text);
                utterance.voice = speechSynthesis.getVoices()[0]; // Default voice
                utterance.rate = 1;
                utterance.pitch = 1;
                speechSynthesis.speak(utterance);
                
                transcriptEl.innerText = `System: "${text}"`;
            }
        }

        // --- Button Handlers ---
        document.getElementById('start-btn').addEventListener('click', () => {
            startVisualizer();
            if(recognizer) recognizer.start();
        });

        document.getElementById('stop-btn').addEventListener('click', () => {
            stopVisualizer();
            if(recognizer) recognizer.stop();
        });

        document.getElementById('speak-btn').addEventListener('click', () => {
            speak("This is a test of the OmniVoice system. Audio visualization is active.");
        });

    </script>
</body>
</html>