-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.html
67 lines (54 loc) · 2 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
<html>
<head>
<script src="/socket.io/socket.io.js"></script>
<script>
// NOTE: use current IP
var socket = io.connect('54.227.47.81:8080');
var recorder = null;
var volume = null;
var audioInput = null;
var audioContext = null;
var context = null;
if (!navigator.getUserMedia)
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia || navigator.msGetUserMedia;
// Setup audio collection if it exists.
if (navigator.getUserMedia){
navigator.getUserMedia({audio:true}, listener, function(e) {
alert('Error.');
});
} else alert('getUserMedia not supported in this browser.');
function listener(e){
// creates the audio context
audioContext = window.AudioContext || window.webkitAudioContext;
context = new audioContext();
// creates a gain node
volume = context.createGain();
// creates an audio node from the microphone incoming stream
audioInput = context.createMediaStreamSource(e);
// connect the stream to the gain node
audioInput.connect(volume);
var bufferSize = 2048;
recorder = context.createJavaScriptNode(bufferSize, 2, 2);
recorder.onaudioprocess = function(e){
var numC = e.inputBuffer.numberOfChannels; // number of channels (e.g. stereo)
var sampleRate = e.inputBuffer.sampleRate; // sample rate
var left = e.inputBuffer.getChannelData (0);
var right = e.inputBuffer.getChannelData (1);
// Merge stereo to mono, simple averaging: this will be the frequency of the mic (44/48 KHz)
for (var i = 0; i < e.inputBuffer.length; i++) {
left[i] = (left[i]+right[i])/2.0
}
// Send the data over socket
socket.emit('data',{'audio':left, 'rate':sampleRate});
}
// Connect the recorder
volume.connect (recorder);
recorder.connect (context.destination);
}
</script>
</head>
<body>
Text goes here.
</body>
</html>