001 /* MetaMessage.java -- A meta message for MIDI files.
002 Copyright (C) 2005 Free Software Foundation, Inc.
003
004 This file is part of GNU Classpath.
005
006 GNU Classpath is free software; you can redistribute it and/or modify
007 it under the terms of the GNU General Public License as published by
008 the Free Software Foundation; either version 2, or (at your option)
009 any later version.
010
011 GNU Classpath is distributed in the hope that it will be useful, but
012 WITHOUT ANY WARRANTY; without even the implied warranty of
013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 General Public License for more details.
015
016 You should have received a copy of the GNU General Public License
017 along with GNU Classpath; see the file COPYING. If not, write to the
018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019 02110-1301 USA.
020
021 Linking this library statically or dynamically with other modules is
022 making a combined work based on this library. Thus, the terms and
023 conditions of the GNU General Public License cover the whole
024 combination.
025
026 As a special exception, the copyright holders of this library give you
027 permission to link this library with independent modules to produce an
028 executable, regardless of the license terms of these independent
029 modules, and to copy and distribute the resulting executable under
030 terms of your choice, provided that you also meet, for each linked
031 independent module, the terms and conditions of the license of that
032 module. An independent module is a module which is not derived from
033 or based on this library. If you modify this library, you may extend
034 this exception to your version of the library, but you are not
035 obligated to do so. If you do not wish to do so, delete this
036 exception statement from your version. */
037
038
039 package 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 */
048 public 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 while ((length >>= 7) > 0)
124 {
125 buffer <<= 8;
126 buffer |= ((length & 0x7F) | 0x80);
127 }
128
129 // Now store the variable length length value
130 int index = 2;
131 do
132 {
133 this.data[index++] = (byte) (buffer & 0xFF);
134 if ((buffer & 0x80) == 0)
135 break;
136 buffer >>= 8;
137 } while (true);
138
139 // Now copy the real data.
140 System.arraycopy(data, 0, this.data, index, length);
141 }
142
143 /**
144 * Get the meta message type.
145 *
146 * @return the meta message type
147 */
148 public int getType()
149 {
150 return data[1];
151 }
152
153 /**
154 * Get the data for this message, not including the status,
155 * type, or length information.
156 *
157 * @return the message data, not including status, type or lenght info
158 */
159 public byte[] getData()
160 {
161 int dataLength = length - 2 - lengthLength;
162 byte[] result = new byte[dataLength];
163 System.arraycopy(data, 2 + lengthLength, result, 0, dataLength);
164 return result;
165 }
166
167 /* Create a deep-copy clone of this object.
168 * @see java.lang.Object#clone()
169 */
170 public Object clone()
171 {
172 byte message[] = new byte[length];
173 System.arraycopy(data, 0, message, 0, length);
174 return new MetaMessage(message);
175 }
176 }