-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
93 lines (80 loc) · 2.26 KB
/
app.js
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
var appStatus = document.getElementById("status");
/*
* sound from MIDI signal
*/
var audioContext = new AudioContext();
var gain = audioContext.createGain();
var oscillator = audioContext.createOscillator();
gain.gain.value = 0;
oscillator.connect(gain);
gain.connect(audioContext.destination);
oscillator.start();
var noteToFrequency = function(note) {
return 440 * Math.pow(2, (note - 69) / 12);
}
var MIDIToSound = function(buffer){
var data = new Uint8Array(buffer);
var cmd = data[0] >> 4;
var channel = data[0] & 0xf;
var type = data[0] & 0xf0;
var note = data[1];
var velocity = data[2];
console.log(noteToFrequency(note));
console.log(note);
switch(type) {
case 176:
if (note != 11) {
oscillator.frequency.value = noteToFrequency(note);
}
gain.gain.value = 1;
break;
case 144:
if (note != 11) {
oscillator.frequency.value = noteToFrequency(note);
}
gain.gain.value = 0;
break;
}
}
/*
* Getting MIDI
*/
var socketId; //preparing for later storing the value
onMIDIMessage = function(message){
var address = document.getElementById("address").value;
var buffer = message.data.buffer;
MIDIToSound(buffer);
chrome.sockets.udp.send(socketId, buffer, address, 1337, function(sendInfo){
//console.log("sending info through udp" + buffer);
});
};
var onMIDISuccess = function(midiAccess){
var inputs = midiAccess.inputs.values();
for (var input = inputs.next(); input && !input.done; input= inputs.next()){
console.log("new input " + input);
input.value.onmidimessage = onMIDIMessage;
};
};
var onMIDIFailure = function(e){
appStatus.innerHTML = "MIDI failed :" + e;
};
if (navigator.requestMIDIAccess) {
navigator.requestMIDIAccess().then(onMIDISuccess, onMIDIFailure);
} else {
appStatus.innerHTML = "No MIDI support in your browser.";
}
/*
* UDP Connection
*/
var properties = {};
chrome.sockets.udp.create(properties, function(createInfo){
socketId = createInfo.socketId;
appStatus.innerHTML = "socket created";
chrome.sockets.udp.onReceive.addListener(function(data){
console.log(data);
MIDIToSound(data.data);
});
chrome.sockets.udp.bind(socketId, '0.0.0.0', 1337, function(r){
appStatus.innerHTML = "using port 1337";
});
});