/* READ BINARY FILE Jeff Thompson Read any binary file into Processing from the 'data' folder. You will need to know the file format's word length and, if necessary, the header size. This example reads a .wav file, which has two-byte words (a sample is two bytes long). WORD LENGTH (bytes) VARIABLE TYPE VALUES RETURNED 2 bytes short -32,768 - 32,768 4 bytes int -2,147,483,647 - 2,147,483,647 8 bytes long -9,223,372,036,854,775,807 - 9,223,372,036,854,775,807 Note: to change variable types, you will need to change all code for the variable 'data'. Partially/mostly pulled from this example: http://www.java-examples.com/read-short-file-using-datainputstream Thanks to Adam Caprez and Ashu Guru for their many-hour troubleshooting! www.jeffreythompson.org */ String filename = "test.wav"; // file to read (include extension) int headerOffset = 22; // if we want to strip the header, number of bytes to ignore int numBytes = 300; // bytes to read, or -1 will read whole file; for .wav this is samples boolean LittleEndian = true; // set 'true' if the file is Little Endian* short data; // value to store the input // *Endianness: Java assumes Big Endian, but many file formats are encoded differently // http://en.wikipedia.org/wiki/Endianness void setup() { try { // open streams to read file DataInputStream input = new DataInputStream(new FileInputStream(sketchPath + "/data/" + filename)); // read header (and do nothing with it) for (int i=0; i= 0) { data = input.readShort(); // read the data as a short if (LittleEndian) { // if we're reading Little Endian data data = Short.reverseBytes(data); // reverse the bytes to get the right value } } } else { // otherwise, just read the # of bytes specified for (int i=0; i