-
Notifications
You must be signed in to change notification settings - Fork 1
/
rotatePiano.java
88 lines (76 loc) · 2.53 KB
/
rotatePiano.java
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
import com.cycling74.max.*;
public class rotatePiano extends MaxObject {
int[] noteToggles = new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
Atom[] noteView = Atom.newAtom(new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 });
Atom[] noteViewPrev = Atom.newAtom(new int[] { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 });
int viewLength = 12;
int steps = 0;
rotatePiano() {
declareInlets(new int[] { DataTypes.LIST, DataTypes.INT, DataTypes.LIST });
declareOutlets(new int[] { DataTypes.LIST, DataTypes.LIST, DataTypes.LIST });
}
protected void inlet(int v) {
int idx = getInlet();
switch (idx) {
case 1:
int l = viewLength;
steps = (-v + l) % l;
setPiano();
break;
}
}
private boolean arrayEquals(int[] a, int[] b) {
if (a == null || b == null || a.length != b.length) {
return false;
}
for (int i = 0; i < a.length; i++) {
if (a[i] != b[i]) {
return false;
}
}
return true;
}
protected void list(Atom[] a) {
int idx = getInlet();
switch (idx) {
case 0:
int[] input = Atom.toInt(a);
int i = (input[0] + steps) % viewLength;
int v = (input[1] == 0 ? 1 : 0);
noteViewPrev[input[0]] = Atom.newAtom(v);
if (noteToggles[i] == v) {
return;
} else {
noteToggles[i] = v;
outlet(2, noteToggles);
calcView();
outlet(1, noteView);
}
break;
case 2:
int[] updatedValues = Atom.toInt(a);
if (arrayEquals(noteToggles, updatedValues)) {
return;
} else {
noteToggles = updatedValues;
setPiano();
}
break;
}
}
private void calcView() {
noteView = Atom.rotate(Atom.newAtom(noteToggles), -steps);
}
private void setPiano() {
calcView();
for (int i = 0; i < noteView.length; i++) {
if (!noteView[i].equals(noteViewPrev[i])) {
outlet(0, new String[] { "set",
Integer.toString(i),
noteView[i].toBoolean() ? "0" : "1" });
}
}
noteViewPrev = noteView;
outlet(1, noteView);
}
}