File size: 22,086 Bytes
9977a2c
 
4cd60b2
9977a2c
 
 
9456679
9977a2c
 
fb6cc47
 
 
 
 
5a218c9
19453ca
 
fb6cc47
19453ca
 
9977a2c
 
 
 
 
 
 
 
 
 
 
19453ca
9977a2c
fb6cc47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a218c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9977a2c
 
 
19453ca
9977a2c
 
fb6cc47
 
19453ca
9977a2c
fb6cc47
 
 
9977a2c
fb6cc47
9977a2c
fb6cc47
9977a2c
fb6cc47
 
9977a2c
fb6cc47
 
 
9977a2c
 
fb6cc47
9977a2c
fb6cc47
19453ca
fb6cc47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9977a2c
fb6cc47
 
 
 
 
 
 
 
910c6cf
 
fb6cc47
910c6cf
fb6cc47
 
 
 
 
 
 
 
9977a2c
fb6cc47
 
 
 
 
 
 
 
 
 
 
 
 
 
9977a2c
fb6cc47
 
 
9977a2c
19453ca
9977a2c
fb6cc47
 
9977a2c
 
 
fb6cc47
 
 
 
 
 
 
 
 
 
 
 
 
 
4cd60b2
fb6cc47
4cd60b2
fb6cc47
4cd60b2
 
 
 
 
fb6cc47
4cd60b2
 
fb6cc47
4cd60b2
fb6cc47
19453ca
fb6cc47
19453ca
 
 
910c6cf
fb6cc47
19453ca
910c6cf
19453ca
9977a2c
 
4cd60b2
19453ca
55525a7
5a218c9
fb6cc47
9977a2c
9456679
 
fb6cc47
 
 
 
 
9977a2c
fb6cc47
 
 
 
 
 
 
 
 
 
9977a2c
fb6cc47
 
 
 
26b739b
 
fb6cc47
26b739b
 
fb6cc47
 
 
 
 
 
 
 
9456679
9977a2c
fb6cc47
9977a2c
fb6cc47
9977a2c
4cd60b2
910c6cf
 
fb6cc47
 
5a218c9
fb6cc47
 
19453ca
5a218c9
fb6cc47
3c613c9
19453ca
 
fb6cc47
 
19453ca
5a218c9
fb6cc47
3c613c9
19453ca
5a218c9
 
fb6cc47
19453ca
 
 
fb6cc47
 
4cd60b2
 
fb6cc47
 
 
 
19453ca
fb6cc47
 
 
 
19453ca
fb6cc47
 
 
 
de74a5b
fb6cc47
 
 
 
 
 
 
 
19453ca
fb6cc47
3c613c9
fb6cc47
5a218c9
 
19453ca
fb6cc47
19453ca
fb6cc47
5a218c9
fb6cc47
19453ca
910c6cf
fb6cc47
4cd60b2
fb6cc47
19453ca
fb6cc47
5a218c9
fb6cc47
910c6cf
fb6cc47
910c6cf
fb6cc47
 
 
4cd60b2
910c6cf
fb6cc47
 
 
 
 
 
 
 
 
 
 
 
 
 
910c6cf
 
fb6cc47
4cd60b2
 
 
5a218c9
 
4cd60b2
 
910c6cf
9977a2c
 
 
 
 
4cd60b2
 
87ab4e6
 
9977a2c
 
 
19453ca
55525a7
 
 
 
fb6cc47
55525a7
 
 
 
19453ca
fb6cc47
 
 
 
 
9456679
fb6cc47
 
9456679
fb6cc47
 
 
 
9977a2c
 
 
 
fb6cc47
9977a2c
 
 
fb6cc47
 
9977a2c
fb6cc47
d75ee3c
fb6cc47
 
 
 
 
 
 
 
d75ee3c
fb6cc47
 
 
4cd60b2
fb6cc47
4cd60b2
d75ee3c
fb6cc47
4cd60b2
9977a2c
 
 
 
 
 
fb6cc47
 
9977a2c
55525a7
 
 
 
 
910c6cf
 
55525a7
 
fb6cc47
55525a7
 
 
910c6cf
 
 
55525a7
 
 
 
fb6cc47
4cd60b2
9977a2c
 
fb6cc47
 
4cd60b2
 
 
 
 
 
 
 
 
 
 
 
 
fb6cc47
 
