File size: 1,410 Bytes
0659f06
 
051e07f
 
 
 
 
0659f06
 
051e07f
 
0659f06
051e07f
0659f06
051e07f
 
 
 
 
 
 
 
 
 
 
0659f06
 
 
051e07f
0659f06
051e07f
0659f06
 
 
 
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
<!DOCTYPE html>
<html>
<body style="background:#000; color:#0f0; font-family:monospace;">
    <h2>JARVIS LIVE</h2>
    <div id="log" style="height:200px; overflow-y:auto; border:1px solid #0f0; padding:10px;"></div>
    <input id="in" style="background:#000; color:#0f0; border:1px solid #0f0; width:80%;">
    <button onclick="send()">TRANSMIT</button>

    <script>
        const ctx = new (window.AudioContext || window.webkitAudioContext)({sampleRate: 24000});
        const ws = new WebSocket((location.protocol === 'https:' ? 'wss:' : 'ws:') + '//' + location.host + '/ws/chat');
        ws.binaryType = 'arraybuffer';
        let nextTime = 0;

        ws.onmessage = (e) => {
            const data = new Float32Array(e.data);
            const buf = ctx.createBuffer(1, data.length, 24000);
            buf.copyToChannel(data, 0);
            const src = ctx.createBufferSource();
            src.buffer = buf;
            src.connect(ctx.destination);
            const start = Math.max(ctx.currentTime, nextTime);
            src.start(start);
            nextTime = start + buf.duration;
            document.getElementById('log').innerHTML += "<div>Audio Chunk Streamed</div>";
        };

        function send() {
            const msg = document.getElementById('in').value;
            ws.send(msg);
            document.getElementById('in').value = '';
        }
    </script>
</body>
</html>