/
brobot.js
80 lines (75 loc) · 2.12 KB
/
brobot.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
var config = require('./config'),
_ = require("underscore"),
midiLibrary = require('midi'),
five = require("johnny-five"),
instrumentController = require("./instrument-controller"),
board = five.Board(),
instruments = {};
module.exports = {
// =====================
// INIT
// =====================
initialize: function(){
// Calculate maximum latency
var slowestInstrument = _.max(config.instruments, function(instrument){
return instrument.latency;
});
var maximumLatency = slowestInstrument.latency;
// instantiate instrument controllers
_.each(config.instruments, function(instrument){
instrument.relativeOffset = maximumLatency - instrument.latency;
instruments[instrument.note] = new instrumentController(instrument);
});
this.initializeArduino();
this.initializeMidi();
},
initializeArduino: function(){
board.on("ready", function() {
console.log('Arduino connected');
_.each(instruments, function(instrument){
instrument.pin = five.Led(instrument.pinNumber);
});
});
},
initializeMidi: function(){
midi = new midiLibrary.input();
midi.openVirtualPort("Brobie Node");
midi.on('message', this.onMidiMessage.bind(this));
},
// =====================
// Event Manager
// =====================
onMidiMessage: function(time, message){
var instruction = message[0],
channel = message[1],
velocity = message[2];
console.log("Midi message incoming", channel, velocity, instruction);
if ([144,155,153].indexOf(instruction) !== -1){
this.onMidiNote(channel, velocity)
}
if([128,129].indexOf(instruction) !== -1){
this.onInstruction({
instruction: instruction,
channel: channel,
velocity: velocity
});
}
},
onInstruction: function(e){
if(e.instruction === 128){
if(instruments[e.channel]){
instruments[e.channel].determineMinimumDurationForContact();
}
}
},
onMidiNote: function (channel, velocity){
console.log("Midi note incoming", channel, velocity);
if(new Date().getHours() >= 10 || true){
if(instruments[channel]){
instruments[channel].queue(velocity);
}
}else{
console.log("Not within operating hours");
}
}
};