-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfootControllerSongController.js
More file actions
87 lines (75 loc) · 1.76 KB
/
footControllerSongController.js
File metadata and controls
87 lines (75 loc) · 1.76 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
inlets = 1;
outlets = 2;
var store = {};
var ButtonTypes = ['clip', 'note', 'note_latch', 'looper'];
function songToState(song) {
switch (song) {
default: return stateFromTypes();
}
}
function init() {
store.state = songToState();
store.inputHandler = songToInputHandler();
}
var defaultScale = [0,2,3,5,7,10,12];
function defaultButtons() {
var buttons = [];
for (var i = 0; i < 7; i++) {
buttons.push(new Button(i, 48 + defaultScale[i], 100, true));
}
return buttons;
}
function stateFromTypes() {
var state = {};
state.buttons = defaultButtons();
state.leds = [0, 0, 0, 0, 0, 0, 0];
return state;
}
function Button(led, note, velocity, latching) {
this.led = led;
this.note = note || 12;
this.latching = latching || 0;
this.velocity = velocity || 100;
this.state = 0;
var weakSelf = this;
this.handle = function (input) {
if (weakSelf.latching != 0) {
if (input != 0) {
weakSelf.state = 1 - weakSelf.state;
weakSelf.trigger();
}
} else {
weakSelf.state = input;
weakSelf.trigger();
}
}
this.trigger = function () {
outlet(1, 'note ' + weakSelf.note + ' ' + weakSelf.state * weakSelf.velocity);
outlet(0, '/led/' + weakSelf.led + '/' + weakSelf.state * 127);
}
this.destruct = function () {
weakSelf.state = 0;
weakSelf.trigger();
}
}
function mutate(newState) {
store.state = newState;
}
function songToInputHandler(song) {
switch (song) {
default: return function handleInput(c) {
if (c[0] == 'b') {
store.state.buttons[c[1]].handle(parseInt(c[2]));
}
}
}
}
function song() {
const args = arrayfromargs(arguments);
mutate(songToState(song));
store.inputHandler = songToInputHandler(args[0]);
}
function rcv() {
const args = arrayfromargs(arguments);
store.inputHandler(args[0].split('/').slice(1));
}