9977a2c
 
 
 
 
 
 
 
 
fb6cc47
9977a2c
55525a7
fb6cc47
87ab4e6
55525a7
 
fb6cc47
9977a2c
fb6cc47
9977a2c
 
55525a7
87ab4e6
9977a2c
 
fb6cc47
4cd60b2
 
fb6cc47
4cd60b2
 
fb6cc47
4cd60b2
fb6cc47
 
 
 
 
 
 
 
4cd60b2
fb6cc47
4cd60b2
 
fb6cc47
9977a2c
 
 
 
fb6cc47
9977a2c
 
 
fb6cc47
9977a2c
 
 
fb6cc47
 
55525a7
9977a2c
fb6cc47
9977a2c
 
 
 
 
 
fb6cc47
9977a2c
 
fb6cc47
 
a6aa023
9977a2c
 
4cd60b2
9977a2c
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Real-Time Experts</title>
    <style>
        :root {
            /* Base colors */
            --color-default: #0066B3;
            --color-behavior: #FCBA40;
            --color-udl: #A50064;
            --color-prompt3: #11C7B5;
            --color-custom: #FF6B35;
            --color-background: #FFFFFF;
            --color-text: #333333;
            --color-light-gray: #F5F5F5;
        }
        
        body {
            margin: 0;
            padding: 0;
            background-color: var(--color-background);
            color: var(--color-text);
            font-family: system-ui, -apple-system, sans-serif;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            transition: all 0.3s ease;
        }
        
        .app-title {
            font-size: 2.25rem;
            font-weight: 600;
            margin-bottom: 0.5rem;
            transition: color 0.3s ease;
            color: #333333;
        }
        
        .subtitle {
            font-size: 1.125rem;
            margin-bottom: 1rem;
            color: #555555;
        }
        
        .api-link {
            margin-bottom: 2rem;
            transition: color 0.3s ease;
        }
        
        .mode-indicator {
            position: absolute;
            top: 15px;
            right: 15px;
            padding: 0.5rem 1rem;
            border-radius: 2rem;
            font-weight: 600;
            color: white;
            transition: all 0.3s ease;
        }
        
        /* Color themes */
        body.theme-default .app-title,
        body.theme-default a {
            color: var(--color-default);
        }
        body.theme-default .box,
        body.theme-default .start-button {
            background-color: var(--color-default);
        }
        body.theme-default .mode-indicator {
            background-color: var(--color-default);
        }
        body.theme-default .prompt-select {
            border-color: var(--color-default);
        }
        
        body.theme-behavior .app-title,
        body.theme-behavior a {
            color: var(--color-behavior);
        }
        body.theme-behavior .box,
        body.theme-behavior .start-button {
            background-color: var(--color-behavior);
        }
        body.theme-behavior .mode-indicator {
            background-color: var(--color-behavior);
            color: #333333;
        }
        body.theme-behavior .prompt-select {
            border-color: var(--color-behavior);
        }
        
        body.theme-udl .app-title,
        body.theme-udl a {
            color: var(--color-udl);
        }
        body.theme-udl .box,
        body.theme-udl .start-button {
            background-color: var(--color-udl);
        }
        body.theme-udl .mode-indicator {
            background-color: var(--color-udl);
        }
        body.theme-udl .prompt-select {
            border-color: var(--color-udl);
        }
        
        body.theme-prompt3 .app-title,
        body.theme-prompt3 a {
            color: var(--color-prompt3);
        }
        body.theme-prompt3 .box,
        body.theme-prompt3 .start-button {
            background-color: var(--color-prompt3);
        }
        body.theme-prompt3 .mode-indicator {
            background-color: var(--color-prompt3);
        }
        body.theme-prompt3 .prompt-select {
            border-color: var(--color-prompt3);
        }
        
        body.theme-custom .app-title,
        body.theme-custom a {
            color: var(--color-custom);
        }
        body.theme-custom .box,
        body.theme-custom .start-button {
            background-color: var(--color-custom);
        }
        body.theme-custom .mode-indicator {
            background-color: var(--color-custom);
        }
        body.theme-custom .prompt-select {
            border-color: var(--color-custom);
        }
        
        .container {
            width: 90%;
            max-width: 800px;
            background-color: var(--color-background);
            padding: 2rem;
            border-radius: 1rem;
            box-shadow: 0 10px 20px rgba(0, 0, 0, 0.05);
            border: 1px solid rgba(0, 0, 0, 0.05);
            transition: all 0.3s ease;
        }
        
        .form-group {
            margin-bottom: 1.5rem;
        }
        
        label {
            display: block;
            font-weight: 500;
            margin-bottom: 0.5rem;
            color: #555555;
        }
        
        .input-field {
            width: calc(100% - 1.5rem);
            padding: 0.75rem;
            border-radius: 0.5rem;
            border: 1px solid #e0e0e0;
            font-size: 1rem;
            background-color: white;
        }
        
        .prompt-select {
            width: 100%;
            padding: 0.75rem;
            border-radius: 0.5rem;
            border-width: 2px;
            border-style: solid;
            font-size: 1rem;
            background-color: white;
            appearance: none;
            background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24' fill='none' stroke='%23555555' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
            background-repeat: no-repeat;
            background-position: right 0.75rem center;
            background-size: 1rem;
            transition: border-color 0.3s ease;
        }
        
        .custom-prompt {
            width: calc(100% - 1.5rem);
            padding: 0.75rem;
            min-height: 80px;
            border-radius: 0.5rem;
            border: 1px solid #e0e0e0;
            font-size: 1rem;
            font-family: inherit;
            resize: vertical;
            display: none;
        }
        
        .visualization {
            height: 100px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            margin: 2rem 0;
            padding: 1rem;
            border-radius: 0.5rem;
            background-color: var(--color-light-gray);
        }
        
        .box {
            height: 80px;
            width: 8px;
            border-radius: 4px;
            transition: transform 0.05s ease, background-color 0.3s ease;
        }
        
        .start-button {
            display: inline-block;
            padding: 0.75rem 2rem;
            font-size: 1rem;
            font-weight: 600;
            color: white;
            border: none;
            border-radius: 0.5rem;
            cursor: pointer;
            transition: all 0.3s ease;
        }
        
        .start-button:hover {
            opacity: 0.9;
            transform: translateY(-1px);
        }
        
        .toast {
            position: fixed;
            top: 20px;
            left: 50%;
            transform: translateX(-50%);
            padding: 0.75rem 1.5rem;
            border-radius: 0.5rem;
            background-color: #f44336;
            color: white;
            font-weight: 500;
            z-index: 1000;
            display: none;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
        }
        
        .spinner {
            display: inline-block;
            width: 20px;
            height: 20px;
            border: 2px solid white;
            border-top-color: transparent;
            border-radius: 50%;
            margin-right: 0.5rem;
            animation: spin 1s linear infinite;
        }
        
        @keyframes spin {
            to { transform: rotate(360deg); }
        }
        
        a {
            text-decoration: none;
            transition: color 0.3s ease;
        }
        
        a:hover {
            text-decoration: underline;
        }
    </style>
