Remove old autovect-branch by moving to "dead" directory.
[official-gcc.git] / old-autovect-branch / libjava / classpath / javax / sound / midi / MetaMessage.java
blob2ca93accd77a94be5b3ab33e640b28555a20f9fa
1 /* MetaMessage.java -- A meta message for MIDI files.
2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.sound.midi;
41 /**
42 * A system exclusive MIDI message.
44 * @author Anthony Green (green@redhat.com)
45 * @since 1.3
48 public class MetaMessage extends MidiMessage
50 /**
51 * The META status code. Only valid for MIDI files, not the wire protocol.
53 public static final int META = 0xFF;
55 // The length of the variable length data length encoding.
56 private int lengthLength = 0;
58 /**
59 * Create a default valid meta message.
61 * The official specs don't specify what message is to be
62 * created. For now, we create a zero length meta message
63 * with a type code of 0.
65 public MetaMessage()
67 super(new byte[4]);
68 data[0] = (byte) META;
69 data[1] = (byte) 0; // Type
70 data[2] = (byte) 1; // Length length
71 data[3] = (byte) 0; // Length
72 lengthLength = 1;
75 /**
76 * Create a MetaMessage object.
77 * @param data a complete system exclusive message
79 public MetaMessage(byte[] data)
81 super(data);
82 int index = 2;
83 lengthLength = 1;
84 while ((data[index++] & 0x80) > 0)
85 lengthLength++;
88 /**
89 * Set the meta message.
91 * @param type the meta type byte (< 128)
92 * @param data the message data
93 * @param length the length of the message data
94 * @throws InvalidMidiDataException if this message is invalid
96 public void setMessage(int type, byte[] data, int length)
97 throws InvalidMidiDataException
99 if (type > 127)
100 throw new InvalidMidiDataException("Meta type 0x"
101 + Integer.toHexString(type)
102 + " must be less than 128");
104 // For a nice description of how variable length values are handled,
105 // see http://www.borg.com/~jglatt/tech/midifile.htm
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);
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;
121 // Now compute the length representation
122 long buffer = length & 0x7F;
123 while ((length >>= 7) > 0)
125 buffer <<= 8;
126 buffer |= ((length & 0x7F) | 0x80);
129 // Now store the variable length length value
130 int index = 2;
133 this.data[index++] = (byte) (buffer & 0xFF);
134 if ((buffer & 0x80) == 0)
135 break;
136 buffer >>= 8;
137 } while (true);
139 // Now copy the real data.
140 System.arraycopy(data, 0, this.data, index, length);
144 * Get the meta message type.
146 * @return the meta message type
148 public int getType()
150 return data[1];
154 * Get the data for this message, not including the status,
155 * type, or length information.
157 * @return the message data, not including status, type or lenght info
159 public byte[] getData()
161 int dataLength = length - 2 - lengthLength;
162 byte[] result = new byte[dataLength];
163 System.arraycopy(data, 2 + lengthLength, result, 0, dataLength);
164 return result;
167 /* Create a deep-copy clone of this object.
168 * @see java.lang.Object#clone()
170 public Object clone()
172 byte message[] = new byte[length];
173 System.arraycopy(data, 0, message, 0, length);
174 return new MetaMessage(message);