+ /* ***
+ ** ENCODING / DECODING METHODS **
+ *** */
+
+
+
+
+
+
+
+
+ /**
+ * Returns the byte array for the given tempo.
+ * @param tempo the tempo in beats per minute
+ * @return an array of bytes representing the given tempo
+ */
+ public static byte[] encodeTempo(int tempo) {
+ int microSecsPerQuarter = 60000000 / tempo;
+ byte[] b = new byte[3];
+ b[0] = (byte)(microSecsPerQuarter / 65536);
+ b[1] = (byte)((microSecsPerQuarter - (b[0] * 65536)) / 256);
+ b[2] = (byte)(microSecsPerQuarter - (b[0] * 65536) - (b[1] * 256));
+ return b;
+ }
+
+ /**
+ * Returns the tempo for the given byte array.
+ * @param an array of three bytes representing the tempo
+ * @return the tempo in beats per minute
+ */
+ public static int decodeTempo(byte[] bytes) {
+ return 60000000 / (bytes[0] * 65536 + bytes[1] * 256 + bytes[2]); // bytes[0] & 0xFF ???
+ }
+
+ /**
+ * Returns the byte array for the given time signature.
+ * @param numerator the numerator of the time signature
+ * @param denominator the denominator of the time signature
+ * @return an array of bytes representing the given time signature
+ */
+ public static byte[] encodeTimeSig(int numerator, int denominator) {
+ byte[] b = {
+ (byte)numerator,
+ (byte)(Math.log(denominator) / Math.log(2)),
+ (byte)96,
+ (byte)8
+ };
+ return b;
+ }
+ /**
+ * Returns the time signature for the given byte array.
+ * @param an array of four bytes representing the time signature
+ * @return an array of two integers where [0] is the numerator and [1] the denominator
+ */
+ public static int[] decodeTimeSig(byte[] bytes) {
+ int[] t = {
+ (int)bytes[0],
+ (int)(1 << bytes[1])
+ };
+ return t;
+ }
+
+
+
+
+
+
+
+