001/* MetaMessage.java -- A meta message for MIDI files. 002 Copyright (C) 2005 Free Software Foundation, Inc. 003 004This file is part of GNU Classpath. 005 006GNU Classpath is free software; you can redistribute it and/or modify 007it under the terms of the GNU General Public License as published by 008the Free Software Foundation; either version 2, or (at your option) 009any later version. 010 011GNU Classpath is distributed in the hope that it will be useful, but 012WITHOUT ANY WARRANTY; without even the implied warranty of 013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014General Public License for more details. 015 016You should have received a copy of the GNU General Public License 017along with GNU Classpath; see the file COPYING. If not, write to the 018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 01902110-1301 USA. 020 021Linking this library statically or dynamically with other modules is 022making a combined work based on this library. Thus, the terms and 023conditions of the GNU General Public License cover the whole 024combination. 025 026As a special exception, the copyright holders of this library give you 027permission to link this library with independent modules to produce an 028executable, regardless of the license terms of these independent 029modules, and to copy and distribute the resulting executable under 030terms of your choice, provided that you also meet, for each linked 031independent module, the terms and conditions of the license of that 032module. An independent module is a module which is not derived from 033or based on this library. If you modify this library, you may extend 034this exception to your version of the library, but you are not 035obligated to do so. If you do not wish to do so, delete this 036exception statement from your version. */ 037 038 039package javax.sound.midi; 040 041/** 042 * A system exclusive MIDI message. 043 * 044 * @author Anthony Green (green@redhat.com) 045 * @since 1.3 046 * 047 */ 048public class MetaMessage extends MidiMessage 049{ 050 /** 051 * The META status code. Only valid for MIDI files, not the wire protocol. 052 */ 053 public static final int META = 0xFF; 054 055 // The length of the variable length data length encoding. 056 private int lengthLength = 0; 057 058 /** 059 * Create a default valid meta message. 060 * 061 * The official specs don't specify what message is to be 062 * created. For now, we create a zero length meta message 063 * with a type code of 0. 064 */ 065 public MetaMessage() 066 { 067 super(new byte[4]); 068 data[0] = (byte) META; 069 data[1] = (byte) 0; // Type 070 data[2] = (byte) 1; // Length length 071 data[3] = (byte) 0; // Length 072 lengthLength = 1; 073 } 074 075 /** 076 * Create a MetaMessage object. 077 * @param data a complete system exclusive message 078 */ 079 protected MetaMessage(byte[] data) 080 { 081 super(data); 082 int index = 2; 083 lengthLength = 1; 084 while ((data[index++] & 0x80) > 0) 085 lengthLength++; 086 } 087 088 /** 089 * Set the meta message. 090 * 091 * @param type the meta type byte (< 128) 092 * @param data the message data 093 * @param length the length of the message data 094 * @throws InvalidMidiDataException if this message is invalid 095 */ 096 public void setMessage(int type, byte[] data, int length) 097 throws InvalidMidiDataException 098 { 099 if (type > 127) 100 throw new InvalidMidiDataException("Meta type 0x" 101 + Integer.toHexString(type) 102 + " must be less than 128"); 103 104 // For a nice description of how variable length values are handled, 105 // see http://www.borg.com/~jglatt/tech/midifile.htm 106 107 // First compute the length of the length value 108 lengthLength = 0; 109 int lengthValue = length; 110 do { 111 lengthValue = lengthValue >> 7; 112 lengthLength++; 113 } while (lengthValue > 0); 114 115 // Now allocate our data array 116 this.length = 2 + lengthLength + length; 117 this.data = new byte[this.length]; 118 this.data[0] = (byte) META; 119 this.data[1] = (byte) type; 120 121 // Now compute the length representation 122 long buffer = length & 0x7F; 123 // Avoid altering length variable; PR42551 124 lengthValue = length; 125 while ((lengthValue >>= 7) > 0) 126 { 127 buffer <<= 8; 128 buffer |= ((lengthValue & 0x7F) | 0x80); 129 } 130 131 // Now store the variable length length value 132 int index = 2; 133 do 134 { 135 this.data[index++] = (byte) (buffer & 0xFF); 136 if ((buffer & 0x80) == 0) 137 break; 138 buffer >>= 8; 139 } while (true); 140 141 // Now copy the real data. 142 System.arraycopy(data, 0, this.data, index, length); 143 } 144 145 /** 146 * Get the meta message type. 147 * 148 * @return the meta message type 149 */ 150 public int getType() 151 { 152 return data[1]; 153 } 154 155 /** 156 * Get the data for this message, not including the status, 157 * type, or length information. 158 * 159 * @return the message data, not including status, type or lenght info 160 */ 161 public byte[] getData() 162 { 163 int dataLength = length - 2 - lengthLength; 164 byte[] result = new byte[dataLength]; 165 System.arraycopy(data, 2 + lengthLength, result, 0, dataLength); 166 return result; 167 } 168 169 /* Create a deep-copy clone of this object. 170 * @see java.lang.Object#clone() 171 */ 172 public Object clone() 173 { 174 byte message[] = new byte[length]; 175 System.arraycopy(data, 0, message, 0, length); 176 return new MetaMessage(message); 177 } 178}