From: Mark Slee Date: Tue, 25 Feb 2014 05:15:16 +0000 (-0800) Subject: New Grizzly code using LXOutput framework X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=344908672b7cfe1efba03f739ef945601dae1b8e;p=SugarCubes.git New Grizzly code using LXOutput framework --- diff --git a/_Grizzly.pde b/_Grizzly.pde new file mode 100644 index 0000000..cc7dec8 --- /dev/null +++ b/_Grizzly.pde @@ -0,0 +1,146 @@ +/** + * DOUBLE BLACK DIAMOND DOUBLE BLACK DIAMOND + * + * //\\ //\\ //\\ //\\ + * ///\\\ ///\\\ ///\\\ ///\\\ + * \\\/// \\\/// \\\/// \\\/// + * \\// \\// \\// \\//H + * + * EXPERTS ONLY!! EXPERTS ONLY!! + * + * If you are an artist, you may ignore this file! It just sets + * up the framework to run the patterns. Should not need modification + * for general animation work. + */ + +GrizzlyOutput[] buildGrizzlies() throws SocketException, UnknownHostException { + return new GrizzlyOutput[] { + new GrizzlyOutput(lx, "192.168.88.100", 1, 2, 3, 4, 5, 6, 7, 8), + new GrizzlyOutput(lx, "192.168.88.101", 9, 10, 11, 12, 13, 14, 15, 16), + }; +} + +class GrizzlyOutput extends LXDatagramOutput { + + private int frameNumber = 0; + + GrizzlyOutput(LX lx, String ipAddress, int ... cubeIndices) throws UnknownHostException, SocketException { + super(lx); + int channelNum = 1; + for (int rawCubeIndex : cubeIndices) { + if (rawCubeIndex > 0) { + Cube cube = model.getCubeByRawIndex(rawCubeIndex); + addDatagram(new GrizzlyDatagram(this, channelNum, cube).setAddress(ipAddress)); + } + ++channelNum; + } + } + + protected void beforeSend(int[] colors) { + ++frameNumber; + } + + public int getFrameNumber() { + return this.frameNumber; + } +} + +static class GrizzlyDatagram extends LXDatagram { + + private static byte[] oscString(String s) { + int len = s.length(); + int padding = (4 - ((len + 1) % 4)) % 4; + byte[] bytes = new byte[len + 1 + padding]; + System.arraycopy(s.getBytes(), 0, bytes, 0, len); + for (int i = len; i < bytes.length; ++i) { + bytes[i] = 0; + } + return bytes; + } + + private static int oscIntCopy(int i, byte[] buffer, int pos) { + buffer[pos] = (byte) ((i >> 24) & 0xff); + buffer[pos + 1] = (byte) ((i >> 16) & 0xff); + buffer[pos + 2] = (byte) ((i >> 8) & 0xff); + buffer[pos + 3] = (byte) (i & 0xff); + return 4; + } + + private final static int[] STRIP_ORDERING = new int[] { 9, 8, 11, 5, 4, 7, 6, 10, 14, 2, 1, 0, 3, 13, 12, 15 }; + + private static int[] cubePointIndices(Cube cube) { + int[] pointIndices = new int[Cube.POINTS_PER_CUBE - 2]; + int pi = 0; + for (int stripIndex : STRIP_ORDERING) { + Strip strip = cube.strips.get(stripIndex); + int stripLen = ((stripIndex == 9) || (stripIndex == 15)) ? 15 : 16; + for (int i = stripLen-1; i >= 0; --i) { + pointIndices[pi++] = strip.points.get(i).index; + } + } + return pointIndices; + } + + private final static byte[] OSC_ADDRESS = oscString("/shady/pointbuffer"); + private final static byte[] OSC_TYPETAG = oscString(",iiiiibi"); + + private final static int HEADER_LENGTH = OSC_ADDRESS.length + OSC_TYPETAG.length + 24; + private final static int FOOTER_LENGTH = 4; + + private final static int OSC_PORT = 779; + + private GrizzlyOutput output; + + private int[] pointIndices; + + private final int frameNumberPos; + + private final int dataPos; + + public GrizzlyDatagram(GrizzlyOutput output, int channelNum, Cube cube) { + this(output, channelNum, cubePointIndices(cube)); + } + + public GrizzlyDatagram(GrizzlyOutput output, int channelNum, int[] pointIndices) { + super(HEADER_LENGTH + 4*pointIndices.length + FOOTER_LENGTH); + setPort(OSC_PORT); + + this.output = output; + this.pointIndices = pointIndices; + int dataLength = 4*pointIndices.length; + + int pos = 0; + + // OSC address + System.arraycopy(OSC_ADDRESS, 0, this.buffer, pos, OSC_ADDRESS.length); + pos += OSC_ADDRESS.length; + + // OSC typetag + System.arraycopy(OSC_TYPETAG, 0, this.buffer, pos, OSC_TYPETAG.length); + pos += OSC_TYPETAG.length; + this.frameNumberPos = pos; + pos += oscIntCopy(0, this.buffer, pos); // placeholder for frame number + pos += oscIntCopy(0xDEADBEEF, this.buffer, pos); + pos += oscIntCopy(channelNum, this.buffer, pos); + pos += oscIntCopy(0xFEEDBEEF, this.buffer, pos); + pos += oscIntCopy(dataLength, this.buffer, pos); + pos += oscIntCopy(dataLength, this.buffer, pos); + this.dataPos = pos; + + // end header + oscIntCopy(0xBEFFFFEB, this.buffer, this.buffer.length - 4); + } + + void onSend(int[] colors) { + oscIntCopy(this.output.getFrameNumber(), this.buffer, frameNumberPos); + int dataIndex = this.dataPos; + for (int index : this.pointIndices) { + color c = (index >= 0) ? colors[index] : 0; + this.buffer[dataIndex] = (byte) 0; // unused, alpha + this.buffer[dataIndex + 1] = (byte) ((c >> 16) & 0xff); // r + this.buffer[dataIndex + 2] = (byte) ((c >> 8) & 0xff); // g + this.buffer[dataIndex + 3] = (byte) (c & 0xff); // b + dataIndex += 4; + } + } +} diff --git a/_Internals.pde b/_Internals.pde index 326f6e3..0100c05 100644 --- a/_Internals.pde +++ b/_Internals.pde @@ -45,6 +45,7 @@ int startMillis, lastMillis; // Core engine variables GLucose glucose; LX lx; +Model model; LXPattern[] patterns; Effects effects; MappingTool mappingTool; @@ -128,7 +129,7 @@ void setup() { logTime("Created viewport"); // Create the GLucose engine to run the cubes - glucose = new GLucose(this, buildModel()); + glucose = new GLucose(this, model = buildModel()); lx = glucose.lx; lx.enableKeyboardTempo(); logTime("Built GLucose engine"); @@ -152,6 +153,15 @@ void setup() { logTime("Setup MIDI devices"); // Build output driver + try { + GrizzlyOutput[] grizzlies = buildGrizzlies(); + for (LXOutput output : grizzlies) { + lx.addOutput(output); + } + } catch (Exception x) { + x.printStackTrace(); + } + PandaMapping[] pandaMappings = buildPandaList(); pandaBoards = new PandaDriver[pandaMappings.length]; int pbi = 0; diff --git a/code/HeronLX.jar b/code/HeronLX.jar index 277c571..4e0447c 100755 Binary files a/code/HeronLX.jar and b/code/HeronLX.jar differ