New Grizzly code using LXOutput framework
[SugarCubes.git] / _Grizzly.pde
1 /**
2 * DOUBLE BLACK DIAMOND DOUBLE BLACK DIAMOND
3 *
4 * //\\ //\\ //\\ //\\
5 * ///\\\ ///\\\ ///\\\ ///\\\
6 * \\\/// \\\/// \\\/// \\\///
7 * \\// \\// \\// \\//H
8 *
9 * EXPERTS ONLY!! EXPERTS ONLY!!
10 *
11 * If you are an artist, you may ignore this file! It just sets
12 * up the framework to run the patterns. Should not need modification
13 * for general animation work.
14 */
15
16 GrizzlyOutput[] buildGrizzlies() throws SocketException, UnknownHostException {
17 return new GrizzlyOutput[] {
18 new GrizzlyOutput(lx, "192.168.88.100", 1, 2, 3, 4, 5, 6, 7, 8),
19 new GrizzlyOutput(lx, "192.168.88.101", 9, 10, 11, 12, 13, 14, 15, 16),
20 };
21 }
22
23 class GrizzlyOutput extends LXDatagramOutput {
24
25 private int frameNumber = 0;
26
27 GrizzlyOutput(LX lx, String ipAddress, int ... cubeIndices) throws UnknownHostException, SocketException {
28 super(lx);
29 int channelNum = 1;
30 for (int rawCubeIndex : cubeIndices) {
31 if (rawCubeIndex > 0) {
32 Cube cube = model.getCubeByRawIndex(rawCubeIndex);
33 addDatagram(new GrizzlyDatagram(this, channelNum, cube).setAddress(ipAddress));
34 }
35 ++channelNum;
36 }
37 }
38
39 protected void beforeSend(int[] colors) {
40 ++frameNumber;
41 }
42
43 public int getFrameNumber() {
44 return this.frameNumber;
45 }
46 }
47
48 static class GrizzlyDatagram extends LXDatagram {
49
50 private static byte[] oscString(String s) {
51 int len = s.length();
52 int padding = (4 - ((len + 1) % 4)) % 4;
53 byte[] bytes = new byte[len + 1 + padding];
54 System.arraycopy(s.getBytes(), 0, bytes, 0, len);
55 for (int i = len; i < bytes.length; ++i) {
56 bytes[i] = 0;
57 }
58 return bytes;
59 }
60
61 private static int oscIntCopy(int i, byte[] buffer, int pos) {
62 buffer[pos] = (byte) ((i >> 24) & 0xff);
63 buffer[pos + 1] = (byte) ((i >> 16) & 0xff);
64 buffer[pos + 2] = (byte) ((i >> 8) & 0xff);
65 buffer[pos + 3] = (byte) (i & 0xff);
66 return 4;
67 }
68
69 private final static int[] STRIP_ORDERING = new int[] { 9, 8, 11, 5, 4, 7, 6, 10, 14, 2, 1, 0, 3, 13, 12, 15 };
70
71 private static int[] cubePointIndices(Cube cube) {
72 int[] pointIndices = new int[Cube.POINTS_PER_CUBE - 2];
73 int pi = 0;
74 for (int stripIndex : STRIP_ORDERING) {
75 Strip strip = cube.strips.get(stripIndex);
76 int stripLen = ((stripIndex == 9) || (stripIndex == 15)) ? 15 : 16;
77 for (int i = stripLen-1; i >= 0; --i) {
78 pointIndices[pi++] = strip.points.get(i).index;
79 }
80 }
81 return pointIndices;
82 }
83
84 private final static byte[] OSC_ADDRESS = oscString("/shady/pointbuffer");
85 private final static byte[] OSC_TYPETAG = oscString(",iiiiibi");
86
87 private final static int HEADER_LENGTH = OSC_ADDRESS.length + OSC_TYPETAG.length + 24;
88 private final static int FOOTER_LENGTH = 4;
89
90 private final static int OSC_PORT = 779;
91
92 private GrizzlyOutput output;
93
94 private int[] pointIndices;
95
96 private final int frameNumberPos;
97
98 private final int dataPos;
99
100 public GrizzlyDatagram(GrizzlyOutput output, int channelNum, Cube cube) {
101 this(output, channelNum, cubePointIndices(cube));
102 }
103
104 public GrizzlyDatagram(GrizzlyOutput output, int channelNum, int[] pointIndices) {
105 super(HEADER_LENGTH + 4*pointIndices.length + FOOTER_LENGTH);
106 setPort(OSC_PORT);
107
108 this.output = output;
109 this.pointIndices = pointIndices;
110 int dataLength = 4*pointIndices.length;
111
112 int pos = 0;
113
114 // OSC address
115 System.arraycopy(OSC_ADDRESS, 0, this.buffer, pos, OSC_ADDRESS.length);
116 pos += OSC_ADDRESS.length;
117
118 // OSC typetag
119 System.arraycopy(OSC_TYPETAG, 0, this.buffer, pos, OSC_TYPETAG.length);
120 pos += OSC_TYPETAG.length;
121 this.frameNumberPos = pos;
122 pos += oscIntCopy(0, this.buffer, pos); // placeholder for frame number
123 pos += oscIntCopy(0xDEADBEEF, this.buffer, pos);
124 pos += oscIntCopy(channelNum, this.buffer, pos);
125 pos += oscIntCopy(0xFEEDBEEF, this.buffer, pos);
126 pos += oscIntCopy(dataLength, this.buffer, pos);
127 pos += oscIntCopy(dataLength, this.buffer, pos);
128 this.dataPos = pos;
129
130 // end header
131 oscIntCopy(0xBEFFFFEB, this.buffer, this.buffer.length - 4);
132 }
133
134 void onSend(int[] colors) {
135 oscIntCopy(this.output.getFrameNumber(), this.buffer, frameNumberPos);
136 int dataIndex = this.dataPos;
137 for (int index : this.pointIndices) {
138 color c = (index >= 0) ? colors[index] : 0;
139 this.buffer[dataIndex] = (byte) 0; // unused, alpha
140 this.buffer[dataIndex + 1] = (byte) ((c >> 16) & 0xff); // r
141 this.buffer[dataIndex + 2] = (byte) ((c >> 8) & 0xff); // g
142 this.buffer[dataIndex + 3] = (byte) (c & 0xff); // b
143 dataIndex += 4;
144 }
145 }
146 }