-
Notifications
You must be signed in to change notification settings - Fork 5
/
mic.html
170 lines (117 loc) · 4.82 KB
/
mic.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
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
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>capture microphone then show time & frequency domain output</title>
<script src="https://unpkg.com/mqtt/dist/mqtt.min.js"></script>
</head>
<body>
<p>Volume</p>
<input id="volume" type="range" min="0" max="1" step="0.1" value="0.0" />
<section class="main-controls">
<canvas class="visualizer" height="60px" width="611"></canvas>
<div id="buttons">
<button class="record">Record</button>
<button class="stop" disabled="">Stop</button>
</div>
</section>
<script type="text/javascript">
var client = mqtt.connect('wss://206.189.131.144:2000');
client.on('connect', function() {
console.log("connected");
client.subscribe('/new/user/', function(err) {
if (!err) {
console.log("subscribed");
}
})
});
var audioCtx = new(window.AudioContext || webkitAudioContext)();
var source = null;
var analyser = audioCtx.createAnalyser();;
var bufferLength = null;
var dataArray = null;
var audioFloatData = null;
var scriptProcessor = audioCtx.createScriptProcessor(1024, 1, 1);
var destination = audioCtx.createMediaStreamDestination();
var canvas = document.querySelector('.visualizer');
var mainSection = document.querySelector('.main-controls');
// disable stop button while not recording
stop.disabled = true;
// visualiser setup - create web audio api context and canvas
var canvasCtx = canvas.getContext("2d");
//main block for doing the audio recording
if (navigator.mediaDevices.getUserMedia) {
console.log('getUserMedia supported.');
var constraints = {
audio: true
};
var onSuccess = function(stream) {
//var mediaRecorder = new MediaRecorder(stream);
visualize(stream);
}
var onError = function(err) {
console.log('The following error occured: ' + err);
}
navigator.mediaDevices.getUserMedia(constraints).then(onSuccess, onError);
} else {
console.log('getUserMedia not supported on your browser!');
}
function visualize(stream) {
source = audioCtx.createMediaStreamSource(stream);
analyser.fftSize = 2048;
bufferLength = analyser.frequencyBinCount;
dataArray = new Uint8Array(bufferLength);
audioFloatData = new Float32Array(bufferLength);
analyser.connect(scriptProcessor);
source.connect(analyser);
//analyser.connect(audioCtx.destination);
// draw()
function draw() {
WIDTH = canvas.width
HEIGHT = canvas.height;
requestAnimationFrame(draw);
analyser.getByteTimeDomainData(dataArray);
canvasCtx.fillStyle = 'rgb(200, 200, 200)';
canvasCtx.fillRect(0, 0, WIDTH, HEIGHT);
canvasCtx.lineWidth = 2;
canvasCtx.strokeStyle = 'rgb(0, 0, 0)';
canvasCtx.beginPath();
var sliceWidth = WIDTH * 1.0 / bufferLength;
var x = 0;
for (var i = 0; i < bufferLength; i++) {
var v = dataArray[i] / 128.0;
var y = v * HEIGHT / 2;
if (i === 0) {
canvasCtx.moveTo(x, y);
} else {
canvasCtx.lineTo(x, y);
}
x += sliceWidth;
}
canvasCtx.lineTo(canvas.width, canvas.height / 2);
canvasCtx.stroke();
}
}
scriptProcessor.onaudioprocess = function(audioProcessingEvent) {
sendData(audioProcessingEvent.inputBuffer.getChannelData(0));
}
scriptProcessor.connect(destination);
var seq = 0;
function sendData(data) {
client.publish("jkhaiwue8r23u923y9r9wuf89y93wyr89aur/client2", new Uint8Array(data.buffer), {
qos: 1
}, () => {
console.log("data published");
});
// client.publish("jkhaiwue8r23u923y9r9wuf89y93wyr89aur/client2",JSON.stringify({seq:seq++,data: data,timmestamp:Date.now()}), {
// qos: 1
// }, () => {
// console.log("data published");
// });
}
window.onresize = function() {
canvas.width = mainSection.offsetWidth;
}
window.onresize();
</script>
</body>
</html>