X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=Output.pde;fp=Output.pde;h=cd7fd8733a4fd776a479eebbe8a07cb4e50a22d0;hb=bffb6a80f723b2795e47e56a7d8eb3d65e9324a5;hp=0000000000000000000000000000000000000000;hpb=30624e974f88224b0a8719b11bd649578eb97ab6;p=SugarCubes.git diff --git a/Output.pde b/Output.pde new file mode 100644 index 0000000..cd7fd87 --- /dev/null +++ b/Output.pde @@ -0,0 +1,270 @@ +/** + * DOUBLE BLACK DIAMOND DOUBLE BLACK DIAMOND + * + * //\\ //\\ //\\ //\\ + * ///\\\ ///\\\ ///\\\ ///\\\ + * \\\/// \\\/// \\\/// \\\/// + * \\// \\// \\// \\//H + * + * EXPERTS ONLY!! EXPERTS ONLY!! + * + * If you are an artist, you may ignore this file! It contains + * the code to drive grizzly board outputs. + */ + +private final static int[] STRIP_ORDERING = new int[] { 9, 8, 11, 5, 4, 7, 6, 10, 14, 2, 1, 0, 3, 13, 12, 15 }; + +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; +} + +abstract class IPOutput extends LXDatagramOutput { + public final String ipAddress; + + protected IPOutput(LX lx, String ipAddress) throws SocketException { + super(lx); + this.ipAddress = ipAddress; + } +} + +static abstract class OSCDatagram extends LXDatagram { + + protected OSCDatagram(int bufferSize) { + super(bufferSize); + } + + protected final static int OSC_PORT = 779; + + protected 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; + } + + protected 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; + } +} + +/** + * Grizzly Output, sends packets to one grizzly board with a fixed IP and a number + * of channels. + */ +class GrizzlyOutput extends IPOutput { + + private int frameNumber = 0; + + public GrizzlyOutput(LX lx, String ipAddress, int ... cubeIndices) throws UnknownHostException, SocketException { + super(lx, ipAddress); + int channelNum = 0; + for (int rawCubeIndex : cubeIndices) { + if (rawCubeIndex > 0) { + Cube cube = model.getCubeByRawIndex(rawCubeIndex); + addDatagram(new GrizzlyDatagram(this, channelNum, cube).setAddress(ipAddress)); + } + ++channelNum; + } + this.enabled.setValue(false); + } + + protected void beforeSend(int[] colors) { + ++frameNumber; + } + + public int getFrameNumber() { + return this.frameNumber; + } +} + +/** + * Datagram to a Grizzlyboard. A simple fixed OSC packet. + */ +static class GrizzlyDatagram extends OSCDatagram { + + 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 GrizzlyOutput output; + + private int[] pointIndices; + + private final int frameNumberPos; + + 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); + + // end header + oscIntCopy(0xBEFFFFEB, this.buffer, this.buffer.length - 4); + } + + void onSend(int[] colors) { + oscIntCopy(this.output.getFrameNumber(), this.buffer, frameNumberPos); + int dataIndex = HEADER_LENGTH; + 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; + } + } +} + +/** + * Grizzly Output, sends packets to one grizzly board with a fixed IP and a number + * of channels. + */ +class PandaOutput extends IPOutput { + + public PandaOutput(LX lx, String ipAddress, String[][] channelMappings) throws UnknownHostException, SocketException { + super(lx, ipAddress); + int packetNum = 0; + int[] packetPointIndices = new int[PandaDatagram.PIXELS_PER_PACKET]; + int[] noCubePoints = new int[Cube.POINTS_PER_CUBE - 2]; // 15-strips + for (int i = 0; i < noCubePoints.length; ++i) { + noCubePoints[i] = -1; + } + int[] cubePointIndices; + int pi = 0; + for (String[] channel : channelMappings) { + for (int i = 0; i < PandaDatagram.CUBES_PER_CHANNEL; ++i) { + Cube cube = model.getCubeById((i < channel.length) ? channel[i] : null); + cubePointIndices = (cube == null) ? noCubePoints : cubePointIndices(cube); + for (int index : cubePointIndices) { + packetPointIndices[pi++] = index; + if (pi >= PandaDatagram.PIXELS_PER_PACKET) { + addDatagram(new PandaDatagram(packetNum, packetPointIndices).setAddress(ipAddress)); + pi = 0; + packetPointIndices = new int[PandaDatagram.PIXELS_PER_PACKET]; + ++packetNum; + } + } + } + // 2 dummy pixels per cube at the end of each channel, due to 15-strips + for (int i = 0; i < PandaDatagram.CUBES_PER_CHANNEL * 2; ++i) { + packetPointIndices[pi++] = -1; + if (pi >= PandaDatagram.PIXELS_PER_PACKET) { + addDatagram(new PandaDatagram(packetNum, packetPointIndices).setAddress(ipAddress)); + pi = 0; + packetPointIndices = new int[PandaDatagram.PIXELS_PER_PACKET]; + ++packetNum; + } + } + } + if (pi > 0) { + addDatagram(new PandaDatagram(packetNum, packetPointIndices).setAddress(ipAddress)); + } + this.enabled.setValue(false); + } +} + +/** + * Datagram to a Panda board. A simple fixed OSC packet. + */ +static class PandaDatagram extends OSCDatagram { + + private final static byte[] OSC_ADDRESS = oscString("/shady/pointbuffer"); + private final static byte[] OSC_TYPETAG = oscString(",iib"); + + private final static int HEADER_LENGTH = OSC_ADDRESS.length + OSC_TYPETAG.length + 12; + + private final static int PORT = 9001; + + public final static int CUBES_PER_CHANNEL = 4; + + public final static int PIXELS_PER_CHANNEL = Cube.POINTS_PER_CUBE * CUBES_PER_CHANNEL; + + public final static int PIXELS_PER_PACKET = 352; + + public final static int BYTES_PER_PIXEL = 4; + + public final static int PACKET_SIZE = BYTES_PER_PIXEL*PIXELS_PER_PACKET; + + private int[] pointIndices; + + public PandaDatagram(int packetNum, int[] pointIndices) { + super(HEADER_LENGTH + PACKET_SIZE); + setPort(PORT); + + this.pointIndices = pointIndices; + + 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; + + // Packet number + pos += oscIntCopy(packetNum, this.buffer, pos); + + // Length of blob + pos += oscIntCopy(PACKET_SIZE, this.buffer, pos); + + // Length of blob in osc-blob + pos += oscIntCopy(PACKET_SIZE, this.buffer, pos); + } + + void onSend(int[] colors) { + int dataIndex = HEADER_LENGTH; + 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; + } + } +}