</head>

<body class="theme-default">
    <div id="error-toast" class="toast"></div>
    <div class="mode-indicator">Default Assistant Mode</div>
    
    <div class="container">
        <h1 class="app-title">Real-Time Experts</h1>
        <p class="subtitle">Speak with Selected Expert Assistants</p>
        <p class="api-link">Get a Gemini API key <a id="api-link" href="https://ai.google.dev/gemini-api/docs/api-key">here</a></p>
        
        <div class="form-group">
            <label for="api-key">API Key</label>
            <input type="password" id="api-key" class="input-field" placeholder="Enter your API key">
        </div>
        
        <div class="form-group">
            <label for="voice">Voice</label>
            <select id="voice" class="input-field">
                <option value="Puck">Puck</option>
                <option value="Charon">Charon</option>
                <option value="Kore">Kore</option>
                <option value="Fenrir">Fenrir</option>
                <option value="Aoede">Aoede</option>
            </select>
        </div>
        
        <div class="form-group">
            <label for="prompt-select">System Prompt</label>
            <select id="prompt-select" class="prompt-select">
                <option value="Default">Default Assistant</option>
                <option value="Behavior Expert">Behavior Expert</option>
                <option value="UDL Expert">UDL Expert</option>
                <option value="Learning Support Expert">Learning Support Expert</option>
                <option value="Custom">Custom Prompt</option>
            </select>
            <textarea id="custom-prompt" class="custom-prompt" placeholder="Enter custom instructions for the AI assistant"></textarea>
        </div>
        
        <div class="visualization" id="visualization">
            <!-- Boxes will be dynamically added here -->
        </div>
        
        <button id="start-button" class="start-button">Start Conversation</button>
    </div>
    
    <audio id="audio-output"></audio>
    
    <script>
        // System prompts data injected from the server
        const SYSTEM_PROMPTS = __SYSTEM_PROMPTS__;
        
        // Theme configuration
        const themeConfig = {
            'Default': {
                color: '#0066B3',
                name: 'Default Assistant'
            },
            'Behavior Expert': {
                color: '#FCBA40',
                name: 'Behavior Expert'
            },
            'UDL Expert': {
                color: '#A50064',
                name: 'UDL Expert'
            },
            'Learning Support Expert': {
                color: '#11C7B5',
                name: 'Learning Support Expert'
            },
            'Custom': {
                color: '#FF6B35',
                name: 'Custom Assistant'
            }
        };
        
        // Elements
        const body = document.body;
        const promptSelect = document.getElementById('prompt-select');
        const customPrompt = document.getElementById('custom-prompt');
        const visualization = document.getElementById('visualization');
        const startButton = document.getElementById('start-button');
        const modeIndicator = document.querySelector('.mode-indicator');
        const appTitle = document.querySelector('.app-title');
        
        // Create visualization bars
        function createVisualizationBars() {
            visualization.innerHTML = '';
            const numBars = 30;
            
            for (let i = 0; i < numBars; i++) {
                const box = document.createElement('div');
                box.className = 'box';
                // Set initial scale
                box.style.transform = `scaleY(0.4)`;
                visualization.appendChild(box);
            }
        }
        
        // Apply theme based on selected prompt
        function applyTheme(promptKey) {
            // Remove existing theme classes
            body.className = '';
            
            // Set the appropriate theme class
            const themeClass = promptKey === 'Behavior Expert' ? 'theme-behavior' :
                               promptKey === 'UDL Expert' ? 'theme-udl' :
                               promptKey === 'Learning Support Expert' ? 'theme-prompt3' :
                               promptKey === 'Custom' ? 'theme-custom' : 'theme-default';
            
            body.classList.add(themeClass);
            
            // Update mode indicator
            const config = themeConfig[promptKey] || themeConfig['Default'];
            modeIndicator.textContent = config.name + ' Mode';
        }
        
        // Handle prompt selection change
        promptSelect.addEventListener('change', function() {
            const selectedValue = this.value;
            
            // Toggle custom prompt textarea
            if (selectedValue === 'Custom') {
                customPrompt.style.display = 'block';
            } else {
                customPrompt.style.display = 'none';
            }
            
            // Apply theme
            applyTheme(selectedValue);
        });
        
        // Initialize
        createVisualizationBars();
        applyTheme(promptSelect.value);
        
        // Animation for visualization (simulated for demo)
        function animateVisualization() {
            const bars = document.querySelectorAll('.box');
            
            bars.forEach(bar => {
                const newScale = Math.random() * 0.8 + 0.2;
                bar.style.transform = `scaleY(${newScale})`;
            });
            
            requestAnimationFrame(animateVisualization);
        }
        
        // Get the selected prompt data
        function getSelectedPrompt() {
            const selectedValue = promptSelect.value;
            return {
                promptKey: selectedValue === 'Custom' ? '' : selectedValue,
                customPrompt: selectedValue === 'Custom' ? customPrompt.value : ''
            };
        }
        
        let peerConnection;
        let audioContext;
        let dataChannel;
        let isRecording = false;
        let webrtc_id;
        let analyser;
        let dataArray;
        let animationId;
        
        const apiKeyInput = document.getElementById('api-key');
        const voiceSelect = document.getElementById('voice');
        const audioOutput = document.getElementById('audio-output');
        
        function showError(message) {
            const toast = document.getElementById('error-toast');
            toast.textContent = message;
            toast.style.display = 'block';
            
            setTimeout(() => {
                toast.style.display = 'none';
            }, 5000);
        }
        
        function updateButtonState() {
            if (peerConnection && (peerConnection.connectionState === 'connecting' || peerConnection.connectionState === 'new')) {
                startButton.innerHTML = '<span class="spinner"></span>Connecting...';
                startButton.disabled = true;
            } else if (peerConnection && peerConnection.connectionState === 'connected') {
                startButton.textContent = 'End Conversation';
                startButton.disabled = false;
            } else {
                startButton.textContent = 'Start Conversation';
                startButton.disabled = false;
            }
        }
        
        async function setupWebRTC() {
            const config = __RTC_CONFIGURATION__;
            peerConnection = new RTCPeerConnection(config);
            webrtc_id = Math.random().toString(36).substring(7);
            
            try {
                const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
                stream.getTracks().forEach(track => peerConnection.addTrack(track, stream));
                
                // Set up audio context for visualization
                audioContext = new AudioContext();
                analyser = audioContext.createAnalyser();
                const source = audioContext.createMediaStreamSource(stream);
                source.connect(analyser);
                analyser.fftSize = 64;
                dataArray = new Uint8Array(analyser.frequencyBinCount);
                
                // Start visualization
                updateVisualization();
                
                // Connection state change listener
                peerConnection.addEventListener('connectionstatechange', () => {
                    console.log('Connection state:', peerConnection.connectionState);
                    updateButtonState();
                    
                    if (peerConnection.connectionState === 'connected') {
                        console.log('WebRTC connection established');
                    }
                });
                
                // Handle incoming audio
                peerConnection.addEventListener('track', (evt) => {
                    if (audioOutput && audioOutput.srcObject !== evt.streams[0]) {
                        audioOutput.srcObject = evt.streams[0];
                        audioOutput.play();
                    }
                });
                
                // Data channel for messages
                dataChannel = peerConnection.createDataChannel('text');
                dataChannel.onmessage = (event) => {
                    const eventJson = JSON.parse(event.data);
                    if (eventJson.type === "error") {
                        showError(eventJson.message);
                    } else if (eventJson.type === "send_input") {
                        const promptData = getSelectedPrompt();
                        
                        fetch('/input_hook', {
                            method: 'POST',
                            headers: { 'Content-Type': 'application/json' },
                            body: JSON.stringify({
                                webrtc_id: webrtc_id,
                                api_key: apiKeyInput.value,
                                voice_name: voiceSelect.value,
                                prompt_key: promptData.promptKey,
                                custom_prompt: promptData.customPrompt
                            })
                        });
                    }
                };
                
                // Create and send offer
                const offer = await peerConnection.createOffer();
                await peerConnection.setLocalDescription(offer);
                
                // Wait for ICE gathering to complete
                await new Promise((resolve) => {
                    if (peerConnection.iceGatheringState === "complete") {
                        resolve();
                    } else {
                        const checkState = () => {
                            if (peerConnection.iceGatheringState === "complete") {
                                peerConnection.removeEventListener("icegatheringstatechange", checkState);
                                resolve();
                            }
                        };
                        peerConnection.addEventListener("icegatheringstatechange", checkState);
                    }
                });
                
                // Send offer to server
                const response = await fetch('/webrtc/offer', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({
                        sdp: peerConnection.localDescription.sdp,
                        type: peerConnection.localDescription.type,
                        webrtc_id: webrtc_id,
                    })
                });
                
                const serverResponse = await response.json();
                if (serverResponse.status === 'failed') {
                    showError(serverResponse.meta.error);
                    stopWebRTC();
                    return;
                }
                
                await peerConnection.setRemoteDescription(serverResponse);
                
            } catch (err) {
                console.error('Error setting up WebRTC:', err);
                showError('Failed to establish connection. Please try again.');
                stopWebRTC();
            }
        }
        
        function updateVisualization() {
            if (!analyser) return;
            
            analyser.getByteFrequencyData(dataArray);
            const bars = document.querySelectorAll('.box');
            
            for (let i = 0; i < bars.length; i++) {
                // Use data if available, otherwise animate randomly
                if (i < dataArray.length) {
                    const barHeight = (dataArray[i] / 255) * 0.8 + 0.2;
                    bars[i].style.transform = `scaleY(${barHeight})`;
                } else {
                    const barHeight = Math.random() * 0.8 + 0.2;
                    bars[i].style.transform = `scaleY(${barHeight})`;
                }
            }
            
            animationId = requestAnimationFrame(updateVisualization);
        }
        
        function stopWebRTC() {
            if (peerConnection) {
                peerConnection.close();
            }
            
            if (animationId) {
                cancelAnimationFrame(animationId);
            }
            
            if (audioContext) {
                audioContext.close();
            }
            
            peerConnection = null;
            updateButtonState();
        }
        
        startButton.addEventListener('click', () => {
            if (!isRecording) {
                setupWebRTC();
            } else {
                stopWebRTC();
            }
            
            isRecording = !isRecording;
        });
        
        // Start simple animation for initial state
        //animateVisualization();
    </script>
</body>

</html>