-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
81 lines (67 loc) · 2.42 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>Socket.io Audio Broadcast</title>
<script src="/socket.io/socket.io.js"></script>
</head>
<body>
<h1>Socket.io Audio Broadcast</h1>
<!-- Add any necessary UI elements here -->
<button id="rec">Rec</button>
<button type="button">Nothing</button>
<script>
const socket = io(undefined, { transports: ["websocket"] });
const ctx = new AudioContext();
const audioBuffers = [];
document.querySelector("#rec").addEventListener("click", function (e) {
e.preventDefault();
// Get access to the microphone and start audio streaming
navigator.mediaDevices
.getUserMedia({ audio: true })
.then((stream) => {
const audioContext = new AudioContext();
const mediaStreamSource =
audioContext.createMediaStreamSource(stream);
const audioProcessor = audioContext.createScriptProcessor(0, 1, 1);
// Process audio data
audioProcessor.addEventListener("audioprocess", (event) => {
const inputBuffer = event.inputBuffer;
const audioData = inputBuffer.getChannelData(0);
socket.emit("audio stream", audioData); // Emit the audio data as a stream
});
// Connect audio nodes
mediaStreamSource.connect(audioProcessor);
audioProcessor.connect(audioContext.destination);
})
.catch((error) => {
console.error("Error accessing microphone:", error);
});
});
const process = (audioStream) => {
audioBuffers.push(audioStream);
if (audioBuffers.length) {
playNextAudio();
}
};
function playNextAudio() {
// If there are no more audio buffers in the queue, stop playing
if (audioBuffers.length === 0) {
return;
}
const chuck = audioBuffers.shift();
const floats = new Float32Array(chuck);
const buffer = ctx.createBuffer(1, floats.length, 48000);
buffer.getChannelData(0).set(floats);
const source = ctx.createBufferSource();
source.buffer = buffer;
source.connect(ctx.destination);
source.onended = () => playNextAudio();
source.start();
}
// Handle incoming audio streams
socket.on("audio stream", (data) => {
process(data);
});
</script>
</body>
</html>