-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcc_viz.html
More file actions
335 lines (275 loc) · 8.83 KB
/
cc_viz.html
File metadata and controls
335 lines (275 loc) · 8.83 KB
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>MIDI signal visalizer</title>
<script type="text/javascript">
if (navigator.requestMIDIAccess) {
navigator.requestMIDIAccess({
sysex: false //not interested in sysex.
}).then(onMIDISuccess, onMIDIFailure);
} else {
alert("No MIDI support in your browser.");
}
var midi = null;
// midi functions
function onMIDISuccess(midiAccess) {
// when we get a succesful response, run this code
midi = midiAccess;
onMidiAccess();
}
function onMidiMessageDo(thisFunction){
var inputs = midi.inputs.values();
// loop over all available inputs and listen for any MIDI input
for (var input = inputs.next(); input && !input.done; input = inputs.next()) {
// each time there is a midi message call the onMIDIMessage function
input.value.onmidimessage = thisFunction;
}
}
function onMIDIFailure(e) {
// when we get a failed response, run this code
console.log("No access to MIDI devices or your browser doesn't support WebMIDI API. Please use Web MIDI APIShim " + e);
}
function sendMidi(mesage,manufacturer){
var list = midi.outputs;
var devices = midi.outputs.values()
var teensy = null
for (let device of devices) {
console.log(device.name)
if(manufacturer == device.manufacturer){
teensy = device
}
}
teensy.send(message)
}
</script>
<style>
body{
display: grid;
margin:0;
grid-template-columns: 1fr 5fr;
grid-template-rows: 1fr;
padding: 0;
width: 100vw;
height: 100vh;
justify-content: center;
font-family: "din-2014",sans-serif;
}
button{
display: inline-block;
}
canvas{
width: 100%;
height: 100%;
grid-row: 1;
grid-column: 2;
/*image-rendering: pixelated;*/
}
#controls{
grid-row: 1;
grid-column: 1;
align-content: center;
height:100%;
border-right: gray 1px solid;
padding-left: 0.5em;
}
label{
display: block;
margin-bottom: 0.2em;
margin-right: 0.2em;
padding: 0.1em;
}
</style>
</head>
<body>
<div id="controls">
<h3>Inputs</h3>
<label style="color:#e6194b"> <b>A0 </b><input type="checkbox" name="analogInput" id="A0" checked></input>
<output name="analogValue"></output>
</label>
<label style="color:#3cb44b"> <b>A1 </b><input type="checkbox" name="analogInput" id="A1" checked></input>
<output name="analogValue"></output>
</label>
<label style="color:#004040" > <b>A2 </b><input type="checkbox" name="analogInput" id="A2" checked></input>
<output name="analogValue"></output>
</label>
<label style="color:#0082c8" > <b>A3 </b><input type="checkbox" name="analogInput" id="A3"></input>
<output name="analogValue"></output>
</label>
<label style="color:#f58231"> <b>A4 </b><input type="checkbox" name="analogInput" id="A4"></input>
<output name="analogValue"></output>
</label>
<label style="color:#911eb4"> <b>A5 </b><input type="checkbox" name="analogInput" id="A5"></input>
<output name="analogValue"></output>
</label>
<label style="color:#46f0f0"> <b>A6 </b><input type="checkbox" name="analogInput" id="A6"></input>
<output name="analogValue"></output>
</label>
<label style="color:#f032e6"> <b>A7 </b><input type="checkbox" name="analogInput" id="A7"></input>
<output name="analogValue"></output>
</label>
<h3>Sample rate <small>(Hz)</small></h3>
<label><input id="samplerate" step="1" min="1" max="8000" value="120" type="number"> <button id="set_sample_rate_button">Set</button></label>
<h3>Data</h3>
<label><button id="download_button">Download</button></label>
</div>
<canvas id="signals"></canvas>
<script type="text/javascript">
var midiManufacturer = "Teensyduino"
var canvas = document.getElementById("signals");
var checkboxes = document.getElementsByName("analogInput");
var outputElements = document.getElementsByName("analogValue");
var samplerateInput = document.getElementById("samplerate");
var setSampleRateButton = document.getElementById("set_sample_rate_button");
var downloadButton = document.getElementById("download_button");
var canvas = document.getElementById("signals");
var ctx = canvas.getContext("2d");
ctx.imageSmoothingEnabled = true;
canvas.width = canvas.clientWidth;
canvas.height = canvas.clientHeight;
//ctx.translate(0.5, 0.5);
var vizDuration = 10.0;//seconds
var currentTime = 0;
var colors = ["#e6194b", "#3cb44b", "#004040", "#0082c8", "#f58231", "#911eb4", "#46f0f0", "#f032e6", "#d2f53c", "#fabebe", "#008080", "#e6beff", "#aa6e28"];
var midiEvents = new Map();
var lastDataMap = new Map();
var incomplete = 0;
function onMidiAccess(){
console.log('MIDI Access Object', midi);
onMidiMessageDo(function (message) {
data = message.data; // this gives us our [command/channel, note, velocity] data.
currentTime = message.timeStamp / 1000.0
if(data[1] % 2 == 0){
incomplete = data[2] * 128
}else{
analogValue = incomplete + data[2]
//see here https://ccrma.stanford.edu/~craig/articles/linuxmidi/misc/essenmidi.html
channel = data[0] - 0xB0
dataobject = { value: analogValue , channel: channel };
//console.log(channel);
//random prevents (limits) multiple objects with same key
midiEvents.set(currentTime + Math.random() / 1000000.0 , dataobject )
}
});
}
setSampleRateButton.onclick = function(){
//note on = sample rate between 1 and 127*127
msb = Math.floor(Number(samplerateInput.value) / 127)
lsb = Math.floor(Number(samplerateInput.value) - msb * 127)
message = [0xE0, msb, lsb]
sendMidi(message,midiManufacturer)
console.log("Change sample rate: " + samplerateInput.value)
}
downloadButton.onclick = function(){
//note on = sample rate between 1 and 127*127
exportToCsv("data.csv");
}
function draw(){
//clear drawing area
ctx.clearRect(0, 0, canvas.width, canvas.height);
//draw layers
drawAxis();
drawSignals();
//drawEvents();
//request new animation frame (draw loop)
currentFrame = window.requestAnimationFrame(draw);
}
function timeToX(timeToTransform){
//currentTime = vid.currentTime
start = currentTime-vizDuration;
stop = currentTime;
return (timeToTransform - start)/vizDuration * canvas.width
}
function signalValueToY(valueToTransform){
maxValue = 127.0 * 127.0;
return canvas.height * 0.98 - valueToTransform/maxValue * canvas.height * 0.96
}
function drawAxis(){
currentSec = Math.floor(currentTime);
//show 10s of data
for(i = currentSec; i>=0 && i >= currentSec - vizDuration ; i--){
ctx.fillRect(timeToX(i),0,1,20);
ctx.fillRect(timeToX(i), canvas.height * 0.98,1, canvas.height * 0.98-25);
}
}
function drawSignals(){
start = currentTime - vizDuration;
first = true;
for(i = 0 ; i < 8; i++){
if(!checkboxes[i].checked){
continue;
}
ctx.beginPath();
ctx.strokeStyle = colors[i];
ctx.lineWidth = 2.5;
for (const [key, value] of midiEvents.entries()) {
if(key > start && key < currentTime && value.channel == i){
//console.log(value)
if(!first){
ctx.lineTo(timeToX(key),signalValueToY(value.value));
}
first = false;
//console.log(value)
ctx.moveTo(timeToX(key),signalValueToY(value.value));
lastDataMap[value.channel] = value.value;
}
}
ctx.stroke();
}
//set output voltage
for(channel = 0 ; channel < 8; channel++){
if(checkboxes[channel].checked){
outputElements[channel].value = Number(lastDataMap[channel] / 16384.0 * 3.3).toFixed(2) + " V"
} else{
outputElements[channel].value = ""
}
}
//console.log(midiEvents.size)
if(midiEvents.size > 3*50*30 * 100 ){
var keyIterator = midiEvents.keys()
while(midiEvents.size > 3*50*30 * 100 ){
midiEvents.delete(keyIterator.next().value );
}
}
}
function exportToCsv(filename) {
console.log("Will export CSV with");
var rows = new Map();
for (const [key, value] of midiEvents.entries()) {
ms_key = Math.round(key*1000);
if(!rows.has(ms_key)){
rows.set(ms_key,[]);
rows.get(ms_key).push(value.value);
}else{
rows.get(ms_key).push(value.value);
}
}
var csvFile = 'Time (ms) ,A0 (V),A1 (V),A2 (V),A3 (V),A4 (V),A5 (V),A6 (V),A7 (V)';
for (const [key, value] of rows.entries()) {
csvFile += key + ",";
for (var i = 0; i < value.length; i++) {
csvFile += ("%.3f", value[i]/(128.0+128.0) * 3.3 ).toFixed(3) + ",";
}
csvFile += "\n";
}
var blob = new Blob([csvFile], { type: 'text/csv;charset=utf-8;' });
if (navigator.msSaveBlob) { // IE 10+
navigator.msSaveBlob(blob, filename);
} else {
var link = document.createElement("a");
if (link.download !== undefined) { // feature detection
// Browsers that support HTML5 download attribute
var url = URL.createObjectURL(blob);
link.setAttribute("href", url);
link.setAttribute("download", filename);
link.style.visibility = 'hidden';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
}
}
draw();
</script>
</body>
</html>